There are two types of users in kubernetes: service account and regular user. These two user types correspond to two usage scenarios.
The service account is provided to the pods running in the cluster, and when these pods want to communicate with the apiserver, it is the serviceaccount that is used for authentication and authorization. Service accounts are stored in the k8s cluster and are RBAC-based and can be bound to roles to have specific permissions for specific resources.
Ordinary users are used for authentication and authorization in non-pod scenarios. For example, key components like k8s: scheduler, kubelet and controller manager, including using kubectl to interact with k8s clusters.
2. Service accounts
Even if we don’t create a service account under namespace or bind any service account to the pod, the serviceAccount field of the pod will be set to default. We can look at the service account named default and it will correspond to a secret. secret contains the values ca.crt, namespace and token.
This entire process is accomplished by three components.
- Service account admission controller
- Token controller
- Service account controller
Among other things, the service account controller is responsible for maintaining the default service account under each namespace. This way, when a new namespace is created, a default service account is automatically created. Even if the service account is deleted, it will be automatically recreated immediately.
The token controller is responsible for several things.
- Detects the creation of a service account and creates the appropriate Secret to support API access.
- Detects the deletion of a service account and removes all corresponding Token Secret for the service account.
- Detect the addition of a Secret, ensure that the corresponding service account exists, and add a token to the Secret if needed.
- Detects the deletion of a Secret and removes the reference from the corresponding service account if needed.
The reason why you need to create a token for the service account and generate a secret will be mentioned later.
Now we know the automation process for service accounts and their tokens. What is the role of the service account access controller? Let’s look at the pod creation process. Most of the time we don’t assign a service account to a pod. So what is the default service account associated with a pod after it has been successfully created?
Here is where the service account admission controller comes into play. It makes changes to the pod through the admission controller mechanism. When a pod is created or updated, the following actions are performed.
- If the pod does not have a
ServiceAccountsetting, set its
- Ensure that the
ServiceAccountassociated with the pod exists, otherwise the pod is rejected.
- If the pod does not contain an
ImagePullSecretssetting, then add the
ServiceAccountto the pod.
- Add a
volumecontaining a token for API access to the pod.
/var/run/secrets/kubernetes.io/serviceaccountto each container under the pod.
Suppose our pod has set up a service account and now wants to communicate with the apiserver, how does the apiserver authenticate that the service account is valid?
As we mentioned above, the token controller creates a secret for the service account, and the token in the secret is the authentication information for the service account, specifically through JWT. This token stores the namespace, name, UID and secret name of the service account. If you are interested in the contents of the token, you can decode the token value in base64 and paste it into here to see the contents of the token. Then you can also paste the private key and public key to verify the signature is correct. The details are as follows.
This token is signed with a private key unique to the service account, which is passed in via
--service-account-private-key-file when the contrller-manager is started. Similarly, in order to verify this token in the apiserver, the corresponding public key needs to be passed in at apiserver startup with the parameter
Once the authentication problem is solved, how does the apiserver know if the requested service account has permission to operate the current resource? In k8s, the common authorization policy is RBAC. By associating a service account with a role, we can give the service account permission to operate the specified resource.
3. General users
Unlike service accounts, k8s itself does not store any information about normal users, so how does apiserver authenticate normal users?
When a k8s cluster is created, there is usually a root certificate that is responsible for issuing the other certificates needed in the cluster. So it is assumed that if a common user can provide a certificate issued by the root certificate, he is a legitimate user. Where the common name of the certificate is the user name and the orgnization is the user group. For example, the certificate information of controller-manager on our local cluster is as follows.
Where subject is the information of the certificate applicant and issuer is the information of the issuer. Here we know that controller-manager is using the user
Like the authorization of service accounts, normal users can bind roles and have certain privileges on certain resources through the RBAC mechanism. For example, the ClusterRoleBinding information for controller-manager is as follows.
That is, it is bound to the
system:kube-controller-manager cluster role, and you can continue to see what resources are bound to this role with extreme operational privileges if you are interested.
Since normal users need to issue their own credentials for them and then authorize them. Here’s a simple example to walk through.
First create a json file for the certificate issuance request.
CN is short for common name. O is short for orgnization, which can be interpreted as a user group. The next step is to issue the root certificate with k8s, which requires ca.crt and ca.key, which can be found in
/etc/kubernetes/certs on the master node or elsewhere.
This command will generate three new files:
- jiang-key.pem: private key
- jiang.pem: certificate
- jiang.csr: certificate issuance request file
We use jiang’s private key and certificate to generate the user in kubeconfig as follows.
Next, generate a new context, specifying the users to be used by the k8s cluster.
Create roles and bindings for the user jiang. Here only the user jiang is allowed to read the pods under default namespace.
This completes the authentication and authorization of the user jiang. Use the following command to switch to this user.