# Provisioning a CA and Generating TLS Certificates In this lab you will provision a [PKI Infrastructure](https://en.wikipedia.org/wiki/Public_key_infrastructure) using the popular openssl tool, then use it to bootstrap a Certificate Authority, and generate TLS certificates for the following components: etcd, kube-apiserver, kube-controller-manager, kube-scheduler, kubelet, and kube-proxy. # Where to do these? You can do these on any machine with `openssl` on it. But you should be able to copy the generated files to the provisioned VMs. Or just do these from one of the master nodes. In our case we do it on the master-1 node, as we have set it up to be the administrative client. ## Certificate Authority In this section you will provision a Certificate Authority that can be used to generate additional TLS certificates. Create a CA certificate, then generate a Certificate Signing Request and use it to create a private key: ``` # Create private key for CA openssl genrsa -out ca.key 2048 # Create CSR using the private key openssl req -new -key ca.key -subj "/CN=KUBERNETES-CA" -out ca.csr # Self sign the csr using its own private key openssl x509 -req -in ca.csr -signkey ca.key -CAcreateserial -out ca.crt -days 1000 ``` Results: ``` ca.crt ca.key ``` The ca.crt is the Kubernetes Certificate Authority certificate and ca.key is the Kubernetes Certificate Authority private key. You will use the ca.crt file in many places, so it will be copied to many places. The ca.key is used by the CA for signing certificates. And it should be securely stored. In this case our master node(s) is our CA server as well, so we will store it on master node(s). There is not need to copy this file to elsewhere. ## Client and Server Certificates In this section you will generate client and server certificates for each Kubernetes component and a client certificate for the Kubernetes `admin` user. ### The Admin Client Certificate Generate the `admin` client certificate and private key: ``` # Geenrate private key for admin user openssl genrsa -out admin.key 2048 # Generate CSR for admin user. Note the OU. openssl req -new -key admin.key -subj "/CN=admin/O=system:masters" -out admin.csr # Sign certificate for admin user using CA servers private key openssl x509 -req -in admin.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out admin.crt -days 1000 ``` Note that the admin user is part of the **system:masters** group. This is how we are able to perform any administrative operations on Kubernetes cluster using kubectl utility. Results: ``` admin.key admin.crt ``` The admin.crt and admin.key file gives you administrative access. We will configure these to be used with the kubectl tool to perform administrative functions on kubernetes. ### The Kubelet Client Certificates We are going to skip certificate configuration for Worker Nodes for now. We will deal with them when we configure the workers. For now let's just focus on the control plane components. ### The Controller Manager Client Certificate Generate the `kube-controller-manager` client certificate and private key: ``` openssl genrsa -out kube-controller-manager.key 2048 openssl req -new -key kube-controller-manager.key -subj "/CN=system:kube-controller-manager" -out kube-controller-manager.csr openssl x509 -req -in kube-controller-manager.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kube-controller-manager.crt -days 1000 ``` Results: ``` kube-controller-manager.key kube-controller-manager.crt ``` ### The Kube Proxy Client Certificate Generate the `kube-proxy` client certificate and private key: ``` openssl genrsa -out kube-proxy.key 2048 openssl req -new -key kube-proxy.key -subj "/CN=system:kube-proxy" -out kube-proxy.csr openssl x509 -req -in kube-proxy.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kube-proxy.crt -days 1000 ``` Results: ``` kube-proxy.key kube-proxy.crt ``` ### The Scheduler Client Certificate Generate the `kube-scheduler` client certificate and private key: ``` openssl genrsa -out kube-scheduler.key 2048 openssl req -new -key kube-scheduler.key -subj "/CN=system:kube-scheduler" -out kube-scheduler.csr openssl x509 -req -in kube-scheduler.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out kube-scheduler.crt -days 1000 ``` Results: ``` kube-scheduler.key kube-scheduler.crt ``` ### The Kubernetes API Server Certificate The kube-apiserver certificate requires all names that various components may reach it to be part of the alternate names. These include the different DNS names, and IP addresses such as the master servers IP address, the load balancers IP address, the kube-api service IP address etc. The `openssl` command cannot take alternate names as command line parameter. So we must create a `conf` file for it: ``` cat > openssl.cnf < openssl-etcd.cnf < The `kube-proxy`, `kube-controller-manager`, `kube-scheduler`, and `kubelet` client certificates will be used to generate client authentication configuration files in the next lab. These certificates will be embedded into the client authentication configuration files. We will then copy those configuration files to the other master nodes. Next: [Generating Kubernetes Configuration Files for Authentication](05-kubernetes-configuration-files.md)