2017-08-29 00:19:25 +03:00
# Provisioning a CA and Generating TLS Certificates
2018-05-12 19:54:18 +03:00
In this lab you will provision a [PKI Infrastructure ](https://en.wikipedia.org/wiki/Public_key_infrastructure ) using CloudFlare's PKI toolkit, [cfssl ](https://github.com/cloudflare/cfssl ), 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.
2017-08-29 00:19:25 +03:00
## Certificate Authority
In this section you will provision a Certificate Authority that can be used to generate additional TLS certificates.
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the CA configuration file, certificate, and private key:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
cat > ca-config.json < < EOF
{
"signing": {
"default": {
"expiry": "8760h"
},
"profiles": {
"kubernetes": {
"usages": ["signing", "key encipherment", "server auth", "client auth"],
"expiry": "8760h"
}
}
}
}
EOF
cat > ca-csr.json < < EOF
{
"CN": "Kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2017-08-29 00:19:25 +03:00
"O": "Kubernetes",
"OU": "CA",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2017-08-29 00:19:25 +03:00
}
]
}
EOF
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
ca-key.pem
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
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the `admin` client certificate and private key:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
cat > admin-csr.json < < EOF
{
"CN": "admin",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2017-08-29 00:19:25 +03:00
"O": "system:masters",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2017-08-29 00:19:25 +03:00
}
]
}
EOF
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
admin-csr.json | cfssljson -bare admin
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
admin-key.pem
admin.pem
```
### The Kubelet Client Certificates
Kubernetes uses a [special-purpose authorization mode ](https://kubernetes.io/docs/admin/authorization/node/ ) called Node Authorizer, that specifically authorizes API requests made by [Kubelets ](https://kubernetes.io/docs/concepts/overview/components/#kubelet ). 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.
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate a certificate and private key for each Kubernetes worker node (you need to replace YOUR_EXTERNAL_IP by your external IP address):
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2020-06-20 14:48:17 +03:00
for id_instance in 0 1 2; do
cat > worker-${id_instance}-csr.json < < EOF
2017-08-29 00:19:25 +03:00
{
2020-06-20 14:48:17 +03:00
"CN": "system:node:worker-${id_instance}",
2017-08-29 00:19:25 +03:00
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2017-08-29 00:19:25 +03:00
"O": "system:nodes",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2017-08-29 00:19:25 +03:00
}
]
}
EOF
2020-06-20 14:48:17 +03:00
EXTERNAL_IP=YOUR_EXTERNAL_IP
2017-08-29 00:19:25 +03:00
2020-06-20 14:48:17 +03:00
INTERNAL_IP=192.168.8.2${id_instance}
2017-08-29 00:19:25 +03:00
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
2020-06-20 14:48:17 +03:00
-hostname=worker-${id_instance},${EXTERNAL_IP},${INTERNAL_IP} \
2017-08-29 00:19:25 +03:00
-profile=kubernetes \
2020-06-20 14:48:17 +03:00
worker-${id_instance}-csr.json | cfssljson -bare worker-${id_instance}
2017-08-29 00:19:25 +03:00
done
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
worker-0-key.pem
worker-0.pem
worker-1-key.pem
worker-1.pem
worker-2-key.pem
worker-2.pem
```
2018-05-12 19:54:18 +03:00
### The Controller Manager Client Certificate
2017-08-29 00:19:25 +03:00
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the `kube-controller-manager` client certificate and private key:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat > kube-controller-manager-csr.json < < EOF
{
"CN": "system:kube-controller-manager",
2017-08-29 00:19:25 +03:00
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2018-05-12 19:54:18 +03:00
"O": "system:kube-controller-manager",
2017-08-29 00:19:25 +03:00
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2017-08-29 00:19:25 +03:00
}
]
}
EOF
2018-05-12 19:54:18 +03:00
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
kube-controller-manager-key.pem
kube-controller-manager.pem
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
### The Kube Proxy Client Certificate
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the `kube-proxy` client certificate and private key:
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat > kube-proxy-csr.json < < EOF
{
"CN": "system:kube-proxy",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2018-05-12 19:54:18 +03:00
"O": "system:node-proxier",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2018-05-12 19:54:18 +03:00
}
]
}
EOF
2017-08-29 00:19:25 +03:00
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
kube-proxy-csr.json | cfssljson -bare kube-proxy
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
kube-proxy-key.pem
kube-proxy.pem
```
2018-05-12 19:54:18 +03:00
### The Scheduler Client Certificate
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the `kube-scheduler` client certificate and private key:
2018-05-12 19:54:18 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat > kube-scheduler-csr.json < < EOF
{
"CN": "system:kube-scheduler",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2018-05-12 19:54:18 +03:00
"O": "system:kube-scheduler",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2018-05-12 19:54:18 +03:00
}
]
}
EOF
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
kube-scheduler-csr.json | cfssljson -bare kube-scheduler
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
kube-scheduler-key.pem
kube-scheduler.pem
```
2017-08-29 00:19:25 +03:00
### 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.
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the Kubernetes API Server certificate and private key (you need to replace YOUR_EXTERNAL_IP by your external IP address):
2017-08-29 00:19:25 +03:00
2020-06-20 10:24:03 +03:00
```bash
2020-06-20 14:48:17 +03:00
KUBERNETES_PUBLIC_ADDRESS=YOUR_EXTERNAL_IP
2017-08-29 00:19:25 +03:00
2019-09-14 21:41:56 +03:00
KUBERNETES_HOSTNAMES=kubernetes,kubernetes.default,kubernetes.default.svc,kubernetes.default.svc.cluster,kubernetes.svc.cluster.local
2017-08-29 00:19:25 +03:00
cat > kubernetes-csr.json < < EOF
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2017-08-29 00:19:25 +03:00
"O": "Kubernetes",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2017-08-29 00:19:25 +03:00
}
]
}
EOF
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
2019-09-14 21:41:56 +03:00
-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_HOSTNAMES} \
2017-08-29 00:19:25 +03:00
-profile=kubernetes \
kubernetes-csr.json | cfssljson -bare kubernetes
```
2019-09-14 21:41:56 +03:00
> The Kubernetes API server is automatically assigned the `kubernetes` internal dns name, which will be linked to the first IP address (`10.32.0.1`) from the address range (`10.32.0.0/24`) reserved for internal cluster services during the [control plane bootstrapping](08-bootstrapping-kubernetes-controllers.md#configure-the-kubernetes-api-server) lab.
2017-08-29 00:19:25 +03:00
Results:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
kubernetes-key.pem
kubernetes.pem
```
2018-05-12 19:54:18 +03:00
## The Service Account Key Pair
2019-09-14 21:41:56 +03:00
The Kubernetes Controller Manager leverages a key pair to generate and sign service account tokens as described in the [managing service accounts ](https://kubernetes.io/docs/admin/service-accounts-admin/ ) documentation.
2018-05-12 19:54:18 +03:00
2020-06-20 14:48:17 +03:00
On the `gateway-01` VM, generate the `service-account` certificate and private key:
2018-05-12 19:54:18 +03:00
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat > service-account-csr.json < < EOF
{
"CN": "service-accounts",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
2020-06-20 14:48:17 +03:00
"C": "FR",
"L": "Rennes",
2018-05-12 19:54:18 +03:00
"O": "Kubernetes",
"OU": "Kubernetes The Hard Way",
2020-06-20 14:48:17 +03:00
"ST": "Bretagne"
2018-05-12 19:54:18 +03:00
}
]
}
EOF
cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=kubernetes \
service-account-csr.json | cfssljson -bare service-account
```
Results:
2020-06-20 10:24:03 +03:00
```bash
2018-05-12 19:54:18 +03:00
service-account-key.pem
service-account.pem
```
2017-08-29 00:19:25 +03:00
## Distribute the Client and Server Certificates
Copy the appropriate certificates and private keys to each worker instance:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
for instance in worker-0 worker-1 worker-2; do
2020-06-20 14:48:17 +03:00
scp ca.pem ${instance}-key.pem ${instance}.pem $root@{instance}:~/
2017-08-29 00:19:25 +03:00
done
```
Copy the appropriate certificates and private keys to each controller instance:
2020-06-20 10:24:03 +03:00
```bash
2017-08-29 00:19:25 +03:00
for instance in controller-0 controller-1 controller-2; do
2020-06-20 14:48:17 +03:00
scp ca.pem ca-key.pem kubernetes-key.pem kubernetes.pem \
service-account-key.pem service-account.pem root@${instance}:~/
2017-08-29 00:19:25 +03:00
done
```
2018-05-12 19:54:18 +03:00
> 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.
2017-08-29 00:19:25 +03:00
Next: [Generating Kubernetes Configuration Files for Authentication ](05-kubernetes-configuration-files.md )