Creating Azure Kubernetes Service (AKS) with Azure DevOps and Terraform
Introduction
Combining Azure Kubernetes Service (AKS) with Azure DevOps and Terraform provides a powerful and efficient way to manage your Kubernetes clusters and CI/CD pipelines. Terraform allows for infrastructure as code, making it easier to provision and manage resources, while Azure DevOps automates the deployment process. This blog post will guide you through setting up AKS with all necessary components using Azure DevOps and Terraform, utilizing main.tf, providers.tf, variables.tf, output.tf, deployment.yaml, and services.yaml files.
Prerequisites
- Azure Subscription: Ensure you have an active Azure subscription.
- Azure DevOps Account: Set up an Azure DevOps organization and project.
- Terraform: Install Terraform for infrastructure provisioning.
- Azure CLI: Install Azure CLI for command-line operations.
- Kubectl: Install kubectl to interact with your Kubernetes cluster.
Step-by-Step Guide
1. Define Providers in providers.tf
Create a providers.tf file in your repository with the following content:
provider "azurerm" {
features {}
}
2. Define Variables in variables.tf.
Create a variables.tf file to manage your configuration variables:
variable "resource_group_name" {
description = "The name of the resource group"
default = "aks-resource-group"
}
variable "location" {
description = "The Azure region to deploy resources"
default = "East US"
}
variable "aks_cluster_name" {
description = "The name of the AKS cluster"
default = "aks-cluster"
}
variable "node_count" {
description = "The number of nodes in the AKS cluster"
default = 1
}
variable "node_size" {
description = "The size of the nodes in the AKS cluster"
default = "Standard_DS2_v2"
}
3. Define Infrastructure in main.tf
Create a main.tf file to define the AKS cluster and its components:
resource "azurerm_resource_group" "aks_rg" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_kubernetes_cluster" "aks_cluster" {
name = var.aks_cluster_name
location = azurerm_resource_group.aks_rg.location
resource_group_name = azurerm_resource_group.aks_rg.name
dns_prefix = "aks"
default_node_pool {
name = "default"
node_count = var.node_count
vm_size = var.node_size
}
identity {
type = "SystemAssigned"
}
}
4. Define Outputs in output.tf
Create an output.tf file to capture the outputs of your Terraform configuration:
output "resource_group_name" {
value = azurerm_resource_group.aks_rg.name
}
output "aks_cluster_name" {
value = azurerm_kubernetes_cluster.aks_cluster.name
}
5. Create Kubernetes Deployment in deployment.yaml
Create a deployment.yaml file to define your Kubernetes deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app-image:latest
ports:
- containerPort: 80
6. Create Kubernetes Service in services.yaml
Create a services.yaml file to define your Kubernetes service:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
7. Set Up Azure DevOps Pipeline
- In your Azure DevOps project, navigate to Pipelines > Create Pipeline.
- Select your repository source (e.g., GitHub, Azure Repos).
- Choose the pipeline configuration (YAML or classic editor).
8. Configure Service Connection
- Go to Project Settings > Service connections.
- Create a new service connection for Azure Resource Manager.
- Authenticate and authorize Azure DevOps to access your Azure subscription.
9. Define Pipeline YAML
Create a azure-pipelines.yml file in your repository with the following content:
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: TerraformInstaller@0
inputs:
terraformVersion: 'latest'
- script: |
terraform init
terraform plan -out=tfplan
terraform apply -auto-approve tfplan
displayName: 'Terraform Apply'
- task: AzureCLI@2
inputs:
azureSubscription: '<Your Service Connection>'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az aks get-credentials --resource-group $(terraform output -raw resource_group_name) --name $(terraform output -raw aks_cluster_name)
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/services.yaml
displayName: 'Deploy to AKS'
10. Deploy Infrastructure and Application
- Commit and push the providers.tf, variables.tf, main.tf, output.tf, deployment.yaml, services.yaml, and azure-pipelines.yml files to your repository.
- The pipeline will trigger automatically, provisioning the AKS cluster with Terraform and deploying your application to the cluster.
Conclusion
By following these steps, you can efficiently set up an Azure Kubernetes Service with all necessary components using Azure DevOps and Terraform. This integration ensures a seamless CI/CD process, enabling continuous deployment and management of your applications in a scalable and reliable manner.
Feel free to reach out if you have any questions or need further assistance! Happy deploying!
I hope this helps! Let me know if you need any more details or have other questions.
Comments
Post a Comment
Comments are always welcome, that will help us to motivate ourselves and improve our services. Thanks!!