【问题标题】:Unable to use-context from kubeconfig file无法使用 kubeconfig 文件中的上下文
【发布时间】:2020-04-22 16:15:54
【问题描述】:

我正在尝试使用用户名和密码在 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 它将不起作用。

【问题讨论】:

    标签: kubernetes kubeconfig


    【解决方案1】:

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

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

    参考文档here

    您也可以按照此文档here 获取不记名令牌。

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多