使用kubeadm搭建高可用k8s v1.16.3集群
部署环境说明
本文通过kubeadm搭建一个高可用的k8s集群,kubeadm可以帮助我们快速的搭建k8s集群,高可用主要体现在对master节点组件及etcd存储的高可用,文中使用到的服务器ip及角色对应如下:
| 主机名称 | ip地址 | 角色 |
|---|---|---|
| - | 192.168.9.80 | 虚拟ip(vip) |
| k8s-master-01 | 192.168.9.81 | master |
| K8s-master-02 | 192.168.9.82 | master |
| K8s-master-03 | 192.168.9.83 | master |
| k8s-node-01 | 192.168.9.84 | node |
| K8s-node-02 | 192.168.9.85 | node |
| K8s-node-03 | 192.168.9.79 | node |
集群架构及部署准备工作
集群架构说明
前面提到高可用主要体现在master相关组件及etcd,master中apiserver是集群的入口,搭建三个master通过keepalived提供一个vip实现高可用,并且添加haproxy来为apiserver提供反向代理的作用,这样来自haproxy的所有请求都将轮询转发到后端的master节点上。如果仅仅使用keepalived,当集群正常工作时,所有流量还是会到具有vip的那台master上,因此加上了haproxy使整个集群的master都能参与进来,集群的健壮性更强。对应架构图如下所示:

修改hosts及hostname
所有节点修改主机名和hosts文件,文件内容如下
192.168.9.80 master.k8s.io k8s-vip
192.168.9.81 master01.k8s.io k8s-master-01
192.168.9.82 master02.k8s.io k8s-master-02
192.168.9.83 master03.k8s.io k8s-master-03
192.168.9.84 node01.k8s.io k8s-node-01
192.168.9.85 node02.k8s.io k8s-node-02
192.168.9.79 node03.k8s.io k8s-node-03
其他准备
所有节点操作
-
主机时间同步 时间同步可以通过
chrony或者ntp来实现,这里不再赘述 -
关闭防火墙 关闭
centos7自带的firewalld防火墙服务 -
关闭selinux
-
禁用swap
kubeadm会检查当前主机是否禁用了swap,如果启动了swap将导致安装不能正常进行,所以需要禁用所有的swap。
# 临时关闭
$ swapoff -a && sysctl -w vm.swappiness=0
# 永久关闭 ,在文件中添加注释
$ vim /etc/fstab
...
UUID=7bf41652-e6e9-415c-8dd9-e112641b220e /boot xfs defaults 0 0
#/dev/mapper/centos-swap swap swap defaults 0 0
# 或者利用sed命令完事儿
$ sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
- 设置系统其它参数
开启路由转发
$ vim /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
$ modprobe br_netfilter
$ sysctl -p /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
设置资源配置文件
$ echo "* soft nofile 65536" >> /etc/security/limits.conf
$ echo "* hard nofile 65536" >> /etc/security/limits.conf
$ echo "* soft nproc 65536" >> /etc/security/limits.conf
$ echo "* hard nproc 65536" >> /etc/security/limits.conf
$ echo "* soft memlock unlimited" >> /etc/security/limits.conf
$ echo "* hard memlock unlimited" >> /etc/security/limits.conf
- 安装相关包
$ yum install -y conntrack-tools libseccomp libtool-ltdl
部署keepalived
在三台master操作
安装
$ yum install -y keepalived
配置
默认的keepalived配置较复杂,这里用更为简明的方式进行配置,另外的两台master配置和上面类似,只需要修改对应的state配置为BACKUP,priority权重值不同即可,配置中的其他字段这里不做说明。
k8s-master-01的配置:
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id k8s
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 250
advert_int 1
authentication {
auth_type PASS
auth_pass ceb1b3ec013d66163d6ab
}
virtual_ipaddress {
192.168.9.80
}
track_script {
check_haproxy
}
}
EOF
k8s-master-02的配置:
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id k8s
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass ceb1b3ec013d66163d6ab
}
virtual_ipaddress {
192.168.9.80
}
track_script {
check_haproxy
}
}
EOF
k8s-master-03的配置:
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id k8s
}
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass ceb1b3ec013d66163d6ab
}
virtual_ipaddress {
192.168.9.80
}
track_script {
check_haproxy
}
}
EOF
启动和检查
在三台master节点都启动服务
# 设置开机启动
$ systemctl enable keepalived.service
# 启动keepalived
$ systemctl start keepalived.service
# 查看启动状态
$ systemctl status keepalived.service
启动后查看k8s-master-01的网卡信息
[root@k8s-master-01 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:84:45:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.9.81/24 brd 192.168.9.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.9.80/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe84:458a/64 scope link
valid_lft forever preferred_lft forever
尝试停掉k8s-master-01的keepalived服务,查看vip是否能漂移到其他的master,并且重新启动k8s-master-01的keepalived服务,查看vip是否能正常漂移回来,证明配置没有问题。
部署haproxy
在三台master操作
安装
$ yum install -y haproxy