Bootstrapping the Kubernetes Control Plane

In this lab you will bootstrap the Kubernetes control plane across three compute instances and configure it for high availability. You will also create an external load balancer that exposes the Kubernetes API Servers to remote clients. The following components will be installed on each node: Kubernetes API Server, Scheduler, and Controller Manager.

Prerequisites

The commands in this lab must be run on each controller instance: controller-0, controller-1, and controller-2. Login to each controller instance using the gcloud command. Example:

  1. gcloud compute ssh controller-0

Running commands in parallel with tmux

tmux can be used to run commands on multiple compute instances at the same time. See the Running commands in parallel with tmux section in the Prerequisites lab.

Provision the Kubernetes Control Plane

Create the Kubernetes configuration directory:

  1. sudo mkdir -p /etc/kubernetes/config

Download and Install the Kubernetes Controller Binaries

Download the official Kubernetes release binaries:

  1. wget -q --show-progress --https-only --timestamping \
  2. "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-apiserver" \
  3. "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-controller-manager" \
  4. "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kube-scheduler" \
  5. "https://storage.googleapis.com/kubernetes-release/release/v1.15.3/bin/linux/amd64/kubectl"

Install the Kubernetes binaries:

  1. {
  2. chmod +x kube-apiserver kube-controller-manager kube-scheduler kubectl
  3. sudo mv kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/
  4. }

Configure the Kubernetes API Server

  1. {
  2. sudo mkdir -p /var/lib/kubernetes/
  3. sudo mv ca.pem ca-key.pem kubernetes-key.pem kubernetes.pem \
  4. service-account-key.pem service-account.pem \
  5. encryption-config.yaml /var/lib/kubernetes/
  6. }

The instance internal IP address will be used to advertise the API Server to members of the cluster. Retrieve the internal IP address for the current compute instance:

  1. INTERNAL_IP=$(curl -s -H "Metadata-Flavor: Google" \
  2. http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip)

Create the kube-apiserver.service systemd unit file:

  1. cat <<EOF | sudo tee /etc/systemd/system/kube-apiserver.service
  2. [Unit]
  3. Description=Kubernetes API Server
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. ExecStart=/usr/local/bin/kube-apiserver \\
  7. --advertise-address=${INTERNAL_IP} \\
  8. --allow-privileged=true \\
  9. --apiserver-count=3 \\
  10. --audit-log-maxage=30 \\
  11. --audit-log-maxbackup=3 \\
  12. --audit-log-maxsize=100 \\
  13. --audit-log-path=/var/log/audit.log \\
  14. --authorization-mode=Node,RBAC \\
  15. --bind-address=0.0.0.0 \\
  16. --client-ca-file=/var/lib/kubernetes/ca.pem \\
  17. --enable-admission-plugins=NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota \\
  18. --etcd-cafile=/var/lib/kubernetes/ca.pem \\
  19. --etcd-certfile=/var/lib/kubernetes/kubernetes.pem \\
  20. --etcd-keyfile=/var/lib/kubernetes/kubernetes-key.pem \\
  21. --etcd-servers=https://10.240.0.10:2379,https://10.240.0.11:2379,https://10.240.0.12:2379 \\
  22. --event-ttl=1h \\
  23. --encryption-provider-config=/var/lib/kubernetes/encryption-config.yaml \\
  24. --kubelet-certificate-authority=/var/lib/kubernetes/ca.pem \\
  25. --kubelet-client-certificate=/var/lib/kubernetes/kubernetes.pem \\
  26. --kubelet-client-key=/var/lib/kubernetes/kubernetes-key.pem \\
  27. --kubelet-https=true \\
  28. --runtime-config=api/all \\
  29. --service-account-key-file=/var/lib/kubernetes/service-account.pem \\
  30. --service-cluster-ip-range=10.32.0.0/24 \\
  31. --service-node-port-range=30000-32767 \\
  32. --tls-cert-file=/var/lib/kubernetes/kubernetes.pem \\
  33. --tls-private-key-file=/var/lib/kubernetes/kubernetes-key.pem \\
  34. --v=2
  35. Restart=on-failure
  36. RestartSec=5
  37. [Install]
  38. WantedBy=multi-user.target
  39. EOF

Configure the Kubernetes Controller Manager

Move the kube-controller-manager kubeconfig into place:

  1. sudo mv kube-controller-manager.kubeconfig /var/lib/kubernetes/

Create the kube-controller-manager.service systemd unit file:

  1. cat <<EOF | sudo tee /etc/systemd/system/kube-controller-manager.service
  2. [Unit]
  3. Description=Kubernetes Controller Manager
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. ExecStart=/usr/local/bin/kube-controller-manager \\
  7. --address=0.0.0.0 \\
  8. --cluster-cidr=10.200.0.0/16 \\
  9. --cluster-name=kubernetes \\
  10. --cluster-signing-cert-file=/var/lib/kubernetes/ca.pem \\
  11. --cluster-signing-key-file=/var/lib/kubernetes/ca-key.pem \\
  12. --kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
  13. --leader-elect=true \\
  14. --root-ca-file=/var/lib/kubernetes/ca.pem \\
  15. --service-account-private-key-file=/var/lib/kubernetes/service-account-key.pem \\
  16. --service-cluster-ip-range=10.32.0.0/24 \\
  17. --use-service-account-credentials=true \\
  18. --v=2
  19. Restart=on-failure
  20. RestartSec=5
  21. [Install]
  22. WantedBy=multi-user.target
  23. EOF

Configure the Kubernetes Scheduler

Move the kube-scheduler kubeconfig into place:

  1. sudo mv kube-scheduler.kubeconfig /var/lib/kubernetes/

Create the kube-scheduler.yaml configuration file:

  1. cat <<EOF | sudo tee /etc/kubernetes/config/kube-scheduler.yaml
  2. apiVersion: kubescheduler.config.k8s.io/v1alpha1
  3. kind: KubeSchedulerConfiguration
  4. clientConnection:
  5. kubeconfig: "/var/lib/kubernetes/kube-scheduler.kubeconfig"
  6. leaderElection:
  7. leaderElect: true
  8. EOF

Create the kube-scheduler.service systemd unit file:

  1. cat <<EOF | sudo tee /etc/systemd/system/kube-scheduler.service
  2. [Unit]
  3. Description=Kubernetes Scheduler
  4. Documentation=https://github.com/kubernetes/kubernetes
  5. [Service]
  6. ExecStart=/usr/local/bin/kube-scheduler \\
  7. --config=/etc/kubernetes/config/kube-scheduler.yaml \\
  8. --v=2
  9. Restart=on-failure
  10. RestartSec=5
  11. [Install]
  12. WantedBy=multi-user.target
  13. EOF

Start the Controller Services

  1. {
  2. sudo systemctl daemon-reload
  3. sudo systemctl enable kube-apiserver kube-controller-manager kube-scheduler
  4. sudo systemctl start kube-apiserver kube-controller-manager kube-scheduler
  5. }

Allow up to 10 seconds for the Kubernetes API Server to fully initialize.

Enable HTTP Health Checks

A Google Network Load Balancer will be used to distribute traffic across the three API servers and allow each API server to terminate TLS connections and validate client certificates. The network load balancer only supports HTTP health checks which means the HTTPS endpoint exposed by the API server cannot be used. As a workaround the nginx webserver can be used to proxy HTTP health checks. In this section nginx will be installed and configured to accept HTTP health checks on port 80 and proxy the connections to the API server on https://127.0.0.1:6443/healthz.

The /healthz API server endpoint does not require authentication by default.

Install a basic web server to handle HTTP health checks:

  1. sudo apt-get update
  2. sudo apt-get install -y nginx
  1. cat > kubernetes.default.svc.cluster.local <<EOF
  2. server {
  3. listen 80;
  4. server_name kubernetes.default.svc.cluster.local;
  5. location /healthz {
  6. proxy_pass https://127.0.0.1:6443/healthz;
  7. proxy_ssl_trusted_certificate /var/lib/kubernetes/ca.pem;
  8. }
  9. }
  10. EOF
  1. {
  2. sudo mv kubernetes.default.svc.cluster.local \
  3. /etc/nginx/sites-available/kubernetes.default.svc.cluster.local
  4. sudo ln -s /etc/nginx/sites-available/kubernetes.default.svc.cluster.local /etc/nginx/sites-enabled/
  5. }
  1. sudo systemctl restart nginx
  1. sudo systemctl enable nginx

Verification

  1. kubectl get componentstatuses --kubeconfig admin.kubeconfig
  1. NAME STATUS MESSAGE ERROR
  2. controller-manager Healthy ok
  3. scheduler Healthy ok
  4. etcd-2 Healthy {"health": "true"}
  5. etcd-0 Healthy {"health": "true"}
  6. etcd-1 Healthy {"health": "true"}

Test the nginx HTTP health check proxy:

  1. curl -H "Host: kubernetes.default.svc.cluster.local" -i http://127.0.0.1/healthz
  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.0 (Ubuntu)
  3. Date: Sat, 14 Sep 2019 18:34:11 GMT
  4. Content-Type: text/plain; charset=utf-8
  5. Content-Length: 2
  6. Connection: keep-alive
  7. X-Content-Type-Options: nosniff
  8. ok

Remember to run the above commands on each controller node: controller-0, controller-1, and controller-2.

RBAC for Kubelet Authorization

In this section you will configure RBAC permissions to allow the Kubernetes API Server to access the Kubelet API on each worker node. Access to the Kubelet API is required for retrieving metrics, logs, and executing commands in pods.

This tutorial sets the Kubelet --authorization-mode flag to Webhook. Webhook mode uses the SubjectAccessReview API to determine authorization.

The commands in this section will effect the entire cluster and only need to be run once from one of the controller nodes.

  1. gcloud compute ssh controller-0

Create the system:kube-apiserver-to-kubelet ClusterRole with permissions to access the Kubelet API and perform most common tasks associated with managing pods:

  1. cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
  2. apiVersion: rbac.authorization.k8s.io/v1beta1
  3. kind: ClusterRole
  4. metadata:
  5. annotations:
  6. rbac.authorization.kubernetes.io/autoupdate: "true"
  7. labels:
  8. kubernetes.io/bootstrapping: rbac-defaults
  9. name: system:kube-apiserver-to-kubelet
  10. rules:
  11. - apiGroups:
  12. - ""
  13. resources:
  14. - nodes/proxy
  15. - nodes/stats
  16. - nodes/log
  17. - nodes/spec
  18. - nodes/metrics
  19. verbs:
  20. - "*"
  21. EOF

The Kubernetes API Server authenticates to the Kubelet as the kubernetes user using the client certificate as defined by the --kubelet-client-certificate flag.

Bind the system:kube-apiserver-to-kubelet ClusterRole to the kubernetes user:

  1. cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
  2. apiVersion: rbac.authorization.k8s.io/v1beta1
  3. kind: ClusterRoleBinding
  4. metadata:
  5. name: system:kube-apiserver
  6. namespace: ""
  7. roleRef:
  8. apiGroup: rbac.authorization.k8s.io
  9. kind: ClusterRole
  10. name: system:kube-apiserver-to-kubelet
  11. subjects:
  12. - apiGroup: rbac.authorization.k8s.io
  13. kind: User
  14. name: kubernetes
  15. EOF

The Kubernetes Frontend Load Balancer

In this section you will provision an external load balancer to front the Kubernetes API Servers. The kubernetes-the-hard-way static IP address will be attached to the resulting load balancer.

The compute instances created in this tutorial will not have permission to complete this section. Run the following commands from the same machine used to create the compute instances.

Provision a Network Load Balancer

Create the external load balancer network resources:

  1. {
  2. KUBERNETES_PUBLIC_ADDRESS=$(gcloud compute addresses describe kubernetes-the-hard-way \
  3. --region $(gcloud config get-value compute/region) \
  4. --format 'value(address)')
  5. gcloud compute http-health-checks create kubernetes \
  6. --description "Kubernetes Health Check" \
  7. --host "kubernetes.default.svc.cluster.local" \
  8. --request-path "/healthz"
  9. gcloud compute firewall-rules create kubernetes-the-hard-way-allow-health-check \
  10. --network kubernetes-the-hard-way \
  11. --source-ranges 209.85.152.0/22,209.85.204.0/22,35.191.0.0/16 \
  12. --allow tcp
  13. gcloud compute target-pools create kubernetes-target-pool \
  14. --http-health-check kubernetes
  15. gcloud compute target-pools add-instances kubernetes-target-pool \
  16. --instances controller-0,controller-1,controller-2
  17. gcloud compute forwarding-rules create kubernetes-forwarding-rule \
  18. --address ${KUBERNETES_PUBLIC_ADDRESS} \
  19. --ports 6443 \
  20. --region $(gcloud config get-value compute/region) \
  21. --target-pool kubernetes-target-pool
  22. }

Verification

The compute instances created in this tutorial will not have permission to complete this section. Run the following commands from the same machine used to create the compute instances.

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)')

Make a HTTP request for the Kubernetes version info:

  1. curl --cacert ca.pem https://${KUBERNETES_PUBLIC_ADDRESS}:6443/version

output

  1. {
  2. "major": "1",
  3. "minor": "15",
  4. "gitVersion": "v1.15.3",
  5. "gitCommit": "2d3c76f9091b6bec110a5e63777c332469e0cba2",
  6. "gitTreeState": "clean",
  7. "buildDate": "2019-08-19T11:05:50Z",
  8. "goVersion": "go1.12.9",
  9. "compiler": "gc",
  10. "platform": "linux/amd64"
  11. }

Next: Bootstrapping the Kubernetes Worker Nodes