在 Ubuntu 上落地 Kubernetes 的 CI/CD 可按“工具选型 → 环境准备 → 流水线实现 → 安全与运维”的路径推进。下面给出可直接复用的方案与关键注意事项。
一、方案总览与工具选型
二、环境与前置准备
三、流水线实现示例
Jenkins 示例(声明式流水线)
pipeline {
agent any
environment {
IMAGE = "your-registry/your-app:${env.BUILD_NUMBER}"
K8S_NS = "default"
}
stages {
stage('Build') {
steps { sh 'docker build -t $IMAGE .' }
}
stage('Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
sh 'docker push $IMAGE'
}
}
}
stage('Deploy') {
steps {
sh "kubectl set image deployment/your-app container=$IMAGE -n $K8S_NS"
sh "kubectl rollout status deployment/your-app -n $K8S_NS"
}
}
}
}
GitLab CI 示例(.gitlab-ci.yml)
stages:
- build
- deploy
build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
deploy:
stage: deploy
script:
- kubectl set image deployment/my-app my-app=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- kubectl rollout status deployment/my-app
only:
- main
Argo CD(GitOps 交付)
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
kubectl port-forward svc/argocd-server -n argocd 8080:443
Tekton(Kubernetes 原生 CI)
四、安全与运维最佳实践