Generating Kubernetes Configuration Files for Authentication

In this lab you will generate Kubernetes configuration files, also known as kubeconfigs, which enable Kubernetes clients to locate and authenticate to the Kubernetes API Servers.

Client Authentication Configs

In this section you will generate kubeconfig files for the controller manager, kubelet, kube-proxy, and scheduler clients and the admin user.

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:

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

The kubelet Kubernetes Configuration File

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.

Generate a kubeconfig file for each worker node:

  1. for instance in worker-0 worker-1 worker-2; do
  2. kubectl config set-cluster kubernetes-the-hard-way \
  3. --certificate-authority=ca.pem \
  4. --embed-certs=true \
  5. --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443 \
  6. --kubeconfig=${instance}.kubeconfig
  7. kubectl config set-credentials system:node:${instance} \
  8. --client-certificate=${instance}.pem \
  9. --client-key=${instance}-key.pem \
  10. --embed-certs=true \
  11. --kubeconfig=${instance}.kubeconfig
  12. kubectl config set-context default \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=system:node:${instance} \
  15. --kubeconfig=${instance}.kubeconfig
  16. kubectl config use-context default --kubeconfig=${instance}.kubeconfig
  17. done

Results:

  1. worker-0.kubeconfig
  2. worker-1.kubeconfig
  3. worker-2.kubeconfig

The kube-proxy Kubernetes Configuration File

Generate a kubeconfig file for the kube-proxy service:

  1. {
  2. kubectl config set-cluster kubernetes-the-hard-way \
  3. --certificate-authority=ca.pem \
  4. --embed-certs=true \
  5. --server=https://${KUBERNETES_PUBLIC_ADDRESS}:6443 \
  6. --kubeconfig=kube-proxy.kubeconfig
  7. kubectl config set-credentials system:kube-proxy \
  8. --client-certificate=kube-proxy.pem \
  9. --client-key=kube-proxy-key.pem \
  10. --embed-certs=true \
  11. --kubeconfig=kube-proxy.kubeconfig
  12. kubectl config set-context default \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=system:kube-proxy \
  15. --kubeconfig=kube-proxy.kubeconfig
  16. kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
  17. }

Results:

  1. kube-proxy.kubeconfig

The kube-controller-manager Kubernetes Configuration File

Generate a kubeconfig file for the kube-controller-manager service:

  1. {
  2. kubectl config set-cluster kubernetes-the-hard-way \
  3. --certificate-authority=ca.pem \
  4. --embed-certs=true \
  5. --server=https://127.0.0.1:6443 \
  6. --kubeconfig=kube-controller-manager.kubeconfig
  7. kubectl config set-credentials system:kube-controller-manager \
  8. --client-certificate=kube-controller-manager.pem \
  9. --client-key=kube-controller-manager-key.pem \
  10. --embed-certs=true \
  11. --kubeconfig=kube-controller-manager.kubeconfig
  12. kubectl config set-context default \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=system:kube-controller-manager \
  15. --kubeconfig=kube-controller-manager.kubeconfig
  16. kubectl config use-context default --kubeconfig=kube-controller-manager.kubeconfig
  17. }

Results:

  1. kube-controller-manager.kubeconfig

The kube-scheduler Kubernetes Configuration File

Generate a kubeconfig file for the kube-scheduler service:

  1. {
  2. kubectl config set-cluster kubernetes-the-hard-way \
  3. --certificate-authority=ca.pem \
  4. --embed-certs=true \
  5. --server=https://127.0.0.1:6443 \
  6. --kubeconfig=kube-scheduler.kubeconfig
  7. kubectl config set-credentials system:kube-scheduler \
  8. --client-certificate=kube-scheduler.pem \
  9. --client-key=kube-scheduler-key.pem \
  10. --embed-certs=true \
  11. --kubeconfig=kube-scheduler.kubeconfig
  12. kubectl config set-context default \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=system:kube-scheduler \
  15. --kubeconfig=kube-scheduler.kubeconfig
  16. kubectl config use-context default --kubeconfig=kube-scheduler.kubeconfig
  17. }

Results:

  1. kube-scheduler.kubeconfig

The admin Kubernetes Configuration File

Generate a kubeconfig file for the admin user:

  1. {
  2. kubectl config set-cluster kubernetes-the-hard-way \
  3. --certificate-authority=ca.pem \
  4. --embed-certs=true \
  5. --server=https://127.0.0.1:6443 \
  6. --kubeconfig=admin.kubeconfig
  7. kubectl config set-credentials admin \
  8. --client-certificate=admin.pem \
  9. --client-key=admin-key.pem \
  10. --embed-certs=true \
  11. --kubeconfig=admin.kubeconfig
  12. kubectl config set-context default \
  13. --cluster=kubernetes-the-hard-way \
  14. --user=admin \
  15. --kubeconfig=admin.kubeconfig
  16. kubectl config use-context default --kubeconfig=admin.kubeconfig
  17. }

Results:

  1. admin.kubeconfig

Distribute the Kubernetes Configuration Files

Copy the appropriate kubelet and kube-proxy kubeconfig files to each worker instance:

  1. for instance in worker-0 worker-1 worker-2; do
  2. gcloud compute scp ${instance}.kubeconfig kube-proxy.kubeconfig ${instance}:~/
  3. done

Copy the appropriate kube-controller-manager and kube-scheduler kubeconfig files to each controller instance:

  1. for instance in controller-0 controller-1 controller-2; do
  2. gcloud compute scp admin.kubeconfig kube-controller-manager.kubeconfig kube-scheduler.kubeconfig ${instance}:~/
  3. done

Next: Generating the Data Encryption Config and Key