kubernetes-the-hard-way/docs/07-bootstrapping-etcd.md

126 lines
4.0 KiB
Markdown
Raw Normal View History

2017-08-29 00:19:25 +03:00
# Bootstrapping the etcd Cluster
2019-09-14 21:41:56 +03:00
Kubernetes components are stateless and store cluster state in [etcd](https://github.com/etcd-io/etcd). In this lab you will bootstrap a three node etcd cluster and configure it for high availability and secure remote access.
2017-08-29 00:19:25 +03:00
## 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:
```
gcloud compute ssh controller-0
```
### Running commands in parallel with tmux
2023-08-01 16:48:20 +03:00
[tmux](https://tmux.github.io/) can be used to run commands on multiple compute instances at the same time. See the [Running commands in parallel with tmux](./01-prerequisites.md#running-commands-in-parallel-with-tmux) section in the Prerequisites lab.
2017-08-29 00:19:25 +03:00
## Bootstrapping an etcd Cluster Member
### Download and Install the etcd Binaries
2019-09-14 21:41:56 +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
```
2023-08-01 16:48:20 +03:00
curl --location --remote-name --time-cond etcd-v3.5.9-linux-amd64.tar.gz \
https://github.com/etcd-io/etcd/releases/download/v3.5.9/etcd-v3.5.9-linux-amd64.tar.gz
2017-08-29 00:19:25 +03:00
```
Extract and install the `etcd` server and the `etcdctl` command line utility:
```
2023-08-01 16:48:20 +03:00
tar --extract --file etcd-v3.5.9-linux-amd64.tar.gz --verbose
sudo cp etcd-v3.5.9-linux-amd64/etcd* /usr/local/bin/
2017-08-29 00:19:25 +03:00
```
### Configure the etcd Server
```
2023-08-01 16:48:20 +03:00
sudo mkdir --parents /etc/etcd /var/lib/etcd
sudo chmod 0700 /etc/etcd/ /var/lib/etcd/
sudo cp ca.pem kubernetes-key.pem kubernetes.pem /etc/etcd/
2017-08-29 00:19:25 +03:00
```
The instance internal IP address will be used to serve client requests and communicate with etcd cluster peers. Retrieve the internal IP address for the current compute instance:
```
2023-08-01 16:48:20 +03:00
INTERNAL_IP="$(curl --silent --header 'Metadata-Flavor: Google' \
http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip)"
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:
```
2023-08-01 16:48:20 +03:00
ETCD_NAME="$(hostname --short)"
2017-08-29 00:19:25 +03:00
```
Create the `etcd.service` systemd unit file:
```
2023-08-01 16:48:20 +03:00
sudo mkdir --parents /usr/local/lib/systemd/system
cat <<EOF | sudo tee /usr/local/lib/systemd/system/etcd.service
2017-08-29 00:19:25 +03:00
[Unit]
Description=etcd
2023-08-01 16:48:20 +03:00
Documentation=https://github.com/etcd-io/etcd
2017-08-29 00:19:25 +03:00
[Service]
2019-09-14 21:41:56 +03:00
Type=notify
2017-08-29 00:19:25 +03:00
ExecStart=/usr/local/bin/etcd \\
--name ${ETCD_NAME} \\
2023-08-01 16:48:20 +03:00
--cert-file /etc/etcd/kubernetes.pem \\
--key-file /etc/etcd/kubernetes-key.pem \\
--peer-cert-file /etc/etcd/kubernetes.pem \\
--peer-key-file /etc/etcd/kubernetes-key.pem \\
--trusted-ca-file /etc/etcd/ca.pem \\
--peer-trusted-ca-file /etc/etcd/ca.pem \\
2017-08-29 00:19:25 +03:00
--peer-client-cert-auth \\
--client-cert-auth \\
--initial-advertise-peer-urls https://${INTERNAL_IP}:2380 \\
--listen-peer-urls https://${INTERNAL_IP}:2380 \\
--listen-client-urls https://${INTERNAL_IP}:2379,https://127.0.0.1:2379 \\
2017-08-29 00:19:25 +03:00
--advertise-client-urls https://${INTERNAL_IP}:2379 \\
--initial-cluster-token etcd-cluster-0 \\
--initial-cluster controller-0=https://10.240.0.10:2380,controller-1=https://10.240.0.11:2380,controller-2=https://10.240.0.12:2380 \\
--initial-cluster-state new \\
2023-08-01 16:48:20 +03:00
--data-dir /var/lib/etcd
2017-08-29 00:19:25 +03:00
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Start the etcd Server
```
2023-08-01 16:48:20 +03:00
sudo systemctl enable --now etcd
2017-08-29 00:19:25 +03:00
```
> Remember to run the above commands on each controller node: `controller-0`, `controller-1`, and `controller-2`.
## Verification
List the etcd cluster members:
```
sudo ETCDCTL_API=3 etcdctl member list \
2023-08-01 16:48:20 +03:00
--cacert /etc/etcd/ca.pem \
--cert /etc/etcd/kubernetes.pem \
--endpoints https://127.0.0.1:2379 \
--key /etc/etcd/kubernetes-key.pem
2017-08-29 00:19:25 +03:00
```
> output
```
2020-07-18 10:24:55 +03:00
3a57933972cb5131, started, controller-2, https://10.240.0.12:2380, https://10.240.0.12:2379, false
f98dc20bce6225a0, started, controller-0, https://10.240.0.10:2380, https://10.240.0.10:2379, false
ffed16798470cab5, started, controller-1, https://10.240.0.11:2380, https://10.240.0.11:2379, false
2017-08-29 00:19:25 +03:00
```
2023-08-01 16:48:20 +03:00
Next: [Bootstrapping the Kubernetes Control Plane](./08-bootstrapping-kubernetes-controllers.md)