生产环境中使用kubeadm和二进制安装的比较多
主机名 | IP | 服务 |
---|---|---|
master1 | 192.168.153.10/24 | kube-apiserver kube-controller-manager kube-scheduler etcd |
node1 | 192.168.150.30/24 | etcd docker kubelet kube-proxyflannel |
node2 | 192.168.150.40/24 | etcd docker kubelet kube-proxyflannel |
准备工作
[root@master1 ~]# mkdir k8s
[root@master1 ~]# cd k8s/
[root@master1 ~/k8s]# vim cfssl.sh #此脚本用于下载证书制作工具
curl -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 /usr/local/bin/cfssljson /usr/local/bin/cfssl-certinfo
[root@master1 ~/k8s]# bash cfssl.sh #执行该脚本
[root@master1 ~/k8s]# mkdir etcd-cert
[root@master1 ~/k8s]# cd etcd-cert/
##定义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
##生产证书,生成ca-key.pem ca.pem
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
##指定etcd三个节点之间的通信验证
cat > server-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"192.168.153.10",
"192.168.153.30",
"192.168.153.40"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
##生成ETCD证书 server-key.pem server.pem
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
[root@master1 ~/k8s/etcd-cert]# cd ..
#拉入压缩包etcd-v3.3.10-linux-amd64.tar.gz
[root@master1 ~/k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
[root@master1 ~/k8s]# ls etcd-v3.3.10-linux-amd64
etcd-v3.3.10-linux-amd64.tar.gz
[root@master1 ~/k8s]# mkdir /opt/etcd/{cfg,bin,ssl} -p #三个目录分别存放配置文件,命令文件,证书
[root@master1 ~/k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin/
[root@master1 ~/k8s]# ls /opt/etcd/bin/
etcd etcdctl
[root@master1 ~/k8s]# cp etcd-cert/*.pem /opt/etcd/ssl/
[root@master1 ~/k8s]# ls /opt/etcd/ssl/
ca-key.pem ca.pem server-key.pem server.pem
创建执行脚本
[root@master1 ~/k8s]# vim etcd.sh
#!/bin/bash
# example: ./etcd.sh etcd01 192.168.1.10 etcd02=https://192.168.1.11:2380,etcd03=https://192.168.1.12:2380
ETCD_NAME=$1
ETCD_IP=$2
ETCD_CLUSTER=$3
WORK_DIR=/opt/etcd
cat <<EOF >$WORK_DIR/cfg/etcd
#[Member]
ETCD_NAME="${ETCD_NAME}"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_LISTEN_CLIENT_URLS="https://${ETCD_IP}:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://${ETCD_IP}:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://${ETCD_IP}:2380,${ETCD_CLUSTER}"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
cat <<EOF >/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=${WORK_DIR}/cfg/etcd
ExecStart=${WORK_DIR}/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=${WORK_DIR}/ssl/server.pem \
--key-file=${WORK_DIR}/ssl/server-key.pem \
--peer-cert-file=${WORK_DIR}/ssl/server.pem \
--peer-key-file=${WORK_DIR}/ssl/server-key.pem \
--trusted-ca-file=${WORK_DIR}/ssl/ca.pem \
--peer-trusted-ca-file=${WORK_DIR}/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable etcd
systemctl restart etcd
进入卡住状态等待其他节点加入
[root@localhost k8s]# bash etcd.sh etcd01 192.168.153.10 etcd02=https://192.168.153.30:2380,etcd03=https://192.168.153.40:2380
打开一个新的终端,会发现etcd进程已经开启
[root@master1 ~]# ps -ef | grep etcd
[root@master1 ~]# scp -r /opt/etcd/ root@192.168.153.30:/opt/
[root@master1 ~]# scp -r /opt/etcd/ root@192.168.153.40:/opt/
[root@master1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.153.30:/usr/lib/systemd/system/
[root@master1 ~]# scp /usr/lib/systemd/system/etcd.service root@192.168.153.40:/usr/lib/systemd/system/
[root@node1 ~]# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd02" #修改数据库名称
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.153.30:2380" #修改节点IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.153.30:2379" #修改节点IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.153.30:2380" #修改节点IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.153.30:2379" #修改节点IP
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.153.10:2380,etcd02=https://192.168.153.30:2380,etcd03=https://192.168.153.40:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
[root@node1 ~]# systemctl stop firewalld.service
[root@node1 ~]# setenforce 0
[root@node1 ~]# systemctl start etcd.service
[root@node2 ~]# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd03" #修改数据库名称
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.153.40:2380" #修改节点IP
ETCD_LISTEN_CLIENT_URLS="https://192.168.153.40:2379" #修改节点IP
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.153.40:2380" #修改节点IP
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.153.40:2379" #修改节点IP
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.153.10:2380,etcd02=https://192.168.153.30:2380,etcd03=https://192.168.153.40:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
[root@node2 ~]# systemctl stop firewalld.service
[root@node2 ~]# setenforce 0
[root@node2 ~]# systemctl start etcd.service
[root@master1 ~]# systemctl start etcd
[root@master1 ~]# systemctl enable etcd
[root@master1 ~]# systemctl status etcd
检查群集状态(需要在有证书的目录下使用此命令)
[root@master1 ~]# cd /opt/etcd/ssl
[root@master1 /opt/etcd/ssl]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.153.10:2379,https://192.168.153.30:2379,https://192.168.153.40:2379" cluster-health