方式 | 说明 |
RoleBinding 引用 Role 对象,为 Subjects 只授予某单个命名空间下资源权限。 | |
多个命名空间下不同的 Rolebinding 可引用同一个 ClusterRole 对象模板为 Subjects 授予相同模板权限。 | |
ClusterRoleBinding 引用 ClusterRole 模板,为 Subjects 授予整个集群的权限。 | |
用户自定义权限,例如给一个用户预设的只读权限额外添加登录容器的权限。 |
USERNAME='sa-acc' # 设置测试账户名NAMESPACE='sa-test' # 设置测试命名空间名CLUSTER_NAME='cluster_name_xxx' # 设置测试集群名# 创建测试命名空间kubectl create namespace ${NAMESPACE}# 创建测试 ServiceAccount 账户kubectl create sa ${USERNAME} -n ${NAMESPACE}# 获取 ServiceAccount 账户自动创建的 Secret token 资源名SECRET_TOKEN=$(kubectl get sa ${USERNAME} -n ${NAMESPACE} -o jsonpath='{.secrets[0].name}')# 获取 secrets 的明文 TokenSA_TOKEN=$(kubectl get secret ${SECRET_TOKEN} -o jsonpath={.data.token} -n sa-test | base64 -d)# 使用获取到的明文 token 信息设置一个 token 类型的访问凭证kubectl config set-credentials ${USERNAME} --token=${SA_TOKEN}# 设置访问集群所需要的 context 条目kubectl config set-context ${USERNAME} --cluster=${CLUSTER_NAME} --namespace=${NAMESPACE} --user=${USERNAME}
kubectl config get-contexts
命令,查看生成的 contexts 条目。如下图所示:
kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:namespace: sa-test # 指定 Namespacename: sa-role-testrules: # 设置权限规则- apiGroups: ["", "extensions", "apps"]resources: ["deployments", "replicasets", "pods"]verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: sa-rb-testnamespace: sa-testsubjects:- kind: ServiceAccountname: sa-accnamespace: sa-test # ServiceAccount 所在 NamespaceapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.ioroleRef:kind: Rolename: sa-role-testapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.io
USERNAME='role_user' # 设置需要创建的用户名NAMESPACE='default' # 设置测试命名空间名CLUSTER_NAME='cluster_name_xxx' # 设置测试集群名# 使用 Openssl 生成自签证书 keyopenssl genrsa -out ${USERNAME}.key 2048# 使用 Openssl 生成自签证书CSR 文件, CN 代表用户名,O 代表组名openssl req -new -key ${USERNAME}.key -out ${USERNAME}.csr -subj "/CN=${USERNAME}/O=${USERNAME}"# 创建 Kubernetes 证书签名请求(CSR)cat <<EOF | kubectl apply -f -apiVersion: certificates.k8s.io/v1beta1kind: CertificateSigningRequestmetadata:name: ${USERNAME}spec:request: $(cat ${USERNAME}.csr | base64 | tr -d '\\n')usages:- digital signature- key encipherment- client authEOF# 证书审批允许信任kubectl certificate approve ${USERNAME}# 获取自签证书 CRTkubectl get csr ${USERNAME} -o jsonpath={.status.certificate} | base64 --decode > ${USERNAME}.crt# 设置集群资源访问凭证(X509 证书)kubectl config set-credentials ${USERNAME} --client-certificate=${USERNAME}.crt --client-key=${USERNAME}.key# 设置 Context 集群、默认Namespace 等kubectl config set-context ${USERNAME} --cluster=${CLUSTER_NAME} --namespace=${NAMESPACE} --user=${USERNAME}
kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: test-clusterrolerules:- apiGroups: [""]resources: ["pods"]verbs: ["get", "watch", "list", "create"]
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: clusterrole-rb-testnamespace: defaultsubjects:- kind: Username: role_usernamespace: default # User 所在 NamespaceapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.ioroleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.io
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: clusterrole-rb-testnamespace: default2subjects:- kind: Username: role_usernamespace: default # User 所在 NamespaceapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.ioroleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.io
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: clusterrole-crb-testsubjects:- kind: Username: role_usernamespace: default # User 所在 NamespaceapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.ioroleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # 默认 apiGroup 组为 rbac.authorization.k8s.io
subjects:- apiGroup: rbac.authorization.k8s.iokind: Username: 700000xxxxxx-1650879262 # RBAC 里指定用户的用户名,需要拿到您指定用户的该信息
apiVersion: rbac.authorization.k8s.io/v1beta1kind: ClusterRolemetadata:name: "700000xxxxxx-ClusterRole-ro" # ClusterRole 的名字rules:- apiGroups:- ""resources:- pods- pods/attach- pods/exec # Pod 的登陆权限- pods/portforward- pods/proxyverbs:- create- get- list- watch
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: "700000xxxxxx-ClusterRoleBinding-ro"roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: "700000xxxxxx-ClusterRole-ro" # 使用步骤 3 中的 ClusterRole 的名字subjects:- apiGroup: rbac.authorization.k8s.iokind: Username: "700000xxxxxx-1650879262" # 使用步骤 2 中的用户信息
本页内容是否解决了您的问题?