1.方式获取k8s密钥
cat .kube/config
2.导出方式或者密钥
kubectl config view --raw > k8s-config-admin
pipeline {
agent {
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: devops
spec:
containers:
- name: docker
image: docker:19.03-dind
securityContext:
privileged: true
tty: true
hostPID: true
- name: kubectl
image: registry.cn-shenzhen.aliyuncs.com/jbjb/dbs:kubectl-128
command:
- cat
tty: true
'''
}
}
environment {
image = "registry.cn-shenzhen.aliyuncs.com/jbjb/dockers:$JOB_NAME-$BUILD_ID"
}
stages {
stage('Get Code') {
steps {
checkout scmGit(branches: [[name: '*/lld']], extensions: [], userRemoteConfigs: [[credentialsId: 'll', url: 'http://git.xxx.com/new-test.git']])
}
}
stage ("Docker Build") {
steps {
container('docker') {
script {
withDockerRegistry(credentialsId: 'mydocker', url: 'https://registry.cn-shenzhen.aliyuncs.com') {
docker.build("${image}").push()
}
}
}
}
}
stage('Deploy to Kubernetes') {
steps {
container('kubectl') {
withKubeCredentials(kubectlCredentials: [[credentialsId: 'kjbs', serverUrl: 'https://192.168.0.192:6443']]) {
sh 'kubectl get pods'
}
}
}
}
}
}
最后一个stps也可以换成这样
stage('Deploy to Kubernetes') {
steps {
container('kubectl') {
withKubeConfig(credentialsId: 'kjbs', serverUrl: 'https://192.168.0.192:6443') {
sh 'kubectl get pods'
}
}
}
}
根据你可以选择适合场景的代码块。如果只需要在单个步骤中使用简单的凭据信息,那么使用 withKubeCredentials 就足够了。而如果需要在多个步骤中使用相同的配置文件和凭据,使用 withKubeConfig 更为方便。
温馨提示debian12部署的k8s使用containerd运行是,jenkins 动态salve构建时,内核docker in docker时会错