1

Kubernetes 版本:v1.19.0

我创建了一个用户并使用角色 cluster-admin 执行了 clusterrolebinding。

[root@project1-master ~]# kubectl describe clusterrole cluster-admin
Name:         cluster-admin
Labels:       kubernetes.io/bootstrapping=rbac-defaults
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources  Non-Resource URLs  Resource Names  Verbs
  ---------  -----------------  --------------  -----
  *.*        []                 []              [*]
             [*]                []              [*]

[root@project1-master ~]# kubectl describe clusterrolebinding sajeesh  cluster-admin
Name:         sajeesh
Labels:       <none>
Annotations:  <none>
Role:
  Kind:  ClusterRole
  Name:  cluster-admin
Subjects:
  Kind  Name     Namespace
  ----  ----     ---------
  User  sajeesh


Name:         cluster-admin
Labels:       kubernetes.io/bootstrapping=rbac-defaults
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
Role:
  Kind:  ClusterRole
  Name:  cluster-admin
Subjects:
  Kind   Name            Namespace
  ----   ----            ---------
  Group  system:masters

我可以使用这个用户帐户运行 kubectl 并获取 pod 信息:

[root@project1-master ~]# kubectl get pods --as sajeesh
NAME    READY   STATUS    RESTARTS   AGE
busyb   1/1     Running   3          21h

但是当我尝试使用 curl 访问 kube-apiserver 时,它显示禁止错误如下:

[root@project1-master ~]# curl --cacert /etc/kubernetes/pki/ca.crt --cert sajeesh.crt --key sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "pods \"busyb\" is forbidden: User \"system:anonymous\" cannot get resource \"pods\" in API group \"\" in the namespace \"default\"",
  "reason": "Forbidden",
  "details": {
    "name": "busyb",
    "kind": "pods"
  },
  "code": 403

我已经重新验证了我为该用户帐户提供的 cacert 、证书和密钥。它们是正确的。

任何建议为什么会发生这种情况以及如何解决它。

4

2 回答 2

1

clusterrolebindingsajeesh具有名称sajeesh和组system:masters。因此证书需要具有sajeesh通用名称(CN)和 system:masters组织(O)。当请求发送到 kubernetes API 服务器时,它将检查证书并与 in 匹配,CN如果不匹配,则会出错。nameOGroupclusterrolebinding403 Forbidden

您可以通过运行以下命令来验证上述内容

openssl x509 -in sajeesh.crt -text -noout
于 2020-09-12T14:59:02.930 回答
1

终于设法找出问题所在。它与 kubernetes 无关,但与我正在使用的 curl 命令有关。

curl --cacert /etc/kubernetes/pki/ca.crt --cert sajeesh.crt --key sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb 

当我将 -v 开关与命令一起使用时,它显示:

* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/kubernetes/pki/ca.crt
  CApath: none
* warning: certificate file name "sajeesh.crt" handled as nickname; please use "./sajeesh.crt" to force file name
* NSS: client certificate not found: newsajeesh.crt
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384

所以基本上它正在寻找绝对路径作为参数的输入 --cert & --key

curl --cacert /etc/kubernetes/pki/ca.crt --cert ./sajeesh.crt --key ./sajeesh.key https://$IP:6443/api/v1/namespaces/default/pods/busyb 

在给出绝对路径后它工作正常,我能够得到输出。

于 2020-09-13T07:29:32.630 回答