1.配置etcd工作目录,在 master1、master2、master3上操作:
mkdir -p /etc/etcd
mkdir -p /etc/etcd/ssl
2.安装签发证书工具 cfssl 用于生成证书使用的,在 master1上操作
mkdir /data/work -p
cd /data/work/
#cfssl-certinfo_linux-amd64 、cfssljson_linux-amd64 、cfssl_linux-amd64 上传到 /data/work/目录下
#把文件变成可执行权限,在 master1上操作
chmod +x *
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo
3.配置 ca 证书,在 master1上操作
生成 ca 证书请求文件,在 master1上操作
vim ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "Hubei",
"L": "Wuhan",
"O": "k8s",
"OU": "system"
}
],
"ca": {
"expiry": "87600h"
}
}
注:
CN:Common Name(公用名称)kube-apiserver 从证书中提取该字段作为请求的用户名 (User Name);
O:Organization(单位名称),kube-apiserver 从证书中提取该字段作为请求用户所属的组 (Group);
L 字段:所在城市
S 字段:所在省份
C 字段:只能是国家字母缩写
生成 ca 证书文件,在 master1上操作
vim ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"kubernetes": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "87600h"
}
}
}
}
制作证书,在 master1上操作
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
4.生成 etcd 证书,在 master1上操作
vim etcd-csr.json
{
"CN": "etcd",
"hosts": [
"127.0.0.1",
"10.10.1.11",
"10.10.1.12",
"10.10.1.13",
"10.10.1.99"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [{
"C": "CN",
"ST": "Hubei",
"L": "Wuhan",
"O": "k8s",
"OU": "system"
}]
}
制作证书,在 master1上操作
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes etcd-csr.json | cfssljson -bare etcd
5.部署 etcd 集群,在 master1上操作:
把 etcd-v3.4.13-linux-amd64.tar.gz 上传到/data/work 目录下
cd /data/work
tar -xf etcd-v3.4.13-linux-amd64.tar.gz
cp -p etcd-v3.4.13-linux-amd64/etcd* /usr/local/bin/
scp -r etcd-v3.4.13-linux-amd64/etcd* master2:/usr/local/bin/
scp -r etcd-v3.4.13-linux-amd64/etcd* master3:/usr/local/bin/
创建配置文件,在 master1上操作:
vim etcd.conf
#[Member]
ETCD_NAME="etcd1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.10.1.11:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.1.11:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.1.11:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.1.11:2379"
ETCD_INITIAL_CLUSTER="etcd1=https://10.10.1.11:2380,etcd2=https://10.10.1.12:2380,etcd3=https://10.10.1.13:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
#注释:
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 表示加入已有集群
创建启动服务文件,在master1上操作
vim etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=-/etc/etcd/etcd.conf
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/local/bin/etcd \
--cert-file=/etc/etcd/ssl/etcd.pem \
--key-file=/etc/etcd/ssl/etcd-key.pem \
--trusted-ca-file=/etc/etcd/ssl/ca.pem \
--peer-cert-file=/etc/etcd/ssl/etcd.pem \
--peer-key-file=/etc/etcd/ssl/etcd-key.pem \
--peer-trusted-ca-file=/etc/etcd/ssl/ca.pem \
--peer-client-cert-auth \
--client-cert-auth
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
移动文件位置,在master1上操作
cp ca*.pem /etc/etcd/ssl/
cp etcd*.pem /etc/etcd/ssl/
cp etcd.conf /etc/etcd/
cp etcd.service /usr/lib/systemd/system/
for i in master2 master3;do rsync -vaz etcd.conf $i:/etc/etcd/;done
for i in master2 master3;do rsync -vaz etcd*.pem ca*.pem $i:/etc/etcd/ssl/;done
for i in master2 master3;do rsync -vaz etcd.service $i:/usr/lib/systemd/system/;done
修改etcd.conf配置文件,在 master2上操作:
vim /etc/etcd/etcd.conf
#[Member]
ETCD_NAME="etcd2"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.10.1.12:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.1.12:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.1.12:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.1.12:2379"
ETCD_INITIAL_CLUSTER="etcd1=https://10.10.1.11:2380,etcd2=https://10.10.1.12:2380,etcd3=https://10.10.1.13:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
修改etcd.conf配置文件,在 master3上操作:
vim /etc/etcd/etcd.conf
#[Member]
ETCD_NAME="etcd3"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://10.10.1.13:2380"
ETCD_LISTEN_CLIENT_URLS="https://10.10.1.13:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://10.10.1.13:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://10.10.1.13:2379"
ETCD_INITIAL_CLUSTER="etcd1=https://10.10.1.11:2380,etcd2=https://10.10.1.12:2380,etcd3=https://10.10.1.13:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
启动 etcd 集群
创建etcd数据存放目录,,在 master1、master2、master3上操作:
mkdir -p /var/lib/etcd/default.etcd
systemctl daemon-reload
systemctl enable etcd.service
systemctl start etcd.service
启动 etcd 的时候,先启动 master1 的 etcd 服务,会一直卡住在启动的状态,然后接着再启动 master2 的 etcd,这样master1 这个节点 etcd 才会正常起来
6.查看 etcd 集群,在 master1上操作:
ETCDCTL_API=3
/usr/local/bin/etcdctl --write-out=table --cacert=/etc/etcd/ssl/ca.pem --cert=/etc/etcd/ssl/etcd.pem --key=/etc/etcd/ssl/etcd-key.pem --endpoints=https://10.10.1.11:2379,https://10.10.1.12:2379,https://10.10.1.13:2379 endpoint health