2017-08-29 00:19:25 +03:00
# Generating Kubernetes Configuration Files for Authentication
In this lab you will generate [Kubernetes configuration files ](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ ), also known as kubeconfigs, which enable Kubernetes clients to locate and authenticate to the Kubernetes API Servers.
## Client Authentication Configs
2018-05-12 19:54:18 +03:00
In this section you will generate kubeconfig files for the `controller manager` , `kubelet` , `kube-proxy` , and `scheduler` clients and the `admin` user.
2017-08-29 00:19:25 +03:00
### Kubernetes Public IP Address
Each kubeconfig requires a Kubernetes API Server to connect to. To support high availability the IP address assigned to the external load balancer fronting the Kubernetes API Servers will be used.
Retrieve the `kubernetes-the-hard-way` static IP address:
```
2023-08-01 16:48:20 +03:00
KUBERNETES_PUBLIC_ADDRESS="$(gcloud compute addresses describe kubernetes-the-hard-way \
--format 'value(address)')"
2017-08-29 00:19:25 +03:00
```
### The kubelet Kubernetes Configuration File
2023-08-01 16:48:20 +03:00
When generating kubeconfig files for Kubelets the client certificate matching the Kubelet's node name must be used. This will ensure Kubelets are properly authorized by the Kubernetes [Node Authorizer ](https://kubernetes.io/docs/reference/access-authn-authz/node/ ).
2017-08-29 00:19:25 +03:00
2023-08-01 16:48:20 +03:00
> The following commands must be run in the same directory used to generate the SSL certificates during the [Generating TLS Certificates](./04-certificate-authority.md) lab.
2019-09-14 21:41:56 +03:00
2017-08-29 00:19:25 +03:00
Generate a kubeconfig file for each worker node:
```
for instance in worker-0 worker-1 worker-2; do
kubectl config set-cluster kubernetes-the-hard-way \
2023-08-01 16:48:20 +03:00
--certificate-authority ca.pem \
--embed-certs \
--kubeconfig "${instance}.kubeconfig" \
--server "https://${KUBERNETES_PUBLIC_ADDRESS}:6443"
2017-08-29 00:19:25 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-credentials "system:node:${instance}" \
--client-certificate "${instance}.pem" \
--client-key "${instance}-key.pem" \
--embed-certs \
--kubeconfig "${instance}.kubeconfig"
2017-08-29 00:19:25 +03:00
kubectl config set-context default \
2023-08-01 16:48:20 +03:00
--cluster "kubernetes-the-hard-way" \
--kubeconfig "${instance}.kubeconfig" \
--user "system:node:${instance}"
2017-08-29 00:19:25 +03:00
2023-08-01 16:48:20 +03:00
kubectl config use-context default \
--kubeconfig "${instance}.kubeconfig"
2017-08-29 00:19:25 +03:00
done
```
Results:
```
worker-0.kubeconfig
worker-1.kubeconfig
worker-2.kubeconfig
```
### The kube-proxy Kubernetes Configuration File
Generate a kubeconfig file for the `kube-proxy` service:
```
2023-08-01 16:48:20 +03:00
kubectl config set-cluster kubernetes-the-hard-way \
--certificate-authority ca.pem \
--embed-certs \
--kubeconfig kube-proxy.kubeconfig \
--server "https://${KUBERNETES_PUBLIC_ADDRESS}:6443"
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-credentials system:kube-proxy \
--client-certificate kube-proxy.pem \
--client-key kube-proxy-key.pem \
--embed-certs \
--kubeconfig kube-proxy.kubeconfig
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-context default \
--cluster kubernetes-the-hard-way \
--kubeconfig kube-proxy.kubeconfig \
--user system:kube-proxy
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config use-context default \
--kubeconfig kube-proxy.kubeconfig
2018-05-12 19:54:18 +03:00
```
Results:
```
kube-proxy.kubeconfig
```
### The kube-controller-manager Kubernetes Configuration File
Generate a kubeconfig file for the `kube-controller-manager` service:
```
2023-08-01 16:48:20 +03:00
kubectl config set-cluster kubernetes-the-hard-way \
--certificate-authority ca.pem \
--embed-certs \
--kubeconfig kube-controller-manager.kubeconfig \
--server https://127.0.0.1:6443
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-credentials system:kube-controller-manager \
--client-certificate kube-controller-manager.pem \
--client-key kube-controller-manager-key.pem \
--embed-certs \
--kubeconfig kube-controller-manager.kubeconfig
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-context default \
--cluster kubernetes-the-hard-way \
--kubeconfig kube-controller-manager.kubeconfig \
--user system:kube-controller-manager
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config use-context default \
--kubeconfig kube-controller-manager.kubeconfig
2018-05-12 19:54:18 +03:00
```
Results:
```
kube-controller-manager.kubeconfig
```
### The kube-scheduler Kubernetes Configuration File
Generate a kubeconfig file for the `kube-scheduler` service:
```
2023-08-01 16:48:20 +03:00
kubectl config set-cluster kubernetes-the-hard-way \
--certificate-authority ca.pem \
--embed-certs \
--kubeconfig kube-scheduler.kubeconfig \
--server https://127.0.0.1:6443
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-credentials system:kube-scheduler \
--client-certificate kube-scheduler.pem \
--client-key kube-scheduler-key.pem \
--embed-certs \
--kubeconfig kube-scheduler.kubeconfig
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-context default \
--cluster kubernetes-the-hard-way \
--kubeconfig kube-scheduler.kubeconfig \
--user system:kube-scheduler
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config use-context default \
--kubeconfig kube-scheduler.kubeconfig
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
Results:
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
kube-scheduler.kubeconfig
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
### The admin Kubernetes Configuration File
Generate a kubeconfig file for the `admin` user:
2017-08-29 00:19:25 +03:00
```
2023-08-01 16:48:20 +03:00
kubectl config set-cluster kubernetes-the-hard-way \
--certificate-authority ca.pem \
--embed-certs \
--kubeconfig admin.kubeconfig \
--server https://127.0.0.1:6443
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-credentials admin \
--client-certificate admin.pem \
--client-key admin-key.pem \
--embed-certs \
--kubeconfig admin.kubeconfig
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config set-context default \
--cluster kubernetes-the-hard-way \
--kubeconfig admin.kubeconfig \
--user admin
2018-05-12 19:54:18 +03:00
2023-08-01 16:48:20 +03:00
kubectl config use-context default \
--kubeconfig admin.kubeconfig
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
Results:
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
admin.kubeconfig
2017-08-29 00:19:25 +03:00
```
2023-08-01 16:48:20 +03:00
##
2018-05-12 19:54:18 +03:00
2017-08-29 00:19:25 +03:00
## Distribute the Kubernetes Configuration Files
Copy the appropriate `kubelet` and `kube-proxy` kubeconfig files to each worker instance:
```
for instance in worker-0 worker-1 worker-2; do
2023-08-01 16:48:20 +03:00
gcloud compute scp \
"${instance}.kubeconfig" \
kube-proxy.kubeconfig \
"${instance}:"
2017-08-29 00:19:25 +03:00
done
```
2018-05-12 19:54:18 +03:00
Copy the appropriate `kube-controller-manager` and `kube-scheduler` kubeconfig files to each controller instance:
```
for instance in controller-0 controller-1 controller-2; do
2023-08-01 16:48:20 +03:00
gcloud compute scp \
admin.kubeconfig \
kube-controller-manager.kubeconfig \
kube-scheduler.kubeconfig \
"${instance}:"
2018-05-12 19:54:18 +03:00
done
```
2023-08-01 16:48:20 +03:00
Next: [Generating the Data Encryption Config and Key ](./06-data-encryption-keys.md )