角色 | IP | 组件 |
---|---|---|
etcd01 | 192.168.1.171 | etcd |
etcd02 | 192.168.1.172 | etcd |
etcd03 | 192.168.1.173 | etcd |
k8s-master01 | 192.168.1.174 | kube-apiserver,kube-controller-manager,kube-scheduler, docker |
k8s-master02 | 192.168.1.175 | kube-apiserver,kube-controller-manager,kube-scheduler, docker |
k8s-node01 | 192.168.1.176 | kubelet,kube-proxy,docker |
k8s-node02 | 192.168.1.177 | kubelet,kube-proxy,docker |
1、操作系统初始化配置(全部机器操作)
systemctl stop firewalld
systemctl disable firewalld
关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 临时
关闭swap
swapoff -a # 临时
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
根据规划设置主机名
hostnamectl set-hostname <hostname>
cat >> /etc/hosts << EOF
192.168.1.71 etcd01
192.168.1.72 etcd02
192.168.1.73 etcd03
192.168.1.74 k8s-master01
192.168.1.75 k8s-master02
192.168.1.76 k8s-node01
192.168.1.77 k8s-node02
EOF
将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
生效
sysctl --system
时间同步
yum install ntpdate -y
ntpdate time.windows.com
2、部署Etcd集群(171、172、173机器操作)
准备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/local/bin/cfssl-certinfo
生成Etcd证书
- 自签证书颁发机构(CA)
mkdir -p ~/TLS/{etcd,k8s}
cd TLS/etcd
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
生成证书
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
- 使用自签CA签发Etcd HTTPS证书
cat > server-csr.json << EOF
{
"CN": "etcd",
"hosts": [
"192.168.1.171",
"192.168.1.172",
"192.168.1.173"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
生成证书
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
Github下载二进制文件
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
部署Etcd集群(先在一台节点上操作)
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/
创建配置
cat > /opt/etcd/cfg/etcd.conf << EOF
#[Member]
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.1.171:2380"
ETCD_LISTEN_CLIENT_URLS="https://192.168.1.171:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.1.171:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.1.171:2379"
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.1.171:2380,etcd-2=https://192.168.1.172:2380,etcd-3=https://192.168.1.173:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
systemd管理etcd
cat > /usr/lib/systemd/system/etcd.service << EOF
[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 \
--logger=zap
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
拷贝刚才生成的证书
cp ~/TLS/etcd/ca*pem ~/TLS/etcd/server*pem /opt/etcd/ssl/
把/opt/etcd目录拷贝到其他两个节点,节点2和节点3分别修改etcd.conf配置文件中的节点名称和当前服务器IP,最后启动etcd并设置开机启动。
scp -r /opt/etcd/ root@192.168.1.172:/opt/
scp /usr/lib/systemd/system/etcd.service root@192.168.1.172:/usr/lib/systemd/system/
scp -r /opt/etcd/ root@192.168.1.173:/opt/
scp /usr/lib/systemd/system/etcd.service root@192.168.1.173:/usr/lib/systemd/system/
vim /opt/etcd/cfg/etcd.conf
#[Member]
ETCD_NAME="" # 修改此处,节点2改为etcd-2,节点3改为etcd-3
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://当前服务器IP:2380" # 修改此处为当前服务器IP
ETCD_LISTEN_CLIENT_URLS="https://当前服务器IP:2379" # 修改此处为当前服务器IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://当前服务器IP:2380" # 修改此处为当前服务器IP
ETCD_ADVERTISE_CLIENT_URLS="https://当前服务器IP:2379" # 修改此处为当前服务器IP
ETCD_INITIAL_CLUSTER="etcd-1=https://192.168.1.171:2380,etcd-2=https://192.168.1.172:2380,etcd-3=https://192.168.1.173:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
查看集群状态
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.1.171:2379,https://192.168.1.172:2379,https://192.168.1.173:2379" endpoint health
https://192.168.1.171:2379 is healthy: successfully committed proposal: took = 8.154404ms
https://192.168.1.173:2379 is healthy: successfully committed proposal: took = 9.044117ms
https://192.168.1.172:2379 is healthy: successfully committed proposal: took = 10.000825ms