build-pod.yaml
在Jenkinsfile里面定义agent 使用这个yaml file
agent {kubernetes {idleMinutes 3// how long the pod will live after no jobs have run on ityamlFile './build-pod.yaml'// path to the pod definition relative to the root of our projectdefaultContainer 'docker'// define a default container if more than a few stages use it, otherwise default to jnlp container}下面步骤是 docker login/build/tag/push
environment { DOCKER_HUB_REGISTRY='https://index.docker.io/v1/' DOCKER_HUB_CREDS = credentials('wade_test_dockerhub') }stage('Build and Push Docker Image') {steps {script {dir(dir_path) {container('docker') {// docker login, Using single-quotes instead of double-quotes when referencing these sensitive environment variables prevents this type of leaking.sh 'echo $DOCKER_HUB_CREDS_PSW | docker login -u $DOCKER_HUB_CREDS_USR --password-stdin $DOCKER_HUB_REGISTRY'// build image with git tagsh """docker build -t $PROJECT_IMAGE_WITH_TAG .docker tag $PROJECT_IMAGE_WITH_TAG $DOCKER_HUB_CREDS_USR/$PROJECT_IMAGE_WITH_TAG"""// push image_tag to docker hubsh """docker push $DOCKER_HUB_CREDS_USR/$PROJECT_IMAGE_WITH_TAG"""}}}}}我这里没有选择用 docker.withRegistry
docker.withRegistry("$DOCKER_HUB_REGISTRY", "$DOCKER_HUB_CREDENTIAL") {}因为会有不安全的log提示
WARNING! Using --password via the CLI is insecure. Use --password-stdin.CI + Kustomize + CD这个例子是上面的 CI 之后 加上 - 利用 Kustomize build K8S resource manifests 然后 CD 到一个 Cluster
Kustomize 可以参考 云原生之旅 - 6)不能错过的一款 Kubernetes 应用编排管理神器 Kustomize

文章插图

文章插图
// assume your k8s manifests in another repo, mine is same repo, just in order to show git clone stepstage('Checkout K8S manifests') {steps {script {dir(dir_path) {container('git') {if (! fileExists('learning_by_doing/README.md')) {sh """git clone https://github.com/wadexu007/learning_by_doing.gitls -lhrt"""} else {sh 'echo manifes repo already exist.'}}}}}}stage('Build manifests with Kustomize') {steps {script {dir(dir_path) {container('kubectl-kustomize') {sh """cd learning_by_doing/Kustomize/demo-manifests/services/demo-app/dev/kustomize edit set image $DOCKER_HUB_CREDS_USR/$PROJECT_IMAGE_WITH_TAGkustomize build > $WORKSPACE/$dir_path/deployment.yaml"""}}}}}stage('Deploy to GKE test cluster') {environment{PROJECT_ID = 'xperiences-eng-cn-dev'CLUSTER_NAME = 'xpe-spark-test-gke'REGION = 'asia-east2'CREDENTIALS_ID = 'gcp_sa_json_key'}steps {script {dir(dir_path) {container('kubectl-kustomize') {sh """chown 1000:1000 deployment.yamlecho start to deploy to cluster $CLUSTER_NAME"""step([$class: 'KubernetesEngineBuilder',projectId: env.PROJECT_ID,clusterName: env.CLUSTER_NAME,location: env.REGION,manifestPattern: 'deployment.yaml',credentialsId: env.CREDENTIALS_ID,verifyDeployments: false])// verifyDeployments does not work for non-default namespace}}}}}Pipeline: Input Step这个例子是利用 Jenkins pipeline的 Input step 来做一个人工介入Approve的步骤 。然后再来一个多cluster 部署,选不同region 部署到不同的cluster的示例 。stage('Wait for SRE Approval') {steps {timeout(time:72, unit:'HOURS') {input message: "Approved Prod deployment?", submitter: 'sre-team'}}}// deployment to multipe k8s clustersstage('Deploy to GKE Prod cluster') {environment{PROJECT_ID = 'sre-cn-dev'CREDENTIALS_ID = 'gcp_sa_json_key'CLUSTER_COMMON_NAME = 'demo-gke-prod'}steps {script {env.REGION = input message: 'Choose which region you want to deploy?',parameters: [choice(name: 'Region',description: 'Select Region to Deloy',choices: ['europe-west1', 'us-central1'])]dir(dir_path) {if ( env.REGION == "europe-west1" ) {def eu_cluster_name = env.CLUSTER_COMMON_NAME + "-eu"container('kubectl-kustomize') {sh "echo deploy to cluster $eu_cluster_name in region: $REGION"}}if ( env.REGION == "us-central1" ) {def us_cluster_name = env.CLUSTER_COMMON_NAME + "-us"container('kubectl-kustomize') {sh "echo deploy to cluster $us_cluster_name in region: $REGION"}}}}}}所有例子均在我的 github repo 。
### 本文首发于博客园 https://www.cnblogs.com/wade-xu/p/16863955.html
测试现在你可以创建一个 Pipeline 或者 Multibranch Pipeline job 来测试 。Repository URL = `https://github.com/wadexu007/learning_by_doing`Script Path, e.g. `Jenkins/k8s_pod_as_build_agent/demo-app-java/Jenkinsfile` 你会看到每启动一个job 都会相应的产生一个pod 来作为Jenkins agent运行,结束后根据idleMinutes自动释放 。

文章插图
总结如果你已经成功创建并测试 CI/CD pipeline,可以继续加强,比如加上 Post notifications 最佳实践
推荐阅读
- 云顶之弈冒险迭嘉阵容搭配推荐攻略
- 云原生之旅 - 10)手把手教你安装 Jenkins on Kubernetes
- 问道手游探案镖局风云任务完成图文攻略
- 又拍云之 Keepalived 高可用部署
- 我的Vue之旅 10 Gin重写后端、实现页面详情页 Mysql + Golang + Gin
- 云顶之弈8月11日12.15版本更新维护公告
- 京东云开发者|mysql基于binlake同步ES积压解决方案
- 奥比岛2022盛夏之旅活动参与方法
- 京东云开发者|软件架构可视化及C4模型:架构设计不仅仅是UML
- 奥比岛:梦想国度暗夜袭击魔药店失窃疑云答案是什么