根据您发布的错误,您的用户仅在身份验证阶段失败(HTTP 错误代码:401),您可以使用以下方法进行验证:
$ k get pods -v=6
...
I0123 16:34:18.842853 29373 helpers.go:203] server response object: [{
...
"code": 401
}]
F0123 16:34:18.842907 29373 helpers.go:114] error: You must be logged in to the server (Unauthorized)
使用以下步骤调试您的设置:
验证您使用的是正确的上下文和正确的用户(在 CURRENT 列中带有 *):
$ kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* context-user-ca-signed kubernetes user-user-ca-signed ns1
kubernetes-admin@kubernetes kubernetes kubernetes-admin
验证 Kubernetes API Server 的 CA 证书(假设 API Server 作为 Pod 运行):
$ sudo cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep -i "\-\-client-ca-file"
- --client-ca-file=/etc/kubernetes/pki/ca.crt
$ openssl x509 -in /etc/kubernetes/pki/ca.crt -text -noout | grep -i "Issuer:\|Subject:"
Issuer: CN = kubernetes
Subject: CN = kubernetes
验证您的用户证书是否由上述 CA 签名(用户证书的颁发者 CN 与 CA 证书的主题 CN 相同,此处为“kubernetes”),在 API 服务器中配置:
$ kubectl config view --raw -o jsonpath="{.users[?(@.name == \"user-user-ca-signed\")].user.client-certificate-data}" | base64 -d > client.crt
$ openssl x509 -in client.crt -text -noout | grep -i "Issuer:\|Subject:"
Issuer: CN = kubernetes
Subject: C = IN, ST = Some-State, O = Some-Organization, CN = user-ca-signed
如果上述步骤对您创建的用户来说没问题,您应该通过身份验证阶段。但是授权阶段仍然需要使用 RBAC、ABAC 或任何其他支持的授权模式进行配置,否则您仍然可能收到 HTTP 错误代码:403
$ kubectl get pods -v=6
I0123 16:59:41.350501 28553 helpers.go:203] server response object: [{
...
"code": 403
}]
F0123 16:59:41.351080 28553 helpers.go:114] Error from server (Forbidden): pods is forbidden: User "user-ca-signed" cannot list resource "pods" in API group "" in the namespace "ns1": No policy matched.