Setting Up CI/CD Pipelines with Docker and Kubernetes on Azure
In today's fast-paced software development landscape, Continuous Integration (CI) and Continuous Deployment (CD) have become essential practices for delivering high-quality applications rapidly. With the increasing adoption of containerization and orchestration technologies like Docker and Kubernetes, setting up CI/CD pipelines has never been more efficient, especially on cloud platforms like Azure. This article will guide you through the process of setting up CI/CD pipelines using Docker and Kubernetes on Azure, providing detailed instructions, code examples, and troubleshooting tips.
What is CI/CD?
Continuous Integration (CI) is the practice of automatically integrating code changes from multiple contributors into a shared repository several times a day. The goal is to detect and address issues early in the development cycle.
Continuous Deployment (CD) extends CI by automatically deploying all code changes to production after the build stage. This allows developers to deliver new features and fixes to users quickly and reliably.
Why Use Docker and Kubernetes?
Docker
Docker is a platform that automates the deployment of applications inside lightweight, portable containers. Containers package the application code along with its dependencies, ensuring consistency across different environments.
Kubernetes
Kubernetes is an orchestration platform for managing containerized applications. It automates deployment, scaling, and operations of application containers across clusters of hosts.
Use Cases
- Microservices Architecture: Docker and Kubernetes excel in deploying microservices, allowing for independent scaling and management.
- Environment Consistency: Developers can ensure that the application runs the same way in development, testing, and production environments.
- Scalability: Kubernetes allows for easy scaling of applications based on demand.
Setting Up the CI/CD Pipeline on Azure
Prerequisites
- Azure Account: Create an Azure account if you don’t have one.
- Azure CLI: Install the Azure CLI to manage Azure resources from the command line.
- Docker: Install Docker to create and manage containers.
- Kubernetes Cluster: Set up an Azure Kubernetes Service (AKS) cluster.
Step 1: Create a Dockerfile
Begin by creating a Dockerfile
for your application. This file defines how your application is built and packaged into a container.
# Use the official Node.js image as a base
FROM node:14
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 8080
# Command to run the application
CMD ["node", "app.js"]
Step 2: Build and Push Docker Image
Build your Docker image and push it to Azure Container Registry (ACR).
- Log in to Azure:
bash
az login
- Create a resource group:
bash
az group create --name myResourceGroup --location eastus
- Create an Azure Container Registry:
bash
az acr create --resource-group myResourceGroup --name myContainerRegistry --sku Basic
- Log in to ACR:
bash
az acr login --name myContainerRegistry
- Build and push the Docker image:
bash
docker build -t myapp:latest .
docker tag myapp:latest myContainerRegistry.azurecr.io/myapp:latest
docker push myContainerRegistry.azurecr.io/myapp:latest
Step 3: Set Up Azure Kubernetes Service (AKS)
- Create an AKS cluster:
bash
az aks create --resource-group myResourceGroup --name myAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
- Configure
kubectl
to use the new AKS cluster:
bash
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
Step 4: Deploy to Kubernetes
Create a Kubernetes deployment YAML file (deployment.yaml
) to deploy your application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myContainerRegistry.azurecr.io/myapp:latest
ports:
- containerPort: 8080
Deploy the application:
kubectl apply -f deployment.yaml
Step 5: Expose the Application
Create a service to expose your application:
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: myapp
Apply the service configuration:
kubectl apply -f service.yaml
Step 6: Set Up CI/CD in Azure DevOps
- Create a new project in Azure DevOps.
- Create a new pipeline and configure it to use your repository.
- Define the build pipeline:
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: Docker@2
inputs:
command: 'buildAndPush'
repository: 'myContainerRegistry/myapp'
dockerfile: '**/Dockerfile'
tags: |
$(Build.BuildId)
- Define the release pipeline to deploy to AKS.
Troubleshooting Tips
- Check Logs: Use
kubectl logs <pod-name>
to check pod logs for errors. - Inspect Pods: Run
kubectl get pods
to see if all pods are running correctly. - Container Registry Issues: Ensure you have pushed the image correctly to ACR and that your AKS has the right permissions to pull images.
Conclusion
Setting up CI/CD pipelines with Docker and Kubernetes on Azure can greatly enhance your development workflow, allowing for rapid deployment and reliable application performance. By following this guide, you can automate your deployment process and focus on delivering high-quality software. Embrace the power of automation and containerization to streamline your development process today!