Yes, you can pre-create your Kubernetes manifests on your MicroK8s cluster to avoid redeploying them with every CI/CD run. By doing this, your CI/CD pipeline will focus solely on building and updating the Docker image, while the existing Kubernetes resources (like Deployments, Services, and Ingresses) remain unchanged unless explicitly modified.
Here's how you can achieve this:
1. Manually Apply Manifests to Your MicroK8s Cluster
First, ensure that your Kubernetes manifests (e.g., deployment.yaml, service.yaml, ingress.yaml) are correctly defined and tailored for your environment.
Steps:
-
Access Your MicroK8s Cluster:
microk8s kubectl get nodes
-
Apply the Manifests:
Navigate to the directory containing your Kubernetes manifests and apply them:
microk8s kubectl apply -f ./kubernetes/deployment.yaml
microk8s kubectl apply -f ./kubernetes/service.yaml
microk8s kubectl apply -f ./kubernetes/ingress.yaml
-
Verify the Resources:
Ensure that the resources are created successfully:
microk8s kubectl get deployments
microk8s kubectl get services
microk8s kubectl get ingress
2. Modify Your CI/CD Pipeline to Update Only the Docker Image
Since the Kubernetes resources are already in place, your CI/CD pipeline doesn't need to handle manifest deployment. Instead, it should focus on building the Docker image and updating the image in the existing Kubernetes Deployment.
Updated GitHub Actions Workflow:
name: CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: '20.x'
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Build project
run: pnpm build
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: testCompany/backend:latest
- name: Setup kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
- name: Update Kubernetes Deployment with New Image
env:
KUBECONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
kubectl set image deployment/<deployment-name> <container-name>=testCompany/backend:latest -n backend-api
Explanation of Changes:
-
Remove the Deploy Step with Manifests:
-
Add Steps to Setup kubectl and Update the Deployment:
-
Setup kubectl:
Ensures that kubectl is available in the runner environment.
- name: Setup kubectl
uses: azure/setup-kubectl@v3
with:
version: 'latest'
-
Update Deployment Image:
Uses kubectl set image to update the container image in the existing Deployment.
- name: Update Kubernetes Deployment with New Image
env:
KUBECONFIG: ${{ secrets.KUBE_CONFIG }}
run: |
kubectl set image deployment/<deployment-name> <container-name>=testCompany/backend:latest -n backend-api
- Replace
<deployment-name> with the name of your Deployment.
- Replace
<container-name> with the name of the container within the Deployment you want to update.
- The
-n backend-api flag specifies the namespace. Ensure it's correct for your setup.
3. Store Kubernetes Manifests Appropriately
Since you're using MicroK8s, the manifests can reside in your repository or a dedicated infrastructure repository. Here's how you can manage them:
4. Ensure Secure Access to Your Cluster
- Kubeconfig Secret:
- Ensure that your
kubeconfig file is securely stored as a GitHub secret (KUBE_CONFIG in your example).
- This file should have the necessary permissions to update the Deployment in the
backend-api namespace.
5. Benefits of This Approach
- Efficiency: Reduces the number of steps in your CI/CD pipeline, leading to faster deployments.
- Stability: Prevents unintended changes to Kubernetes resources, as manifests aren't redeployed on every run.
- Separation of Concerns: Keeps infrastructure management separate from application code deployment.
6. Additional Recommendations
By pre-applying your Kubernetes manifests to the MicroK8s cluster and modifying your CI/CD pipeline to update only the Docker image, you streamline your deployment process, reduce redundancy, and maintain a clear separation between your application code and infrastructure configurations.