目录
1、集群设计
2、环境准备
2.1 工具下载地址
证书管理工具下载:
Etcd包下载
安全策略
2.2、先下载软件
2.3 cfssl证书
2.3.1创建工作目录
2.3.2 自签证书颁发机构(CA)
2.3.3 生成根证书
2.3.4 签发 Etcd https 证书
2.3.5 生成证书
3. Etcd安装
3.1 下载Etcd
3.2 解压
3.3 安装目录
3.3.1 创建安装目录
3.3.2将生成的 etcd 证书 cp 到 etcd 工作目录下的 ssl 目录(包括 ca 应为 自签)
3.3.3 将 etcd etcdctl 可执行文件移动到 etcd 的 bin 工作目录下
3.3.4创建 etcd 配置文件,存放到 etcd 工作目录
3.4 将etcd服务添加到systemd中
3.4.1 设置开机自启
4、剩余节点通过scp进行上传
4.1修改节点配置
4.2 设置开机自启
4.3 检查集群
master | 192.168.17.138 |
node1 | 192.168.17.143 |
node2 | 192.168.17.144 |
wget https://github.com/cloudflare/cfssl/releases/download/1.2.0/cfssl_linux-amd64
wget https://github.com/cloudflare/cfssl/releases/download/1.2.0/cfssljson_linux-amd64
wget https://github.com/cloudflare/cfssl/releases/download/1.2.0/cfssl-certinfo_linux-amd64
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
安装前需要添加防火墙策略:
firewall-cmd --add-port=2380/tcp --permanent //添加端口
firewall-cmd --add-port=2379/tcp --permanent
systemctl reload firewalld.service //重载防火墙
关闭Selinux
使用setenforce或者 vi /etc/sysconfig/selinux
setenforce 0 //设置为允许
getenforce //查看
cfssl 是一个开源的证书管理工具,使用 json 文件生成证书,相比 openssl 更方便使用。
首先,下载工具,再赋予操作权限,最后移动到 bin 目录方便使用。
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
进入工作目录,这个目录作用存放证书,并将证书 copy 到集群服务器
mkdir -p ~/TLS/etcd
cd ~/TLS/etcd
1)CA年限可以自己设置
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
生成证书前要检查时间是否同步呦 可以同步物理服务器的时间 clock -w
根据 CA 信息生成证书,执行命令后会生成 ca.pem 和 ca-key.pem 文件,即根证书。
fssl gencert -initca ca-csr.json | cfssljson -bare ca -
hosts写上集群内所有IP,如后续有扩容规划,可以提前把IP写上
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"192.168.17.138",
"192.168.17.143",
"192.168.17.144"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
执行下面命令,会生成 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
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
mkdir /opt/etcd/{bin,cfg,ssl} -p
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
vi /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.17.138:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.17.138:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.17.138:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.17.138:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.17.138:2380,etcd-2=https://192.168.17.143:2380,etcd-3=https://192.168.17.144:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_ENABLE_V2="true"
ETCD_NAME:节点名称,集群中唯一(当前节点 etcd 名称)
ETCD_DATA_DIR:数据目录
ETCD_LISTEN_PEER_URLS:集群通信监听地址(当前节点地址)
ETCD_LISTEN_CLIENT_URLS:客户端访问监听地址(当前节点地址)
ETCD_INITIAL_ADVERTISE_PEERURLS:集群通告地址(当前节点地址)
ETCD_ADVERTISE_CLIENT_URLS:客户端通告地址(当前节点地址)
ETCD_INITIAL_CLUSTER:集群节点地址
ETCD_INITIALCLUSTER_TOKEN:集群Token
ETCD_INITIALCLUSTER_STATE:加入集群的当前状态,new是新集群,existing表示加入已有集群
vi /usr/lib/systemd/system/etcd.service
[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 \
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload //每次写etcd.serive文件都要进行重启
systemctl start etcd
systemctl enable etcd
scp -r /opt/etcd/ [email protected]:/opt/ //etcd 配置、证书、可执行文件
scp -r /opt/etcd/ [email protected]:/opt/
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/ //添加到服务文件
scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
剩余两节点 执行如下操作
#[Member]
ETCD_NAME="etcd-2" //名称换为当前名称
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.17.143:2380" //换为当前IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.17.143:2379" //换为当前IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.17.143:2380" //换为当前IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.17.143:2379" //换为当前IP
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.17.138:2380,etcd-2=https://192.168.17.143:2380,etcd-3=https://192.168.17.144:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_ENABLE_V2="true"
systemctl daemon-reload //修改完服务文件后,需要执行该操作
systemctl start etcd
systemctl enable etcd
执行改命令,检查集群是否成功
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="https://192.168.17.138:2379,https://192.168.17.143:2379,https://192.168.17.144:2379" endpoint health --write-out=table
集群搭建成功