2017-08-29 00:19:25 +03:00
|
|
|
# Generating Kubernetes Configuration Files for Authentication
|
|
|
|
|
2025-06-03 05:13:21 +03:00
|
|
|
In this lab you will generate [Kubernetes client configuration files],
|
2025-06-02 06:37:55 +03:00
|
|
|
typically called kubeconfigs, which configure Kubernetes clients to connect
|
|
|
|
and authenticate to Kubernetes API Servers.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
|
|
|
## Client Authentication Configs
|
|
|
|
|
2025-06-02 06:37:55 +03:00
|
|
|
In this section you will generate kubeconfig files for the `kubelet` and the
|
|
|
|
`admin` user.
|
2017-08-29 00:19:25 +03:00
|
|
|
|
|
|
|
### The kubelet Kubernetes Configuration File
|
|
|
|
|
2025-06-02 06:37:55 +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
|
2025-06-03 05:13:21 +03:00
|
|
|
authorized by the Kubernetes [Node Authorizer].
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2025-06-02 06:37:55 +03:00
|
|
|
> The following commands must be run in the same directory used to generate
|
2025-06-03 05:13:21 +03:00
|
|
|
> the SSL certificates during the [Generating TLS Certificates] lab.
|
2019-09-14 21:41:56 +03:00
|
|
|
|
2025-06-02 05:33:01 +03:00
|
|
|
Generate a kubeconfig file for the `node01` and `node02` worker nodes:
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2025-06-02 05:33:01 +03:00
|
|
|
for host in node01 node02; do
|
2017-08-29 00:19:25 +03:00
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2017-08-29 00:19:25 +03:00
|
|
|
--embed-certs=true \
|
2025-06-02 05:59:11 +03:00
|
|
|
--server=https://controlplane.kubernetes.local:6443 \
|
2023-11-01 09:16:49 +03:00
|
|
|
--kubeconfig=${host}.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config set-credentials system:node:${host} \
|
|
|
|
--client-certificate=${host}.crt \
|
|
|
|
--client-key=${host}.key \
|
2017-08-29 00:19:25 +03:00
|
|
|
--embed-certs=true \
|
2023-11-01 09:16:49 +03:00
|
|
|
--kubeconfig=${host}.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
|
|
|
|
kubectl config set-context default \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--user=system:node:${host} \
|
|
|
|
--kubeconfig=${host}.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config use-context default \
|
|
|
|
--kubeconfig=${host}.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
done
|
|
|
|
```
|
|
|
|
|
|
|
|
Results:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
2025-06-02 05:33:01 +03:00
|
|
|
node01.kubeconfig
|
|
|
|
node02.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### The kube-proxy Kubernetes Configuration File
|
|
|
|
|
|
|
|
Generate a kubeconfig file for the `kube-proxy` service:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2018-05-12 19:54:18 +03:00
|
|
|
{
|
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
2025-06-02 05:59:11 +03:00
|
|
|
--server=https://controlplane.kubernetes.local:6443 \
|
2018-05-12 19:54:18 +03:00
|
|
|
--kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-credentials system:kube-proxy \
|
2023-11-01 09:16:49 +03:00
|
|
|
--client-certificate=kube-proxy.crt \
|
|
|
|
--client-key=kube-proxy.key \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
|
|
|
--kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-context default \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
|
|
|
--user=system:kube-proxy \
|
|
|
|
--kubeconfig=kube-proxy.kubeconfig
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config use-context default \
|
|
|
|
--kubeconfig=kube-proxy.kubeconfig
|
2018-05-12 19:54:18 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Results:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
2018-05-12 19:54:18 +03:00
|
|
|
kube-proxy.kubeconfig
|
|
|
|
```
|
|
|
|
|
|
|
|
### The kube-controller-manager Kubernetes Configuration File
|
|
|
|
|
|
|
|
Generate a kubeconfig file for the `kube-controller-manager` service:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2018-05-12 19:54:18 +03:00
|
|
|
{
|
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
2025-06-02 05:59:11 +03:00
|
|
|
--server=https://controlplane.kubernetes.local:6443 \
|
2018-05-12 19:54:18 +03:00
|
|
|
--kubeconfig=kube-controller-manager.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-credentials system:kube-controller-manager \
|
2023-11-01 09:16:49 +03:00
|
|
|
--client-certificate=kube-controller-manager.crt \
|
|
|
|
--client-key=kube-controller-manager.key \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
|
|
|
--kubeconfig=kube-controller-manager.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-context default \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
|
|
|
--user=system:kube-controller-manager \
|
|
|
|
--kubeconfig=kube-controller-manager.kubeconfig
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config use-context default \
|
|
|
|
--kubeconfig=kube-controller-manager.kubeconfig
|
2018-05-12 19:54:18 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Results:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
2018-05-12 19:54:18 +03:00
|
|
|
kube-controller-manager.kubeconfig
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### The kube-scheduler Kubernetes Configuration File
|
|
|
|
|
|
|
|
Generate a kubeconfig file for the `kube-scheduler` service:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2018-05-12 19:54:18 +03:00
|
|
|
{
|
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
2025-06-02 05:59:11 +03:00
|
|
|
--server=https://controlplane.kubernetes.local:6443 \
|
2018-05-12 19:54:18 +03:00
|
|
|
--kubeconfig=kube-scheduler.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-credentials system:kube-scheduler \
|
2023-11-01 09:16:49 +03:00
|
|
|
--client-certificate=kube-scheduler.crt \
|
|
|
|
--client-key=kube-scheduler.key \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
|
|
|
--kubeconfig=kube-scheduler.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-context default \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
|
|
|
--user=system:kube-scheduler \
|
|
|
|
--kubeconfig=kube-scheduler.kubeconfig
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config use-context default \
|
|
|
|
--kubeconfig=kube-scheduler.kubeconfig
|
2018-05-12 19:54:18 +03:00
|
|
|
}
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
2018-05-12 19:54:18 +03:00
|
|
|
Results:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
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:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
2018-05-12 19:54:18 +03:00
|
|
|
{
|
|
|
|
kubectl config set-cluster kubernetes-the-hard-way \
|
2023-11-01 09:16:49 +03:00
|
|
|
--certificate-authority=ca.crt \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
|
|
|
--server=https://127.0.0.1:6443 \
|
|
|
|
--kubeconfig=admin.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-credentials admin \
|
2023-11-01 09:16:49 +03:00
|
|
|
--client-certificate=admin.crt \
|
|
|
|
--client-key=admin.key \
|
2018-05-12 19:54:18 +03:00
|
|
|
--embed-certs=true \
|
|
|
|
--kubeconfig=admin.kubeconfig
|
|
|
|
|
|
|
|
kubectl config set-context default \
|
|
|
|
--cluster=kubernetes-the-hard-way \
|
|
|
|
--user=admin \
|
|
|
|
--kubeconfig=admin.kubeconfig
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
kubectl config use-context default \
|
|
|
|
--kubeconfig=admin.kubeconfig
|
2018-05-12 19:54:18 +03:00
|
|
|
}
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
2018-05-12 19:54:18 +03:00
|
|
|
Results:
|
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```text
|
2018-05-12 19:54:18 +03:00
|
|
|
admin.kubeconfig
|
2017-08-29 00:19:25 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## Distribute the Kubernetes Configuration Files
|
|
|
|
|
2025-06-03 05:13:21 +03:00
|
|
|
Copy the `kubelet` and `kube-proxy` kubeconfig files to the `node01` and
|
|
|
|
`node02` machines:
|
2023-11-01 09:16:49 +03:00
|
|
|
|
|
|
|
```bash
|
2025-06-02 05:33:01 +03:00
|
|
|
for host in node01 node02; do
|
2025-06-03 05:13:21 +03:00
|
|
|
ssh vagrant@${host} "sudo mkdir -p /var/lib/{kube-proxy,kubelet}"
|
2025-04-07 04:32:30 +03:00
|
|
|
|
2025-06-03 05:13:21 +03:00
|
|
|
scp kube-proxy.kubeconfig vagrant@${host}:~/
|
|
|
|
ssh vagrant@${host} "sudo mv kube-proxy.kubeconfig /var/lib/kube-proxy/kubeconfig"
|
2025-04-07 04:32:30 +03:00
|
|
|
|
2025-06-03 05:13:21 +03:00
|
|
|
scp ${host}.kubeconfig vagrant@${host}:~/
|
|
|
|
ssh vagrant@${host} "sudo mv ${host}.kubeconfig /var/lib/kubelet/kubeconfig"
|
2017-08-29 00:19:25 +03:00
|
|
|
done
|
|
|
|
```
|
|
|
|
|
2025-06-03 05:13:21 +03:00
|
|
|
Copy the `kube-controller-manager` and `kube-scheduler` kubeconfig files to
|
|
|
|
the `controlplane` machine:
|
2018-05-12 19:54:18 +03:00
|
|
|
|
2023-11-01 09:16:49 +03:00
|
|
|
```bash
|
|
|
|
scp admin.kubeconfig \
|
|
|
|
kube-controller-manager.kubeconfig \
|
|
|
|
kube-scheduler.kubeconfig \
|
2025-06-03 05:13:21 +03:00
|
|
|
vagrant@controlplane:~/
|
2018-05-12 19:54:18 +03:00
|
|
|
```
|
|
|
|
|
2017-08-29 00:19:25 +03:00
|
|
|
Next: [Generating the Data Encryption Config and Key](06-data-encryption-keys.md)
|
2025-06-03 05:13:21 +03:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
[Kubernetes client configuration files]: https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/
|
|
|
|
[Node Authorizer]: https://kubernetes.io/docs/reference/access-authn-authz/node/
|
|
|
|
[Generating TLS Certificates]: 04-certificate-authority.md
|