Sample code to create Azure Kubernetes Service with Terraform
In this code sample, we'll guide you through setting up an Azure Kubernetes Service (AKS) using Azure Pipelines. It's assumed that the developer has a basic understanding of Azure, Terraform, Docker, GitFlow, and Kubernetes. A demonstration video is available for a hands-on experience, and personalized support plans are offered for additional assistance.
Topics covered in the demonstration include:
- Infrastructure Creation: Learn how to create infrastructure for different environments like Development, Test, and Production.
- GitFlow Concept: Understand the GitFlow workflow for effective version control.
- Resource Group Creation: Use a pipeline to create a dedicated resource group for each environment.
- VNET Creation: Implement a pipeline to set up a Virtual Network (VNET) for each environment.
- Azure Log Analytics Setup: Create Azure Log Analytics for each environment through a pipeline.
- AKS Connectivity: Learn to connect to AKS using Kubectl.
- Docker Image Deployment: Understand how to deploy a Docker image on AKS.
- Lens Configuration: Configure and utilize Lens, a Kubernetes IDE.
- Resource Cleanup: Discover how to use Pipelines to safely destroy previously created resources.
Recommended knowledge:
- Programming Language Experience: Familiarity with a programming language is essential for understanding the codebase.
- Azure Portal and Azure DevOps: Prior experience or knowledge of navigating the Azure Portal and Azure DevOps will be beneficial.
- GIT Proficiency: Solid understanding and experience with GIT, including basic GitFlow concepts.
- Docker Command Familiarity: Experience with Docker commands such as pull, push, and tag will aid in managing container images.
- Kubectl and AKS: Practical experience using Kubectl with Azure Kubernetes Service (AKS) is recommended.
Prerequisites:
- Completed Previous Code Samples: It's assumed that you have a good understanding of the code explained in previous samples.
- Terminal Bash (WSL on Windows 10): We will use Terminal Bash, with a preference for Windows Subsystem for Linux (WSL) on Windows 10.
- Azure CLI >= 2.56: Make sure you have Azure CLI version 2.56 or higher installed.
- Terraform >= 1.64: Install Terraform version 1.64 or later for managing infrastructure as code.
- kubectl >= 1.28: Ensure you have Kubernetes command-line tool kubectl version 1.28 or later.
- Active Azure Account: You need at least one active primary account in the Azure Portal.
- Azure DevOps Organization: Link your Azure DevOps organization to the same directory as the main Azure account.
- Docker Hub Account: Have an active account on Docker Hub for managing container images.
- Lens IDE: Install the latest version of the Lens IDE for Kubernetes management.
- Azure DevOps Projects from Previous Samples: Utilize Azure DevOps projects created in the previous samples for continuity.
Checking Prerequisites:
Verifying the AZ CLI version
az -v
azure-cli 2.56.0
core 2.56.0
telemetry 1.1.0
Dependencies:
msal 1.24.0b2
azure-mgmt-resource 23.1.0b2
Verifying the Terraform version
terraform -v
Terraform v1.6.4
on linux_amd64
Check access to image located in my repository
docker pull registry.hub.docker.com/jbaldeon/slave-terraform:7
docker pull registry.hub.docker.com/jbaldeon/nettools:2
Verifying the Kubectl version
kubectl version
Client Version: v1.28.4
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Initial Settings:
Linking our PC to the Azure portal through our Microsoft account
az login
Configuring to interact with Azure DevOps
export AZDO_PERSONAL_ACCESS_TOKEN=MY_CREATED_TOKEN_ON_AZURE_DEVOPS
export AZDO_ORG_SERVICE_URL=https://dev.azure.com/MY_AZURE_DEVOPS_ORGANIZATION
Commands to run this sample:
Validating that our code and configurations are correct
terraform init -reconfigure -backend-config="variables/backend_dev.conf" && terraform validate && terraform plan -var-file="variables/variable_dev.tfvars"
Create the resources
terraform apply -var-file="variables/variable_dev.tfvars" -auto-approve
Commands to destroy this sample:
Validating that our code and configurations are correct
terraform init -reconfigure -backend-config="variables/backend_dev.conf" && terraform validate && terraform plan -var-file="variables/variable_dev.tfvars" -destroy
Destroying the resources
terraform apply -var-file="variables/variable_dev.tfvars" -auto-approve -destroy
Connecting to AKS:
az account set --subscription SET_YOUR_SUBSCRIPTION
az aks get-credentials --resource-group SET_YOUR_RG --name SET_YOUR_AKS_NAME --overwrite-existing
ls -al ~/.kube/config
Copying kubeconfig to Windows
mkdir /mnt/c/Users/SET_WINDOWS_USER/.kube
cp ~/.kube/config /mnt/c/Users/SET_WINDOWS_USER/.kube/
Switch between clusters
kubectl config use-context SET_YOUR_AKS_NAME
Testing K8S commands:
kubectl get ns
kubectl -n kube-system get po
Creating a deployment in AKS:
kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nettools
namespace: default
labels:
app: nettools
version: v1
spec:
replicas: 1
selector:
matchLabels:
app: nettools
version: v1
template:
metadata:
labels:
app: nettools
version: v1
spec:
containers:
- name: nettools
image: jbaldeon/nettools:2
command:
- sh
- '-c'
- sleep 3000
ports:
- containerPort: 22
protocol: TCP
imagePullPolicy: Always
restartPolicy: Always
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25%
maxSurge: 25%
revisionHistoryLimit: 1
EOF
Price: USD $5
Jimmy Baldeón
19 december, 2023
Secure your copy now. After payment, receive an email with the download link. Let's innovate together!
Unlock the full potential of Terraform and Azure Pipeline effortlessly with my affordable, meticulously crafted sample codes. Elevate your projects without the hassle. If you encounter challenges tailoring the code to your needs, fear not - I offer comprehensive support plans tailored to assist you at every step. Choose the level of support that fits you best and let's build together with confidence.