二进制部署单Master群集
一、ETCD群集部署
配置环境:
服务器 | IP | 服务 |
---|---|---|
Master | 20.0.0.20/24 | kube-apiserver kube-controller-manager kube-scheduler etcd |
Node01 | 20.0.0.21/24 | kubelet kube-proxy docker flannel(网络组件) etcd |
Node02 | 20.0.0.22/24 | kubelet kube-proxy docker flannel(网络组件) etcd |
1.1 master 操作
[root@localhost ~]# mkdir k8s
[root@localhost ~]# cd k8s/
[root@localhost k8s]# ls //从宿主机拖进来
etcd-cert.sh etcd.sh //etcd证书创建的脚本、etcd服务脚本(包含配置文件,启动脚本)
[root@localhost k8s]# mkdir etcd-cert //创建一个证书目录,最后把证书都放在这个目录下
[root@localhost k8s]# mv etcd-cert.sh etcd-cert //把etcd证书脚本放在etcd目录下
kubernetes系统各组件需要使用TLS(SSL)证书对通信进行加密,本文档使用CloudFlare的PKI(公共密钥)工具集cfssl 来生成Certificate Authority (CA) 证书和秘钥文件,CA是自签名的证书,用来签名后续创建的其它TLS证书。
[root@localhost k8s]# vim cfssl.sh //创建一个cfssl工具(cfssl:做证书的工具)。下面就是从指定网站下载相对应的工具,四行。
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
//下载cfssl官方包
[root@localhost k8s]# bash cfssl.sh //执行脚本,开始下载
[root@master ~]# ls /usr/local/bin/ //查看一下,下载的工具
cfssl cfssl-certinfo cfssljson
//cfssl生成证书工具 cfssljson通过传入json文件生成证书 cfssl-certinfo查看证书信息
1.2 创建CA匹配文件
[root@master ~]#cd /root/k8s/etcd-cert
//定义ca证书,到了这个目录下,直接复制下面信息执行(创建CA匹配文件)
cat > ca-config.json <<EOF
{
"signing": {
"default": {
"expiry": "87600h"
},
"profiles": {
"www": {
"expiry": "87600h",
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
]
}
}
}
}
EOF
1.3 创建CA证书签名
//实现证书签名(创建CA证书签名请求)
cat > ca-csr.json <<EOF
{
"CN": "etcd CA",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
1.4 生成CA证书和私钥
//生成CA证书和私钥,ca-key.pem(CA私钥) ca.pem (CA证书)
cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
2020/11/18 06:28:09 [INFO] generating a new CA key and certificate from CSR
2020/11/18 06:28:09 [INFO] generate received request
2020/11/18 06:28:09 [INFO] received CSR
2020/11/18 06:28:09 [INFO] generating key: rsa-2048
2020/11/18 06:28:09 [INFO] encoded CSR
2020/11/18 06:28:09 [INFO] signed certificate with serial number 661373536924470066534166565896161497791686693383
1.5 指定etcd三个节点之间的通信验证
(群集内部的验证)
cat > server-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"20.0.0.20",
"20.0.0.21",
"20.0.0.22"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "BeiJing",
"ST": "BeiJing"
}
]
}
EOF
#### 1.6 生成 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
2020/11/18 06:44:48 [INFO] generate received request
2020/11/18 06:44:48 [INFO] received CSR
2020/11/18 06:44:48 [INFO] generating key: rsa-2048
2020/11/18 06:44:48 [INFO] encoded CSR
2020/11/18 06:44:48 [INFO] signed certificate with serial number 724848866698552383119616458595891001569559257796
2020/11/18 06:44:48 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
1.6 ETCD 二进制包下载地址
https://github.com/etcd-io/etcd/releases
1.7 解压、部署
我有这三个包,那就直接将三个包复制到k8s目录中
[root@master etcd-cert]# cd /root/k8s //退到k8s目录下
[root@master k8s]# ls //查看一下
cfssl.sh etcd.sh flannel-v0.10.0-linux-amd64.tar.gz
etcd-cert etcd-v3.3.10-linux-amd64.tar.gz kubernetes-server-linux-amd64.tar.gz
[root@master k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz //解压etcd
[root@master etcd-v3.3.10-linux-amd64]# cd etcd-v3.3.10-linux-amd64/
[root@master etcd-v3.3.10-linux-amd64]# ls //会有etcd etcdctl这两个文件,为后面的实现群集连接提供有效的作用
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
[root@master k8s]# mkdir -p /opt/etcd/{cfg,bin,ssl} //建立存放etcd配置文件、命令文件(二进制)、证书目录
[root@master k8s]# ls /opt/etcd/ //查看一下
bin cfg ssl
[root@master k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin/ //把两个脚本文件拷贝到bin目录下进行管理
//证书拷贝
[root@master k8s]# cd etcd-cert/
[root@master etcd-cert]# cp *.pem /opt/etcd/ssl/
1.8 进入卡住状态等待其他节点加入
[root@master ~]# cd /root/k8s/
[root@master k8s]# bash etcd.sh etcd01 20.0.0.20 etcd02=https://20.0.0.21:2380,etcd03=https://20.0.0.22:2380 注意:如果长时间没搜索到节点就会报错退出,不是配置出错了!!!
//需要重新开一个窗口
[root@master ~]# ps aux | grep etcd //然后检查一下是否启动
1.9 拷贝证书去其他节点
[root@master k8s]# scp -r /opt/etcd/ root@20.0.0.21:/opt/ //拷贝到node1节点下的/opt目录
yes,输入密码
[root@master k8s]# scp -r /opt/etcd/ root@20.0.0.22:/opt/ //拷贝到node2节点下的/opt目录
yes,输入密码
//启动脚本拷贝到其他节点
[root@master cfg]# scp /usr/lib/systemd/system/etcd.service root@20.0.0.21:/usr/lib/systemd/system/
yes,输入密码
[root@master cfg]# scp /usr/lib/systemd/system/etcd.service root@20.0.0.22:/usr/lib/systemd/system/
yes,输入密码
1.10 在node1节点修改
[root@node1 ~]# cd /opt/etcd/cfg/
[root@node1 cfg]# vim etcd
把2行改成etcd02,4、5、8、9行ip地址改成节点本地的
//在node2节点修改
[root@node2 ~]# cd /opt/etcd/cfg/
[root@node2 cfg]# vim etcd
把2行改成etcd03,4、5、8、9行ip地址改成节点本地的
//在master在开启刚刚的监控,因为已经退出了
[root@master k8s]# bash etcd.sh etcd01 20.0.0.20 etcd02=https://20.0.0.21:2380,etcd03=https://20.0.0.22:2380
注意:要在k8s目录下
//启动node1、node2节点etcd
[root@node1 cfg]# systemctl start etcd
//master就会退出刚刚的搜索,因为已经同步成功了!
1.11 检查群集状态,在master上
[root@master ~]# cd /root/k8s/etcd-cert/ //要用到证书,证书都在这里,所以要先到证书目录下
[root@master etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://20.0.0.20:2379,https://20.0.0.21:2379,https://20.0.0.22:2379" cluster-health
member 9525206b892f7ae is healthy: got healthy result from https://20.0.0.22:2379
member 1185e885480ea0f3 is healthy: got healthy result from https://20.0.0.21:2379
member e3197fd6a5933614 is healthy: got healthy result from https://20.0.0.20:2379
cluster is healthy
etcd但master群集就构建成功了!!!