Provisioning a CA and Generating TLS Certificates

In this lab you will provision a PKI Infrastructure using CloudFlare’s PKI toolkit, cfssl, then use it to bootstrap a Certificate Authority, and generate TLS certificates for the following components: etcd, kube-apiserver, kubelet, and kube-proxy.

Certificate Authority

In this section you will provision a Certificate Authority that can be used to generate additional TLS certificates.

Create the CA configuration file:

  1. cat > ca-config.json <<EOF
  2. {
  3. "signing": {
  4. "default": {
  5. "expiry": "8760h"
  6. },
  7. "profiles": {
  8. "kubernetes": {
  9. "usages": ["signing", "key encipherment", "server auth", "client auth"],
  10. "expiry": "8760h"
  11. }
  12. }
  13. }
  14. }
  15. EOF

Create the CA certificate signing request:

  1. cat > ca-csr.json <<EOF
  2. {
  3. "CN": "Kubernetes",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "US",
  11. "L": "Portland",
  12. "O": "Kubernetes",
  13. "OU": "CA",
  14. "ST": "Oregon"
  15. }
  16. ]
  17. }
  18. EOF

Generate the CA certificate and private key:

  1. cfssl gencert -initca ca-csr.json | cfssljson -bare ca

Results:

  1. ca-key.pem
  2. ca.pem

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

Create the admin client certificate signing request:

  1. cat > admin-csr.json <<EOF
  2. {
  3. "CN": "admin",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "US",
  11. "L": "Portland",
  12. "O": "system:masters",
  13. "OU": "Kubernetes The Hard Way",
  14. "ST": "Oregon"
  15. }
  16. ]
  17. }
  18. EOF

Generate the admin client certificate and private key:

  1. cfssl gencert \
  2. -ca=ca.pem \
  3. -ca-key=ca-key.pem \
  4. -config=ca-config.json \
  5. -profile=kubernetes \
  6. admin-csr.json | cfssljson -bare admin

Results:

  1. admin-key.pem
  2. admin.pem

The Kubelet Client Certificates

Kubernetes uses a special-purpose authorization mode called Node Authorizer, that specifically authorizes API requests made by Kubelets. In order to be authorized by the Node Authorizer, Kubelets must use a credential that identifies them as being in the system:nodes group, with a username of system:node:<nodeName>. In this section you will create a certificate for each Kubernetes worker node that meets the Node Authorizer requirements.

Generate a certificate and private key for each Kubernetes worker node:

  1. for instance in worker-0 worker-1 worker-2; do
  2. cat > ${instance}-csr.json <<EOF
  3. {
  4. "CN": "system:node:${instance}",
  5. "key": {
  6. "algo": "rsa",
  7. "size": 2048
  8. },
  9. "names": [
  10. {
  11. "C": "US",
  12. "L": "Portland",
  13. "O": "system:nodes",
  14. "OU": "Kubernetes The Hard Way",
  15. "ST": "Oregon"
  16. }
  17. ]
  18. }
  19. EOF
  20. EXTERNAL_IP=$(gcloud compute instances describe ${instance} \
  21. --format 'value(networkInterfaces[0].accessConfigs[0].natIP)')
  22. INTERNAL_IP=$(gcloud compute instances describe ${instance} \
  23. --format 'value(networkInterfaces[0].networkIP)')
  24. cfssl gencert \
  25. -ca=ca.pem \
  26. -ca-key=ca-key.pem \
  27. -config=ca-config.json \
  28. -hostname=${instance},${EXTERNAL_IP},${INTERNAL_IP} \
  29. -profile=kubernetes \
  30. ${instance}-csr.json | cfssljson -bare ${instance}
  31. done

Results:

  1. worker-0-key.pem
  2. worker-0.pem
  3. worker-1-key.pem
  4. worker-1.pem
  5. worker-2-key.pem
  6. worker-2.pem

The kube-proxy Client Certificate

Create the kube-proxy client certificate signing request:

  1. cat > kube-proxy-csr.json <<EOF
  2. {
  3. "CN": "system:kube-proxy",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "US",
  11. "L": "Portland",
  12. "O": "system:node-proxier",
  13. "OU": "Kubernetes The Hard Way",
  14. "ST": "Oregon"
  15. }
  16. ]
  17. }
  18. EOF

Generate the kube-proxy client certificate and private key:

  1. cfssl gencert \
  2. -ca=ca.pem \
  3. -ca-key=ca-key.pem \
  4. -config=ca-config.json \
  5. -profile=kubernetes \
  6. kube-proxy-csr.json | cfssljson -bare kube-proxy

Results:

  1. kube-proxy-key.pem
  2. kube-proxy.pem

The Kubernetes API Server Certificate

The kubernetes-the-hard-way static IP address will be included in the list of subject alternative names for the Kubernetes API Server certificate. This will ensure the certificate can be validated by remote clients.

Retrieve the kubernetes-the-hard-way static IP address:

  1. KUBERNETES_PUBLIC_ADDRESS=$(gcloud compute addresses describe kubernetes-the-hard-way \
  2. --region $(gcloud config get-value compute/region) \
  3. --format 'value(address)')

Create the Kubernetes API Server certificate signing request:

  1. cat > kubernetes-csr.json <<EOF
  2. {
  3. "CN": "kubernetes",
  4. "key": {
  5. "algo": "rsa",
  6. "size": 2048
  7. },
  8. "names": [
  9. {
  10. "C": "US",
  11. "L": "Portland",
  12. "O": "Kubernetes",
  13. "OU": "Kubernetes The Hard Way",
  14. "ST": "Oregon"
  15. }
  16. ]
  17. }
  18. EOF

Generate the Kubernetes API Server certificate and private key:

  1. cfssl gencert \
  2. -ca=ca.pem \
  3. -ca-key=ca-key.pem \
  4. -config=ca-config.json \
  5. -hostname=10.32.0.1,10.240.0.10,10.240.0.11,10.240.0.12,${KUBERNETES_PUBLIC_ADDRESS},127.0.0.1,kubernetes.default \
  6. -profile=kubernetes \
  7. kubernetes-csr.json | cfssljson -bare kubernetes

Results:

  1. kubernetes-key.pem
  2. kubernetes.pem

Distribute the Client and Server Certificates

Copy the appropriate certificates and private keys to each worker instance:

  1. for instance in worker-0 worker-1 worker-2; do
  2. gcloud compute scp ca.pem ${instance}-key.pem ${instance}.pem ${instance}:~/
  3. done

Copy the appropriate certificates and private keys to each controller instance:

  1. for instance in controller-0 controller-1 controller-2; do
  2. gcloud compute scp ca.pem ca-key.pem kubernetes-key.pem kubernetes.pem ${instance}:~/
  3. done

The kube-proxy and kubelet client certificates will be used to generate client authentication configuration files in the next lab.

Next: Generating Kubernetes Configuration Files for Authentication