注意:本人使用二进制包来搭建k8s集群,比较耗费CPU内存,笔记本电脑的运行内存至少是32G,后期我会更新搭建多master节点的 高可用 k8s集群博客,大家可以关注我的博客后续。
搭建k8s集群所使用的安装包:(这是我是用的安装包版本)
搭建节点服务器:(三个节点)
服务器 | 需要安装的软件 |
---|---|
master(192.168.100.128) | kube-apiserver、kube-controller-manager、kube-scheduler、etcd |
node01(192.168.100.131) | kubelet、kube-proxy、docker 、flannel 、etcd |
node02(192.168.100.136) | kubelet、kube-proxy、docker 、flannel 、etcd |
1、每个虚拟机配置相对应的静态IP地址
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl stop NetworkManager
systemctl enable NetworkManager
service network restart #重启网络
ping www.baidu.com #要实现网络通信
3、防火墙不要关闭。
systemctl start firewalld #开启防火墙
iptables -F #清空防火墙规则
setenforce 0 #关闭核心防护
master节点:
[root@localhost ~]# mkdir k8s
[root@localhost ~]# cd k8s/
//编写cfssl.sh脚本,从官网下载制作证书的工具cfssl,直接放在/usr/local/bin目录下,方便系统识别,最后给工具加执行权限
[root@localhost k8s]# vi 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 #执行脚本等待安装下载软件
[root@localhost k8s]# ls /usr/local/bin/ #可以看到三个制作证书的工具
cfssl cfssl-certinfo cfssljson
#cfssl:生成证书工具
#cfssl-certinfo:查看证书信息
#cfssljson:通过传入json文件生成证书
[root@localhost k8s]# mkdir etcd-cert
[root@localhost k8s]# cd etcd-cert/
1、创建生成ca证书的配置文件
[root@localhost etcd-cert]# cat > ca-config.json <
2、创建ca证书的签名证书
[root@localhost etcd-cert]# cat > ca-csr.json <
3、用ca签名证书生成ca证书,得到ca-key.pem ca.pem
[root@localhost etcd-cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
4、指定etcd三个节点之间的通信验证—需要服务器签名证书 server-csr.json
[root@localhost etcd-cert]# cat > server-csr.json <
5、用ca-key.pem、ca.pem、服务器签名证书 生成ETCD证书 ----server-key.pem、server.pem
[root@localhost etcd-cert]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server
上传一个生成ETCD配置文件的脚本etcd.sh到 /root/k8s 目录下,脚本内容如下:
[root@localhost k8s]# vi /root/k8s/etcd.sh
#!/bin/bash
# example: ./etcd.sh etcd01 192.168.100.128 etcd02=https://192.168.100.131:2380,etcd03=https://192.168.100.136:2380
ETCD_NAME=$1
ETCD_IP=$2
ETCD_CLUSTER=$3
WORK_DIR=/opt/etcd
#创建节点的配置文件模板
cat <$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 </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
把下载好的三个软件上传到k8s目录下
先解压 etcd软件包到当前目录下,再创建etcd集群的工作目录
[root@localhost k8s]# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
[root@localhost k8s]# ls etcd-v3.3.10-linux-amd64
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
#稍后使用源码包中的etcd、etcdctl 应用程序命令
[root@localhost k8s]# mkdir -p /opt/etcd/{cfg,bin,ssl}
[root@localhost k8s]# ls /opt/etcd/
bin cfg ssl
1、把etcd、etcdctl 执行文件放在/opt/etcd/bin/
[root@localhost k8s]# mv etcd-v3.3.10-linux-amd64/etcd etcd-v3.3.10-linux-amd64/etcdctl /opt/etcd/bin/
[root@localhost k8s]# cp etcd-cert/*.pem /opt/etcd/ssl/
[root@localhost k8s]# ls /opt/etcd/ssl/
ca-key.pem ca.pem server-key.pem server.pem
执行 etcd.sh 脚本产生etcd集群的配置脚本和服务启动脚本,进入卡住状态等待其他节点加入
#注意:修改成自己的ip地址
[root@localhost k8s]# bash etcd.sh etcd01 192.168.100.128 etcd02=https://192.168.100.131:2380,etcd03=https://192.168.100.136:2380
//使用另外一个会话窗口,会发现etcd进程己经开启
[root@localhost k8s]# ps -ef | grep etcd
1、在master节点上拷贝证书去其他node节点
[root@localhost k8s]# scp -r /opt/etcd/ [email protected]:/opt/
[root@localhost k8s]# scp -r /opt/etcd/ [email protected]:/opt/
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
[email protected]'s password:
etcd.service 100% 923 105.2KB/s 00:00
[root@localhost k8s]# scp /usr/lib/systemd/system/etcd.service [email protected]:/usr/lib/systemd/system/
[email protected]'s password:
etcd.service 100% 923 830.1KB/s 00:00
[root@localhost k8s]#
3、在node01 节点上修改配置文件
[root@localhost ~]# vi /opt/etcd/cfg/etcd
4、在node02 节点上修改配置文件
[root@localhost ~]# vi /opt/etcd/cfg/etcd
[root@localhost k8s]# bash etcd.sh etcd01 192.168.100.128 etcd02=https://192.168.100.131:2380,etcd03=https://192.168.100.136:2380
6、同时快速启动 node01、node02节点
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# systemctl status etcd
在master节点上执行,注意:要在ca.pem文件的路径/opt/etcd/ssl/下执行检查集群的命令
[root@localhost k8s]# cd /opt/etcd/ssl/
[root@localhost ssl]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.100.128:2379,https://192.168.100.131:2379,https://192.168.100.136:2379" cluster-health
1、在master节点上,将分配的子网段写入到ETCD中,供flannel使用
注意:必须在证书存放的路径/root/k8s/etcd-cert下执行此命令。
[root@localhost etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.100.128:2379,https://192.168.100.131:2379,https://192.168.100.136:2379" set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'
[root@localhost etcd-cert]# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.100.128:2379,https://192.168.100.131:2379,https://192.168.100.136:2379" get /coreos.com/network/config
{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}
2、两个node节点:上传软件包flannel并解压到宿主目录下 。
[root@localhost ~]# tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
[root@localhost ~]# mkdir -p /opt/kubernetes/{cfg,bin,ssl}
[root@localhost ~]# mv mk-docker-opts.sh flanneld /opt/kubernetes/bin/
[root@localhost ~]# ls /opt/kubernetes/bin/
//脚本内容:
[root@localhost ~]# vi flannel.sh
#!/bin/bash
ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}
cat </opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat </usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
[root@localhost ~]# bash flannel.sh https://192.168.100.128:2379,https://192.168.100.131:2379,https://192.168.100.136:2379
[root@localhost ~]# systemctl status flanneld
两个node节点:修改docker的配置文件
[root@localhost ~]# vi /usr/lib/systemd/system/docker.service
//修改添加两处:
EnvironmentFile=/run/flannel/subnet.env
$DOCKER_NETWORK_OPTIONS
[root@localhost ~]# cat /run/flannel/subnet.env
可以看到node1节点是172.17.14.0网段,node2节点是172.17.32.0网段。
重启docker服务
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
1、两个node节点分别创建并自动进入centos:7容器。
[root@localhost ~]# docker run -it centos:7 /bin/bash
Unable to find image 'centos:7' locally
7: Pulling from library/centos
ab5ef0e58194: Pull complete
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
[root@690ec8bdaa81 /]# yum install -y net-tools #安装后可以使用ifconfig命令