By default, the kubeconfig file provided by CCE for users has permissions bound to the cluster-admin role, which are equivalent to the permissions of user root. It is difficult to implement refined management on users with such permissions.
Cluster resources are managed in a refined manner so that specific users have only certain permissions (such as adding, querying, and modifying resources).
Ensure that kubectl is available on your host. If not, download it from here (corresponding to the cluster version or the latest version).
In the following example, only pods and Deployments in the test space can be viewed and added, and they cannot be deleted.
kubectl create sa my-sa -n test
vi role-test.yaml
In this example, the permission rules include the read-only permission (get/list/watch) of pods in the test namespace, and the read (get/list/watch) and create permissions of deployments.
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: annotations: rbac.authorization.kubernetes.io/autoupdate: "true" labels: kubernetes.io/bootstrapping: rbac-defaults name: myrole namespace: test rules: - apiGroups: - "" resources: - pods verbs: - get - list - watch - apiGroups: - apps resources: - pods - deployments verbs: - get - list - watch - create
Create a Role.
kubectl create -f role-test.yaml
vi myrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: myrolebinding namespace: test roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: myrole subjects: - kind: ServiceAccount name: my-sa namespace: test
Create a RoleBinding.
kubectl create -f myrolebinding.yaml
The user information is configured. Now perform 5 to 7 to write the user information to the configuration file.
vi my-sa-token.yaml
apiVersion: v1 kind: Secret metadata: name: my-sa-token-secret namespace: test annotations: kubernetes.io/service-account.name: my-sa type: kubernetes.io/service-account-token
Create a token:
kubectl create -f my-sa-token.yaml
kubectl get secret my-sa-token-secret -n test -oyaml |grep ca.crt: | awk '{print $2}' |base64 -d > /home/ca.crt
kubectl config set-cluster test-arm --server=https://192.168.0.110:5443 --certificate-authority=/home/ca.crt --embed-certs=true --kubeconfig=/home/test.config
kubectl config set-cluster test-arm --server=https://192.168.0.110:5443 --kubeconfig=/home/test.config --insecure-skip-tls-verify=true
If you perform operations on a node in the cluster or the node that uses the configuration is a cluster node, do not set the path of kubeconfig to /root/.kube/config.
By default, the apiserver IP address of the cluster is a private IP address. After an EIP is bound, you can use the public network IP address to access the apiserver.
token=$(kubectl describe secret my-sa-token-secret -n test | awk '/token:/{print $2}')
kubectl config set-credentials ui-admin --token=$token --kubeconfig=/home/test.config
kubectl config set-context ui-admin@test --cluster=test-arm --user=ui-admin --kubeconfig=/home/test.config
kubectl config use-context ui-admin@test --kubeconfig=/home/test.config
If you want to assign other users the above permissions to perform operations on the cluster, provide the generated configuration file /home/test.config to the user after performing step 7. The user must ensure that the host can access the API server address of the cluster. When performing step 8 on the host and using kubectl, the user must set the kubeconfig parameter to the path of the configuration file.
kubectl get pod -n test --kubeconfig=/home/test.config
For more information about users and identity authentication in Kubernetes, see Authenticating.