{"id":4219,"date":"2023-11-04T23:14:09","date_gmt":"2023-11-04T23:14:09","guid":{"rendered":"http:\/\/localhost:10003\/implementing-azure-kubernetes-service-with-terraform\/"},"modified":"2023-11-05T05:47:55","modified_gmt":"2023-11-05T05:47:55","slug":"implementing-azure-kubernetes-service-with-terraform","status":"publish","type":"post","link":"http:\/\/localhost:10003\/implementing-azure-kubernetes-service-with-terraform\/","title":{"rendered":"Implementing Azure Kubernetes Service with Terraform"},"content":{"rendered":"
Azure Kubernetes Service (AKS) is a fully managed Kubernetes container orchestration service provided by Microsoft Azure. It enables developers to deploy and manage containerized applications easier and more efficiently. Terraform is an infrastructure as code tool that allows you to define and manage your infrastructure as configuration files. In this tutorial, we will implement AKS with Terraform.<\/p>\n
Before we can create resources in Azure, we need to authenticate using the Azure CLI. Open a terminal window and run the following command:<\/p>\n
az login\n<\/code><\/pre>\nThis command will prompt you to open a web browser and authenticate with your Azure account.<\/p>\n
Step 2: Create a Resource Group<\/h2>\n
Next, we will create a resource group in Azure where we will deploy our AKS cluster. Run the following command to create a new resource group:<\/p>\n
az group create --name myResourceGroup --location eastus\n<\/code><\/pre>\nReplace myResourceGroup<\/code> with a name of your choice. The --location<\/code> parameter specifies the region where the resource group will be created.<\/p>\nStep 3: Create an AKS Cluster<\/h2>\n
We will use Terraform to create our AKS cluster. Start by creating a new directory for your Terraform configuration files. Navigate to this directory in your terminal and create a new file called main.tf<\/code>.<\/p>\nAdd the following code to main.tf<\/code>:<\/p>\nprovider \"azurerm\" {\n features {}\n}\n\nresource \"azurerm_kubernetes_cluster\" \"aks\" {\n name = \"myAKSCluster\"\n location = \"eastus\"\n resource_group_name = \"myResourceGroup\"\n dns_prefix = \"myakscluster\"\n\n linux_profile {\n admin_username = \"azureuser\"\n\n ssh_key {\n key_data = \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCdQiq8+Nez8Kwj4C+cn1evFF\/4xjiTpIrlb1gbv9e8ssCdIP\/mD0Tg+HpaJG\/UHPFdOeqD+vq4QY92zNH60hzGgZI\/7UQsNfYJlVdVX888JAv6wPhK3v3aMzJZU6zYkD0H0XjNNYDbMYAYG+Ob080XMGk2N2Cux7RhtkwSm+aT\/\/OR1nqGIZ22bbh45anK0JOWyPlp8Gi2T7fy42JeuV\/lmtq8e5VYMhJTvJbO0qM27edSm5rGpo2X416rTAnb40msT\/TVRAOr5GoMp2UuJp5wE2\/\/nP2WgwVZ0gqQHKNb4OCQzjPFEv\/H9D+mJtOxKq8mRpfwQA5CfPeYx root@ubuntu\"\n }\n }\n\n service_principal {\n client_id = \"YOUR_APP_ID\"\n client_secret = \"YOUR_APP_PASSWORD\"\n }\n\n agent_pool_profile {\n name = \"default\"\n count = 1\n vm_size = \"Standard_DS2_v2\"\n os_type = \"Linux\"\n os_disk_size_gb = 30\n }\n\n tags = {\n Environment = \"dev\"\n }\n}\n<\/code><\/pre>\nThis code creates a new AKS cluster in your Azure account. Replace myAKSCluster<\/code> with a name of your choice.<\/p>\nThe service_principal<\/code> block defines the Azure Active Directory (AD) service principal used to manage the AKS cluster. Replace the placeholders YOUR_APP_ID<\/code> and YOUR_APP_PASSWORD<\/code> with the ID and password of your service principal. Refer to Azure documentation for details on creating a service principal.<\/p>\nStep 4: Initialize and Apply Terraform Configuration<\/h2>\n
Initialize Terraform in the directory with the following command:<\/p>\n
terraform init\n<\/code><\/pre>\nThis will download the required modules and providers. Then, apply the Terraform configuration with the following command:<\/p>\n
terraform apply\n<\/code><\/pre>\nThis command will prompt you for confirmation before applying the configuration. Type yes<\/code> to confirm and begin the deployment process.<\/p>\nAfter a few minutes, Terraform will have created your AKS cluster in Azure.<\/p>\n
Step 5: Configure Kubernetes<\/h2>\n
Now that our AKS cluster is up and running, we need to configure our Kubernetes command line tool (kubectl) to use it. Start by downloading the Kubernetes configuration file for your AKS cluster with the following command:<\/p>\n
az aks get-credentials --name myAKSCluster --resource-group myResourceGroup\n<\/code><\/pre>\nThis command downloads the AKS-specific Kubernetes configuration file and adds it to your default Kubernetes configuration file (~\/.kube\/config<\/code>) so that kubectl can access your AKS cluster.<\/p>\nVerify that you can connect to your AKS cluster with the following command:<\/p>\n
kubectl get nodes\n<\/code><\/pre>\nThis command should return a list of nodes in your AKS cluster.<\/p>\n
Step 6: Create a Deployment<\/h2>\n
We will now create a simple deployment to test our AKS cluster. Create a new file called deployment.yaml<\/code> in your Terraform directory and add the following code:<\/p>\napiVersion: apps\/v1\nkind: Deployment\nmetadata:\n name: nginx-deployment\nspec:\n selector:\n matchLabels:\n app: nginx\n replicas: 3\n template:\n metadata:\n labels:\n app: nginx\n spec:\n containers:\n - name: nginx\n image: nginx:latest\n ports:\n - containerPort: 80\n<\/code><\/pre>\nThis code defines a deployment with 3 replicas of the latest version of the nginx image.<\/p>\n
Apply the deployment to your AKS cluster with the following command:<\/p>\n
kubectl apply -f deployment.yaml\n<\/code><\/pre>\nThis command creates the deployment and its associated objects (replica sets and pods) in your AKS cluster.<\/p>\n
Step 7: Expose the Deployment<\/h2>\n
We will now expose the deployment to the internet by creating a service. Create a new file called service.yaml<\/code> in your Terraform directory and add the following code:<\/p>\napiVersion: v1\nkind: Service\nmetadata:\n name: nginx-service\nspec:\n selector:\n app: nginx\n ports:\n - name: http\n protocol: TCP\n port: 80\n targetPort: 80\n type: LoadBalancer\n<\/code><\/pre>\nThis code defines a load balancer service for our nginx deployment.<\/p>\n
Apply the service to your AKS cluster with the following command:<\/p>\n
kubectl apply -f service.yaml\n<\/code><\/pre>\nThis command creates the service and a public IP address in your AKS cluster.<\/p>\n
Step 8: Verify the Deployment<\/h2>\n
To verify that our deployment is working correctly and accessible from the internet, we need to obtain the public IP address of our load balancer service. Use the following command to get the IP address:<\/p>\n
kubectl get services\n<\/code><\/pre>\nThis command should return a list of services in your AKS cluster.<\/p>\n
Look for the EXTERNAL-IP<\/code> column for your nginx-service<\/code>. If the IP address is shown as <pending><\/code>, wait a minute and try the command again.<\/p>\nOnce you have the IP address, test the deployment by opening a web browser and navigating to http:\/\/<IP-ADDRESS><\/code>. You should see the default nginx welcome page.<\/p>\nConclusion<\/h2>\n
In this tutorial, we have seen how to create an AKS cluster with Terraform, configure Kubernetes to use the cluster, and deploy a simple application. This should give you a good starting point for using AKS and Terraform to manage your containerized applications in Azure. Remember to delete your AKS cluster and associated resources when you are done to avoid incurring unnecessary costs.<\/p>\n","protected":false},"excerpt":{"rendered":"
Azure Kubernetes Service (AKS) is a fully managed Kubernetes container orchestration service provided by Microsoft Azure. It enables developers to deploy and manage containerized applications easier and more efficiently. Terraform is an infrastructure as code tool that allows you to define and manage your infrastructure as configuration files. In this Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[87,1748,1750,1751,700,1749,1747,1746],"yoast_head":"\nImplementing Azure Kubernetes Service with Terraform - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n