Skip to content
LF Cert Trainer

Lesson 74 · Section 7: Security

Lesson 74: Certificate creation: making the cluster's certificates with OpenSSL

How every certificate in a cluster is made with the same three steps (key, signing request, signed certificate), which names to put in client and server certificates, and how to create or repair one by hand or with kubeadm. · 18 min read · 4 min video

1/12Certificate creation
0:00 / 4:26

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

What certificate creation is

The previous lesson listed the certificates a cluster needs: a server certificate for everything that listens, a client certificate for everything that connects, and a certificate authority (CA) that all of them trust. This lesson is about making those files. You will do it with openssl, the tool that is installed on every exam machine, and then see how kubeadm does the same work for you.

There are about twenty certificates in a kubeadm cluster, but there is only one recipe.

Every certificate is made in three stepsreq -newsendx509 -req signsusestays with owneradmin.keyprivate, never sharedadmin.csrpublic key + namesCAca.crt + ca.keyadmin.crtsigned, expireskubectlsends crt, proves key
Every certificate is made in three steps. The private key never leaves its owner. Only the signing request travels to the CA, and the CA answers with a certificate signed by its own private key.

Parts:

  • admin.key (private, never shared)
  • admin.csr (public key + names)
  • CA (ca.crt + ca.key)
  • admin.crt (signed, expires)
  • kubectl (sends crt, proves key)

Connections:

  • admin.key to admin.csr: req -new
  • admin.csr to CA: send
  • CA to admin.crt: x509 -req signs
  • admin.crt to kubectl: use
  • admin.key to kubectl: stays with owner

So the real skill is not the commands, which never change. It is knowing which names each certificate must carry, because Kubernetes gives those names a meaning.

How it works

Step 0: the certificate authority

A CA is nothing more than a key pair whose certificate everybody has agreed to trust. Its certificate is self-signed: the CA signs it with its own key, because there is nobody above it.

openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=kubernetes-ca" -days 3650 -out ca.crt
PartMeaning
genrsa -out ca.key 2048Create a 2048-bit RSA private key
req -x509 -newCreate a new self-signed certificate instead of a request
-nodesDo not encrypt the key with a passphrase
-key ca.keyThe key that both owns and signs this certificate
-subj "/CN=kubernetes-ca"The name of the CA. Any name works; it only has to be recognisable
-days 3650Valid for ten years, the same as kubeadm's default for a CA

You now have the two files that everything else depends on:

  • ca.crt is public. Every component and every client gets a copy, so that it can verify certificates signed by this CA.
  • ca.key is the most sensitive file in the cluster. Whoever holds it can create a certificate for any user, including an administrator.
Under the hood · The three-command version of the CA

Many courses create the CA in the same three steps as every other certificate, using -signkey to self-sign:

openssl genrsa -out ca.key 2048
openssl req -new -key ca.key -subj "/CN=KUBERNETES-CA" -out ca.csr
openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt -days 3650

Both versions give a working CA. The one-command version, which is the one in the Kubernetes documentation, also writes the extension CA:TRUE into the certificate, which states plainly that this certificate is allowed to sign others. You can see it with openssl x509 -in ca.crt -noout -text | grep -A1 "Basic Constraints".

Steps 1 to 3: a client certificate

Here is the full recipe for an administrator's certificate.

# 1. Private key
openssl genrsa -out admin.key 2048

# 2. Signing request: the public key plus the identity
openssl req -new -key admin.key -subj "/CN=kube-admin/O=system:masters" -out admin.csr

# 3. The CA signs the request
openssl x509 -req -in admin.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out admin.crt -days 365
Certificate request self-signature ok
subject=CN = kube-admin, O = system:masters

The third command is where trust is created. -CA and -CAkey name the authority that signs, -CAcreateserial lets OpenSSL create the file ca.srl in which it keeps track of serial numbers, and -days sets the lifetime. The .csr file has done its job after signing and can be deleted.

The subject is the identity

Kubernetes has no table of users. For a client certificate, the certificate is the account.

A client certificate is an identityTLSreads CNreads Oadmin.crtsigned by the CAkube-apiserver--client-ca-fileUserCN=kube-adminRBACallow or denyGroupO=system:masters
A client certificate is an identity. The API server trusts any certificate signed by its client CA. It reads the user name from the CN field and the groups from the O fields, and RBAC then decides what that user may do.

Parts:

  • admin.crt (signed by the CA)
  • kube-apiserver (--client-ca-file)
  • User (CN=kube-admin)
  • RBAC (allow or deny)
  • Group (O=system:masters)

Connections:

  • admin.crt to kube-apiserver: TLS
  • kube-apiserver to User: reads CN
  • kube-apiserver to Group: reads O
  • User to RBAC
  • Group to RBAC

When a client connects, the API server checks that the certificate was signed by the CA named in its --client-ca-file flag. If so, it reads the identity straight from the subject:

Subject fieldBecomesExample
CN (common name)The user nameCN=kube-admin
O (organization)A group. Repeat it for several groupsO=dev/O=qa

Nothing else is looked up. RBAC then decides what that user name and those groups may do. This is why the subject in the example contains O=system:masters: that group is a built-in superuser group, so the certificate is an administrator without any further configuration.

Client certificates for the components

The control plane components and the kubelets are clients of the API server too. Their certificates are made with exactly the same three commands. Only the subject changes, and here the names are not free to choose: the built-in RBAC roles and the Node authorizer are bound to these exact strings.

ClientCNO
kube-schedulersystem:kube-schedulernone
kube-controller-managersystem:kube-controller-managernone
kube-proxysystem:kube-proxynone
kubelet on node01system:node:node01system:nodes
API server, when it calls a kubeletkube-apiserver-kubelet-clientan administrators group
API server, when it calls etcdkube-apiserver-etcd-clientnone (signed by the etcd CA)

For example, the scheduler:

openssl genrsa -out scheduler.key 2048
openssl req -new -key scheduler.key -subj "/CN=system:kube-scheduler" -out scheduler.csr
openssl x509 -req -in scheduler.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out scheduler.crt -days 365

A wrong name does not fail when you sign. It fails later and confusingly: the component authenticates, because the signature is valid, and is then forbidden to do its work, because no role is bound to the name you typed.

User "system:kube-schedular" cannot list resource "pods" in API group "" at the cluster scope

Server certificates: every name must be listed

A server certificate answers a different question. The client asks: "I dialled this address. Is the certificate really for this address?" The answer is in the certificate's subject alternative names (SANs), a list of DNS names and IP addresses.

A server certificate must list every name clients usename or 10.96.0.1by IPby host nameheld byPodkubernetes.default.svckubectlhttps://192.168.1.10kubelethttps://controlplaneapiserver.crtSAN lists every namekube-apiserver--tls-cert-file
A server certificate must list every name clients use. Clients reach the API server by Service name, by Service IP, by node IP and by host name. Each client checks that the address it dialled is in the certificate's subject alternative names.

Parts:

  • Pod (kubernetes.default.svc)
  • kubectl (https://192.168.1.10)
  • kubelet (https://controlplane)
  • apiserver.crt (SAN lists every name)
  • kube-apiserver (--tls-cert-file)

Connections:

  • Pod to apiserver.crt: name or 10.96.0.1
  • kubectl to apiserver.crt: by IP
  • kubelet to apiserver.crt: by host name
  • apiserver.crt to kube-apiserver: held by

The API server is reached in many ways, and each one must be in the list:

  • kubernetes, kubernetes.default, kubernetes.default.svc and kubernetes.default.svc.cluster.local, the names of the kubernetes Service that Pods use.
  • The cluster IP of that Service, which is the first address of the Service range (10.96.0.1 by default).
  • The host name and the IP address of each control plane node, plus the load balancer address if there is one.

A -subj string cannot hold a list, so the names go in a small configuration file:

# openssl.cnf
[ req ]
prompt = no
distinguished_name = dn
req_extensions = v3_req

[ dn ]
CN = kube-apiserver

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster.local
DNS.5 = controlplane
IP.1 = 10.96.0.1
IP.2 = 192.168.1.10
openssl genrsa -out apiserver.key 2048
openssl req -new -key apiserver.key -out apiserver.csr -config openssl.cnf
openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
  -out apiserver.crt -days 365 -extensions v3_req -extfile openssl.cnf

The other servers follow the same pattern with their own names:

ServerNames its certificate needs
etcd (server)Host name and IP of the etcd member, localhost, 127.0.0.1
etcd (peer)The same, for traffic between etcd members
kubeletThe node's name and IP. One certificate per node, named after the node

Where the files go

A certificate does nothing until a component is told to use it. Each one is wired in with a flag (or a field in a configuration file), always as a pair of certificate and key, plus the CA that the component should trust.

ComponentFlags
kube-apiserver, as a server--tls-cert-file, --tls-private-key-file, --client-ca-file
kube-apiserver, as a client of etcd--etcd-cafile, --etcd-certfile, --etcd-keyfile
kube-apiserver, as a client of kubelets--kubelet-client-certificate, --kubelet-client-key
etcd--cert-file, --key-file, --trusted-ca-file, and the --peer-... versions
Scheduler, controller manager, kubelet, kube-proxy, kubectlA kubeconfig file that holds the client certificate, the key and the CA

The last row matters: a client does not take its certificate on the command line in daily use. It is stored in a kubeconfig file, which a later lesson covers. For a quick test you can pass the three files directly:

kubectl get pods --server https://192.168.1.10:6443 \
  --certificate-authority ca.crt --client-certificate admin.crt --client-key admin.key

curl https://192.168.1.10:6443/api/v1/namespaces/default/pods \
  --cacert ca.crt --cert admin.crt --key admin.key

What kubeadm does for you

kubeadm init runs these same three steps for every certificate and puts the results in /etc/kubernetes/pki. You can run that stage on its own, and manage the results later:

CommandWhat it does
kubeadm init phase certs allCreates every missing CA and certificate. Existing files are kept
kubeadm init phase certs apiserverCreates only the API server certificate
kubeadm certs check-expirationLists every certificate with its expiry date and its CA
kubeadm certs renew allIssues new certificates with the same names and a fresh lifetime
kubeadm kubeconfig user --client-name janePrints a kubeconfig with a new client certificate for a user

kubeadm certificates live for one year and its CAs for ten. Three separate CAs are created: one for the cluster, one for etcd and one for the front proxy. That is why /etc/kubernetes/pki/etcd/ has its own ca.crt, and why an etcd certificate signed with the cluster CA is rejected.

Under the hood · sa.key and sa.pub are not certificates

Next to the certificates in /etc/kubernetes/pki you will find sa.key and sa.pub. This is a bare key pair with no certificate and no CA. The controller manager signs ServiceAccount tokens with the private key, and the API server verifies them with the public key. Nobody needs to check a name or an expiry date on it, so a certificate would add nothing. kubeadm certs check-expiration does not list it, because a key alone never expires.

Using it

Example · Create a certificate for a new user

A colleague, Jane, needs access as the user jane in the group dev. On a kubeadm control plane node, sign with the cluster's own CA:

openssl genrsa -out jane.key 2048
openssl req -new -key jane.key -subj "/CN=jane/O=dev" -out jane.csr
sudo openssl x509 -req -in jane.csr \
  -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial \
  -out jane.crt -days 90

Register the pair as a user entry in your kubeconfig, then ask the API server who it thinks this certificate is:

kubectl config set-credentials jane --client-certificate jane.crt --client-key jane.key
kubectl auth whoami --user jane
ATTRIBUTE   VALUE
Username    jane
Groups      [dev system:authenticated]

The identity is accepted, but Jane can do nothing yet. Authentication is finished; authorization is a separate step that you complete with a Role and a RoleBinding for the user jane or the group dev.

This route needs the CA key, so it only works on a control plane node. The Certificates API, covered two lessons from now, lets you sign a request through the API server instead.

Example · Add a missing name to the API server certificate

kubectl fails from a new address with:

tls: failed to verify certificate: x509: certificate is valid for 10.96.0.1, 192.168.1.10, not 192.168.1.50

The address is not in the certificate's alternative names. On a kubeadm cluster, let kubeadm make a new certificate. It only creates files that are missing, so move the old pair away first:

cd /etc/kubernetes/pki
sudo mkdir -p /root/pki-backup && sudo mv apiserver.crt apiserver.key /root/pki-backup/
sudo kubeadm init phase certs apiserver --apiserver-cert-extra-sans=192.168.1.50
openssl x509 -in apiserver.crt -noout -ext subjectAltName

The API server reads its certificate at start, so restart its static Pod. Moving the manifest out of the folder and back in does it:

sudo mv /etc/kubernetes/manifests/kube-apiserver.yaml /tmp/
sleep 20
sudo mv /tmp/kube-apiserver.yaml /etc/kubernetes/manifests/

Example · A server certificate without a configuration file

With OpenSSL 3, which current distributions ship, you can put the names on the command line and tell the signing step to copy them from the request:

openssl genrsa -out etcd-server.key 2048
openssl req -new -key etcd-server.key -subj "/CN=etcd-server" \
  -addext "subjectAltName=DNS:controlplane,DNS:localhost,IP:192.168.1.10,IP:127.0.0.1" \
  -out etcd-server.csr
openssl x509 -req -in etcd-server.csr \
  -CA /etc/kubernetes/pki/etcd/ca.crt -CAkey /etc/kubernetes/pki/etcd/ca.key -CAcreateserial \
  -out etcd-server.crt -days 365 -copy_extensions copy
openssl x509 -in etcd-server.crt -noout -ext subjectAltName

Note the CA: etcd certificates are signed by the etcd CA in /etc/kubernetes/pki/etcd/, not by the cluster CA.

Example · Check that a key and a certificate belong together

A component that is given a certificate and a key from two different pairs refuses to start. Compare the public key inside each file. The two hashes must be identical:

openssl x509 -in admin.crt -noout -pubkey | openssl sha256
openssl pkey -in admin.key -pubout | openssl sha256

And check that a certificate really was signed by the CA you expect:

openssl verify -CAfile ca.crt admin.crt
admin.crt: OK

Tricks

Common mistakes

  • Typing a component name wrong. system:kube-scheduler, system:kube-controller-manager, system:kube-proxy and system:node:<node name> must match exactly, or the component authenticates and is then forbidden.
  • Forgetting O=system:nodes on a kubelet certificate. The Node authorizer requires both the group and the system:node: prefix on the name.
  • Losing the alternative names when signing. Pass -extensions and -extfile (or -copy_extensions copy) to openssl x509 -req, then check with -ext subjectAltName.
  • Leaving out the Service IP. Without 10.96.0.1 (or the first address of your Service range) in the API server certificate, every Pod that talks to the API fails, while kubectl from outside still works.
  • Signing an etcd certificate with the cluster CA. etcd trusts only its own CA.
  • Mixing up openssl req and openssl x509. req creates requests (and, with -x509, a self-signed certificate). x509 -req turns a request into a certificate signed by a CA.
  • Expecting a new certificate to take effect by itself. The component must point at the file and be restarted.
  • Handing out ca.key, or a system:masters certificate, to a user. Neither can be revoked.

Recap

  • Every certificate is a private key, a signing request and a signature from the CA: openssl genrsa, openssl req -new, openssl x509 -req -CA -CAkey.
  • The CA is a self-signed certificate. ca.crt goes everywhere; ca.key stays locked away.
  • In a client certificate CN is the user and O is the group. Component names such as system:kube-scheduler and system:node:<name> with O=system:nodes are fixed.
  • A server certificate must list every DNS name and IP that clients use as subject alternative names, including the kubernetes Service names and its cluster IP for the API server.
  • Signing drops the request's extensions unless you pass them again. Always pass -days, and read the result back.
  • kubeadm does all of this in /etc/kubernetes/pki: kubeadm init phase certs, kubeadm certs check-expiration and kubeadm certs renew.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Use kubeadm on a running cluster: read the configuration it was built with, print a join command that works, and renew the API server's certificate and prove that the new one is served.

    Build · Level 2 · standard · target 20 min

  • Use a certificate authority's key and openssl to issue a short-lived client certificate, then hand it to the app as a Secret.

    Build · Level 2 · standard · target 20 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.