Generate a kubeconfig
With kubectl, you can generate your own kubeconfig for a user with restricted permissions: you have the cluster sign a client certificate via a CertificateSigningRequest, grant permissions with a RoleBinding and assemble the configuration file from it. Developers and tools can then work without the administrative kubeconfig from the Control Panel.
Prerequisites
- A cluster with the status Running and a verified
kubectlconnection using the kubeconfig from the Control Panel, see Connect to the cluster opensslon your machine- A namespace the new user is to access –
entwicklungin the examples
In the examples, the user is called entwickler. Replace the user and namespace names with your own.
1. Create a key and a certificate signing request
Generate a private key and a certificate signing request (CSR) from it:
openssl genrsa -out entwickler.key 4096
openssl req -new -key entwickler.key -out entwickler.csr \
-subj "/CN=entwickler/O=entwicklung-team"
Kubernetes takes the user name from the CN field and the groups from the O fields. You refer to these names later in the RoleBinding.
Never enter system:masters as a group. Members of this group have unrestricted access to the cluster, regardless of any RoleBinding.
The private key does not need to leave the user's device. Have the user generate the key and CSR themselves and send you only the entwickler.csr file.
2. Create the CertificateSigningRequest in the cluster
Submit the CSR to the cluster as a CertificateSigningRequest object. expirationSeconds sets the requested validity period, 90 days in this example:
cat <<EOF | kubectl apply -f -
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
name: entwickler
spec:
request: $(base64 < entwickler.csr | tr -d '\n')
signerName: kubernetes.io/kube-apiserver-client
expirationSeconds: 7776000
usages:
- client auth
EOF
The request is now in the Pending state:
kubectl get csr entwickler
3. Approve the request and retrieve the certificate
kubectl certificate approve entwickler
As soon as the CONDITION column shows Approved,Issued, retrieve the signed certificate:
kubectl get csr entwickler -o jsonpath='{.status.certificate}' \
| base64 -d > entwickler.crt
Check the user name and expiry date:
openssl x509 -in entwickler.crt -noout -subject -enddate
4. Grant permissions
A new user has no permissions at first. Assign a role with a RoleBinding – in this example the predefined ClusterRole edit, restricted to the entwicklung namespace:
kubectl create rolebinding entwickler-edit \
--clusterrole=edit \
--user=entwickler \
--namespace=entwicklung
Instead of individual users, you can also grant permissions to the group from the O field with --group=entwicklung-team. Custom roles and further options are described in Setting up RoleBindings.
5. Assemble the kubeconfig
Read the cluster address and CA certificate from your current kubeconfig:
SERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
kubectl config view --minify --raw \
-o jsonpath='{.clusters[0].cluster.certificate-authority-data}' \
| base64 -d > ca.crt
Use them to build the new file entwickler-kubeconfig.yaml. With --embed-certs, all certificates and the key are stored directly in the file:
NEW_KUBECONFIG=entwickler-kubeconfig.yaml
kubectl config set-cluster centron-cluster \
--server="$SERVER" \
--certificate-authority=ca.crt \
--embed-certs=true \
--kubeconfig="$NEW_KUBECONFIG"
kubectl config set-credentials entwickler \
--client-certificate=entwickler.crt \
--client-key=entwickler.key \
--embed-certs=true \
--kubeconfig="$NEW_KUBECONFIG"
kubectl config set-context entwickler@centron-cluster \
--cluster=centron-cluster \
--user=entwickler \
--namespace=entwicklung \
--kubeconfig="$NEW_KUBECONFIG"
kubectl config use-context entwickler@centron-cluster \
--kubeconfig="$NEW_KUBECONFIG"
chmod 600 "$NEW_KUBECONFIG"
6. Verify access
# Show the authenticated user
kubectl --kubeconfig=entwickler-kubeconfig.yaml auth whoami
# Permitted action in the user's namespace
kubectl --kubeconfig=entwickler-kubeconfig.yaml auth can-i create deployments
# Action that is not permitted – expected answer: no
kubectl --kubeconfig=entwickler-kubeconfig.yaml auth can-i list nodes
7. Clean up and hand over
Delete the CSR object and the local helper files. The key is now contained in the kubeconfig:
kubectl delete csr entwickler
rm entwickler.csr entwickler.crt ca.crt
If the user generated the key themselves, send them only entwickler.crt and ca.crt together with the server address – they then carry out step 5 with their own key.
Treat the finished kubeconfig like a password: do not check it into version control and do not share it via unencrypted channels.
Revoke access
Kubernetes cannot revoke client certificates. An issued certificate remains valid until it expires. For this reason, do not set expirationSeconds longer than necessary.
To revoke a user's access before the certificate expires, delete their RoleBindings. The user can still authenticate, but can no longer read or change anything:
kubectl delete rolebinding entwickler-edit --namespace=entwicklung
If you granted permissions via a group, remove the group's binding – or issue new certificates with a different group to users who should keep access.