K8S 高可用外部 etcd运行时 (一) 安装 etcd 集群
K8S 1.20.5 高可用外部 etcd , Docker 容器运行时 (一) 安装 etcd 集群
架构
文中使用到的服务器ip及角色对应如下:
IP | 角色 | 其他软件 |
---|---|---|
172.16.16.180 | k8s-master-01 | etcd |
172.16.16.181 | k8s-master-02 | etcd |
172.16.16.182 | k8s-master-03 | etcd |
172.16.16.190 | k8s-node-01 | |
172.16.16.188 | VIP | nginx |
生成 etcd 证书
在 k8s-master-01 上操作
mkdir -p /data/software/
cd /data/software/
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
# 自签证书颁发机构(CA)
mkdir -p ~/etcd_tls
cd ~/etcd_tls
cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
cat > ca-csr.json << EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
# 生成 ca 证书 (ca.pem 和 ca-key.pem 文件)
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
# 使用自签CA签发Etcd HTTPS证书
# 创建证书申请文件:
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"172.16.16.180",
"172.16.16.181",
"172.16.16.182"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
# 注:上述文件hosts字段中IP为所有etcd节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。
# 生成证书 (server.pem 和 server-key.pem):
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
mkdir /opt/etcd/{bin,cfg,ssl} -p
cp ~/etcd_tls/ca*pem ~/etcd_tls/server*pem /opt/etcd/ssl/
安装 etcd
在 k8s-master-01 上操作
cd /data/software/
# 下载,可能会出现网络不通的情况
wget https://github.com/etcd-io/etcd/releases/download/v3.4.14/etcd-v3.4.14-linux-amd64.tar.gz
tar zxvf etcd-v3.4.14-linux-amd64.tar.gz
mv etcd-v3.4.14-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
cat > /opt/etcd/cfg/etcd.conf << EOFETCD_NAME="etcd-1"
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://172.16.16.180:2380"
ETCD_LISTEN_CLIENT_URLS="https://172.16.16.180:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.16.16.180:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://172.16.16.180:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://172.16.16.180:2380,etcd-2=https://172.16.16.181:2380,etcd-3=https://172.16.16.182:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
# 参数说明
ETCD_NAME : 节点名称,集群中唯一
ETCD_DATA_DIR : 数据目录
ETCD_LISTEN_PEER_URLS : 集群通信监听地址
ETCD_LISTEN_CLIENT_URLS : 客户端访问监听地址
ETCD_INITIAL_ADVERTISE_PEER_URLS : 集群通告地址
ETCD_ADVERTISE_CLIENT_URLS : 客户端通告地址
ETCD_INITIAL_CLUSTER : 集群节点地址
ETCD_INITIAL_CLUSTER_TOKEN : 集群Token
ETCD_INITIAL_CLUSTER_STATE : 加入集群的当前状态,new是新集群,existing表示加入已有集群
# systemd 管理 etcd
cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/opt/etcd/cfg/etcd.conf
ExecStart=/opt/etcd/bin/etcd --cert-file=/opt/etcd/ssl/server.pem --key-file=/opt/etcd/ssl/server-key.pem --peer-cert-file=/opt/etcd/ssl/server.pem --peer-key-file=/opt/etcd/ssl/server-key.pem --trusted-ca-file=/opt/etcd/ssl/ca.pem --peer-trusted-ca-file=/opt/etcd/ssl/ca.pem --logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# 把配置拷贝到其他 etcd 节点
scp -r /opt/etcd/ root@172.16.16.181:/opt/
scp /usr/lib/systemd/system/etcd.service root@172.16.16.181:/usr/lib/systemd/system/
scp -r /opt/etcd/ root@172.16.16.182:/opt/
scp /usr/lib/systemd/system/etcd.service root@172.16.16.182:/usr/lib/systemd/system/
# 启动 etcd
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
其他两个 etcd 节点安装
在 k8s-master-02 和 k8s-master-03 上操作
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1" # 修改此处,节点2改为etcd-2,节点3改为etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://172.16.16.180:2380" # 修改此处为当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://172.16.16.180:2379" # 修改此处为当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://172.16.16.180:2380" # 修改此处为当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://172.16.16.180:2379" # 修改此处为当前服务器IP
ETCD_INITIAL_CLUSTER="etcd-1=https://172.16.16.180:2380,etcd-2=https://172.16.16.181:2380,etcd-3=https://172.16.16.182:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
# 启动 etcd
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
检查
在 k8s-master-01 上操作
ETCDCTL_API=3 /opt/etcd/bin/etcdctl \
--cacert=/opt/etcd/ssl/ca.pem \
--cert=/opt/etcd/ssl/server.pem \
--key=/opt/etcd/ssl/server-key.pem \
--endpoints=172.16.16.180:2379,172.16.16.181:2379,172.16.16.182:2379 \
endpoint health --write-out=table
+--------------------+--------+-------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+--------------------+--------+-------------+-------+
| 172.16.16.180:2379 | true | 18.057212ms | |
| 172.16.16.181:2379 | true | 18.966895ms | |
| 172.16.16.182:2379 | true | 20.987225ms | |
+--------------------+--------+-------------+-------+