Skip to main content

Command Palette

Search for a command to run...

Day 34: CI/CD - GitHub Actions with Docker, and Kubernetes Deployment

Updated
3 min readView as Markdown
A

Experienced Senior DevOps Engineer with a passion for optimizing software development and delivery processes. Excels in designing and implementing CI/CD pipelines, automating infrastructure, and optimizing cloud architectures. Proficient in a wide range of DevOps tools such as Docker, Kubernetes, Jenkins, Ansible, Git, and AWS services. Strong collaborator, adept at fostering cross-functional teamwork and continuous improvement. Thrives in dynamic environments, utilizing problem-solving skills to overcome complex challenges. Dedicated to delivering high-quality software products on time and within budget.

In today's fast-paced software development landscape, automating the deployment process is crucial to ensure efficiency and consistency. This guide will walk you through the process of setting up a GitHub Action workflow, building a Docker image using a Dockerfile, and deploying the image to a Kubernetes cluster.

Deploying a Node.js Application to Kubernetes

Step 1: Setting up GitHub Repository

  1. Create a new repository named "nodejs-k8s-deployment" on GitHub.

  2. Clone the repository to your local machine: git clone https://github.com/your-username/nodejs-k8s-deployment.git

  3. Navigate to the repository's directory: cd nodejs-k8s-deployment

Step 2: Dockerfile Creation

  1. Create a Dockerfile in the repository directory:

     # Use the official Node.js image as the base image
     FROM node:14
    
     # Set the working directory
     WORKDIR /app
    
     # Copy package.json and package-lock.json to the working directory
     COPY package*.json ./
    
     # Install dependencies
     RUN npm install
    
     # Copy application code
     COPY . .
    
     # Specify the startup command
     CMD ["node", "app.js"]
    
  2. Commit the Dockerfile to your repository:

     git add Dockerfile
     git commit -m "Add Dockerfile for Node.js app"
     git push origin main
    

Step 3: GitHub Action Workflow

  1. Create a .github/workflows/build-deploy.yaml file:

     name: Build and Deploy
    
     on:
       push:
         branches:
           - main
    
     jobs:
       build-deploy:
         runs-on: ubuntu-latest
    
         steps:
           - name: Checkout code
             uses: actions/checkout@v2
    
           - name: Set up Docker
             uses: actions/setup-docker@v2
    
           - name: Build Docker image
             run: docker build -t your-username/nodejs-app:latest .
    
           - name: Log in to Docker Hub
             uses: docker/login-action@v1
             with:
               username: ${{ secrets.DOCKER_USERNAME }}
               password: ${{ secrets.DOCKER_PASSWORD }}
    
           - name: Push Docker image
             run: docker push your-username/nodejs-app:latest
    
           - name: Deploy to Kubernetes
             run: kubectl apply -f kubernetes/
    
  2. Commit and push the workflow YAML file:

     git add .github/workflows/build-deploy.yaml
     git commit -m "Add GitHub Action workflow for build and deploy"
     git push origin main
    

Step 4: Docker Registry Setup (if needed)

  1. Create Docker Hub account and create a repository named "nodejs-app".

  2. Generate a Docker Hub access token and save it as a secret in your GitHub repository named DOCKER_USERNAME and DOCKER_PASSWORD.

Step 5: Kubernetes Configuration

  1. Create a kubernetes directory in the repository.

  2. Inside the kubernetes directory, create a deployment.yaml file:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: nodejs-app
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: nodejs-app
       template:
         metadata:
           labels:
             app: nodejs-app
         spec:
           containers:
             - name: nodejs-app
               image: your-username/nodejs-app:latest
               ports:
                 - containerPort: 3000
    
  3. Create a service.yaml file:

     apiVersion: v1
     kind: Service
     metadata:
       name: nodejs-app
     spec:
       selector:
         app: nodejs-app
       ports:
         - protocol: TCP
           port: 80
           targetPort: 3000
       type: LoadBalancer
    
  4. Commit the Kubernetes manifests to your repository:

     git add kubernetes/
     git commit -m "Add Kubernetes deployment and service manifests"
     git push origin main
    

Step 6: Deploying to Kubernetes

  1. Update the GitHub Action workflow to apply Kubernetes manifests:

     # ...
           - name: Deploy to Kubernetes
             run: |
               kubectl apply -f kubernetes/
               kubectl rollout restart deployment/nodejs-app
    
  2. Commit and push the changes to the workflow YAML file.

Step 7: Testing the Workflow

  1. Make a code change in your Node.js application.

  2. Push the change to your repository.

  3. Monitor the GitHub Actions workflow under the "Actions" tab in your repository to ensure it completes successfully.

  4. Access your Kubernetes cluster and verify that the updated application is running.

Conclusion:

Congratulations! You've successfully set up a GitHub Action workflow that builds a Docker image using a Dockerfile and deploys it to a Kubernetes cluster. This automated process will streamline your development and deployment workflows, making them more consistent and efficient.

Remember to regularly review and update your workflows as your application and infrastructure evolve. Happy coding and deploying!

Thanks for reading! I hope you found this blog informative and insightful. For more technology-related content, don't forget to follow me on GitHub and LinkedIn

More from this blog

Untitled Publication

68 posts