{"id":2074,"date":"2021-03-30T20:55:29","date_gmt":"2021-03-30T17:55:29","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/deploy-kubernetes-using-kubespray\/"},"modified":"2026-04-23T13:57:12","modified_gmt":"2026-04-23T12:57:12","slug":"deploy-kubernetes-using-kubespray","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/deploy-kubernetes-using-kubespray\/","title":{"rendered":"How to deploy Kubernetes using Kubespray"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Kubernetes is an open-source orchestration system that automates the process of deploying and maintaining containerized applications. It gives you the mechanism to schedule and run containers on clusters of several physical and\/or virtual machines. For more information please read the official <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/what-is-kubernetes\/\" target=\"_blank\" rel=\"noopener\">Kubernetes documentation<\/a>.<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\"><a rel=\"noopener\" href=\"https:\/\/github.com\/kubernetes-sigs\/kubespray\" target=\"_blank\">Kubespray<\/a> is a composition of Ansible playbooks, inventory, provisioning tools, and domain knowledge for generic OS\/Kubernetes cluster configuration management tasks. It provides a highly available cluster, composable attributes and support for the most popular Linux distributions. It has become the de-facto production-ready Kubernetes installer that is trusted throughout the cloud-native community (10k stars on GitHub).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we\u2019ll show the steps required to deploy a Kubernetes cluster on UpCloud using Kubespray. The tutorial assumes that you have basic knowledge about Kubernetes and the different terminologies that comes with it but the steps are easy enough for beginners to follow along as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In principle, the steps in this guide can be divided into the following two main procedures which are required in order to set up a new Kubernetes cluster.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create the infrastructure<\/li>\n\n\n\n<li>Deploy Kubernetes<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Before delving into the actual steps, clone Kubespray onto your own computer, for example by using the git command-line tool. If you do not already have git installed, you can use the command below to install git on Ubuntu or other Debian-based operating systems or check the <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Getting-Started-Installing-Git\" target=\"_blank\" rel=\"noopener\">git install guide<\/a> for other OS options.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install git-all<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then download the Kubespray package and change to the new directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git clone <a href=\"https:\/\/github.com\/kubernetes-sigs\/kubespray.git\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/kubernetes-sigs\/kubespray.git<\/a>\ncd kubespray<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll also need to install Ansible and other dependencies. Luckily, Kubespray provides a handy list of the requirements which can be used to install all prerequisites with a single command. However, for this to work, you\u2019ll first need to have Python\u2019s package installer, pip, available.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt install python3-pip\nsudo pip3 install -r requirements.txt<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you face any issue while installing the prerequisites, please check the official <a href=\"https:\/\/github.com\/kubernetes-sigs\/kubespray\" target=\"_blank\" rel=\"noopener\">Kubespray repository<\/a> for troubleshooting steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Terraform<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.terraform.io\/\" target=\"_blank\" rel=\"noopener\">Terraform<\/a> is an infrastructure provisioning tool. It is used for building, changing, and versioning infrastructure safely and efficiently. Installing Terraform CLI on your computer provides you with all the tools you need to manage your infrastructure in the cloud.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To install Terraform, find the <a href=\"https:\/\/www.terraform.io\/downloads.html\" target=\"_blank\" rel=\"noopener\">appropriate package<\/a> for your system, download and install it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to install Terraform on most Linux systems, first, download the latest version.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget https:\/\/releases.hashicorp.com\/terraform\/0.14.7\/terraform_0.14.7_linux_amd64.zip<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then extract the binaries to a suitable location, such as <tt>\/usr\/local\/bin<\/tt> and make sure it is included in your <tt>PATH<\/tt> environment variable. For example with the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo unzip terraform_0.14.7_linux_amd64.zip -d \/usr\/local\/bin<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can verify that the installation worked by listing Terraform\u2019s available subcommands in a terminal.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform -help<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Usage: terraform [-version] [-help]  [args]\nThe available commands for execution are listed below.\nThe primary workflow commands are given first, followed by\nless common or more advanced commands.\n...<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up API access<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For Terraform to be able to deploy cloud resources on your UpCloud account, you first need to grant it access. To do so, follow the steps below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a separate API account in your UpCloud Control Panel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It is recommended to create a separate API user for Terraform to interact with UpCloud API during infrastructure deployment instead of using your main account. You can do this at the <a href=\"https:\/\/hub.upcloud.com\/people\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel<\/a> using the workspace member accounts. Your API account name and password are very much comparable to a traditional API ID and key pair with the added benefit of being able to set them freely yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create a new account, select <i>People<\/i> on the left side of the UpCloud Control Panel and follow the instructions. For more information, see this tutorial on <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/getting-started-upcloud-api\/\" target=\"_blank\" rel=\"noreferrer noopener\">how to get started with the UpCloud API<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Allow API Access to your UpCloud Account<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve created your API account, you will need to allow it to access the UpCloud API so that Terraform can create your infrastructure on UpCloud. For this purpose, make sure you select the <strong>Allow API connections<\/strong> on the new account and set it to <strong>All addresses<\/strong> to easily ensure later steps in this guide will work.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/allow-api-connection-all-addresses.png\" alt=\"Setting API connection permissions\" class=\"wp-image-20401\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can and for security reasons perhaps <i>should<\/i> restrict this later to your own IP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Set up UpCloud user credentials<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, you\u2019ll need to pass the new API account credentials to Terraform. Use the commands below to export your new API username and password as environment variables in your current shell session. The username and password will then be accessible to Terraform CLI when creating your cluster.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export TF_VAR_UPCLOUD_USERNAME=\nexport TF_VAR_UPCLOUD_PASSWORD=<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note: The credentials above are in plaintext. It is advisable to store the username and password Base64 encoded for more permanent use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Overview of the infrastructure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A cluster in Kubernetes is composed of multiple control plane and worker nodes. &nbsp;Control plane nodes are nodes that control and manage a set of worker nodes (workloads runtime) and worker nodes are nodes that run containerized applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of a Kubernetes cluster setup could look like the following.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/kubespray-terraform.png\" alt=\"Kubernetes cluster\" class=\"wp-image-20396\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You need to decide the number of both control plane and worker nodes for your cluster. In this post, we will use one control plane node and three worker nodes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To create the Kubernetes cluster, you need to make sure you have Terraform CLI installed on your system as well as the proper configuration for your cluster. Terraform helps us to define infrastructure as a code. Defining infrastructure as code brings many advantages such as simple editing, reviewing, and versioning, as well as easy sharing amongst team members.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring the cluster<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we\u2019ll set up the configuration for our cluster. To avoid modifying the template files, let us copy the required files into a new directory and do the changes there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a directory called my-upcloud-cluster as follows. The CLUSTER variable here is a shorthand for our directory name. If you want to name your directory differently, just change the next line and the rest of the configuration works the same.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CLUSTER=my-upcloud-cluster<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">mkdir inventory\/$CLUSTER<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy the sample inventory and the default cluster configuration to the new directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cp -r inventory\/sample inventory\/$CLUSTER\ncp -r contrib\/terraform\/upcloud\/* inventory\/$CLUSTER\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Change your working directory to the new configuration directory and edit the <tt>cluster-settings.tfvars<\/tt> file to match your requirements.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd inventory\/$CLUSTER<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">vim cluster-settings.tfvars<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The following are the main Terraform variables that you can change in <i>cluster-settings.tfvars<\/i>&nbsp; file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><i>hostname<\/i><\/strong><i>:<\/i> A valid domain name, e.g. example.com. The maximum length is 128 characters.<\/li>\n\n\n\n<li><strong><i>template_name<\/i><\/strong>: The name or UUID of a base image.<\/li>\n\n\n\n<li><strong><i>username<\/i><\/strong>: A user to access the nodes e.g., via SSH. <i>Note<\/i> that the username&nbsp;<i>kube<\/i> is reserved by kubernetes.<\/li>\n\n\n\n<li><strong><i>ssh_public_keys<\/i><\/strong>: One or more public SSH keys is required to be able to access and provision the machines after deployment.<\/li>\n\n\n\n<li><strong><i>zone<\/i><\/strong>: The zone where the cluster will be created. Check the <a href=\"https:\/\/developers.upcloud.com\/1.3\/5-zones\/\" target=\"_blank\" rel=\"noopener\">available zones<\/a>&nbsp;for reference.<\/li>\n\n\n\n<li><strong><i>machines<\/i><\/strong>: The Cloud Servers that will be provisioned. Contain the list of machines composing the cluster. The key of this object will be used as the name of the machine.\n<ul class=\"wp-block-list\">\n<li><strong><i>node_type<\/i><\/strong>: The role of this node (<i>master|worker<\/i>, in Kubespray and hence in this guide called \u201cmaster\u201d due to legacy naming of the control plane \u2014 is likely to change in the future).<\/li>\n\n\n\n<li><strong><i>cpu<\/i><\/strong>: Number of CPU cores.<\/li>\n\n\n\n<li><strong><i>mem<\/i><\/strong>: Memory size in MB.<\/li>\n\n\n\n<li><strong><i>disk_size<\/i><\/strong>: The size of the storage in GB.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to create a cluster with two control plane nodes, three worker nodes, and each node with 2 cores, 4GB memory, and 250GB disk size, replace the <i>machines<\/i> section in the variables with the following code snippet.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">machines = {\n  \"master-0\" : {\n    \"node_type\" : \"master\",\n    #number of cpu cores\n    \"cpu\" : \"2\",\n    #memory size in MB\n    \"mem\" : \"4096\"\n    # The size of the storage in GB\n    \"disk_size\" : 250\n  },\n  \"worker-0\" : {\n    \"node_type\" : \"worker\",\n    #number of cpu cores\n    \"cpu\" : \"2\",\n    #memory size in MB\n    \"mem\" : \"4096\"\n    # The size of the storage in GB\n    \"disk_size\" : 250\n  },\n  \"worker-1\" : {\n    \"node_type\" : \"worker\",\n    #number of cpu cores\n    \"cpu\" : \"2\",\n    #memory size in MB\n    \"mem\" : \"4096\"\n    # The size of the storage in GB\n    \"disk_size\" : 250\n  },\n  \"worker-2\" : {\n    \"node_type\" : \"worker\",\n    #number of cpu cores\n    \"cpu\" : \"2\",\n    #memory size in MB\n    \"mem\" : \"4096\"\n    # The size of the storage in GB\n    \"disk_size\" : 250\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to replace the value of the <i>ssh_public_keys<\/i> variable with your public SSH key as it will be used to ssh to each machine when installing Kubernetes using Kubespray.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deploying the cluster<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now that the configurations are done, you can start deploying your cluster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialise your configuration directory<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <tt>terraform init<\/tt> command is used to initialize a working directory containing Terraform configuration files. It will download and install the UpCloud Terraform provider plugin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following command to initialize Terraform.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform init<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Verify your infrastructure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <tt>terraform plan<\/tt> command verifies your configuration is syntactically correct and creates an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following command to see your execution plan:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform plan --var-file cluster-settings.tfvars \n-state=tfstate-$CLUSTER.tfstate<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Deploy the infrastructure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <tt>terraform apply<\/tt> command is used to apply the changes required to reach the desired state of the configuration or the predetermined set of actions generated by a Terraform execution plan. It creates new or makes changes to the existing infrastructure as defined in your configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the following command to create your cluster and answer <strong>yes<\/strong> when asked to confirm:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply --var-file cluster-settings.tfvars \n-state=tfstate-$CLUSTER.tfstate<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once Terraform has finished deploying, you can go and check your cluster resources at your <a href=\"https:\/\/hub.upcloud.com\/\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel<\/a>. The following figure shows the four servers (one control plane and three worker nodes) created by Terraform.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/kubernetes-cluster-nodes.png\" alt=\"Kubernetes cluster as seen in UpCloud Control Panel\" class=\"wp-image-20573\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You should also get an inventory file named <i>inventory.ini <\/i>that you can use with Kubespray. We will use the inventory file to set up the Kubernetes cluster later.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ls<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">cluster-settings.tfvars  <span style=\"color: #ff0000;\">inventory.ini<\/span>  sample\nterraform.tfstate tfstate-my-upcloud-cluster.tfstate<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up Kubernetes with Kubespray<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Ansible<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Set&nbsp; ANSIBLE_CONFIG environment variables to Kubespray\u2019s ansible configuration file as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export ANSIBLE_CONFIG=..\/..\/ansible.cfg<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Check that you have basic SSH connectivity to the nodes. You can do this by running the following ansible command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ansible -i inventory.ini -m ping all<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see similar to the following figure if all nodes are reachable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">master-0.example.com | SUCCESS =&gt; {\n    \"ansible_facts\": {\n        \"discovered_interpreter_python\": \"\/usr\/bin\/python3\"\n    },\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\nworker-0.example.com | SUCCESS =&gt; {\n    \"ansible_facts\": {\n        \"discovered_interpreter_python\": \"\/usr\/bin\/python3\"\n    },\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\nworker-1.example.com | SUCCESS =&gt; {\n    \"ansible_facts\": {\n        \"discovered_interpreter_python\": \"\/usr\/bin\/python3\"\n    },\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\nworker-2.example.com | SUCCESS =&gt; {\n    \"ansible_facts\": {\n        \"discovered_interpreter_python\": \"\/usr\/bin\/python3\"\n    },\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Deploy Kubernetes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can now deploy Kubernetes with Kubespray using the inventory file generated during the Terraform apply step as follows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><i>Note:<\/i> if you use a different user to access the nodes other than the default <i>ubuntu<\/i>, please replace <i>ubuntu<\/i> with the new user in the <tt>inventory.ini<\/tt> file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ansible-playbook -i inventory.ini ..\/..\/cluster.yml -b -v<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then sit back and relax while Ansible and Kubespray do all the heavy lifting!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, you will see a play recap of the deployment like in the screenshot below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/ansible-play-recap.png\" alt=\"Ansible deployment completed\" class=\"wp-image-20452\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing your new Kubernetes cluster<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By default, Kubespray configures <tt>kube-master<\/tt> hosts with access to <tt>kube-apiserver<\/tt> via port 6443 as http:\/\/127.0.0.1:6443. You can connect to this from one of the master nodes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Get the IP address of one of the master nodes and SSH to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the following script retrieves the IP of the master-0 node from the inventory file and opens an SSH connection to it using the default username.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># get the&nbsp; IP address of&nbsp; master-0\nip=$(grep -m 1&nbsp; \"master-0\" inventory.ini | grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | head -n 1)\n# ssh to master-0 node\nssh ubuntu@$ip<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once you are logged into one of the master nodes, you can run any of the <tt>kubectl<\/tt> commands. For example, use the command below to show the list of nodes in the cluster.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo kubectl get nodes<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">NAME                   STATUS   ROLES                  AGE    VERSION\nmaster-0.example.com   Ready    control-plane,master   10m   v1.20.5\nworker-0.example.com   Ready                           10m   v1.20.5\nworker-1.example.com   Ready                           10m   v1.20.5\nworker-2.example.com   Ready                           10m   v1.20.5<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Accessing Kubernetes Cluster from a workstation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">While in the example above, we logged into one of the nodes in the cluster, it\u2019s also possible to command Kubernetes right from your own workstation. To make this work, simply copy the <tt>\/etc\/kubernetes\/admin.conf<\/tt> from a master node to your workstation and use it with <em>kubectl<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following script does the trick.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># get the&nbsp; IP address of&nbsp; master-0\nip=$(grep -m 1&nbsp; \"master-0\" inventory.ini | grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | head -n 1)\n# copy \/etc\/kubernetes\/admin.conf file on the local system\nssh ubuntu@$ip 'sudo cat \/etc\/kubernetes\/admin.conf' &gt; admin.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure you have installed the <tt>kubectl<\/tt> tool on your local machine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you haven\u2019t done so already please follow the instructions <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/tools\/\" target=\"_blank\" rel=\"noopener\">here<\/a>.&nbsp; Once you have installed the tool you can interact with your cluster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To access your Kubernetes cluster remotely, you need to tell <tt>kubectl<\/tt> where your configuration file is. One way to do that is to point the KUBECONFIG environment variable to your cluster kubeconfig file as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export KUBECONFIG=admin.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One last thing you need to do before you run any of the kubectl commands is to replace the Kubernetes API IP address in the <tt>admin.conf<\/tt> with one of the control plane node IP addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open <tt>admin.conf<\/tt> file with your favourite text editor and replace 127.0.0.1 with the IP address you get from above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">vim admin.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, if you already have the IP address of the first master node save in the <tt>$ip<\/tt> variable. You can swap the 127.0.0.1 IP with the IP saved in the variable using the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sed -i \"s\/127.0.0.1\/$ip\/g\" admin.conf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the master node IP set in the admin file, you are ready to start playing with your cluster from your local machine!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, use the following command to show a list of namespaces in your cluster.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get namespace<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">NAME             STATUS  AGE\ndefault          Active  12m\nkube-node-lease  Active  12m\nkube-public      Active  12m\nkube-system      Active  12m<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations, you now have a fully functional production-ready Kubernetes cluster up and running!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Teardown<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you are done testing the cluster and no longer need it, you can use Terraform to tear down the deployed infrastructure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <tt>terraform destroy<\/tt> command is used to destroy the Terraform-managed infrastructure. It terminates resources defined in your Terraform configuration and performs the reverse of what the <tt>terraform apply<\/tt> does.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can tear down your infrastructure using the following Terraform command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform destroy --var-file cluster-settings.tfvars \n-state=tfstate-$CLUSTER.tfstate ..\/..\/contrib\/terraform\/upcloud\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After deletion, you can always use the same configuration files to take and modify your cluster and deploy it again with a moment&#8217;s notice!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"author":53,"featured_media":20396,"comment_status":"open","ping_status":"closed","template":"","community-category":[256,229],"class_list":["post-2074","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/53"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2074"}],"version-history":[{"count":2,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2074\/revisions"}],"predecessor-version":[{"id":6424,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2074\/revisions\/6424"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2074"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}