Day 34: CI/CD - GitHub Actions with Docker, and Kubernetes Deployment
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
Create a new repository named "nodejs-k8s-deployment" on GitHub.
Clone the repository to your local machine:
git clonehttps://github.com/your-username/nodejs-k8s-deployment.gitNavigate to the repository's directory:
cd nodejs-k8s-deployment
Step 2: Dockerfile Creation
Create a
Dockerfilein 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"]Commit the
Dockerfileto your repository:git add Dockerfile git commit -m "Add Dockerfile for Node.js app" git push origin main
Step 3: GitHub Action Workflow
Create a
.github/workflows/build-deploy.yamlfile: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/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)
Create Docker Hub account and create a repository named "nodejs-app".
Generate a Docker Hub access token and save it as a secret in your GitHub repository named
DOCKER_USERNAMEandDOCKER_PASSWORD.
Step 5: Kubernetes Configuration
Create a
kubernetesdirectory in the repository.Inside the
kubernetesdirectory, create adeployment.yamlfile: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: 3000Create a
service.yamlfile:apiVersion: v1 kind: Service metadata: name: nodejs-app spec: selector: app: nodejs-app ports: - protocol: TCP port: 80 targetPort: 3000 type: LoadBalancerCommit 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
Update the GitHub Action workflow to apply Kubernetes manifests:
# ... - name: Deploy to Kubernetes run: | kubectl apply -f kubernetes/ kubectl rollout restart deployment/nodejs-appCommit and push the changes to the workflow YAML file.
Step 7: Testing the Workflow
Make a code change in your Node.js application.
Push the change to your repository.
Monitor the GitHub Actions workflow under the "Actions" tab in your repository to ensure it completes successfully.
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




