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

156 lines
4.9 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 master instance: `master-0`, `master-1`, and `master-2`. Login to each master instance using ssh command. Example:
2017-08-29 00:19:25 +03:00
```
$ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-xxxxxxxxxxxxxxxxx \
--query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0],InstanceId,Placement.AvailabilityZone,PrivateIpAddress,PublicIpAddress,State.Name]' \
--output text | sort | grep master
master-0 i-xxxxxxxxxxxxxxxxx ap-northeast-1c 10.240.0.10 xx.xxx.xxx.xxx running
...
$ ssh -i ~/.ssh/your_ssh_key ubuntu@xx.xxx.xxx.xxx
2017-08-29 00:19:25 +03:00
```
### Running commands in parallel with tmux
[tmux](https://github.com/tmux/tmux/wiki) can be used to run commands on multiple EC2 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
```
master-x $ wget -q --show-progress --https-only --timestamping \
2019-09-14 21:41:56 +03:00
"https://github.com/etcd-io/etcd/releases/download/v3.4.0/etcd-v3.4.0-linux-amd64.tar.gz"
2017-08-29 00:19:25 +03:00
```
Extract and install the `etcd` server and the `etcdctl` command line utility:
```
master-x $ tar -xvf etcd-v3.4.0-linux-amd64.tar.gz
master-x $ sudo mv etcd-v3.4.0-linux-amd64/etcd* /usr/local/bin/
```
Results:
```
master-x $ ls /usr/local/bin/
etcd etcdctl
2017-08-29 00:19:25 +03:00
```
### Configure the etcd Server
```
master-x $ sudo mkdir -p /etc/etcd /var/lib/etcd
master-x $ 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 EC2 instance via [instance metadata on EC2 instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html):
2017-08-29 00:19:25 +03:00
Example:
2017-08-29 00:19:25 +03:00
```
master-0 $ curl 169.254.169.254/latest/meta-data/local-ipv4
10.240.0.10
2017-08-29 00:19:25 +03:00
```
Set `INTERNAL_IP` environemnt variable:
2017-08-29 00:19:25 +03:00
```
master-x $ INTERNAL_IP=$(curl 169.254.169.254/latest/meta-data/local-ipv4)
```
Each etcd member must have a unique name within an etcd cluster. Set the etcd name to match the hostname of the current EC2 instance:
Example:
```
master-0 $ hostname -s
master-0
```
Set `ETCD_NAME` environemnt variable:
```
master-x $ ETCD_NAME=$(hostname -s)
2017-08-29 00:19:25 +03:00
```
Create the `etcd.service` systemd unit file:
```
master-x $ 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]
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} \\
--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 \\
--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 master-0=https://10.240.0.10:2380,master-1=https://10.240.0.11:2380,master-2=https://10.240.0.12: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
```
master-x $ sudo systemctl daemon-reload
master-x $ sudo systemctl enable etcd
master-x $ sudo systemctl start etcd
2017-08-29 00:19:25 +03:00
```
> Remember to run the above commands on each master node: `master-0`, `master-1`, and `master-2`.
Verify etcd servers are running as systemd services.
```
master-x $ systemctl status etcd.service
● etcd.service - etcd
Loaded: loaded (/etc/systemd/system/etcd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2020-01-20 18:01:29 UTC; 21s ago
...
```
2017-08-29 00:19:25 +03:00
## Verification
List the etcd cluster members:
```
master-0 $ sudo ETCDCTL_API=3 etcdctl member list \
--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
3a57933972cb5131, started, master-2, https://10.240.0.12:2380, https://10.240.0.12:2379, false
f98dc20bce6225a0, started, master-0, https://10.240.0.10:2380, https://10.240.0.10:2379, false
ffed16798470cab5, started, master-1, https://10.240.0.11:2380, https://10.240.0.11:2379, false
2017-08-29 00:19:25 +03:00
```
Next: [Bootstrapping the Kubernetes Control Plane](08-bootstrapping-kubernetes-controllers.md)