部署etcd集群
etcd是基于Raft的分布式key-value存储系统,由CoreOS开发,常用于服务发现、共享配置以及并发控制(如 leader 选举、分布式锁等,kubernetes 使用 etcd 存储所有运行数据。
本文档介绍部署一个三节点高可用 etcd 集群的步骤:
-下载和分发 etcd 二进制文件;
-创建 etcd 集群各节点的 x509 证书,用于加密客户端(如 etcdctl) 与 etcd 集群、etcd 集群之间的数据流;
-创建 etcd 的 systemd unit 文件,配置服务参数;
-检查集群工作状态;
下载和分发 etcd 二进制文件
$cd /root/work
$wget https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
$tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
#分发二进制文件到集群所有节点:
scp etcd-v3.3.10-linux-amd64/etcd* root@10.0.0.10:/usr/bin
scp etcd-v3.3.10-linux-amd64/etcd* root@10.0.0.11:/usr/bin
scp etcd-v3.3.10-linux-amd64/etcd* root@10.0.0.12:/usr/bin
创建 etcd 证书和私钥
创建证书签名请求:
#下面写入自己etcd集群的ip地址。
cat > etcd-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"127.0.0.1",
"10.0.0.10",
"10.0.0.11",
"10.0.0.12"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "k8s",
"OU": "4Paradigm"
}
]
}
EOF
hosts 字段指定授权使用该证书的 etcd 节点 IP 或域名列表,这里将 etcd 集群的三个节点 IP 都列在其中;
生成证书和私钥:
$cfssl gencert -ca=/root/work/ca.pem
-ca-key=/root/work/ca-key.pem
-config=/root/work/ca-config.json
-profile=kubernetes etcd-csr.json | cfssljson -bare etcd
$ls etcd*pem
etcd-key.pem etcd.pem
创建证书目录和工作目录
#所有etcd节点分别执行
mkdir -p /etc/etcd/cert
mkdir /var/lib/etcd
分发生成的证书和私钥到各 etcd 节点:
scp etcd*.pem root@10.0.0.10:/etc/etcd/cert/
scp etcd*.pem root@10.0.0.11:/etc/etcd/cert/
scp etcd*.pem root@10.0.0.12:/etc/etcd/cert/
scp /root/work/ca.pem root@10.0.0.10:/etc/etcd/cert/
scp /root/work/ca.pem root@10.0.0.11:/etc/etcd/cert/
scp /root/work/ca.pem root@10.0.0.12:/etc/etcd/cert/
分别为各节点创建etcd配置文件
注:
1、IP地址替换为自己环境节点的IP即可。
2、三个配置文件中ETCD_INITIAL_CLUSTER集群地址都是一样的。
3、注意ETCD_NAME每个节点名称是不一样的,且要与ETCD_INITIAL_CLUSTER里面的主机名一一对应。
10.0.0.10:
cat > /etc/etcd/etcd.conf <<eof
#[member]
ETCD_NAME="etcd-node1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.0.0.10:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.0.0.10:2379,https://127.0.0.1:2379"
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.0.0.10:2380"
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.0.0.10:2380,etcd-node2=https://10.0.0.11:2380,etcd-node3=https://10.0.0.12:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.0.0.10:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_PEER_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_PEER_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
eof
10.0.0.11:
cat > /etc/etcd/etcd.conf <<eof
#[member]
ETCD_NAME="etcd-node2"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.0.0.11:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.0.0.11:2379,https://127.0.0.1:2379"
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.0.0.11:2380"
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.0.0.10:2380,etcd-node2=https://10.0.0.11:2380,etcd-node3=https://10.0.0.12:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.0.0.11:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_PEER_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_PEER_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
eof
10.0.0.12:
cat > /etc/etcd/etcd.conf <<eof
#[member]
ETCD_NAME="etcd-node3"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.0.0.12:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.0.0.12:2379,https://127.0.0.1:2379"
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.0.0.12:2380"
ETCD_INITIAL_CLUSTER="etcd-node1=https://10.0.0.10:2380,etcd-node2=https://10.0.0.11:2380,etcd-node3=https://10.0.0.12:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://10.0.0.12:2379"
#[security]
CLIENT_CERT_AUTH="true"
ETCD_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
PEER_CLIENT_CERT_AUTH="true"
ETCD_PEER_CA_FILE="/etc/etcd/cert/ca.pem"
ETCD_PEER_CERT_FILE="/etc/etcd/cert/etcd.pem"
ETCD_PEER_KEY_FILE="/etc/etcd/cert/etcd-key.pem"
eof
注:
WorkingDirectory、--data-dir:指定工作目录和数据目录为 ${ETCD_DATA_DIR},需在启动服务前创建这个目录;
--wal-dir:指定 wal 目录,为了提高性能,一般使用 SSD 或者和 data-dir 不通的磁盘;
--name:指定节点名称,当 --initial-cluster-state 值为 new 时,--name 的参数值必须位于 --initial-cluster 列表中;
--cert-file、--key-file:etcd server 与 client 通信时使用的证书和私钥;
--trusted-ca-file:签名 client 证书的 CA 证书,用于验证 client 证书;
--peer-cert-file、--peer-key-file:etcd 与 peer 通信使用的证书和私钥;
--peer-trusted-ca-file:签名 peer 证书的 CA 证书,用于验证 peer 证书;
--quota-backend-bytes=6442450944:设置为最大 6GB,最大为 8GB;
--auto-compaction-retention=1:每 1 小时压缩一次,提高性能,节省磁盘空间;
--heartbeat-interva=250、--election-timeout=2000:磁盘性能低的情况下,增加时间间隔和超时时间;
为各节点创建etcd配置文件,都是一样的。
cat > /usr/lib/systemd/system/etcd.service <<eof
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=simple
WorkingDirectory=/var/lib/etcd
EnvironmentFile=-/etc/etcd/etcd.conf
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd"
Type=notify
[Install]
WantedBy=multi-user.target
eof
启动 etcd 服务
systemctl daemon-reload && systemctl enable etcd
systemctl restart etcd && systemctl status etcd
查看集群状态
部署完 etcd 集群后,在任一 etc 节点上执行如下命令:
etcdctl --endpoints=https://10.0.0.10:2379,https://10.0.0.11:2379,https://10.0.0.12:2379
--ca-file=/etc/etcd/cert/ca.pem
--cert-file=/etc/etcd/cert/etcd.pem
--key-file=/etc/etcd/cert/etcd-key.pem cluster-health
查看当前的leader 命令注意大写
ETCDCTL_API=3 etcdctl
-w table --cacert=/etc/etcd/cert/ca.pem
--cert=/etc/etcd/cert/etcd.pem
--key=/etc/etcd/cert/etcd-key.pem
--endpoints=https://10.0.0.10:2379,https://10.0.0.11:2379,https://10.0.0.12:2379 endpoint status