Implementing Continuous Integration and Continuous Deployment (CI/CD) pipelines is essential for modern software development, especially when deploying applications on Kubernetes. This article guides you through setting up CI/CD pipelines for Express applications hosted on Kubernetes using Jenkins and GitLab.

Prerequisites

  • Basic knowledge of Docker, Kubernetes, Jenkins, and GitLab
  • Access to a Kubernetes cluster
  • GitLab repository for your Express application
  • Jenkins server installed and configured
  • kubectl configured to access your cluster

Setting Up the GitLab Repository

Start by creating a GitLab repository for your Express application. Ensure your project includes a Dockerfile for containerizing the app and a .gitlab-ci.yml file to define CI/CD stages.

Sample Dockerfile

FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "app.js"]

Sample .gitlab-ci.yml

stages: - build - deploy build: stage: build image: docker:latest services: - docker:dind script: - docker build -t registry.gitlab.com/yourgroup/yourproject:latest . - docker push registry.gitlab.com/yourgroup/yourproject:latest deploy: stage: deploy image: bitnami/kubectl:latest script: - kubectl apply -f k8s/deployment.yaml - kubectl rollout status deployment/your-deployment

Configuring Jenkins

Set up Jenkins with necessary plugins such as GitLab Plugin and Kubernetes Plugin. Configure Jenkins credentials to access your GitLab repository and Kubernetes cluster.

Creating a Jenkins Pipeline

In Jenkins, create a new pipeline job. Use the following pipeline script as a starting point:

pipeline { agent any stages { stage('Checkout') { steps { git 'https://gitlab.com/yourgroup/yourproject.git' } } stage('Build Docker Image') { steps { script { docker.build('registry.gitlab.com/yourgroup/yourproject:latest') } } } stage('Push Docker Image') { steps { script { docker.withRegistry('https://registry.gitlab.com', 'gitlab-registry-credentials') { docker.image('registry.gitlab.com/yourgroup/yourproject:latest').push() } } } } stage('Deploy to Kubernetes') { steps { kubernetesDeploy( configs: 'k8s/deployment.yaml', kubeconfigId: 'kubeconfig-credentials' ) } } } }

Creating Kubernetes Deployment Files

Create a deployment.yaml file in your k8s directory with the following content:

apiVersion: apps/v1 kind: Deployment metadata: name: express-app spec: replicas: 3 selector: matchLabels: app: express template: metadata: labels: app: express spec: containers: - name: express image: registry.gitlab.com/yourgroup/yourproject:latest ports: - containerPort: 3000

Additionally, create a service.yaml to expose your application:

apiVersion: v1 kind: Service metadata: name: express-service spec: type: LoadBalancer ports: - port: 80 targetPort: 3000 selector: app: express

Testing and Monitoring

After setting up the pipelines, trigger a build from GitLab or Jenkins. Monitor the pipeline stages for successful execution. Ensure your application is accessible via the Kubernetes service endpoint.

Implement monitoring tools such as Prometheus and Grafana for real-time insights and alerts on your application's health and performance.

Conclusion

Automating CI/CD pipelines for Kubernetes-hosted Express applications streamlines deployment processes, reduces errors, and accelerates delivery. By integrating GitLab and Jenkins, developers can maintain robust workflows that enhance productivity and application reliability.