kubernetes - 如何在 Kubernetes 集群中创建用户?
问题描述
我正在尝试在 Kubernetes 集群中创建一个用户。
我使用我的 Terraform 脚本在 DigitalOcean 上旋转了 2 个液滴。
然后我使用以下命令登录主节点 droplet ssh
:
doctl compute ssh droplet1
在此之后,我在其中创建了一个新集群和一个命名空间:
kubectl create namespace thalasoft
我在文件中创建了一个用户角色role-deployment-manager.yml
:
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: thalasoft
name: deployment-manager
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["deployments", "replicasets", "pods"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
并执行命令:
kubectl create -f role-deployment-manager.yml
我在文件中创建了一个角色授予rolebinding-deployment-manager.yml
:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: deployment-manager-binding
namespace: thalasoft
subjects:
- kind: User
name: stephane
apiGroup: ""
roleRef:
kind: Role
name: deployment-manager
apiGroup: ""
并执行命令:kubectl create -f rolebinding-deployment-manager.yml
这是我的终端输出:
Last login: Wed Dec 19 10:48:48 2018 from 90.191.151.182
root@droplet1:~# kubectl create namespace thalasoft
namespace/thalasoft created
root@droplet1:~# vi role-deployment-manager.yml
root@droplet1:~# kubectl create -f role-deployment-manager.yml
role.rbac.authorization.k8s.io/deployment-manager created
root@droplet1:~# vi rolebinding-deployment-manager.yml
root@droplet1:~# kubectl create -f rolebinding-deployment-manager.yml
rolebinding.rbac.authorization.k8s.io/deployment-manager-binding created
root@droplet1:~#
现在我想先在集群中创建一个用户,然后kubectl
用这个用户配置客户端,以便在我的笔记本电脑上进行操作,避免通过ssh̀
droplet 登录。
我知道我可以在客户端配置用户kubectl
:
# Create a context, that is, a user against a namespace of a cluster, in the client configuration
kubectl config set-context digital-ocean-context --cluster=digital-ocean-cluster --namespace=digital-ocean-namespace --user=stephane
# Configure the client with a user credentials
cd;
kubectl config set-credentials stephane --client-certificate=.ssh/id_rsa.pub --client-key=.ssh/id_rsa
但这只是我理解的一些客户端配置。
更新:我可以使用由 Kubernetes CA 签名的证书添加用户凭据,在托管 Kubernetes 主节点的 Droplet 上运行以下命令:
# Create a private key
openssl genrsa -out .ssh/thalasoft.key 4096
# Create a certificate signing request
openssl req -new -key .ssh/thalasoft.key -out .ssh/thalasoft.csr -subj "/CN=stephane/O=thalasoft"
# Sign the certificate
export CA_LOCATION=/etc/kubernetes/pki/
openssl x509 -req -in .ssh/thalasoft.csr -CA $CA_LOCATION/ca.crt -CAkey $CA_LOCATION/ca.key -CAcreateserial -out .ssh/thalasoft.crt -days 1024
# Configure a cluster in the client
kubectl config set-cluster digital-ocean-cluster --server=https://${MASTER_IP}:6443 --insecure-skip-tls-verify=true
# Configure a user in the client
# Copy the key and the certificate to the client
scp -o "StrictHostKeyChecking no" root@165.227.171.72:.ssh/thalasoft.* .
# Configure the client with a user credentials
kubectl config set-credentials stephane --client-certificate=.ssh/thalasoft.crt --client-key=.ssh/thalasoft.key
# Create a context, that is, a user against a namespace of a cluster, in the client configuration
kubectl config set-context digital-ocean-context --cluster=digital-ocean-cluster --namespace=digital-ocean-namespace --user=stephane
解决方案
但这只是我理解的一些客户端配置。
我应该使用什么命令来创建用户?
Kubernetes 不提供用户管理。这是通过可以由集群 CA 签名的 x509 证书来处理的。
首先,您需要创建一个密钥:
openssl genrsa -out my-user.key 4096
其次,您需要创建一个签名请求:
openssl req -new -key my-user.key -out my-user.csr -subj "/CN=my-user/O=my-organisation"
三、签署证书请求:
openssl x509 -req -in my-user.csr -CA CA_LOCATION/ca.crt -CAkey CA_LOCATION/ca.key -CAcreateserial -out my-user.crt -days 500
ca.crt
并且是由您的主配置提供或在您的主配置中ca.key
提供的相同证书/密钥。kubeadm
然后,您可以将此签名证书连同他们的密钥一起提供给您的用户,然后可以使用以下命令配置访问权限:
kubectl config set-credentials my-user --client-certificate=my-user.crt --client-key=my-user.key
kubectl config set-context my-k8s-cluster --cluster=cluster-name --namespace=whatever --user=my-user
Bitnami 提供了一个很好的资源来解释所有这些:
推荐阅读
- apache-spark - 有没有办法将在 for 循环期间创建的变量写入 pyspark 中的数据帧?
- r - 读入数值数据
- audio-player - AUDIOKIT - 为什么 AKOscillatorBank RELASE、DECAY、SUSTAIN 和 ATTACK 在我更改它们的值后很久才更新?
- python - 如何为数组列中的每个元素分配其有序位置?
- android - Android Mapview 标记延迟
- python - IndexError:索引 2 超出轴 0 的范围,大小为 2 的矩阵不是正方形
- pyspark - 如何使用 pyspark graphframe pregel API 实现循环检测
- r - 许多列中的 str_replace 问题和
- reactjs - 无法在事件处理程序中获取更新的状态值
- scala - Spark使用wholeTextFiles获取行号zipWithIndex