kubernetes是Google在2014年开源的一个容器群集管理系统,kubernetes简称k8s
master 组件
Node组件
Pod
Controllers
Service
组件 | 使用的证书 |
---|---|
etcd | ca.pem,server.pem,server-key.pem |
flannel | ca.pem,server.pem,server-key.pem |
kube-apiserver | ca.pem,server.pem,server-key.pem |
kubelet | ca.pem,ca-key.pem |
kube-proxy | ca.pem,kube-proxy.pem,kube-proxy-key.pem |
kubectl | ca.pem,admin.pem,admin-key.pem |
生产环境中使用kubeadm和二进制安装的比较多
实现环境:
Master:192.168.223.10/24 kube-apiserver kube-controller-manager kube-scheduler etcd
Node01:192.168.223.20/24 kubelet kube-proxy docker flannel etcd
Node02:192.168.223.30/24 kubelet kube-proxy docker flannel etcd
master操作:
[root@localhost ~]# mkdir k8s
[root@localhost ~]# cd k8s/
[root@localhost k8s]# ls #从宿主机拖进来
etcd-cert.sh etcd.sh
[root@localhost k8s]# mkdir etcd-cert
[root@localhost k8s]# mv etcd-cert.sh etcd-cert
下载证书制作工具:
[root@localhost 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@localhost k8s]# bash cfssl.sh #下载cfssl官方包
如果有证书制作工具的
[root@localhost k8s]# cd /usr/local/bin/ #宿主机直接拖进此目录
cfssl cfssl-certinfo cfssljson
[root@localhost k8s]# chmod + /usr/local/bin/*
开始制作证书:
cfssl 生成证书工具 cfssljson通过传入json文件生成证书 cfssl-certinfo查看证书信息
[root@localhost k8s]# cd etcd-cert/
cat > ca-config.json <<EOF #定义ca证书
{
"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-key.pem ca.pem
cat > server-csr.json <<EOF #指定etcd三个节点之间的通信验证
{
"CN": "etcd",
"hosts": [
"192.168.241.20",
"192.168.241.4",
"192.168.241.3"
],
"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 #生成ETCD证书 server-key.pem server.pem
源码安装etcd:
[root@localhost k8s]# ls
etcd-cert etcd.sh etcd-v3.3.10-linux-amd64.tar.gz
[root@localhost k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
etcd-cert etcd.sh etcd-v3.3.10-linux-amd64 etcd-v3.3.10-linux-amd64.tar.gz
[root@localhost k8s]# mkdir /opt/etcd/{
cfg,bin,ssl} -p #配置文件,命令文件,证书
[root@localhost k8s]# cd etcd-v3.3.10-linux-amd64/
[root@localhost etcd-v3.3.10-linux-amd64]# ls
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
[root@localhost etcd-v3.3.10-linux-amd64]# mv etcd etcdctl /opt/etcd/bin/
[root@localhost k8s]# cd etcd-cert/
[root@localhost etcd-cert]# ls
ca-config.json ca.csr ca-csr.json ca-key.pem ca.pem etcd-cert.sh server.csr server-csr.json server-key.pem server.pem
[root@localhost etcd-cert]# mv *.pem /opt/etcd/ssl #证书拷贝
[root@localhost k8s]# bash etcd.sh etcd01 192.168.223.10 etcd02=https://192.168.223.20:2380,etcd03=https://192.168.223.30:2380 #进入卡住状态等待其他节点加入
[root@localhost ~]# ps -ef | grep etcd #使用另外一个会话打开,会发现etcd进程已经开启
将证书和启动脚本推送/复制到两台node节点中
[root@localhost k8s]# scp -r /opt/etcd/ root@192.168.223.20:/opt/ #拷贝证书去其他节点
[root@localhost k8s]# scp -r /opt/etcd/ root@192.168.223.30:/opt #拷贝证书去其他节点
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service root@192.168.223.20:/usr/lib/systemd/system/ #启动脚本拷贝其他节点
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service root@192.168.223.30:/usr/lib/systemd/system/ #启动脚本拷贝其他节点
在node01和node02节点修改
[root@localhost ~]# vim /opt/etcd/cfg/etcd
#[Member]
ETCD_NAME="etcd02"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://192.168.223.20:2380" #ip地址修改对应的node节点
ETCD_LISTEN_CLIENT_URLS="https://192.168.223.20:2379" #ip地址修改对应的node节点
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.223.30:2380" #ip地址修改对应的node节点
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.223.30:2379" #ip地址修改对应的node节点
ETCD_INITIAL_CLUSTER="etcd01=https://192.168.241.20:2380,etcd02=https://192.168.241.4:2380,etcd03=https://192.168.241.3:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
[root@localhost ssl]# systemctl start etcd
[root@localhost ssl]# systemctl enable etcd
[root@localhost ssl]# systemctl status etcd
在Master检查集群状态
检查群集状态(需要在有证书的目录下使用此命令)
[root@localhost ~]# cd /opt/etcd/ssl
[root@localhost ~]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.223:10:2379,https://192.168.223.20:2379,https://192.168.223.30:2379" cluster-health