2017-08-29 00:19:25 +03:00
# Bootstrapping the etcd Cluster
2022-09-20 09:17:00 +03:00
Kubernetes components are stateless and store cluster state in [etcd ](https://etcd.io/ ). In this lab you will bootstrap a two node etcd cluster and configure it for high availability and secure remote access.
2017-08-29 00:19:25 +03:00
2024-03-18 08:16:56 +03:00
If you examine the command line arguments passed to etcd in its unit file, you should recognise some of the certificates and keys created in earlier sections of this course.
2017-08-29 00:19:25 +03:00
## Prerequisites
2024-03-18 08:16:56 +03:00
The commands in this lab must be run on each controller instance: `controlplane01` , and `controlplane02` . Login to each of these using an SSH terminal.
2017-08-29 00:19:25 +03:00
2018-05-12 19:54:18 +03:00
### Running commands in parallel with tmux
2019-11-08 09:53:15 +03:00
[tmux ](https://github.com/tmux/tmux/wiki ) can be used to run commands on multiple compute instances at the same time.
2018-05-12 19:54:18 +03:00
2017-08-29 00:19:25 +03:00
## Bootstrapping an etcd Cluster Member
### Download and Install the etcd Binaries
2022-09-20 09:17:00 +03:00
Download the official etcd release binaries from the [etcd ](https://github.com/etcd-io/etcd ) GitHub project:
2017-08-29 00:19:25 +03:00
2024-03-18 08:16:56 +03:00
[//]: # (host:controlplane01-controlplane02)
2022-09-20 09:17:00 +03:00
```bash
2023-11-23 22:52:14 +03:00
ETCD_VERSION="v3.5.9"
2017-08-29 00:19:25 +03:00
wget -q --show-progress --https-only --timestamping \
2024-03-18 08:16:56 +03:00
"https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-${ARCH}.tar.gz"
2017-08-29 00:19:25 +03:00
```
Extract and install the `etcd` server and the `etcdctl` command line utility:
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
{
2024-03-18 08:16:56 +03:00
tar -xvf etcd-${ETCD_VERSION}-linux-${ARCH}.tar.gz
sudo mv etcd-${ETCD_VERSION}-linux-${ARCH}/etcd* /usr/local/bin/
2018-05-12 19:54:18 +03:00
}
2017-08-29 00:19:25 +03:00
```
### Configure the etcd Server
2022-09-20 09:17:00 +03:00
Copy and secure certificates. Note that we place `ca.crt` in our main PKI directory and link it from etcd to not have multiple copies of the cert lying around.
```bash
2018-05-12 19:54:18 +03:00
{
2022-09-20 09:17:00 +03:00
sudo mkdir -p /etc/etcd /var/lib/etcd /var/lib/kubernetes/pki
sudo cp etcd-server.key etcd-server.crt /etc/etcd/
sudo cp ca.crt /var/lib/kubernetes/pki/
sudo chown root:root /etc/etcd/*
sudo chmod 600 /etc/etcd/*
sudo chown root:root /var/lib/kubernetes/pki/*
sudo chmod 600 /var/lib/kubernetes/pki/*
sudo ln -s /var/lib/kubernetes/pki/ca.crt /etc/etcd/ca.crt
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 serve client requests and communicate with etcd cluster peers.< br >
2024-03-18 08:16:56 +03:00
Retrieve the internal IP address of the controlplane(etcd) nodes, and also that of controlplane01 and controlplane02 for the etcd cluster member list
2017-08-29 00:19:25 +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)
2017-08-29 00:19:25 +03:00
```
Each etcd member must have a unique name within an etcd cluster. Set the etcd name to match the hostname of the current compute instance:
2022-09-20 09:17:00 +03:00
```bash
2017-08-29 00:19:25 +03:00
ETCD_NAME=$(hostname -s)
```
Create the `etcd.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 / etcd . service
2017-08-29 00:19:25 +03:00
[Unit]
Description=etcd
Documentation=https://github.com/coreos
[Service]
ExecStart=/usr/local/bin/etcd \\
--name ${ETCD_NAME} \\
2019-03-20 07:34:49 +03:00
--cert-file=/etc/etcd/etcd-server.crt \\
--key-file=/etc/etcd/etcd-server.key \\
--peer-cert-file=/etc/etcd/etcd-server.crt \\
--peer-key-file=/etc/etcd/etcd-server.key \\
--trusted-ca-file=/etc/etcd/ca.crt \\
--peer-trusted-ca-file=/etc/etcd/ca.crt \\
2017-08-29 00:19:25 +03:00
--peer-client-cert-auth \\
--client-cert-auth \\
2024-03-18 08:16:56 +03:00
--initial-advertise-peer-urls https://${PRIMARY_IP}:2380 \\
--listen-peer-urls https://${PRIMARY_IP}:2380 \\
--listen-client-urls https://${PRIMARY_IP}:2379,https://127.0.0.1:2379 \\
--advertise-client-urls https://${PRIMARY_IP}:2379 \\
2017-08-29 00:19:25 +03:00
--initial-cluster-token etcd-cluster-0 \\
2024-03-18 08:16:56 +03:00
--initial-cluster controlplane01=https://${CONTROL01}:2380,controlplane02=https://${CONTROL02}:2380 \\
2017-08-29 00:19:25 +03:00
--initial-cluster-state new \\
--data-dir=/var/lib/etcd
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Start the etcd Server
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
{
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
}
2017-08-29 00:19:25 +03:00
```
2024-03-18 08:16:56 +03:00
> Remember to run the above commands on each controller node: `controlplane01`, and `controlplane02`.
2017-08-29 00:19:25 +03:00
## Verification
2022-09-20 09:17:00 +03:00
[//]: # (sleep:5)
2024-03-18 08:16:56 +03:00
List the etcd cluster members.
After running the abovre commands on both controlplane nodes, run the following on either or both of `controlplane01` and `controlplane02`
2017-08-29 00:19:25 +03:00
2022-09-20 09:17:00 +03:00
```bash
2018-05-12 19:54:18 +03:00
sudo ETCDCTL_API=3 etcdctl member list \
--endpoints=https://127.0.0.1:2379 \
2019-03-20 07:34:49 +03:00
--cacert=/etc/etcd/ca.crt \
--cert=/etc/etcd/etcd-server.crt \
--key=/etc/etcd/etcd-server.key
2017-08-29 00:19:25 +03:00
```
2024-03-18 08:16:56 +03:00
Output will be similar to this
2017-08-29 00:19:25 +03:00
```
2024-03-18 08:16:56 +03:00
45bf9ccad8d8900a, started, controlplane02, https://192.168.56.12:2380, https://192.168.56.12:2379
54a5796a6803f252, started, controlplane01, https://192.168.56.11:2380, https://192.168.56.11:2379
2017-08-29 00:19:25 +03:00
```
2019-11-19 06:57:45 +03:00
Reference: https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#starting-etcd-clusters
2024-03-18 08:16:56 +03:00
Next: [Bootstrapping the Kubernetes Control Plane ](./08-bootstrapping-kubernetes-controllers.md )< br >
Prev: [Generating the Data Encryption Config and Key ](./06-data-encryption-keys.md )