我在 VirtualBox(ubuntu)上安装了 minikube 来模拟部署环境并使用 Jenkins 管道。
在我的 jenkins 容器上,我还安装了 kubectl 命令来控制 minikube。
所以我创建了以下阶段:
stage("k8s command test") {
withCredentials([usernamePassword(credentialsId: 'cbcba826-ef01-4b18-856f-e6dc4eb27c1f', usernameVariable: 'my-user', passwordVariable: 'PASSWORD')]) {
sh """
kubectl config view
kubectl create -f /home/my-user/file-svc.yml
"""
}
}
我的集群在输出中的第一件事似乎是空的:
+ kubectl config view
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
如果我检查 .kube/config 我有以下配置:
apiVersion: v1
clusters:
- cluster:
certificate-authority: /home/my-user/.minikube/ca.crt
server: https://127.0.0.1:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
as-user-extra: {}
client-certificate: /home/my-user/.minikube/client.crt
client-key: /home/my-user/.minikube/client.key
由于我是 kubernetes 的新手,我需要帮助来了解如何使 jenkins 和 minikube 之间的通信成为可能。
因此 kubectl create -f/home/my-user/file-svc.yml 引发了关于身份验证的错误。
我阅读了很多文章 kubectl 需要证书才能连接到 API 服务器我如何在 jenkins 中做到这一点
最佳答案
好像 KUBECONFIG 环境变量没有分配到正确的配置文件中,所以需要先按照命令进行分配
export KUBECONFIG=~/.kube/config
现在 kubectl 可以与 kube-api 服务器通信。但是,它需要显示其身份和对 k8s 资源执行操作的能力,因此,每当它与 kube-api 服务器通信时,它都会获取 client.crt 和 client.key 文件。
现在它可以执行以下任务
kubectl apply -f deployment.yaml
kubectl get pods
kubectl get nodes
您的问题的解决方案
Jenkins 需要拥有这些证书和 key 才能执行创建、应用、获取、监视等操作。
关于jenkins - 从 jenkins 容器运行 kubectl 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47536238/