Skip to main content

Introduction

Automating the deployment of applications to a Kubernetes cluster is a crucial aspect of modern DevOps practices. GitLab CI/CD provides a robust platform for continuous integration and continuous deployment (CI/CD), and it can be seamlessly integrated with Kubernetes to automate the entire deployment process. In this blog post, we will explore how to use GitLab CI/CD to deploy applications on a Kubernetes cluster using a private Docker repository for storing container images.

Prerequisites

Before we begin, ensure you have the following:

  1. A running Kubernetes cluster.
  2. GitLab instance (self-hosted or GitLab.com).
  3. Access to a private Docker registry (e.g., Docker Hub, GitLab Container Registry).
  4. kubectl configured to interact with your Kubernetes cluster.
  5. Docker installed on your local machine.

Step 1: Set Up GitLab Project

Create a new GitLab project or use an existing one. This project will contain the source code of your application and the configuration for the CI/CD pipeline.

Step 2: Write a Dockerfile

Create a Dockerfile in your project root to define how your application should be containerized. Here is an example Dockerfile for a simple Node.js application:

Dockerfile
# Use the official Node.js image as the base image
FROM node:14
# Set the working directory
WORKDIR /app
# Copy the package.json and package-lock.json files
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Command to run the application
CMD ["npm", "start"]

Step 3: Set Up GitLab CI/CD Pipeline

Create a .gitlab-ci.yml file in the root of your project to define the CI/CD pipeline. This file will contain the stages and jobs for building the Docker image, pushing it to the private registry, and deploying it to the Kubernetes cluster.

Here’s an example .gitlab-ci.yml:

yaml
stages:
- build
- deploy
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build:
stage: build
script:
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
only:
- main
deploy:
stage:
deploy
image: bitnami/kubectl:latest
script:
- kubectl set image deployment/my-app my-app=$IMAGE_TAG --record
only:
- main
environment:
name: production
url: https://my-app.example.com
dependencies:
- build

Step 4: Configure CI/CD Variables

In your GitLab project, go to Settings > CI / CD > Variables and add the following variables:

  • CI_REGISTRY: The URL of your private Docker registry (e.g., registry.gitlab.com).
  • CI_REGISTRY_USER: Your Docker registry username.
  • CI_REGISTRY_PASSWORD: Your Docker registry password or access token.
  • KUBECONFIG: The base64-encoded content of your ~/.kube.config file. This provides GitLab with access to your Kubernetes cluster.

Step 5: Create Kubernetes Deployment

Create a Kubernetes deployment file (deployment.yaml) in your project to define how your application should be deployed. Here’s an example for a Node.js application:

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: registry.gitlab.com/your-namespace/your-project:$CI_COMMIT_SHA
ports:
- containerPort: 3000

Step 6: Apply the Kubernetes Deployment

Ensure that the deployment file is applied to your Kubernetes cluster. You can manually apply it once to create the initial deployment:

sh
kubectl apply -f deployment.yaml

Subsequent updates to the image will be handled by the GitLab CI/CD pipeline, which uses kubectl set image to update the deployment.

Step 7: Commit and Push Changes

Commit and push your changes to the GitLab repository:

sh
git add .
git commit -m "Add Dockerfile and CI/CD pipeline configuration"
git push origin main

Step 8: Monitor the Pipeline

Navigate to the CI/CD > Pipelines section in your GitLab project to monitor the progress of your pipeline. The pipeline should go through the build and deploy stages, building the Docker image, pushing it to the private registry, and updating the Kubernetes deployment.

Conclusion

Using GitLab CI/CD to deploy applications on Kubernetes with a private Docker repository streamlines your development workflow, ensuring that your applications are automatically built, tested, and deployed with every code change. This setup not only enhances productivity but also improves the reliability and security of your deployments. By following the steps outlined in this post, you can leverage the full power of GitLab CI/CD and Kubernetes to achieve continuous deployment for your containerized applications.

By adopting these best practices, you can ensure a smooth and efficient CI/CD process, leading to faster and more reliable application deployments. Happy deploying!