1

我尝试使用客户端证书创建用户帐户。

我遵循了两个教程,但在消息错误中坚持使用这两个选项

https://medium.com/better-programming/k8s-tips-give-access-to-your-clusterwith-a-client-certificate-dfb3b71a76fe

https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/

我设置了正确的用户、服务器和正确的上下文。我设置了命名空间,但仍然是同样的错误。

> kubectl get pods
You must be logged in to the server (Unauthorized) 

有人已经经历过类似的事情吗?或者有人知道我做错了什么?

我的k3s集群版本是1.15.4。

4

2 回答 2

3

根据您发布的错误,您的用户仅在身份验证阶段失败(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)

使用以下步骤调试您的设置:

  1. 验证您使用的是正确的上下文和正确的用户(在 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        
    
  2. 验证 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
    
  3. 验证您的用户证书是否由上述 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.
于 2020-01-28T06:55:01.150 回答
1

我终于在这张票中找到了答案。

https://github.com/rancher/k3s/issues/684

用户 huapox 贴出如下代码:

[root@(⎈ |default:default) sec-rbac]$ cat t2.sh 
ws=/opt/sec-rbac
day=3650

clus_name="t1.k3s"
clus_ns="default"
user="koper"
#clus_url="https://10.200.100.183:7442"
clus_url="https://server:6443"  ##
ca_path=$ws/server/tls
rm -f $ca_path/*-ca.srl

ctx=gen && mkdir -p $ws/$ctx/{kube,keys} && cd $ws/$ctx
#############
ca1=client-ca
generate="keys/u-"$user
echo -e "\033[32m#>>GEN-KEY\033[0m"
#openssl genrsa -out $generate.key 2048
openssl ecparam -name prime256v1 -genkey -noout -out $generate.key
openssl req -new -key $generate.key -out $generate.csr -subj "/CN=${user}@${clus_name}/O=key-gen"
openssl x509 -req -in $generate.csr -CA $ca_path/$ca1.crt -CAkey $ca_path/$ca1.key -CAcreateserial -out $generate.crt -days $day

#-----------
#generate=$ca_path/client-admin  ##test
ca2=server-ca
embed=false
ctx2="$user@$clus_name"
config="kube/$user.kubeconfig"
echo -e "\033[32m#>>KUBE-CONFIG\033[0m" 
kubectl --kubeconfig=$config config set-cluster $clus_name --embed-certs=$embed --server=$clus_url --certificate-authority=$ca_path/$ca2.crt
kubectl --kubeconfig=$config config set-credentials $user --embed-certs=$embed --client-certificate=$generate.crt  --client-key=$generate.key
kubectl --kubeconfig=$config config set-context $ctx2 --cluster=$clus_name --namespace=$clus_ns --user=$user
kubectl --kubeconfig=$config config set current-context $ctx2
kubectl --kubeconfig=$config --context=$ctx2 get pods

非常感谢 huapox。

于 2020-01-28T16:57:36.533 回答