2017-08-29 00:19:25 +03:00
# Bootstrapping the Kubernetes Control Plane
2019-03-20 07:34:49 +03:00
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.
2017-08-29 00:19:25 +03:00
2024-03-18 08:16:56 +03:00
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.
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
## Prerequisites
2017-08-29 00:19:25 +03:00
2024-03-18 08:16:56 +03:00
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.
2018-05-12 19:54:18 +03:00
Adjust markdown formatting (#328)
* Adjust markdown formatting:
* Remove extra capitalization.
* Remove extra curly braces {} inside Bash code blocks.
* Use in-line code block `` for IP-addresses, file names and commands.
* Add a dot at the end of sentences.
* Use list formatting in `differences-to-original.md`. Also add escaping for angle brackets <>.
* No logic changes were made, only formatting improvements.
* 01-prerequisites.md: remove extra capitalization, remove extra space in "Virtual Box"
* 01-prerequisites.md: split text into different lines (before, it was rendered into one line)
* Remove extra capitalization, use inline code blocks, add a dot at the end of sentences.
* 02-compute-resources.md: add escaping for angle brackets <>.
* 03-client-tools.md: remove extra capitalization, use inline code blocks
* 04-certificate-authority.md: remove extra capitalization, use inline code blocks, remove extra curly braces {} inside Bash code blocks
* 04-certificate-authority.md: remove extra curly braces {} inside Bash code blocks
* Revert back: all "remove extra curly braces {} inside Bash code blocks"
As per @fireflycons https://github.com/mmumshad/kubernetes-the-hard-way/pull/328#issuecomment-1926329908 :
> They are there for a reason. If you paste a block of code within braces, then it is not executed immediately by the shell - you have to press ENTER. Quite often when making changes to this repo and I have multiple terminals open, it gives me a chance to check that I have pasted the block into the correct terminal before it executes in the wrong terminal and borks everything.
* Revert back: all "remove extra curly braces {} inside Bash code blocks"
* Revert back all "Remove extra capitalization", as per request @fireflycons
https://github.com/mmumshad/kubernetes-the-hard-way/pull/328#issuecomment-1944388993
2024-02-21 23:50:31 +03:00
You can perform this step with [tmux ](01-prerequisites.md#running-commands-in-parallel-with-tmux ).
2018-05-12 19:54:18 +03:00
2017-08-29 00:19:25 +03:00
## Provision the Kubernetes Control Plane
2024-03-18 08:16:56 +03:00
[//]: # (host:controlplane01-controlplane02)
2018-05-12 19:54:18 +03:00
2017-08-29 00:19:25 +03:00
### Download and Install the Kubernetes Controller Binaries
2023-11-23 22:52:14 +03:00
Download the latest official Kubernetes release binaries:
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
```bash
2023-11-23 22:52:14 +03:00
KUBE_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
2017-08-29 00:19:25 +03:00
wget -q --show-progress --https-only --timestamping \
2024-03-18 08:16:56 +03:00
"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"
2017-08-29 00:19:25 +03:00
```
2022-09-20 09:17:00 +03:00
Reference: https://kubernetes.io/releases/download/#binaries
2019-11-19 07:14:21 +03:00
2017-08-29 00:19:25 +03:00
Install the Kubernetes binaries:
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
{
chmod +x kube-apiserver kube-controller-manager kube-scheduler kubectl
sudo mv kube-apiserver kube-controller-manager kube-scheduler kubectl /usr/local/bin/
}
2017-08-29 00:19:25 +03:00
```
### Configure the Kubernetes API Server
2022-09-20 09:17:00 +03:00
Place the key pairs into the kubernetes data directory and secure
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
```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/*
2018-05-12 19:54:18 +03:00
}
2017-08-29 00:19:25 +03:00
```
2022-09-20 09:17:00 +03:00
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.< br >
Retrieve these internal IP addresses:
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
```bash
LOADBALANCER=$(dig +short loadbalancer)
2019-03-20 07:34:49 +03:00
```
2024-03-18 08:16:56 +03:00
IP addresses of the two controlplane nodes, where the etcd servers are.
2019-03-20 07:34:49 +03:00
2022-09-20 09:17:00 +03:00
```bash
2024-03-18 08:16:56 +03:00
CONTROL01=$(dig +short controlplane01)
CONTROL02=$(dig +short controlplane02)
2019-03-20 07:34:49 +03:00
```
2022-09-20 09:17:00 +03:00
CIDR ranges used *within* the cluster
```bash
POD_CIDR=10.244.0.0/16
SERVICE_CIDR=10.96.0.0/16
2017-08-29 00:19:25 +03:00
```
Create the `kube-apiserver.service` systemd unit file:
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat < < EOF | sudo tee / etc / systemd / system / kube-apiserver . service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes API Server
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-apiserver \\
2024-03-18 08:16:56 +03:00
--advertise-address=${PRIMARY_IP} \\
2017-08-29 00:19:25 +03:00
--allow-privileged=true \\
2020-10-03 07:41:21 +03:00
--apiserver-count=2 \\
2017-08-29 00:19:25 +03:00
--audit-log-maxage=30 \\
--audit-log-maxbackup=3 \\
--audit-log-maxsize=100 \\
--audit-log-path=/var/log/audit.log \\
--authorization-mode=Node,RBAC \\
--bind-address=0.0.0.0 \\
2022-09-20 09:17:00 +03:00
--client-ca-file=/var/lib/kubernetes/pki/ca.crt \\
2019-03-20 07:34:49 +03:00
--enable-admission-plugins=NodeRestriction,ServiceAccount \\
--enable-bootstrap-token-auth=true \\
2022-09-20 09:17:00 +03:00
--etcd-cafile=/var/lib/kubernetes/pki/ca.crt \\
--etcd-certfile=/var/lib/kubernetes/pki/etcd-server.crt \\
--etcd-keyfile=/var/lib/kubernetes/pki/etcd-server.key \\
2024-03-18 08:16:56 +03:00
--etcd-servers=https://${CONTROL01}:2379,https://${CONTROL02}:2379 \\
2017-08-29 00:19:25 +03:00
--event-ttl=1h \\
2019-03-20 07:34:49 +03:00
--encryption-provider-config=/var/lib/kubernetes/encryption-config.yaml \\
2022-09-20 09:17:00 +03:00
--kubelet-certificate-authority=/var/lib/kubernetes/pki/ca.crt \\
--kubelet-client-certificate=/var/lib/kubernetes/pki/apiserver-kubelet-client.crt \\
--kubelet-client-key=/var/lib/kubernetes/pki/apiserver-kubelet-client.key \\
2020-11-26 05:01:38 +03:00
--runtime-config=api/all=true \\
2022-09-20 09:17:00 +03:00
--service-account-key-file=/var/lib/kubernetes/pki/service-account.crt \\
--service-account-signing-key-file=/var/lib/kubernetes/pki/service-account.key \\
--service-account-issuer=https://${LOADBALANCER}:6443 \\
--service-cluster-ip-range=${SERVICE_CIDR} \\
2017-08-29 00:19:25 +03:00
--service-node-port-range=30000-32767 \\
2022-09-20 09:17:00 +03:00
--tls-cert-file=/var/lib/kubernetes/pki/kube-apiserver.crt \\
--tls-private-key-file=/var/lib/kubernetes/pki/kube-apiserver.key \\
2017-08-29 00:19:25 +03:00
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Configure the Kubernetes Controller Manager
2022-09-20 09:17:00 +03:00
Move the `kube-controller-manager` kubeconfig into place:
2018-05-12 19:54:18 +03:00
2022-09-20 09:17:00 +03:00
```bash
sudo mv kube-controller-manager.kubeconfig /var/lib/kubernetes/
2018-05-12 19:54:18 +03:00
```
2017-08-29 00:19:25 +03:00
Create the `kube-controller-manager.service` systemd unit file:
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat < < EOF | sudo tee / etc / systemd / system / kube-controller-manager . service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes Controller Manager
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-controller-manager \\
2022-09-20 09:17:00 +03:00
--allocate-node-cidrs=true \\
--authentication-kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
--authorization-kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
--bind-address=127.0.0.1 \\
--client-ca-file=/var/lib/kubernetes/pki/ca.crt \\
--cluster-cidr=${POD_CIDR} \\
2017-08-29 00:19:25 +03:00
--cluster-name=kubernetes \\
2022-09-20 09:17:00 +03:00
--cluster-signing-cert-file=/var/lib/kubernetes/pki/ca.crt \\
--cluster-signing-key-file=/var/lib/kubernetes/pki/ca.key \\
--controllers=*,bootstrapsigner,tokencleaner \\
2018-05-12 19:54:18 +03:00
--kubeconfig=/var/lib/kubernetes/kube-controller-manager.kubeconfig \\
2017-08-29 00:19:25 +03:00
--leader-elect=true \\
2022-09-20 09:17:00 +03:00
--node-cidr-mask-size=24 \\
--requestheader-client-ca-file=/var/lib/kubernetes/pki/ca.crt \\
--root-ca-file=/var/lib/kubernetes/pki/ca.crt \\
--service-account-private-key-file=/var/lib/kubernetes/pki/service-account.key \\
--service-cluster-ip-range=${SERVICE_CIDR} \\
2018-05-12 19:54:18 +03:00
--use-service-account-credentials=true \\
2017-08-29 00:19:25 +03:00
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Configure the Kubernetes Scheduler
2022-09-20 09:17:00 +03:00
Move the `kube-scheduler` kubeconfig into place:
2018-05-12 19:54:18 +03:00
2022-09-20 09:17:00 +03:00
```bash
sudo mv kube-scheduler.kubeconfig /var/lib/kubernetes/
2018-05-12 19:54:18 +03:00
```
2017-08-29 00:19:25 +03:00
Create the `kube-scheduler.service` systemd unit file:
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
cat < < EOF | sudo tee / etc / systemd / system / kube-scheduler . service
2017-08-29 00:19:25 +03:00
[Unit]
Description=Kubernetes Scheduler
2017-12-18 18:07:54 +03:00
Documentation=https://github.com/kubernetes/kubernetes
2017-08-29 00:19:25 +03:00
[Service]
ExecStart=/usr/local/bin/kube-scheduler \\
2019-03-20 07:34:49 +03:00
--kubeconfig=/var/lib/kubernetes/kube-scheduler.kubeconfig \\
--leader-elect=true \\
2017-08-29 00:19:25 +03:00
--v=2
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
2022-09-20 09:17:00 +03:00
## Secure kubeconfigs
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
```bash
sudo chmod 600 /var/lib/kubernetes/*.kubeconfig
2017-08-29 00:19:25 +03:00
```
2022-09-20 09:17:00 +03:00
## Optional - Check Certificates and kubeconfigs
2024-03-18 08:16:56 +03:00
At `controlplane01` and `controlplane02` nodes, run the following, selecting option 3
2022-09-20 09:17:00 +03:00
2023-11-23 22:52:14 +03:00
[//]: # (command:./cert_verify.sh 3)
```
2022-09-20 09:17:00 +03:00
./cert_verify.sh
```
### Start the Controller Services
```bash
2018-05-12 19:54:18 +03:00
{
sudo systemctl daemon-reload
sudo systemctl enable kube-apiserver kube-controller-manager kube-scheduler
sudo systemctl start kube-apiserver kube-controller-manager kube-scheduler
}
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
> Allow up to 10 seconds for the Kubernetes API Server to fully initialize.
2017-08-29 00:19:25 +03:00
### Verification
2022-09-20 09:17:00 +03:00
[//]: # (sleep:10)
2024-03-18 08:16:56 +03:00
After running the abovre commands on both controlplane nodes, run the following on `controlplane01`
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
kubectl get componentstatuses --kubeconfig admin.kubeconfig
2017-08-29 00:19:25 +03:00
```
2022-09-20 09:17:00 +03:00
It will give you a deprecation warning here, but that's ok.
> Output
2017-08-29 00:19:25 +03:00
```
2022-09-20 09:17:00 +03:00
Warning: v1 ComponentStatus is deprecated in v1.19+
2017-08-29 00:19:25 +03:00
NAME STATUS MESSAGE ERROR
2017-09-04 00:18:03 +03:00
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
2017-08-29 00:19:25 +03:00
etcd-1 Healthy {"health": "true"}
```
2024-03-18 08:16:56 +03:00
> Remember to run the above commands on each controller node: `controlplane01`, and `controlplane02`.
2018-05-12 19:54:18 +03:00
2019-03-20 07:34:49 +03:00
## The Kubernetes Frontend Load Balancer
2017-08-29 00:19:25 +03:00
2019-03-20 07:34:49 +03:00
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.
2017-10-02 06:37:09 +03:00
2019-03-20 07:34:49 +03:00
### Provision a Network Load Balancer
2017-10-02 06:37:09 +03:00
2022-09-20 09:17:00 +03:00
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.
2024-03-18 08:16:56 +03:00
Login to `loadbalancer` instance using `vagrant ssh` (or `multipass shell` on Apple Silicon).
2020-05-18 03:29:49 +03:00
2022-09-20 09:17:00 +03:00
[//]: # (host:loadbalancer)
```bash
2021-05-18 11:28:08 +03:00
sudo apt-get update & & sudo apt-get install -y haproxy
2017-10-02 06:37:09 +03:00
```
2024-03-18 08:16:56 +03:00
Read IP addresses of controlplane nodes and this host to shell variables
2022-09-20 09:17:00 +03:00
```bash
2024-03-18 08:16:56 +03:00
CONTROL01=$(dig +short controlplane01)
CONTROL02=$(dig +short controlplane02)
2022-09-20 09:17:00 +03:00
LOADBALANCER=$(dig +short loadbalancer)
2017-10-02 06:37:09 +03:00
```
2022-09-20 09:17:00 +03:00
2024-03-18 08:16:56 +03:00
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 ).
2022-09-20 09:17:00 +03:00
```bash
cat < < EOF | sudo tee / etc / haproxy / haproxy . cfg
2019-03-20 07:34:49 +03:00
frontend kubernetes
2022-09-20 09:17:00 +03:00
bind ${LOADBALANCER}:6443
2019-03-20 07:34:49 +03:00
option tcplog
mode tcp
2024-03-18 08:16:56 +03:00
default_backend kubernetes-controlplane-nodes
2019-03-20 07:34:49 +03:00
2024-03-18 08:16:56 +03:00
backend kubernetes-controlplane-nodes
2019-03-20 07:34:49 +03:00
mode tcp
balance roundrobin
option tcp-check
2024-03-18 08:16:56 +03:00
server controlplane01 ${CONTROL01}:6443 check fall 3 rise 2
server controlplane02 ${CONTROL02}:6443 check fall 3 rise 2
2017-10-02 06:37:09 +03:00
EOF
```
2022-09-20 09:17:00 +03:00
```bash
sudo systemctl restart haproxy
2017-08-29 00:19:25 +03:00
```
### Verification
2022-09-20 09:17:00 +03:00
[//]: # (sleep:2)
2017-08-29 00:19:25 +03:00
Make a HTTP request for the Kubernetes version info:
2022-09-20 09:17:00 +03:00
```bash
2024-03-18 08:16:56 +03:00
curl -k https://${LOADBALANCER}:6443/version
2017-08-29 00:19:25 +03:00
```
2024-03-18 08:16:56 +03:00
This should output some details about the version and build information of the API server.
2017-08-29 00:19:25 +03:00
2024-03-18 08:16:56 +03:00
Next: [Installing CRI on the Kubernetes Worker Nodes ](./09-install-cri-workers.md )< br >
Prev: [Bootstrapping the etcd Cluster ](./07-bootstrapping-etcd.md )