一、部署etcd集群
使用cfssl来生成自签证书,先下载cfssl工具:
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
1.1 生成证书
# 可以随便建个etcd目录,在目录里创建证书,后面用到的时候拷贝过去就好
# 创建 ETCD 证书
cat << EOF | tee ca-config.json
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
# 创建 ETCD CA 配置文件
cat << EOF | tee ca-csr.json
{
"CN": "kubernetes",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "DaLian",
"ST": "DaLian",
"O": "k8s",
"OU": "System"
}
]
}
EOF
# 创建 ETCD Server 证书(注意更改host字段)
cat << EOF | tee server-csr.json
{
"CN": "etcd",
"hosts": [
"22.22.22.10",
"22.22.22.11",
"22.22.22.12"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "DaLian",
"ST": "DaLian"
}
]
}
EOF
# 生成 ETCD CA 证书和私钥
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
[root@k8s-master ssl]# ls *.pem
ca-key.pem ca.pem server-key.pem server.pem
1.2 部署Etcd
二进制包下载地址:hhttps://github.com/etcd-io/etcd/releases/tag/v3.3.10
以下部署步骤在规划的三个etcd节点操作一样,唯一不同的是etcd配置文件中的服务器IP要写当前的:
解压二进制包:
[root@k8s-master k8s1.13]# mkdir /opt/etcd/{bin,cfg,ssl} -p
[root@k8s-master k8s1.13]# ls
etcd-v3.3.10-linux-amd64.tar.gz kubernetes-node-linux-amd64.tar.gz
flannel-v0.10.0-linux-amd64.tar.gz kubernetes-server-linux-amd64.tar.gz
kubernetes-client-linux-amd64.tar.gz
[root@k8s-master k8s1.13]# tar zxf etcd-v3.3.10-linux-amd64.tar.gz
[root@k8s-master k8s1.13]# mv etcd-v3.3.10-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
[root@k8s-master k8s1.13]# ls /opt/etcd/bin/
etcd etcdctl
创建etcd配置文件:
[root@k8s-master k8s1.13]# cat << Rainy > /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://22.22.22.10:2380"
ETCD_LISTEN_CLIENT_URLS="https://22.22.22.10:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://22.22.22.10:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://22.22.22.10:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://22.22.22.10:2380,etcd02=https://22.22.22.11:2380,etcd03=https://22.22.22.12:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
Rainy
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:
[root@k8s-master k8s1.13]# vim /usr/lib/systemd/system/etcd.service
[root@k8s-master k8s1.13]# cat /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
ExecStart=/opt/etcd/bin/etcd \
--name=${ETCD_NAME} \
--data-dir=${ETCD_DATA_DIR} \
--listen-peer-urls=${ETCD_LISTEN_PEER_URLS} \
--listen-client-urls=${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
--advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} \
--initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
--initial-cluster=${ETCD_INITIAL_CLUSTER} \
--initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} \
--initial-cluster-state=new \
--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
把刚才生成的证书拷贝到配置文件中的位置:
[root@k8s-master ssl]# cd /usr/local/src/ssl/
[root@k8s-master ssl]# ls
ca-config.json ca-csr.json ca.pem server-csr.json server.pem
ca.csr ca-key.pem server.csr server-key.pem
[root@k8s-master ssl]# cp ca*pem server*pem /opt/etcd/ssl/
[root@k8s-master ssl]# ls /opt/etcd/ssl/
ca-key.pem ca.pem server-key.pem server.pem
配置node1、node2节点(切记修改node服务器中的IP地址以及节点名称)
[root@k8s-master /]# scp -r /opt/etcd 22.22.22.11:/opt/
[root@k8s-master /]# scp -r /opt/etcd 22.22.22.12:/opt/
[root@k8s-master /]# scp -r /usr/lib/systemd/system/etcd.service 22.22.22.11:/usr/lib/systemd/system/
[root@k8s-master /]# scp -r /usr/lib/systemd/system/etcd.service 22.22.22.12:/usr/lib/systemd/system/
启动并设置开启启动:(建议先启动node节点)
[root@k8s-node1 ~]# systemctl start etcd
[root@k8s-node1 ~]# systemctl enable etcd
Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /usr/lib/systemd/system/etcd.service.
全部部署完成,启动成功后检查集群状态
[root@k8s-master ssl]# cd /opt/etcd/ssl/
[root@k8s-master ssl]# /opt/etcd/bin/etcdctl \
--ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem \
--endpoints="https://22.22.22.10:2379,https://22.22.22.11:2379,https://22.22.22.12:2379" \
cluster-health
member 4f2fb2f4220e432b is healthy: got healthy result from https://22.22.22.11:2379
member 644240f514a66ef7 is healthy: got healthy result from https://22.22.22.12:2379
member f99f0f6c040601bf is healthy: got healthy result from https://22.22.22.10:2379
cluster is healthy
如果输出上面信息,就说明集群部署成功。如果有问题第一步先看日志:/var/log/message 或 journalctl -u etcd
网友评论