K8S CI/CD Automated Deployment
Services created by sponge support automated building and deployment to a Kubernetes cluster via Jenkins. Below is a detailed guide on using Jenkins for the complete CI/CD process.
Alternative
If you prefer to use GitLab CI/CD, you can write your own .gitlab-ci.yml
file. All build and deployment scripts are provided in the project's scripts
directory.
Setting up the Jenkins-Go Build Platform
Preparing the Base Image
We provide a pre-built Docker image jenkins-go, which integrates the Go language compilation environment. If you need a custom image, you can refer to the Dockerfile for building.
Environment Requirements
- An available Kubernetes cluster
- kubectl command-line tool installed
- Ensure the container has permissions to operate on k8s (needs to copy authentication files from the
.kube
directory)
Starting the Jenkins Service
Use the following docker-compose.yml configuration:
version: "3.7"
services:
jenkins-go:
image: zhufuyi/jenkins-go:2.37
restart: always
container_name: "jenkins-go"
ports:
- 38080:8080
#- 50000:50000
volumes:
- $PWD/jenkins-volume:/var/jenkins_home
# docker configuration
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /root/.docker/:/root/.docker/
# k8s api configuration directory, including config file
- /usr/local/bin/kubectl:/usr/local/bin/kubectl
- /root/.kube/:/root/.kube/
# go related tools
- /opt/go/bin/golangci-lint:/usr/local/bin/golangci-lint
Start command:
docker-compose up -d
Access address: http://localhost:38080
Getting the initial password:
docker exec jenkins-go cat /var/jenkins_home/secrets/initialAdminPassword
Initial Configuration
Install necessary plugins:
- Locale (for Chinese support)
- Extended Choice Parameter (for parameterized builds)
- Git Parameter (for Git parameter support)
- Role-based Authorization Strategy (for permission management)
Set Chinese interface:
[Manage Jenkins] → [Configure System] → Set Locale tozh_CN
Configure Image Repository:
# Development environment DEV_REGISTRY_HOST http://localhost:27070 # Test environment TEST_REGISTRY_HOST http://localhost:28080 # Production environment PROD_REGISTRY_HOST http://localhost:29090
Creating a Jenkins Job Template
Create a New Job
Parameterized Configuration
UseGIT_parameter
as the parameter namePipeline Configuration
Build Execution
Select Branch/Tag via [Build with Parameters]
Service Deployment Practice
Taking the "user" web service as an example, the steps are as follows:
Prerequisites
Upload the code to a Git repository
Configure Docker image repository access permissions and create a Kubernetes access secret:
kubectl create secret generic docker-auth-secret \ --from-file=.dockerconfigjson=/root/.docker/config.json \ --type=kubernetes.io/dockerconfigjson
Create Kubernetes resources:
cd deployments/kubernetes kubectl apply -f ./*namespace.yml kubectl apply -f ./*configmap.yml kubectl apply -f ./*svc.yml
(Optional) Configure DingTalk notifications:
Modify thetel_num
andaccess_token
fields inJenkinsfile
Deployment Verification
Check Pod status:
kubectl get all -n user
Expected output:
NAME READY STATUS RESTARTS AGE pod/user-dm-77b4bcccc5-8xt8v 1/1 Running 0 21m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/user-svc ClusterIP 10.108.31.220 <none> 8080/TCP 27m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/user-dm 1/1 1 1 21m NAME DESIRED CURRENT READY AGE replicaset.apps/user-dm-77b4bcccc5 1 1 1 21m
Local Testing:
# Port forwarding kubectl port-forward --address=0.0.0.0 service/user-svc 8080:8080 -n user # API test curl http://localhost:8080/api/v1/teacher/1
Service Template Description
Web/gRPC services created by sponge already include:
- Jenkinsfile pipeline configuration
- Image build scripts
- Kubernetes deployment manifests
Users can use them directly or modify them according to actual needs.
