整体流程规划#查看服务的状态systemctlstatusetcd.servicek8s-master192.168.31.71etcd-1#master存在
整体流程
规划
systemctl status etcd.servicek8s-master
192.168.31.71
etcd-1
kube-apiserver,
kube-controller-manager,
kube-scheduler,
etcdk8s-node1
192.168.31.72
etcd-2
kubelet,
kube-proxy,
docker etcd
3、操作系统初始化配
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
hostnamectl set-hostname <hostname>
cat >> /etc/hosts << EOF
192.168.44.147 m1
192.168.44.148 n1
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables &#61; 1
net.bridge.bridge-nf-call-iptables &#61; 1
EOF
sysctl --system
yum install ntpdate -y
ntpdate time.windows.com
4、部署 Etcd 集群
Etcd 是一个分布式键值存储系统&#xff0c;
Kubernetes 使用 Etcd 进行数据存储&#xff0c;
所以先准备 一个 Etcd 数据库&#xff0c;为解决 Etcd 单点故障&#xff0c;应采用集群方式部署&#xff0c;这里使用 3 台组建集 群&#xff0c;可容忍 1 台机器故障&#xff0c;
当然&#xff0c;你也可以使用 5 台组建集群&#xff0c;可容忍 2 台机器故障。
4.1 准备 cfssl 证书生成工具
cfssl 是一个开源的证书管理工具&#xff0c;使用 json 文件生成证书&#xff0c;相比 openssl 更方便使用。 找任意一台服务器操作&#xff0c;这里用 Master 节点。
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 &#43;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
4.2 生成 Etcd 证书
&#xff08;1&#xff09;自签证书颁发机构&#xff08;CA&#xff09; 创建工作目录&#xff1a;
mkdir -p ~/TLS/{etcd,k8s}
cd TLS/etcd
自签 CA&#xff1a;
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
生成证书&#xff1a;
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
ls *pem
ca-key.pem ca.pem
&#xff08;2&#xff09;使用自签 CA 签发 Etcd HTTPS 证书 创建证书申请文件&#xff1a;
cat > server-csr.json<< EOF
{
"CN": "etcd",
"hosts": [
"192.168.31.71",
"192.168.31.72",
"192.168.31.73"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
注&#xff1a;上述文件 hosts 字段中 IP 为所有 etcd 节点的集群内部通信 IP&#xff0c;一个都不能少&#xff01;为了 方便后期扩容可以多写几个预留的 IP。
生成证书&#xff1a; cfssl gencert
cfssl gencert -ca&#61;ca.pem -ca-key&#61;ca-key.pem -config&#61;ca-config.json -
profile&#61;www server-csr.json | cfssljson -bare server
ls server*pem
server-key.pem server.pem
4.3 从 Github 下载二进制文件 下载地址&#xff1a;https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9- linux-amd64.tar.gz
4.4 部署 Etcd 集群
以下在节点 1 上操作&#xff0c;为简化操作&#xff0c;待会将节点 1 生成的所有文件拷贝到节点 2 和节点 3. &#xff08;1&#xff09;创建工作目录并解压二进制包
mkdir /opt/etcd/{bin,cfg,ssl} –p
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
mv etcd-v3.4.9-linux-amd64/{etcd,etcdctl} /opt/etcd/bin/
&#xff08;2&#xff09;创建 etcd 配置文件
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME&#61;"etcd-1"
ETCD_DATA_DIR&#61;"/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS&#61;"https://192.168.31.71:2380"
ETCD_LISTEN_CLIENT_URLS&#61;"https://192.168.31.71:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS&#61;"https://192.168.31.71:2380"
ETCD_ADVERTISE_CLIENT_URLS&#61;"https://192.168.31.71:2379"
ETCD_INITIAL_CLUSTER&#61;"etcd-1&#61;https://192.168.31.71:2380,etcd2&#61;https://192.168.31.72:2380,etcd-3&#61;https://192.168.31.73:2380"
ETCD_INITIAL_CLUSTER_TOKEN&#61;"etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE&#61;"new"
EOF
ETCD_NAME&#xff1a;节点名称&#xff0c;集群中唯一 ETCD_DATA_DIR&#xff1a;数据目录 ETCD_LISTEN_PEER_URLS&#xff1a;集群通信监听地址 ETCD_LISTEN_CLIENT_URLS&#xff1a;客户端访问监听地址 ETCD_INITIAL_ADVERTISE_PEER_URLS&#xff1a;集群通告地址 ETCD_ADVERTISE_CLIENT_URLS&#xff1a;客户端通告地址 ETCD_INITIAL_CLUSTER&#xff1a;集群节点地址 ETCD_INITIAL_CLUSTER_TOKEN&#xff1a;集群 Token ETCD_INITIAL_CLUSTER_STATE&#xff1a;加入集群的当前状态&#xff0c;new 是新集群&#xff0c;existing 表示加入 已有集群
&#xff08;3&#xff09;systemd 管理 etcd
cat > /usr/lib/systemd/system/etcd.service << EOF
[Unit]
Description&#61;Etcd Server
After&#61;network.target
After&#61;network-online.target
Wants&#61;network-online.target
[Service]
Type&#61;notify
EnvironmentFile&#61;/opt/etcd/cfg/etcd.conf
ExecStart&#61;/opt/etcd/bin/etcd \
--cert-file&#61;/opt/etcd/ssl/server.pem \
--key-file&#61;/opt/etcd/ssl/server-key.pem \
--peer-cert-file&#61;/opt/etcd/ssl/server.pem \
--peer-key-file&#61;/opt/etcd/ssl/server-key.pem \
--trusted-ca-file&#61;/opt/etcd/ssl/ca.pem \
--peer-trusted-ca-file&#61;/opt/etcd/ssl/ca.pem \
--logger&#61;zap
Restart&#61;on-failure
LimitNOFILE&#61;65536
[Install]
WantedBy&#61;multi-user.target
EOF
&#xff08;4&#xff09;拷贝刚才生成的证书 把刚才生成的证书拷贝到配置文件中的路径&#xff1a;
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
&#xff08;5&#xff09;启动并设置开机启动
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
&#xff08;6&#xff09;将上面节点 1 所有生成的文件拷贝到节点 2 和节点 3
scp -r /opt/etcd/ root&#64;192.168.31.72:/opt/
scp /usr/lib/systemd/system/etcd.service
root&#64;192.168.31.72:/usr/lib/systemd/system/scp -r /opt/etcd/ root&#64;192.168.31.73:/opt/scp /usr/lib/systemd/system/etcd.service
root&#64;192.168.31.73:/usr/lib/systemd/system/
然后在节点 2 和节点 3 分别修改 etcd.conf 配置文件中的节点名称和当前服务器 IP&#xff1a;
vi /opt/etcd/cfg/etcd.conf
ETCD_NAME&#61;"etcd-1" ETCD_DATA_DIR&#61;"/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS&#61;"https://192.168.31.71:2380"
ETCD_LISTEN_CLIENT_URLS&#61;"https://192.168.31.71:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS&#61;"https://192.168.31.71:2380"
服务器 IP
ETCD_ADVERTISE_CLIENT_URLS&#61;"https://192.168.31.71:2379"
IP
ETCD_INITIAL_CLUSTER&#61;"etcd-1&#61;https://192.168.31.71:2380,etcd2&#61;https://192.168.31.72:2380,etcd-3&#61;https://192.168.31.73:2380"
ETCD_INITIAL_CLUSTER_TOKEN&#61;"etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE&#61;"new
最后启动 etcd 并设置开机启动&#xff0c;同上。
&#xff08;7&#xff09;查看集群状态
ETCDCTL_API&#61;3 /opt/etcd/bin/etcdctl --cacert&#61;/opt/etcd/ssl/ca.pem --
cert&#61;/opt/etcd/ssl/server.pem --key&#61;/opt/etcd/ssl/server-key.pem --
endpoints&#61;"https://192.168.31.71:2379,https://192.168.31.72:2379,https://192.16
8.31.73:2379" endpoint health
https://192.168.31.71:2379 is healthy: successfully committed proposal: took &#61;
8.154404ms
https://192.168.31.73:2379 is healthy: successfully committed proposal: took &#61;
9.044117ms
https://192.168.31.72:2379 is healthy: successfully committed proposal: took &#61;
10.000825ms
如果输出上面信息&#xff0c;就说明集群部署成功。如果有问题第一步先看日志&#xff1a; /var/log/message 或 journalctl -u etcd
5、安装 Docker