This document provides a detailed step-by-step guide for setting up CI/CD deployment using the Kubernetes client-go library within your Go application. If your project has not yet set up a CI/CD pipeline, suggestions for next steps are included.

Prerequisites

  1. Ensure you have a working Kubernetes cluster and that your kubectl is configured:

    kubectl get nodes
    
  2. Clone the example repository for deployment operations:

    git clone https://github.com/kubernetes/client-go.git
    cd client-go/examples/create-update-delete-deployment
    
  3. Make sure you use the correct Golang Module for your project:

    go mod init k8s.io/client-go
    

Building the Application

To create an executable application that manages Kubernetes deployments, compile the example code:

go build -o ./app

Running the Application

You can run the application with your local kubeconfig file. The following command will execute the deployment management operations on your cluster:

./app
# Optionally, specify a kubeconfig file with the flag
./app -kubeconfig=$HOME/.kube/config

Deployment Operations

The application will perform a series of operations to manage deployments:

  1. Create Deployment: This will create a deployment with a specified number of replicas. The application will then wait for the user to confirm before proceeding.

    The expected output will look like:

    Creating deployment...
    Created deployment "demo-deployment".
    -> Press Return key to continue.
    

    After creation, you can verify the pods:

    kubectl get pods
    
  2. Update Deployment: The next step updates the existing deployment by changing the replica count and the container image. The output will reflect the update:

    Updating deployment...
    Updated deployment...
    -> Press Return key to continue.
    

    Verify the update with:

    kubectl describe deployment demo
    
  3. List Deployments: This action retrieves deployments in the default namespace, displaying their names and replica counts:

    Listing deployments in namespace "default":
    * demo-deployment (1 replicas)
    -> Press Return key to continue.
    
  4. Delete Deployment: Finally, you can delete the deployment which will remove both the deployment object and its dependent ReplicaSet resource. The application will display:

    Deleting deployment...
    Deleted deployment.
    

    Validate the deletion by running:

    kubectl get deployments
    

Implementing CI/CD

To implement CI/CD for your project, consider leveraging platforms like GitHub Actions, GitLab CI/CD, or Jenkins. Below are key steps to set up CI/CD:

  1. Containerization: Use Docker to package the application. Refer to the Dockerfile below that sets the application as an entry point:

    FROM golang:alpine AS builder
    WORKDIR /app
    COPY . .
    RUN go build -o ./app
    
    FROM alpine:latest
    WORKDIR /root/
    COPY --from=builder /app .
    ENTRYPOINT ["/app"]
    
  2. Continuous Integration: Configure your CI pipeline to run tests and build your container image when code is pushed to the repository.

  3. Continuous Deployment: Integrate deployment steps in your CI pipeline to deploy the container to your Kubernetes cluster. You could use kubectl commands directly within your CI configuration to apply changes when a new image is available.

If CI/CD Is Not Yet Setup

If your project does not currently have CI/CD configured, it is recommended to take the following next steps:

  1. Define a CI/CD Strategy: Determine your deployment frequency and available resources.

  2. Select CI/CD Tools: Research and choose a tool that fits your requirements.

  3. Create a Build Pipeline: Start with a simple build pipeline and gradually include testing and deployment stages.

  4. Integrate Kubernetes: Ensure your CI/CD pipeline has access to your Kubernetes context (e.g., kubeconfig) for deploying applications.

By following these instructions, you will successfully incorporate CI/CD practices into your Kubernetes-based applications utilizing the client-go library.

For further details on the implementation, refer to the Kubernetes documentation: Kubernetes.