xiguaqq20 发表于 2018-12-29 09:17:27

keepalived + lvs 高可用负载均衡

主分发器设置
  安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.116
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29
  编辑/etc/keepalived/keepalived.conf
写入如下内容:

# vim /etc/keepalived/keepalived.conf
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
virtual_server 192.168.130.100 80 {
#(每隔10秒查询realserver状态)
delay_loop 10
#(lvs 算法)
lb_algo wlc
#(DR模式)
lb_kind DR
#(同一IP的连接0秒内被分配到同一台realserver)
persistence_timeout 0
#(用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.130.118 80 {
#(权重)
weight 100
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.130.119 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
  定义监控脚本/usr/local/sbin/check_ng.sh

# vim /usr/local/sbin/check_ng.sh
  写入下面内容

#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0"]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
  更改/usr/local/sbin/check_ng.sh权限

# chmod 755 /usr/local/sbin/check_ng.sh
  编写脚本 /usr/local/sbin/lvs_dr.sh

# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2
  执行脚本 /usr/local/sbin/lvs_dr.sh

# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
  启动keepalived

# systemctl start keepalived
# ps aux |grep keepalived
root       21900.10.1 1207401404 ?      Ss   14:33   0:00 /usr/sbin/keepalived -D
root       21910.10.3 1274803336 ?      S    14:33   0:00 /usr/sbin/keepalived -D
root       21920.00.3 1317803108 ?      S    14:33   0:00 /usr/sbin/keepalived -D
root       22060.00.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived
  启动nginx

# /etc/init.d/nginx start
# ps aux |grep nginx
root      9120.00.1459881240 ?      Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      9130.00.4484764388 ?      S    08:06   0:00 nginx: worker process
nobody      9150.00.4484764392 ?      S    08:06   0:00 nginx: worker process
root       20720.00.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx
  把nginx的默认页做个标记

# echo master > /data/wwwroot/default/index.html
  在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.116
master
从分发器设置
  安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=fef8a686-6a1e-49bc-b03e-7797fc4c58bb
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.128
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29
  编辑/etc/keepalived/keepalived.conf
写入如下内容:

# vim /etc/keepalived/keepalived.conf
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
virtual_server 192.168.130.100 80 {
#(每隔10秒查询realserver状态)
delay_loop 10
#(lvs 算法)
lb_algo wlc
#(DR模式)
lb_kind DR
#(同一IP的连接0秒内被分配到同一台realserver)
persistence_timeout 0
#(用TCP协议检查realserver状态)
protocol TCP
real_server 192.168.130.118 80 {
#(权重)
weight 100
TCP_CHECK {
#(10秒无响应超时)
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.130.119 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
  定义监控脚本/usr/local/sbin/check_ng.sh

# vim /usr/local/sbin/check_ng.sh
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0"]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
  更改/usr/local/sbin/check_ng.sh权限

# chmod 755 /usr/local/sbin/check_ng.sh
  编写脚本 /usr/local/sbin/lvs_dr.sh

# vim /usr/local/sbin/lvs_dr.sh
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/usr/sbin/ipvsadm
vip=192.168.130.100
rs1=192.168.130.118
rs2=192.168.130.119
#注意这里的网卡名字
ifdown ens33
ifup ens33
ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev ens33:2
  执行脚本 /usr/local/sbin/lvs_dr.sh

# sh /usr/local/sbin/lvs_dr.sh
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
  启动keepalived

# systemctl start keepalived
# ps aux |grep keepalived
root       21900.10.1 1207401404 ?      Ss   14:33   0:00 /usr/sbin/keepalived -D
root       21910.10.3 1274803336 ?      S    14:33   0:00 /usr/sbin/keepalived -D
root       21920.00.3 1317803108 ?      S    14:33   0:00 /usr/sbin/keepalived -D
root       22060.00.0 112676   988 pts/0    S+   14:33   0:00 grep --color=auto keepalived
  启动nginx

# systemctl start nginx
# ps aux |grep nginx
root      9120.00.1459881240 ?      Ss   08:06   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody      9130.00.4484764388 ?      S    08:06   0:00 nginx: worker process
nobody      9150.00.4484764392 ?      S    08:06   0:00 nginx: worker process
root       20720.00.0 112680   980 pts/0    S+   10:23   0:00 grep --color=auto nginx
  把nginx的默认页做个标记

# echo backup > /usr/share/nginx/html/index.html
  在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.128
backup
rs1设置
  安装nginx
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.118
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29
  编写脚本/usr/local/sbin/lvs_rs.sh

# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  执行脚本/usr/local/sbin/lvs_rs.sh

# sh /usr/local/sbin/lvs_rs.sh
  启动nginx

# systemctl start nginx
# ps aux |grep nginx
root      9950.00.2 1229082236 ?      Ss   14:29   0:00 nginx: master process /usr/sbin/nginx
nginx       9960.00.3 1232923136 ?      S    14:29   0:00 nginx: worker process
nginx       9970.00.3 1232923136 ?      S    14:29   0:00 nginx: worker process
nginx       9980.00.3 1232923136 ?      S    14:29   0:00 nginx: worker process
nginx       9990.00.3 1232923136 ?      S    14:29   0:00 nginx: worker process
root       10060.00.0 112676   980 pts/0    R+   14:30   0:00 grep --color=auto nginx
  把nginx的默认页做个标记

# echo This is lynn-05 > /usr/share/nginx/html/index.html
  在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.118
This is lynn-05
rs2设置
  安装nginx
yum install -y nginx
关闭防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置网卡ens33

# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.130.119
NETMASK=255.255.255.0
GATEWAY=192.168.130.2
DNS1=119.29.29.29
  编写脚本/usr/local/sbin/lvs_rs.sh

# vim /usr/local/sbin/lvs_rs.sh
#/bin/bash
vip=192.168.130.100
#把vip绑定在lo上,是为了实现rs直接把结果返回给客户端
ifdown lo
ifup lo
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端
#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  执行脚本/usr/local/sbin/lvs_rs.sh

# sh /usr/local/sbin/lvs_rs.sh
  启动nginx

# systemctl start nginx
# ps aux |grep nginx
root      9900.00.2 1229082244 ?      Ss   14:30   0:00 nginx: master process /usr/sbin/nginx
nginx       9910.00.3 1232923144 ?      S    14:30   0:00 nginx: worker process
nginx       9920.00.3 1232923144 ?      S    14:30   0:00 nginx: worker process
nginx       9930.00.3 1232923144 ?      S    14:30   0:00 nginx: worker process
nginx       9940.00.3 1232923144 ?      S    14:30   0:00 nginx: worker process
root      9960.00.0 112676   980 pts/0    S+   14:30   0:00 grep --color=auto nginx
  把nginx的默认页做个标记

# echo This is lynn-07 > /usr/share/nginx/html/index.html
  在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.119
This is lynn-07
测试
  从下图可以看出当vip在主机上时可以实现负载均衡
http://i2.运维网.com/images/blog/201804/10/de4724bbdc4a860a037a4ac0b903e832.png
从下图可以看出当主机keepalived停止后vip就出现在从机上时也能可以实现负载均衡
http://i2.运维网.com/images/blog/201804/10/755678aaecdf1a4aa3b73c549be973d8.png
从下图可以看出当主机keepalived再次启动时vip又回到主机负载均衡还是可以实现
http://i2.运维网.com/images/blog/201804/10/63ec4ec03c6910c97bdad088e2f8ecfd.png
综上这里就同时实现了高可用和负载均衡



页: [1]
查看完整版本: keepalived + lvs 高可用负载均衡