Kubernetes 无法使用kubeconfig文件中的上下文

Kubernetes 无法使用kubeconfig文件中的上下文,kubernetes,kubeconfig,Kubernetes,Kubeconfig,我试图使用用户名和密码在kubeconfig文件中添加一个新集群及其上下文,但失败了。下面是我用来设置上下文的命令 kubectl config set-cluster lab101 --server=https://api-kube.example.com:8443 --insecure-skip-tls-verify --context=lab101 kubectl config set-credentials kubeadmin --username=kubeadmin --passwo

我试图使用用户名和密码在kubeconfig文件中添加一个新集群及其上下文,但失败了。下面是我用来设置上下文的命令

kubectl config  set-cluster lab101 --server=https://api-kube.example.com:8443 --insecure-skip-tls-verify --context=lab101
kubectl config set-credentials kubeadmin --username=kubeadmin --password=xxxxxxx --cluster=lab101
kubectl config  set-context lab101 --cluster=lab101 --namespace=default --user=kubeadmin
kubectl config use-context lab101
日志:

GET https://api-kube.example.com:8443/api?timeout=32s 403 Forbidden in 19 milliseconds
I0422 11:37:31.741005   18972 round_trippers.go:411] Response Headers:
I0422 11:37:31.741005   18972 round_trippers.go:414]     Cache-Control: no-cache, private
I0422 11:37:31.741005   18972 round_trippers.go:414]     Content-Type: application/json
I0422 11:37:31.741005   18972 round_trippers.go:414]     X-Content-Type-Options: nosniff
I0422 11:37:31.741005   18972 round_trippers.go:414]     Content-Length: 188
I0422 11:37:31.741005   18972 round_trippers.go:414]     Date: Wed, 22 Apr 2020 15:37:31 GMT
I0422 11:37:31.762977   18972 request.go:897] Response Body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"forbidden: User \"system:anonymous\" cannot get path \"/api\"","reason":"Forbidden","details":{},"code":403}

注意:如果我使用同一个用户,则使用
oc login
的密码可以正常工作。不明白如果我手动设置kubeconfig,为什么它不起作用。

OpenShift Kubernetes不太可能允许使用身份验证。
oc login
命令使用用户名和密码对a进行内部身份验证,以获取自动存储在kubectl使用的kubeconfig文件中的承载令牌。当您执行任何kubectl命令时,该令牌用于向Kubernetes集群进行身份验证

您可以通过
oc config view
检查令牌。您可以获取令牌并使用
kubectl-config-set-credentials-kubeadmin--token=bearertoken
设置它,它应该可以工作

参考文档

或者,您可以按照此文档获取不记名令牌