簡體   English   中英

如何在Kubernetes集群中創建用戶?

[英]How to create a user in a Kubernetes cluster?

我正在嘗試在Kubernetes集群中創建用戶。

我使用我的Terraform腳本在DigitalOcean上旋轉了2個水滴。

然后我使用ssh登錄主節點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̀kubectl

我知道我可以在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.crtca.keykubeadm或主配置中提供的相同證書/密鑰。

然后,您可以將此簽名證書及其密鑰提供給用戶,然后可以使用以下命令配置訪問權限:

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提供了一個很好的資源來解釋所有這些:

https://docs.bitnami.com/kubernetes/how-to/configure-rbac-in-your-kubernetes-cluster/#use-case-1-create-user-with-limited-namespace-access

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM