{"id":1969,"date":"2023-07-30T17:36:39","date_gmt":"2023-07-30T14:36:39","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/deploy-managed-kubernetes-cluster-terraform\/"},"modified":"2023-07-30T17:36:39","modified_gmt":"2023-07-30T14:36:39","slug":"deploy-managed-kubernetes-cluster-terraform","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/deploy-managed-kubernetes-cluster-terraform\/","title":{"rendered":"How to deploy Managed Kubernetes cluster using Terraform"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Managed Kubernetes easily automates the deployment, scaling, and management of container workloads but takes a couple of steps to deploy by itself. So, what better solution to combine with Managed Kubernetes than the simple yet powerful open-source infrastructure management tool, Terraform. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll show to set up a Terraform configuration for a Managed Kubernetes cluster and how to get it up and running. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Terraform<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First things first. If you haven&#8217;t used Terraform before, you will need to start by installing it. You can follow our <a rel=\"noreferrer noopener\" href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/get-started-terraform\" target=\"_blank\">beginners&#8217; tutorial<\/a> to accomplish this and then come back and continue here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also check out&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/registry.terraform.io\/providers\/UpCloudLtd\/upcloud\/latest\/docs\" target=\"_blank\">the official UpCloud Terraform provider documentation<\/a>&nbsp;if you want to learn more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up Managed Kubernetes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have Terraform installed and ready, start by creating a new directory on your own computer to house your Terraform configuration files. The actual location of this folder doesn&#8217;t matter as such as the only requirement is that only one Terraform configuration is saved in any specific directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Terraform configuration structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, you&#8217;ll need a starting point. Rather than having you copy and paste from our tutorial, we&#8217;ve made public <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/UpCloudLtd\/uks-instructions\/tree\/main\" target=\"_blank\">Managed Kubernetes example files<\/a> available in our GitHub repository.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The main repo contains a whole host of information about example integrations with Managed Kubernetes but we&#8217;ll focus here on the cluster creation. You can download the Terraform cluster <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/UpCloudLtd\/uks-instructions\/tree\/main\/terraform\/cluster\" target=\"_blank\">example configuration folder<\/a> individually using the handy <a rel=\"noreferrer noopener\" href=\"https:\/\/download-directory.github.io\/?url=https%3A%2F%2Fgithub.com%2FUpCloudLtd%2Fuks-instructions%2Ftree%2Fmain%2Fterraform%2Fcluster\" target=\"_blank\">download-directory.github.io<\/a> web tool and ignore the rest for now.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The cluster configuration is organized into the following files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>main.tf <br>This defines the managed resources: upcloud_kubernetes_cluster,&nbsp;upcloud_network<\/li>\n\n\n\n<li>provider.tf <br>Includes the main&nbsp;terraform&nbsp;block and provider-specific configurations<\/li>\n\n\n\n<li>variables.tf <br>These define the input variables used in the configuration, e.g. zone<\/li>\n\n\n\n<li>outputs.tf <br>Output values provide information about our deployment when it&#8217;s ready, like the cluster ID<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">There are default values for&nbsp;<em>provider.tf<\/em>&nbsp;that allow the provider to authenticate against the Kubernetes cluster defined in this configuration. The credentials acquired by&nbsp;<em>upcloud_kubernetes_cluster<\/em> and defined in&nbsp;<em>data.tf<\/em> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to make modifications to the files as necessary. The official<a rel=\"noreferrer noopener\" href=\"https:\/\/registry.terraform.io\/providers\/hashicorp\/kubernetes\/latest\/docs\" target=\"_blank\"> documentation<\/a>&nbsp;covers additional information needed on how to use the provider and its resources.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Provisioning<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you have the configuration files all set, you can begin by initializing the directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform init<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform will then download the required plugins. You should see an output similar to below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Initializing the backend...<\/strong>\n\n<strong>Initializing provider plugins...<\/strong>\n- Finding latest version of hashicorp\/local...\n- Finding latest version of hashicorp\/kubernetes...\n- Finding upcloudltd\/upcloud versions matching \"&gt;= 2.11.0\"...\n- Installing hashicorp\/local v2.4.0...\n- Installed hashicorp\/local v2.4.0 (signed by HashiCorp)\n- Installing hashicorp\/kubernetes v2.22.0...\n- Installed hashicorp\/kubernetes v2.22.0 (signed by HashiCorp)\n- Installing upcloudltd\/upcloud v2.12.0...\n- Installed upcloudltd\/upcloud v2.12.0 (self-signed, key ID <strong>60B4E1988F222907<\/strong>)\n\nPartner and community providers are signed by their developers.\nIf you'd like to know more about provider signing, you can read about it here:\nhttps:\/\/www.terraform.io\/docs\/cli\/plugins\/signing.html\n\nTerraform has created a lock file <strong>.terraform.lock.hcl<\/strong> to record the provider\nselections it made above. Include this file in your version control repository\nso that Terraform can guarantee to make the same selections by default when\nyou run \"terraform init\" in the future.\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\"><strong>Terraform has been successfully initialized!<\/strong>\n\nYou may now begin working with Terraform. Try running \"terraform plan\" to see\nany changes that are required for your infrastructure. All Terraform commands\nshould now work.\n\nIf you ever set or change modules or backend configuration for Terraform,\nrerun this command to reinitialize your working directory. If you forget, other\ncommands will detect it and remind you to do so if necessary.<\/mark><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, test that the configuration is ready to deploy by running the <em>terraform plan<\/em> command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform plan<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the\nfollowing symbols:\n  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">+<\/mark> create\n <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">&lt;-<\/mark> read (data resources)\n\nTerraform will perform the following actions:\n\n  # <strong>data.upcloud_kubernetes_cluster.example<\/strong> will be read during apply\n  # (config refers to values not yet known)\n \n...\n\n<strong>Plan:<\/strong> 4 to add, 0 to change, 0 to destroy.\n\nChanges to Outputs:\n  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">+<\/mark> cluster_id = (known after apply)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above example output has been truncated for brevity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lastly, if the plan looks all correct and Terraform didn&#8217;t report any issues, go ahead and deploy the setup. Confirm the deployment by answering yes when prompted.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You will then see a confirmation of the configuration followed by the deployment process. Running apply will create a network, a cluster, and a Kubernetes namespace in the new cluster.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">...\n\n<strong>Plan:<\/strong> 4 to add, 0 to change, 0 to destroy.\n\nChanges to Outputs:\n  <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">+<\/mark> cluster_id = (known after apply)\nupcloud_network.example: Creating...\nupcloud_network.example: Creation complete after 2s [id=03e416f9-0812-48bb-b58b-cf4ac74161eb]\nupcloud_kubernetes_cluster.example: Creating...\nupcloud_kubernetes_cluster.example: Still creating... [10s elapsed]\n...\nupcloud_kubernetes_cluster.example: Creation complete after 6m57s [id=0d1f7c52-4910-4970-b150-a07dce8615e2]\ndata.upcloud_kubernetes_cluster.example: Reading...\nupcloud_kubernetes_node_group.group: Creating...\ndata.upcloud_kubernetes_cluster.example: Read complete after 0s [id=0d1f7c52-4910-4970-b150-a07dce8615e2]\nlocal_file.kubeconfig[0]: Creating...\nlocal_file.kubeconfig[0]: Creation complete after 0s [id=68796076a5b3afcce490db2a9464019b94a662ad]\nupcloud_kubernetes_node_group.group: Creation complete after 1s [id=0d1f7c52-4910-4970-b150-a07dce8615e2\/medium]\n\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">Apply complete! Resources: 4 added, 0 changed, 0 destroyed.\n\nOutputs:<\/mark>\n\ncluster_id = \"0d1f7c52-4910-4970-b150-a07dce8615e2\"<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it! You should now have a fully functional Managed Kubernetes cluster running on your UpCloud account just waiting for a workload to run. <\/p>\n","protected":false},"author":3,"featured_media":34478,"comment_status":"open","ping_status":"closed","template":"","community-category":[229,268,235],"class_list":["post-1969","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/1969","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=1969"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/1969\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=1969"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=1969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}