# Bootstrapping the Kubernetes Control Plane In this lab you will bootstrap the Kubernetes control plane across 2 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. Note that in a production-ready cluster it is recommended to have an odd number of controlplane nodes as for multi-node services like etcd, leader election and quorum work better. See lecture on this ([KodeKloud](https://kodekloud.com/topic/etcd-in-ha/), [Udemy](https://www.udemy.com/course/certified-kubernetes-administrator-with-practice-tests/learn/lecture/14296192#overview)). We're only using two here to save on RAM on your workstation. If you examine the command line arguments passed to the various control plane components, you should recognise many of the files that were created in earlier sections of this course, such as certificates, keys, kubeconfigs, the encryption configuration etc. ## Prerequisites The commands in this lab up as far as the load balancer configuration must be run on each controller instance: `controlplane01`, and `controlplane02`. Login to each controller instance using SSH Terminal. You can perform this step with [tmux](01-prerequisites.md#running-commands-in-parallel-with-tmux). ## Provision the Kubernetes Control Plane [//]: # (host:controlplane01-controlplane02) ### Download and Install the Kubernetes Controller Binaries Download the latest official Kubernetes release binaries: ```bash KUBE_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt) wget -q --show-progress --https-only --timestamping \ "https://dl.k8s.io/release/${KUBE_VERSION}/bin/linux/${ARCH}/kube-apiserver" \ "https://dl.k8s.io/release/${KUBE_VERSION}/bin/linux/${ARCH}/kube-controller-manager" \ "https://dl.k8s.io/release/${KUBE_VERSION}/bin/linux/${ARCH}/kube-scheduler" \ "https://dl.k8s.io/release/${KUBE_VERSION}/bin/linux/${ARCH}/kubectl" ``` Reference: https://kubernetes.io/releases/download/#binaries Install the Kubernetes binaries: ```bash { chmod +x kube-apiserver kube-controller-manager kube-scheduler kubectl sudo mv kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/ } ``` ### Configure the Kubernetes API Server Place the key pairs into the kubernetes data directory and secure ```bash { sudo mkdir -p /var/lib/kubernetes/pki # Only copy CA keys as we'll need them again for workers. sudo cp ca.crt ca.key /var/lib/kubernetes/pki for c in kube-apiserver service-account apiserver-kubelet-client etcd-server kube-scheduler kube-controller-manager do sudo mv "$c.crt" "$c.key" /var/lib/kubernetes/pki/ done sudo chown root:root /var/lib/kubernetes/pki/* sudo chmod 600 /var/lib/kubernetes/pki/* } ``` The instance internal IP address will be used to advertise the API Server to members of the cluster. The load balancer IP address will be used as the external endpoint to the API servers.
Retrieve these internal IP addresses: ```bash LOADBALANCER=$(dig +short loadbalancer) ``` IP addresses of the two controlplane nodes, where the etcd servers are. ```bash CONTROL01=$(dig +short controlplane01) CONTROL02=$(dig +short controlplane02) ``` CIDR ranges used *within* the cluster ```bash POD_CIDR=10.244.0.0/16 SERVICE_CIDR=10.96.0.0/16 ``` Create the `kube-apiserver.service` systemd unit file: ```bash cat < Allow up to 10 seconds for the Kubernetes API Server to fully initialize. ### Verification [//]: # (sleep:10) After running the above commands on both controlplane nodes, run the following on `controlplane01` ```bash kubectl get componentstatuses --kubeconfig admin.kubeconfig ``` It will give you a deprecation warning here, but that's ok. > Output ``` Warning: v1 ComponentStatus is deprecated in v1.19+ NAME STATUS MESSAGE ERROR controller-manager Healthy ok scheduler Healthy ok etcd-0 Healthy {"health": "true"} etcd-1 Healthy {"health": "true"} ``` > Remember to run the above commands on each controller node: `controlplane01`, and `controlplane02`. ## 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. ### Provision a Network Load Balancer A NLB operates at [layer 4](https://en.wikipedia.org/wiki/OSI_model#Layer_4:_Transport_layer) (TCP) meaning it passes the traffic straight through to the back end servers unfettered and does not interfere with the TLS process, leaving this to the Kube API servers. Login to `loadbalancer` instance using `vagrant ssh` (or `multipass shell` on Apple Silicon). [//]: # (host:loadbalancer) ```bash sudo apt-get update && sudo apt-get install -y haproxy ``` Read IP addresses of controlplane nodes and this host to shell variables ```bash CONTROL01=$(dig +short controlplane01) CONTROL02=$(dig +short controlplane02) LOADBALANCER=$(dig +short loadbalancer) ``` Create HAProxy configuration to listen on API server port on this host and distribute requests evently to the two controlplane nodes. We configure it to operate as a [layer 4](https://en.wikipedia.org/wiki/Transport_layer) loadbalancer (using `mode tcp`), which means it forwards any traffic directly to the backends without doing anything like [SSL offloading](https://ssl2buy.com/wiki/ssl-offloading). ```bash cat < Prev: [Bootstrapping the etcd Cluster](./07-bootstrapping-etcd.md)