Method | Description |
RoleBinding references a Role object to grant Subjects resource permissions in a namespace. | |
Different RoleBinding objects in multiple namespaces can reference the same ClusterRole object template to grant Subjects the same template permissions. | |
ClusterRoleBinding references the ClusterRole template to grant Subjects permissions for the entire cluster. | |
You can customize permissions, for example, grant a user the permission to log in to the TKE cluster in addition to the preset read-only permission. |
USERNAME='sa-acc' # Set the test account nameNAMESPACE='sa-test' # Set the test namespace nameCLUSTER_NAME='cluster_name_xxx' # Set the test cluster name# Create the test namespacekubectl create namespace ${NAMESPACE}# Create the test ServiceAccount accountkubectl create sa ${USERNAME} -n ${NAMESPACE}# Obtain the Secret token resource name automatically created by the ServiceAccount accountSECRET_TOKEN=$(kubectl get sa ${USERNAME} -n ${NAMESPACE} -o jsonpath='{.secrets[0].name}')# Get the plaintext token of the SecretsSA_TOKEN=$(kubectl get secret ${SECRET_TOKEN} -o jsonpath={.data.token} -n sa-test | base64 -d)# Set an access credential of token type using the obtained plaintext token informationkubectl config set-credentials ${USERNAME} --token=${SA_TOKEN}# Set the context entries for accessing the clusterkubectl config set-context ${USERNAME} --cluster=${CLUSTER_NAME} --namespace=${NAMESPACE} --user=${USERNAME}
kubectl config get-contexts
command to view the generated contexts as shown below:
sa-role.yaml
as shown below:kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:namespace: sa-test # Specify the namespacename: sa-role-testrules: # Set the permission rule- apiGroups: ["", "extensions", "apps"]resources: ["deployments", "replicasets", "pods"]verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
sa-rb-test.yaml
. The following permission binding indicates that the sa-acc
user of ServiceAccount type has sa-role-test
(Role type) permissions in the sa-test
namespace, as shown below:apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: sa-rb-testnamespace: sa-testsubjects:- kind: ServiceAccountname: sa-accnamespace: sa-test # The namespace of the ServiceAccountapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.roleRef:kind: Rolename: sa-role-testapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.
sa-context
, the default namespace is sa-test
, and it has the permissions configured in the sa-role-test
(Role) object under the sa-test
namespace, but it has no permissions under the default
namespace.USERNAME='role_user' # Set the usernameNAMESPACE='default' # Set the test namespace nameCLUSTER_NAME='cluster_name_xxx' # Set the test cluster name# Use OpenSSL to generate a self-signed certificate keyopenssl genrsa -out ${USERNAME}.key 2048# Use OpenSSL to generate a self-signed CSR file, where `CN` indicates the username and `O` indicates the group nameopenssl req -new -key ${USERNAME}.key -out ${USERNAME}.csr -subj "/CN=${USERNAME}/O=${USERNAME}"# Create a Kubernetes CSRcat <<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# Approve the certificate as trustworthykubectl certificate approve ${USERNAME}# Obtain the self-signed certificate CRTkubectl get csr ${USERNAME} -o jsonpath={.status.certificate} | base64 --decode > ${USERNAME}.crt# Set the cluster resource access credential (X.509 certificate)kubectl config set-credentials ${USERNAME} --client-certificate=${USERNAME}.crt --client-key=${USERNAME}.key# Set the Context cluster and default namespacekubectl config set-context ${USERNAME} --cluster=${CLUSTER_NAME} --namespace=${NAMESPACE} --user=${USERNAME}
test-clusterrole.yaml
as shown below:kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:name: test-clusterrolerules:- apiGroups: [""]resources: ["pods"]verbs: ["get", "watch", "list", "create"]
clusterrole-rb-test.yaml
. The following permission binding indicates that the role_user
user with the self-signed certificate authentication has test-clusterrole
(ClusterRole type) permissions in the default
namespace, as shown below:apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: clusterrole-rb-testnamespace: defaultsubjects:- kind: Username: role_usernamespace: default # The namespace of the userapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.roleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.
role_user
, the default namespace is default
, and it has the permissions configured by the test-clusterrole
permission object.clusterrole-rb-test2.yaml
. The following permission binding indicates that the role_user
user with the self-signed certificate authentication has test-clusterrole
(ClusterRole type) permissions in the default2
namespace.apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: clusterrole-rb-testnamespace: default2subjects:- kind: Username: role_usernamespace: default # The namespace of the userapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.roleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.
default2
namespace, role_user
also has the permissions configured by test-clusterrole
. At this point, you have implemented permission reuse and binding in multiple namespaces.clusterrole-crb-test3.yaml
. The following permission binding indicates that the role_user
user with the certificate authentication has test-clusterrole
(ClusterRole type) permissions in the entire cluster.apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: clusterrole-crb-testsubjects:- kind: Username: role_usernamespace: default # The namespace of the userapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.roleRef:kind: ClusterRolename: test-clusterroleapiGroup: "" # The default apiGroup is `rbac.authorization.k8s.io`.
role_user
has the cluster-wide test-clusterrole
permissions.subjects:- apiGroup: rbac.authorization.k8s.iokind: Username: 700000xxxxxx-1650879262 # The username of the specified user in RBAC. You need to get this information of the specified user.
apiVersion: rbac.authorization.k8s.io/v1beta1kind: ClusterRolemetadata:name: "700000xxxxxx-ClusterRole-ro" # ClusterRole namerules:- apiGroups:- ""resources:- pods- pods/attach- pods/exec # Pod login permission- 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" # Use the ClusterRole name in step 3subjects:- apiGroup: rbac.authorization.k8s.iokind: Username: "700000xxxxxx-1650879262" # Use the user information in step 2
Was this page helpful?