使用k8s的小伙伴,对k8s用户权限管理可能有很多困惑,如何建立外部用户,并赋予k8s的访问权限,对很多开发者来说,是分厂困惑的。
下面,本人以jerry用户的创建为例,为linux系统中的k8s-jerry用户配置k8s集群管理权限。
注意: 此处配置的k8s-jerry用户为linux系统用户; 而jerry用户为k8s集群中对应的用户。
(umask 077; openssl genrsa -out jerry.key 2048)
openssl req -new -key jerry.key -out jerry.csr -subj "/CN=jerry"
如果定义组,则组可以作为权限分配的主体,比如:
openssl req -new -key jerry.key -out jerry.csr -subj "/CN=jerry/O=jerry-admin”
角色授权绑定文件可以定义如下:
subjects:
openssl x509 -req -in jerry.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out jerry.crt -days 365
openssl x509 -in jerry.crt -text -noout
export KUBE_APISERVER="https://172.16.1.101:6443"
kubectl config set-cluster kubernetes --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true --server=${KUBE_APISERVER} --kubeconfig=kube-config
注意:
使用ca.crt证书,作为集群授权证书
当前目录下生成kube-config配置文件
kubectl config set-credentials jerry --client-certificate=/root/kube-cluster/pki/jerry.crt --embed-certs=true --client-key=/root/kube-cluster/pki/jerry.key --kubeconfig=kube-config
注意:
1. 在kube-config集群配置文件中查看user 名为 jerry
2. 采用客户端证书和私钥
kubectl config set-context jerry@kubernetes --cluster=kubernetes --user=jerry --kubeconfig=kube-config
注意: 上下文名称为 jerry@kubernetes
集群名称为kubernetes
用户名称为jerry
kubectl config use-context jerry@kubernetes --kubeconfig=kube-config
kubectl create role pods-reader --verb=get,list,watch --resource=pods --dry-run -oyaml > role-demo.yaml
文件内容如下:
[root@master kube-cluster]# more role-demo.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: pods-reader
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
kubectl create rolebinding jerry-read-pods --role=pods-reader --user=jerry --dry-run -oyaml > jerry-read-pods.yaml
文件内容如下:
[root@master kube-cluster]# more jerry-read-pods.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
creationTimestamp: null
name: jerry-read-pods
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pods-reader
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: jerry
**<1>切换上下文**
kubectl config use-context jerry@kubernetes --kubeconfig=kubeconf/kube-config
**<2>查看pods(有权)**
[root@master kube-cluster]# kubectl get pods --kubeconfig=kubeconf/kube-config
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-5584cd9578-fqbjg 1/1 Running 0 15h
test-pod 0/1 CrashLoopBackOff 176 15h
web-0 1/1 Running 0 13h
web-1 1/1 Running 0 13h
<3>查看deployment(无权限)
kubectl get deployment --kubeconfig=kubeconf/kube-config
Error from server (Forbidden): deployments.apps is forbidden: User “jerry” cannot list resource “deployments” in API group “apps” in the namespace “default”
useradd k8s-jerry
chown -R k8s-jerry.k8s-jerry /home/k8s-jerry
[root@master kube-cluster]# ls
cluster-pods-reader.yaml jerry-read-pods.yaml kubeconf pki role-demo.yaml
[root@master kube-cluster]# cp kubeconf/kube-config /home/k8s-jerry/.kube/config
[root@master kube-cluster]# su k8s-jerry
[k8s-jerry@master kube-cluster]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-5584cd9578-fqbjg 1/1 Running 0 15h
test-pod 0/1 CrashLoopBackOff 180 15h
web-0 1/1 Running 0 14h
web-1 1/1 Running 0 14h