kubeadm ansible快速部署kubernetes生产级高可用集群
背景:
项目特性:
项目特性: 不受国内网络限制、所有组件使用
kubelet
托管、多master
高可用、tls
双向认证、自定义tls
证书有效期、RBAC
授权、支持Network Policy
相关支持:
类别 支持 Architecture amd64, arm64 OS Ubuntu 16.04+, Debian 9, CentOS 7.4+, CentOS 8, RedHat 7 Etcd 3.4.13-0 Container runtimes Docker, containerd Kubernetes v1.13, v1.14, v1.15, v1.16, v1.17, v1.18, v1.19, v1.20 Kube-apiserver lb slb, haproxy, envoy, openresty, nginx Network plugin flannel, calico Ingress controller traefik, nginx-ingress
安装须知:
硬件系统要求
Master节点:2c2g+
Worker节点:2c4g+
软件系统要求
运行以下之一的一台或多台机器:
RHEL 7
CentOS 7.4+
CentOS 8
Debian 9
Ubuntu 16.04+
各节点时区时间一致
墙裂建议升级内核到4.17+,关于Bug,你可以从以下地方追踪到:
https://github.com/fho/docker-samba-loop
https://github.com/moby/moby/issues/5618
https://github.com/coreos/bugs/issues/254
Kube-apiserver负载模式
本地负载均衡
该负载均衡模式是在节点本地部署一个负载均衡器,节点本地所有需要链接 apiserver 的组件均通过本地负载均衡器进行访问。
优点:兼容所有云;无额外的网络消耗(共用主机 network namespace);不会出现 lb 宕机而整个集群崩溃的情况。
缺点:集群外需要链接 apiserver 无法做到高可用(除非再搭建一套负载均衡);节点本地负载均衡器宕机则该节点无法正常工作;添加或删除 master 节点会涉及所有节点更新负载均衡器配置(当然不更新也是可以的)。
+------------------------+ +------------------------+
| master-A | | master-B |
+------------------------+ +------------------------+
| nginx +-------+--------> apiserver |
+-----^------+------^----+ | +-----------^------------+
| | | | | | | | |
+-----+----+ | +----+----+ | +---------+ | +----------+
|controller| | |scheduler| | |scheduler| | |controller|
+----------+ | +---------+ | +----+----+ | +-----+----+
| | | | | | | | |
+------------v-----------+ | +----v------+-------v----+
| apiserver <-------+--------+ nginx |
+------------------------+ | +------------------------+
|
>----------------------^----------------------<
| | |
+---------+----------+ +---------+----------+ +---------+----------+
| nginx | | nginx | | nginx |
+---^-----------^----+ +---^-----------^----+ +---^-----------^----+
| | | | | | | | | | | |
+---+---+ +-----+----+ +---+---+ +-----+----+ +---+---+ +-----+----+
|kubelet| |kube-proxy| |kubelet| |kube-proxy| |kubelet| |kube-proxy|
+-------+ +----------+ +-------+ +----------+ +-------+ +----------+
| | | | | |
+--------------------+ +--------------------+ +--------------------+
| node-A | | node-B | | node-C |
+--------------------+ +--------------------+ +--------------------+
VIP负载均衡
该负载均衡模式是在集群外搭建一个主备负载,虚拟IP(VIP)飘在这些节点上,当节点挂掉虚拟IP(VIP)会迅速转移到正常工作的节点上,该模式常见的组合即为:HAproxy + keepalived。
优点:集群内外链接 apiserver 均为高可用。
缺点:公有云无法使用;额外的网络消耗;所有node的网络I/O都会高度集中于一台机器上(VIP),一旦集群节点增多,pod增多,单机的网络I/O迟早是网络隐患;lb 宕机整个集群崩溃(当然这种情况很少)。
+----------------------+ +----------------------+
| master-A | | master-B |
+----------------------+ +----------------------+
| apiserver <---------+--------> apiserver |
+----------------------+ | +----------------------+
| | | | |
+----------+ +---------+ | +---------+ +----------+
|controller| |scheduler| | |scheduler| |controller|
+----+-----------+-----+ | +------+----------+----+
| | | | |
v------v-----------v >------------^------------< v----------v------v
| | | |
| +-------------------+-------------------------+------------------+ |
| |keepalived| |HAproxy| |HAprox | |keepalived| |
| +----------+ +-------+ +-------+ +----------+ |
| | | VIP | | |
| | LB-A | | LB-B | |
| +-----------------------+--------^--------+----------------------+ |
| | |
>---->----------->---------->------^----<----------<-----------<-----<
| | | | | |
+---+-----------+----+ +---+-----------+----+ +---+-----------+----+
|kubelet| |kube-proxy| |kubelet| |kube-proxy| |kubelet| |kube-proxy|
+-------+ +----------+ +-------+ +----------+ +-------+ +----------+
| | | | | |
+--------------------+ +--------------------+ +--------------------+
| node-A | | node-B | | node-C |
+--------------------+ +--------------------+ +--------------------+
集群安装
节点信息
ip | hostname | OS | kernel version | role |
---|---|---|---|---|
192.168.56.11 | node1 | CentOS 7.8 | 4.20.13-1 | master etcd worker |
192.168.56.12 | node2 | CentOS 7.8 | 4.20.13-1 | master etcd worker |
192.168.56.13 | node3 | CentOS 7.8 | 4.20.13-1 | master etcd worker |
192.168.56.14 | node4 | CentOS 7.8 | 4.20.13-1 | worker |
安装 Ansible 运行环境
# 任意节点上安装 Ansible 运行环境
sudo ansible/install.sh
集群规划
搭建集群后有以下两种“样式”显示,请自行选择:
样式一
NAME STATUS ROLES AGE VERSION
192.168.56.11 Ready control-plane,etcd,master,worker 7m25s v1.20.6
192.168.56.12 Ready control-plane,etcd,master,worker 5m18s v1.20.6
192.168.56.13 Ready control-plane,etcd,master,worker 5m18s v1.20.6
192.168.56.14 Ready worker 4m37s v1.20.6样式二
NAME STATUS ROLES AGE VERSION
node1 Ready control-plane,etcd,master,worker 7m25s v1.20.6
node2 Ready control-plane,etcd,master,worker 5m18s v1.20.6
node3 Ready control-plane,etcd,master,worker 5m18s v1.20.6
node4 Ready worker 4m37s v1.20.6对应的 ansible inventory 配置文件示例如下:
节点分配 样式一 样式二 单节点 hosts.allinone.ip hosts.allinone.hostname 单主多节点 hosts.s-master.ip hosts.s-master.hostname 多主多节点 hosts.m-master.ip hosts.m-master.hostname
部署集群
可选: 升级内核,默认安装集群是不会升级内核的,若需升级内核,请执行(内核升级完成后请手动重启所有节点):
ansible-playbook -i example/hosts.m-master.ip.ini 00-kernel.yml
一句命令拥有一个高可用 kubernetes 集群:
基本配置执行
ansible-playbook -i example/hosts.m-master.ip.ini 90-init-cluster.yml
高级配置执行,注意: 如果安装集群时使用高级配置则以后所有操作都需将
-e @example/variables.yaml
参数添加在ansible-playbook
命令中ansible-playbook -i example/hosts.m-master.ip.ini -e @example/variables.yaml 90-init-cluster.yml
本项目所有可配置项都在
example/variables.yaml
文件中体现,需自定义配置时删除配置项前注释符即可。若
example/hosts.m-master.ip.ini
文件中与example/variables.yaml
变量值冲突时,example/variables.yaml
文件中的变量值优先级最高。
其他功能请参考:
https://github.com/chinaboy007/kubeadm-ha/tree/master/docs