集群使用containerd 作为运行时

调度到test k8s集群构建


test k8s 集群必须要存在这个devops命名空间

docker in docker 模版

pods 模版
yaml
apiVersion: v1
kind: Pod
metadata:
spec:
volumes:
- name: certs-ca
emptyDir: {}
- name: certs-client
emptyDir: {}
- name: mvn-cache
persistentVolumeClaim:
claimName: mvn-cache-pvc
containers:
- name: dind
image: docker:24.0-dind
securityContext:
privileged: true
env:
- name: DOCKER_TLS_CERTDIR
value: /certs
volumeMounts:
- name: certs-ca
mountPath: /certs/ca
- name: certs-client
mountPath: /certs/client
tty: true
- name: docker
image: docker:24.0
command:
- cat
tty: true
env:
- name: DOCKER_TLS_CERTDIR
value: /certs
- name: DOCKER_HOST
value: tcp://localhost:2376
- name: DOCKER_TLS_VERIFY
value: "1"
- name: DOCKER_CERT_PATH
value: /certs/client
volumeMounts:
- name: certs-client
mountPath: /certs/client
readOnly: true
最后写jenkinsfile进行构建
c
pipeline {
options{
timestamps ()
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
}
environment {
image ="docker.com/xxx/$JOB_NAME:$BUILD_ID"
}
agent {
kubernetes {
cloud 'test-k8s'
inheritFrom 'test'
}
}
parameters {
string(name: 'BRANCH', defaultValue: 'v10.1.3', description: 'Enter the branch name')
}
stages {
stage('Get Code ') {
steps {
script {
def branch = params.BRANCH
sh("ls -al ${env.WORKSPACE}")
deleteDir()
sh("ls -al ${env.WORKSPACE}")
checkout scmGit(branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'git', url: 'http://dockert-boot.git']])
}
}
}
stage ('Maven Buid') {
steps {
container('mvn') {
sh 'mvn clean install -DskipTests'
}
}
}
stage('Docker Build and Push') {
steps {
container('docker') {
withDockerRegistry(credentialsId: 'hub', url: 'https://hub.docker.com') {
script {
def builtImage = docker.build("${image}", ".")
builtImage.push()
sh "docker rmi ${image}"
}
}
}
}
}
stage('Connect K8s and TEST') {
steps {
container('kubectl') {
withKubeConfig(credentialsId: 'test-k8s') {
sh 'kubectl -n XX set image deployment/boot boot=${image}'
}
}
}
}
}
}
注意

模版也是

关键点否则无法调度test 集群,jenkins添加多集群管理不在讲了,之前有讲过
js
environment {
image ="docker.com/xxx/$JOB_NAME:$BUILD_ID"
}
agent {
kubernetes {
cloud 'test-k8s'
inheritFrom 'test'
}
}