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
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
```
## 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:
```
wget -q --show-progress --https-only --timestamping \
2018-02-25 22:13:30 +03:00
"https://github.com/coreos/etcd/releases/download/v3.3.1/etcd-v3.3.1-linux-amd64.tar.gz"
2017-08-29 00:19:25 +03:00
```
Extract and install the `etcd` server and the `etcdctl` command line utility:
```
2018-02-25 22:13:30 +03:00
tar -xvf etcd-v3.3.1-linux-amd64.tar.gz
2017-08-29 00:19:25 +03:00
```
```
2018-02-25 22:13:30 +03:00
sudo mv etcd-v3.3.1-linux-amd64/etcd* /usr/local/bin/
2017-08-29 00:19:25 +03:00
```
### Configure the etcd Server
```
sudo mkdir -p /etc/etcd /var/lib/etcd
```
```
sudo cp ca.pem kubernetes-key.pem kubernetes.pem /etc/etcd/
```
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:
```
INTERNAL_IP=$(curl -s -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip)
```
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:
```
ETCD_NAME=$(hostname -s)
```
Create the `etcd.service` systemd unit file:
```
cat > etcd.service < < EOF
[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-03-26 00:16:04 +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 \\
--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 \\
--data-dir=/var/lib/etcd
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
```
### Start the etcd Server
```
sudo mv etcd.service /etc/systemd/system/
```
```
sudo systemctl daemon-reload
```
```
sudo systemctl enable etcd
```
```
sudo systemctl start etcd
```
> Remember to run the above commands on each controller node: `controller-0`, `controller-1`, and `controller-2`.
## Verification
2018-02-24 22:51:23 +03:00
[Source ](https://github.com/kelseyhightower/kubernetes-the-hard-way/issues/220 )
2018-02-24 22:52:02 +03:00
2018-02-24 22:45:07 +03:00
In order to list the members of the etcd cluster from any of the controller hosts, add the following to the logged in user's `.bashrc` file:
2017-08-29 00:19:25 +03:00
```
2018-02-24 22:45:07 +03:00
export ETCDCTL_API=3
export ETCDCTL_ENDPOINTS="https://127.0.0.1:2379"
export ETCDCTL_CACERT="/etc/etcd/ca.pem"
export ETCDCTL_CERT="/etc/etcd/kubernetes.pem"
export ETCDCTL_KEY="/etc/etcd/kubernetes-key.pem"
```
You might have to change file permissions (or ownership) of the PEM files to make sure that there are no permission issues while running the following command:
```
etcdctl member list
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
f98dc20bce6225a0, started, controller-0, https://10.240.0.10:2380, https://10.240.0.10:2379
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 )