keepalived+haproxy安装(高可用)
keepalived是定一个没有机器再用的ip(也就是ping不通的地址),做虚拟ip,这个ip下面绑定几个真实node的ip,当请求这个虚拟ip的时候就会被转发到某个子ip机器上,来实现高可用。
Haproxy是类似于nginx的转发的软件。当你请求我这里一个端口,我把他转发到几个其他的机器的端口。
上面两个配合使用,keepalived定义一个虚拟ip,当请求过来后到某一个当选的节点,然后这个节点把请求再转发到这几个节点上,来实现高可用。
yum install -ykeepalived
vim /etc/keepalived/keepalived.conf
修改方法(参考文件在最下面):
-------------------------------------------------------------------
关键的是virtual_ipaddress 这里需要写一个虚拟ip地址 找一个你ping 不同的也就是不用的,建议在同一网段,也就是差距不大的比较靠近的如:我的节点是172.20.0.68,我找了一个这个范围靠后的172.20.0.251
易错点interface 目前ip地址所在的网口
Priority 权限依次排列不能超过250
State 一个MASTER 其他都是BACKUP
K8s的每个master节点都要部署
---------------------------------------------------------------
启动语句:
systemctl daemon-reload && systemctl enable keepalived && systemctl start keepalived && systemctl status keepalived
启动后,在master节点输入ip address 查看是否有172.20.0.251/32(注意只有一个节点能竞争到,其他的节点就没有,你可以重启该节点,你就会在别的节点ip address 下看到)
重启语句:
systemctldaemon-reload && systemctl enable keepalived && systemctlrestart keepalived && systemctl status keepalived
yum install -yhaproxy
vim/etc/haproxy/haproxy.cfg
参考修改:
-------------------------------------------------------------
bind 后面的填入代表了匹配所有发送到该端口的数据包 *:19900 但是我们需要它来转发虚拟ip的数据包时,发往真实地址的数据包就不适合被接受所以需要写成172.20.0.251:19900 但是这样需要添加一个配置 让节点忽略不需要的数据包
cat < /etc/sysctl.d/k8s.conf
net.ipv4.ip_nonlocal_bind = 1
EOF
sysctl --system
易错点
Frontend下面的default_backend 需要和backend 一一对应同名
我这里配置了两个端口转发规则,一般要一个16443就好,19900不需要
对应的6443是自己的master节点
K8s的每个master节点都要部署
------------------------------------------------------------
启动语句:
systemctl daemon-reload && systemctl enable haproxy && systemctl start haproxy && systemctl status haproxy
重启语句
systemctl daemon-reload && systemctl enable haproxy && systemctl restart haproxy && systemctl status haproxy
keepalived.conf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Configuration File for keepalived
global_defs {
router_id LVS_k8s
}
vrrp_script CheckK8sMaster {
script "killall -0 haproxy"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface macvlan
virtual_router_id 51
# 主节点权重最高 依次减少
priority 150
advert_int 1
#修改为本地IP
#mcast_src_ip 172.20.0.68
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.20.0.251
}
track_script {
CheckK8sMaster
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
haproxy.cfg
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# kubernetes apiserver frontend which proxys to the backend
#---------------------------------------------------------------------
frontend kubernetes-apiserver
mode tcp
bind 172.20.0.251:16443
option tcplog
default_backend kubernetes-apiserver
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend kubernetes-apiserver
mode tcp
balance roundrobin
server sz-pg-oam-k8snode-010 172.20.0.71:6443 check
# server sz-pg-oam-k8snode-009 172.20.0.70:6443 check
server sz-pg-oam-k8snode-007 172.20.0.68:6443 check
#---------------------------------------------------------------------
# kubernetes apiserver frontend which proxys to the backends
#---------------------------------------------------------------------
frontend ipallocator-server
mode tcp
bind *:19900
option tcplog
default_backend ipallocator-server
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend ipallocator-server
mode tcp
balance roundrobin
server k8snode-010 172.20.0.71:9900 check
# server k8snode-009 172.20.0.70:9900 check
server k8snode-007 172.20.0.68:9900 check
#---------------------------------------------------------------------
# collection haproxy statistics message
#---------------------------------------------------------------------
listen stats
bind *:1080
stats auth admin:awesomePassword
stats refresh 5s
stats realm HAProxy\ Statistics
stats uri /admin?statseck