需求:外部 Jenkins 需要连接 Rancher 中的 Kubernetes 集群。
1、集群 config 文件
Rancher 首页,"集群" --> 右上角"Kubeconfig 文件",后续需要用到该文件内容。
apiVersion: v1 kind: Config clusters: - name: "cicd" cluster: server: "https://rancher.xxxxxx.com/k8s/clusters/c-b5g7r" - name: "cicd-dev-cicd-k8s-master" cluster: server: "https://172.16.5.83:6443" certificate-authority-data: "LS0tLSerashtyjyurkilyrderhaURS0tLS0tCk1JSUN3akNDQ\ WFxZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFTTVJBd0RnWURWUVFERXdkcmRXSmwKT\ FdOaE1CNFhEVEU1TURnd05UQTJORFl6TWsdfgRUSTVNRGd3TWpBMk5EWXpNbG93RWpFUU1BNEdBM\ VVFQXhNSAphM1ZpWlMxallUQ0NBU0l3RFFZSktvWklodmNOQVFFQkJRQURnZ0VQQURDQ0FRb0NnZ\ GFiSXpBMC9KOE5GaDZQektYVWNKTApMZE1RQkUrOTBJbStBaWZRWENuaG9iRUZEYWh6ZDkyeFArU\ 1RYTlZsK0Q3RDBlUFVyRjdoUExKdENWL2lPekxYCmxLLzRUTzZmVG1XZmo2NlBqRXk5djB5OHpxT\ DhDazVKem9jTDkzZDlVSnZPTmtjdy8wRHk0SVVJMHlBZnRmb3IKaUJmbXh4K0ZnK2hRK0xMZEN1M\ Hd0MHBMY2lZMWhoS1FqOFdHc09ER0hLeHBrNlZDa2dPbHRyVURwc2E5UmtCdAp2SVVlTjNWSjNie\ mlia1NSVHhMc0t5S3FyRXZ0Y3Zldnd4enM0OTlRbWlFZmI3dmxucXM9Ci0tLS0tRU5EIENFUlRJR\ klDQVRFLS0tLS0K" users: - name: "u-42lyr7ytst" user: token: "kubeconfig-u-42lyr7ytst.c-b5g7r:45kclvgd4bh87rtvtpbqhwhf7s62fqpl" contexts: - name: "cicd" context: user: "u-42lyr7ytst" cluster: "cicd" - name: "cicd-dev-cicd-k8s-master" context: user: "u-42lyr7ytst" cluster: "cicd-dev-cicd-k8s-master" current-context: "cicd"
2、新建凭据
新建一个连接 kubernetes 集群的凭据
1)类型:Secret text 2)范围:全局 3)Secret:confi文件中的token值 4)id:kube(自定义命名)
3、配置kubernetes云
系统设置 --> 增加一个云(kubernetes)
1)名称:kubernetes 2)Kubernetes地址:https://rancher.xxxxxx.com/k8s/clusters/c-xg99q 3)Kubernetes服务证书key:config文件中的certificate-authority-data值,但是这个值需要进行转换,命令:echo xxxxxxxxx | base64 -d >ca.crt 4)凭据:之前创建的凭据 5)Jenkins地址:http://jenkinscicd.xxxxxx.com
连接测试提示:Connection test successful,即表示连接成功。