本记录参考自 Kubernetes 深入学习(一) —— 入门和集群安装部署
主要参考的这一篇,是根据这一篇部署过程中的捉虫和报错处理
etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统,可以用于配置共享和服务的注册和发现。一般使用奇数个节点构成集群。
为了加强集群安全性这里使用CA自签证书来进行互相认证
① 准备 cfssl 工具
bashcurl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl
curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson
curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
chmod +x /usr/local/bin/cfssl*
② 自签名
我们首先为 etcd 签发一套SSL证书
bashcd /
mkdir -p /k8s/etcd/{ssl,cfg,bin}
#/k8s/etcd/ssl 用于存放 etcd 自签证书,/k8s/etcd/cfg 用于存放 etcd 配置文件,/k8s/etcd/bin 用于存放 etcd 执行程序。
mkdir -p /k8s/data/default.etcd
#/k8s/data 用于存放数据文件
cd /k8s/etcd/ssl # 进入ssl目录
① 创建 CA 配置文件:ca-config.json
bashcat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"etcd": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "87600h"
}
}
}
}
EOF
bashcat > ca-csr.json <<EOF
{
"CN": "etcd",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "Shanghai",
"L": "Shanghai",
"O": "etcd",
"OU": "System"
}
],
"ca": {
"expiry": "87600h"
}
}
EOF
③ 生成 CA 证书和私钥
bashcfssl gencert -initca ca-csr.json | cfssljson -bare ca
# 生成的 ca-key.pem为CA 私钥 ca.pem为CA 数字证书
④ 创建证书签名请求文件:etcd-csr.json
bashcat > etcd-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"10.1.1.140",
"10.1.1.141",
"10.1.1.142"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "BeiJing",
"L": "BeiJing",
"O": "etcd",
"OU": "System"
}
]
}
EOF
bashcfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=etcd etcd-csr.json | cfssljson -bare etcd
# 生成的 etcd-key.pem为etcd 私钥 etcd.pem为etcd 数字证书
bash#下载
cd /k8s/etcd
wget https://github.com/etcd-io/etcd/releases/download/v3.2.28/etcd-v3.2.28-linux-amd64.tar.gz
#解压
tar zxf etcd-v3.2.28-linux-amd64.tar.gz
#放入 /k8s/etcd/bin 并复制进/user/local/bin
cp etcd-v3.2.28-linux-amd64/{etcd,etcdctl} /k8s/etcd/bin
rm -rf etcd-v3.2.28-linux-amd64*
cp /k8s/etcd/bin/* /usr/local/bin/
创建 etcd 配置文件:etcd.conf
bashcat > /k8s/etcd/cfg/etcd.conf <<EOF
# [member]
ETCD_NAME=etcd-1
ETCD_DATA_DIR=/k8s/data/default.etcd
ETCD_LISTEN_PEER_URLS=https://10.1.1.140:2380
ETCD_LISTEN_CLIENT_URLS=https://10.1.1.140:2379
# [cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://10.1.1.140:2380
ETCD_ADVERTISE_CLIENT_URLS=https://10.1.1.140:2379
ETCD_INITIAL_CLUSTER=etcd-1=https://10.1.1.140:2380,etcd-2=https://10.1.1.141:2380,etcd-3=https://10.1.1.142.71:2380
ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
ETCD_INITIAL_CLUSTER_STATE=new
# [security]
ETCD_CERT_FILE=/k8s/etcd/ssl/etcd.pem
ETCD_KEY_FILE=/k8s/etcd/ssl/etcd-key.pem
ETCD_TRUSTED_CA_FILE=/k8s/etcd/ssl/ca.pem
ETCD_PEER_CERT_FILE=/k8s/etcd/ssl/etcd.pem
ETCD_PEER_KEY_FILE=/k8s/etcd/ssl/etcd-key.pem
ETCD_PEER_TRUSTED_CA_FILE=/k8s/etcd/ssl/ca.pem
EOF
创建 etcd 服务:etcd.service
bashcat > /k8s/etcd/etcd.service <<'EOF'
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/k8s/etcd/cfg/etcd.conf
WorkingDirectory=/k8s/data/default.etcd
ExecStart=/k8s/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 \
--initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
--advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS} \
--initial-cluster=${ETCD_INITIAL_CLUSTER} \
--initial-cluster-token=${ETCD_INITIAL_CLUSTER_TOKEN} \
--initial-cluster-state=${ETCD_INITIAL_CLUSTER_STATE} \
--cert-file=${ETCD_CERT_FILE} \
--key-file=${ETCD_KEY_FILE} \
--trusted-ca-file=${ETCD_TRUSTED_CA_FILE} \
--peer-cert-file=${ETCD_PEER_CERT_FILE} \
--peer-key-file=${ETCD_PEER_KEY_FILE} \
--peer-trusted-ca-file=${ETCD_PEER_TRUSTED_CA_FILE}
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
将 etcd 目录拷贝到另外两个节点
bashscp -r /k8s root@node1:/k8s
scp -r /k8s root@node2:/k8s
#已更改 hosts文件 没改就用ip地址传 记得修改各个node的配置文件!
启动 etcd 服务
bash# 首先在三个节点将 etcd.service 拷贝到 /usr/lib/systemd/system/ 下
cp /k8s/etcd/etcd.service /usr/lib/systemd/system/
systemctl daemon-reload
#启动服务
systemctl start etcd
这里如果出现如下报错
bashsda: failed to get udev uid: Invalid argument sda: failed to get sysfs uid: Invalid argument sda: failed to get sgio uid: No such file or directory sda: add missing path
则是虚拟机多路径问题,可以通过修改配置文件解决
bashvim /etc/multipath.conf
# 在文件中添加
blacklist {
device {
vendor "VMware"
product "Virtual disk"
}
}
#重启 mutipath-tools 服务
service multipath-tools restart
然后再重新启动etcd,如一次启动为成功需要再次启动有可能已经初始化了成员,会出现:
member 45d15357d3f7b39 has already been bootstrapped
错误,需要删除 /k8s/data/default.etcd
,如果依然没有用,则修改配置文件
ETCD_INITIAL_CLUSTER_STATE=existing
至此 Etcd部署完成