插件安装
安装Kubernetes需要的插件
- Kubernetes :: Pipeline :: DevOps Steps
- Kubernetes CLI Plugin
- Kubernetes plugin
- Kubernetes Credentials Plugin
创建token并绑定
kubectl create sa jenkins kubectl create clusterrolebinding jenkins --clusterrole cluster-admin --serviceaccount=default:jenkins
apiVersion: v1
kind: Secret
metadata:
name: secret-sa-jenkins
namespace: default
annotations:
kubernetes.io/service-account.name: "jenkins"
type: kubernetes.io/service-account-token
kubectl apply -f jenkins_token.yaml kubectl describe sa jenkins -n default kubectl describe secrets secret-sa-jenkins -n default
eyJhbGciOiJSUzI1NiIsImtpZCI6Ijd4X0JfQ3d4ZzJNTF9jMmhCRUpLdFVJVll6S3FmczRDRTRXTlFIelhJOTgifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6InNlY3JldC1zYS1qZW5raW5zIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImplbmtpbnMiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIxOGYxNWY2MS0zMjlmLTQxOGItYWM3NS1iM2QyODdjNzQ1ZmYiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6ZGVmYXVsdDpqZW5raW5zIn0.TLm2kreeajgLMBGkPkzTJHSM-jlq1RQkWlLHOQ48fY2nDBqINGhIYN6TVXmuoTgZ_KhzCHxk4RyD7jrPZpsfiG6_LNrc_tDSw-i3clyWWhar_ZQMayXkYMXq2YdgHKvMqbAEsm4HdTa2iRZOgzn3b0vh5z-SFAy0ieJO4LajCBliiurqRrPn8yBXycK6uorxbS6GO64KnNdXNVkx_pIEB_HXwRP07ngazoiKD3HRz4aBsSpDOmCElgec4Uz5ey1gBq6orgR_tfj28J4UUNpOEpCbWWE9R4PLx9-FlglsEzFuyWVmDnfwdxpXtPhkjOGZ2hGTgcCKA4uwlfXgHrKVtQ
主要配置字段说明:
Kubernetes 地址
Kubernetes中Api-server的地址,具体可以通过cat ~/.kube/config来进行查看
Kubernetes 命名空间
jenkins-agent的pod默认执行的命名空间。
凭据
用于访问Kubernetes集群的凭据,选择上面添加到凭据
Jenkins 地址
是Jenkins-agent在启动时回调的地址,如果填写错误,则会出现重复创建pod的情况
配给K8S集群到Jenkins
流水线调用k8s集群
stage('K8S部署') {
when {
branch 'master'
}
steps {
// Get some code from a GitHub repository
echo "验证kubectl"
echo pwd
withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
echo "查看 K8S 集群 Pod 列表"
sh "mkdir -p ~/.kube && cp ${KUBECONFIG} ~/.kube/config"
sh 'kubectl get pod -A'
sh "kubectl set image deployment/${env.BACKEND_POD} -n ${env.NAMESPACE} ${env.BACKEND_CONTAINER}=55.235.30.100:60000/platform/jenkins-${env.BACKEND_NAME}:${env.VERSION}"
sh "kubectl rollout restart deployment/${env.BACKEND_POD} -n ${env.NAMESPACE}"
sh "kubectl describe deployment ${env.BACKEND_POD} -n ${env.NAMESPACE}"
}
}
}
评论区