本文为k8s master节点 高可用基础章,即多master节点,均衡api-server 6443端口
一、环境:
192.168.0.47 master
192.168.0.33 backup
192.168.0.37 backup
vip :192.168.0.232
要求:一主两备,VIP自动漂移
二、Haproxy安装配置
1、Haproxy编译及安装
Haproxy安装配置步骤相对比较简单,跟其他源码软件安装方法大致相同,如下为Haproxy配置方法及步骤:
cd /usr/src
yum install kernel-devel -y
wget -c https://www.haproxy.org/download/2.3/src/haproxy-2.3.1.tar.gz
yum install wget gcc -y
tar -xzf haproxy-2.3.1.tar.gz
cd haproxy-2.3.1
make TARGET=linux310 PREFIX=/usr/local/haproxy/
make install PREFIX=/usr/local/haproxy
2、配置Haproxy服务
useradd -s /sbin/nologin haproxy -M
cd /usr/local/haproxy ;mkdir -p etc/
touch /usr/local/haproxy/etc/haproxy.cfg
cd /usr/local/haproxy/etc/
haproxy.cfg配置文件内容如下:
vim haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /usr/local/haproxy
stats socket /usr/local/haproxy/haproxy-admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
nbproc 1
defaults
log global
timeout connect 5000
timeout client 10m
timeout server 10m
listen admin_stats
bind 0.0.0.0:10080
mode http
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /status
stats realm welcome login\ Haproxy
stats auth admin:123456
stats hide-version
stats admin if TRUE
listen kube-master
bind 0.0.0.0:8443
mode tcp
option tcplog
balance source
server master1 192.168.0.47:6443 check inter 2000 fall 2 rise 2 weight 1
server master2 192.168.0.33:6443 check inter 2000 fall 2 rise 2 weight 1
server master3 192.168.0.37:6443 check inter 2000 fall 2 rise 2 weight 1
3、启动Haproxy服务
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
[root@localhost etc]# ps -ef|grep haproxy
haproxy 20329 1 0 14:32 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
root 20333 1614 0 14:33 pts/0 00:00:00 grep --color=auto haproxy
三、配置Keepalived服务
1、安装
cd /usr/src ;
yum install openssl-devel popt* -y
wget -c http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
tar -xzf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1 &&
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/
make && make install DIR=/usr/local/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
systemctl enable keepalived
2、配置文件keepalived.conf - master(上线抢vip) - 192.168.0.47
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
toyix@139.com
}
notification_email_from wgkgood@139.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/data/sh/check_haproxy.sh"
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.0.232
}
track_script {
chk_haproxy
}
}
3、backup 备用机 配置 192.168.0.33、192.168.0.37
! Configuration File for keepalived
global_defs {
notification_email {
toyix@139.com
}
notification_email_from wgkgood@139.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/data/sh/check_haproxy.sh"
interval 2
weight 2
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 151
priority 90
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.0.232
}
track_script {
chk_haproxy
}
}
4、创建haproxy检查脚本 ,
mkdir -p /data/sh/
touch /data/sh/check_haproxy.sh
chmod a+x /data/sh/check_haproxy.sh
脚本内容
#!/bin/bash
#auto check haprox process
#2021-1-12 jfedu.net
NUM=$(ps -ef|grep haproxy|grep -aivcE "grep|check")
if
[[ $NUM -eq 0 ]];then
systemctl stop keepalived.service
fi
启动:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
systemctl enable keepalived
systemctl start keepalived
四、防火墙配置
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.0/24" accept"
firewall-cmd --reload
五、测试 - 日志
master日志
[root@localhost etc]# systemctl restart keepalived && tailf /var/log/messages
Jun 22 16:48:34 localhost systemd: Stopped SYSV: Start and stop Keepalived.
Jun 22 17:01:01 localhost systemd: Started Session 2 of user root.
Jun 22 17:06:26 localhost systemd: Starting SYSV: Start and stop Keepalived...
Jun 22 17:06:26 localhost Keepalived: Starting Keepalived v1.2.1 (06/22,2021)
Jun 22 17:06:26 localhost keepalived: Starting keepalived: [ OK ]
Jun 22 17:06:26 localhost Keepalived: Starting VRRP child process, pid=6799
Jun 22 17:06:26 localhost Keepalived_vrrp: Registering Kernel netlink reflector
Jun 22 17:06:26 localhost Keepalived_vrrp: Registering Kernel netlink command channel
Jun 22 17:06:26 localhost Keepalived_vrrp: Registering gratutious ARP shared channel
Jun 22 17:06:26 localhost systemd: Started SYSV: Start and stop Keepalived.
Jun 22 17:06:26 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Jun 22 17:06:26 localhost Keepalived_vrrp: Configuration is using : 64750 Bytes
Jun 22 17:06:26 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Jun 22 17:06:26 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Jun 22 17:06:26 localhost Keepalived_vrrp: VRRP_Script(chk_haproxy) succeeded
Jun 22 17:06:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 22 17:06:31 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 22 17:06:31 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Jun 22 17:06:31 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens32 for 192.168.0.232
Jun 22 17:06:36 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens32 for 192.168.0.232
backup日志
[root@localhost ~]# systemctl restart keepalived && tailf /var/log/messages
Jun 22 17:06:24 localhost keepalived: Starting keepalived: [ OK ]
Jun 22 17:06:24 localhost Keepalived_vrrp: Registering Kernel netlink reflector
Jun 22 17:06:24 localhost Keepalived_vrrp: Registering Kernel netlink command channel
Jun 22 17:06:24 localhost Keepalived_vrrp: Registering gratutious ARP shared channel
Jun 22 17:06:24 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Jun 22 17:06:24 localhost Keepalived_vrrp: Configuration is using : 64748 Bytes
Jun 22 17:06:24 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Jun 22 17:06:24 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun 22 17:06:24 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Jun 22 17:06:24 localhost systemd: Started SYSV: Start and stop Keepalived.
Jun 22 17:06:24 localhost Keepalived_vrrp: VRRP_Script(chk_haproxy) succeeded
master 停止服务后,backup 升级为master日志
systemctl stop keepalived
Jun 22 17:07:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 22 17:07:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Jun 22 17:07:54 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun 22 17:08:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 22 17:08:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Jun 22 17:08:10 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Jun 22 17:08:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Jun 22 17:08:30 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Jun 22 17:08:30 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Jun 22 17:08:30 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens32 for 192.168.0.232
Jun 22 17:08:35 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on ens32 for 192.168.0.232
注:haproxy别忘记加开机启动,这里简单点,直接加rc.local
vim /etc/rc.local
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
chmod a+x /etc/rc.d/rc.local
--------------------end