HighSkill

Automate Docker Deployment to Azure AKS with GitLab CI CD

Automate Docker Deployment to Azure AKS with GitLab CI CD

Automating Docker image deployment to Azure AKS using GitLab can drastically enhance your workflow efficiency, save time, and reduce manual errors. In this post, we'll walk through the precise steps needed to achieve seamless automation, ensuring your development pipeline is both streamlined and powerful.

Why Automate Docker Image Deployment?

Before diving into the how-to, it's crucial to understand why automating Docker image deployment to Azure AKS is essential:

  • Efficiency: Automation reduces the time required for deployments, allowing teams to focus on development and innovation.
  • Consistency: Automated deployment ensures that the environment setup is consistent across all deployments.
  • Scalability: As the project grows, manual deployment becomes untenable. Automation scales with the project's needs.
  • Error Reduction: Automation minimizes human errors that occur during manual deployment processes.

Prerequisites

To follow this tutorial, you will need:

  1. Docker knowledge: Basic understanding of Docker and containers.
  2. GitLab account: A GitLab account to set up CI/CD pipelines.
  3. Azure account: Access to Azure for AKS setup.
  4. AKS cluster: A running AKS (Azure Kubernetes Service) cluster.

Step 1: Setting Up GitLab Repository

Start by setting up a repository on GitLab. If you haven't created one yet, here's how to go about it:

  1. Go to your GitLab dashboard.
  2. Click on the "New Project" button.
  3. Follow the prompts to create your project.

Step 2: Dockerfile Creation

Create a Dockerfile at the root of your project. This file will define the Docker image for your application.

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Step 3: GitLab CI/CD Configuration

To automate the deployment, create a .gitlab-ci.yml file at the root of your repository. This file defines the CI/CD pipeline.

stages:
  - build
  - test
  - deploy

variables:
  DOCKER_TLS_CERTDIR: ""

before_script:
  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

build:
  stage: build
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

test:
  stage: test
  script:
    - docker run $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA python -m unittest discover

deploy:
  stage: deploy
  script:
    - echo "$KUBECONFIG_CONTENT" | base64 -d > $KUBECONFIG_FILE
    - kubectl apply -f kubernetes/deployment.yaml
    - kubectl set image deployment/myapp myapp=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  only:
    - master

Explanation:

  • Stages: Define the stages of the pipeline (build, test, deploy).
  • Variables: Set Docker-related environment variables.
  • Before Script: Log into the Docker registry.
  • Build Stage: Build the Docker image and push it to the registry.
  • Test Stage: Run unit tests using the built Docker image.
  • Deploy Stage: Deploy the Docker image to AKS.

Step 4: Kubernetes Deployment File

Create a kubernetes/deployment.yaml file to define the Kubernetes deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
        ports:
        - containerPort: 80

Step 5: Configuring Azure AKS

Now, configure your AKS cluster. This step assumes you already have an AKS cluster running.

  1. Get AKS credentials:
az aks get-credentials --resource-group <ResourceGroup> --name <ClusterName>
  1. Create a Service Principal:
az ad sp create-for-rbac --skip-assignment

Note the appId, password, and tenant.

  1. Assign roles:
az role assignment create --assignee <appId> --role Contributor --scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group>"

Step 6: Environment Variables in GitLab

Set the necessary environment variables in GitLab CI/CD Settings:

  • AZURE_CLIENT_ID: Your Service Principal appId.
  • AZURE_CLIENT_SECRET: Your Service Principal password.
  • AZURE_TENANT_ID: Your Azure tenant.
  • AZURE_SUBSCRIPTION_ID: Your Azure subscription ID.
  • KUBECONFIG_CONTENT: The base64 encoded content of your KUBECONFIG file.

Conclusion

Automating Docker image deployment to Azure AKS using GitLab significantly enhances your development workflow. By following the above steps, you can ensure a seamless, efficient, and error-free deployment process.

Feel free to leave a comment or share this post if you found it helpful. Automation is key to modern DevOps practices, and mastering this can set you apart in your career.

Key Takeaways:

  • Automation saves time and reduces errors.
  • Setting up a CI/CD pipeline with GitLab is straightforward.
  • Integrating with AKS ensures your applications are deployed consistently and reliably.

Think of this as the start of your automation journey. Keep exploring, and happy deploying!

Want more insights? Subscribe to our newsletter for the latest updates and guides in the world of DevOps.