2017-08-29 00:19:25 +03:00
# Bootstrapping the etcd Cluster
Kubernetes components are stateless and store cluster state in [etcd ](https://github.com/coreos/etcd ). In this lab you will bootstrap a three node etcd cluster and configure it for high availability and secure remote access.
## Prerequisites
2019-02-17 12:53:14 +03:00
The commands in this chapter must be run on each controller instance: `controller-1` , `controller-2` , and `controller-3` . Login to each controller node:
2017-08-29 00:19:25 +03:00
```
2019-02-17 12:53:14 +03:00
$ ssh -i ~/.ssh/id_rsa-k8s 10.240.0.11
2017-08-29 00:19:25 +03:00
```
2018-05-12 19:54:18 +03:00
### Running commands in parallel with tmux
2019-02-16 12:35:21 +03:00
[tmux ](https://github.com/tmux/tmux/wiki ) can be used to run commands on multiple virtual machines 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.
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
Download the official etcd release binaries from the [coreos/etcd ](https://github.com/coreos/etcd ) GitHub project:
```
2019-02-16 12:35:21 +03:00
$ wget -q --show-progress --https-only --timestamping \
2018-09-30 22:35:05 +03:00
"https://github.com/coreos/etcd/releases/download/v3.3.9/etcd-v3.3.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:
```
2019-02-16 12:35:21 +03:00
$ {
2018-09-30 22:35:05 +03:00
tar -xvf etcd-v3.3.9-linux-amd64.tar.gz
sudo mv etcd-v3.3.9-linux-amd64/etcd* /usr/local/bin/
2018-05-12 19:54:18 +03:00
}
2017-08-29 00:19:25 +03:00
```
### Configure the etcd Server
```
2019-02-16 12:35:21 +03:00
$ {
2018-05-12 19:54:18 +03:00
sudo mkdir -p /etc/etcd /var/lib/etcd
sudo cp ca.pem kubernetes-key.pem kubernetes.pem /etc/etcd/
}
2017-08-29 00:19:25 +03:00
```
2019-02-17 12:53:14 +03:00
The virtual machine's IP address will be used to serve client requests and communicate with etcd cluster peers. Get IP address for the current virtual machine:
2017-08-29 00:19:25 +03:00
```
2019-02-16 12:35:21 +03:00
$ INTERNAL_IP=$(ip a s | grep 'inet 10' | awk '{ print $2 }' | awk -F"/" '{ print $1 }')
2017-08-29 00:19:25 +03:00
```
2019-02-17 12:53:14 +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 virtual machine:
2017-08-29 00:19:25 +03:00
```
2019-02-17 12:53:14 +03:00
$ ETCD_NAME=$(hostname -s)
2017-08-29 00:19:25 +03:00
```
Create the `etcd.service` systemd unit file:
```
2019-02-17 12:53:14 +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} \\
--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 \\
--peer-client-cert-auth \\
--client-cert-auth \\
--initial-advertise-peer-urls https://${INTERNAL_IP}:2380 \\
--listen-peer-urls https://${INTERNAL_IP}:2380 \\
2018-05-12 19:54:18 +03:00
--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 \\
2019-02-16 12:35:21 +03:00
--initial-cluster controller-1=https://10.240.0.11:2380,controller-2=https://10.240.0.12:2380,controller-3=https://10.240.0.13: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
```
2019-02-17 12:53:14 +03:00
$ {
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
```
2019-02-16 12:35:21 +03:00
> Remember to run the above commands on each controller node: `controller-1`, `controller-2`, and `controller-3`.
2017-08-29 00:19:25 +03:00
## Verification
List the etcd cluster members:
```
2019-02-17 12:53:14 +03:00
$ sudo ETCDCTL_API=3 etcdctl member list \
2018-05-12 19:54:18 +03:00
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/etcd/ca.pem \
--cert=/etc/etcd/kubernetes.pem \
--key=/etc/etcd/kubernetes-key.pem
2017-08-29 00:19:25 +03:00
```
> output
```
3a57933972cb5131, started, controller-2, https://10.240.0.12:2380, https://10.240.0.12:2379
2019-02-16 12:35:21 +03:00
f98dc20bce6225a0, started, controller-3, https://10.240.0.13:2380, https://10.240.0.13:2379
2017-08-29 00:19:25 +03:00
ffed16798470cab5, started, controller-1, https://10.240.0.11:2380, https://10.240.0.11:2379
```
Next: [Bootstrapping the Kubernetes Control Plane ](08-bootstrapping-kubernetes-controllers.md )