lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(二) LVS+Keepalived

时间:2023-03-08 21:17:44

一、安装ipvs

sudo apt-get install ipvsadm

二、安装keepalived

sudo apt-get install keepalived

三、创建keepalived.conf文件

sudo gedit /etc/keepalived/keepalived.conf

四、配置keepalived.conf

  说明:VIP: 192.168.2.68

     real_server: 192.168.2.66(MASTER), 192.168.2.67(BACKUP)

# Global Configuration
global_defs {
lvs_id director1
} # VRRP Configuration
vrrp_instance LVS {       #定义虚拟路由,LVS 为虚拟路由的标示符,自己定义名称
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51    #虚拟路由的ID,而且这个ID也是虚拟MAC最后一段的来源,这个ID号一般不能大于255,且这个ID一定不能有冲突
priority 150         #初始优先级
advert_int 1         #通告的个数
authentication {      #认证机制
auth_type PASS     #认证类型
auth_pass 123456    #密码,应该为随机的字符串
} virtual_ipaddress {    #虚拟地址,即VIP
192.168.2.68
     #(如果有多个VIP,继续换行填写.) 
} # Virtual Server Configuration - for WWW service
virtual_server 192.168.2.68 {
delay_loop 1 #(每隔1秒查询realserver状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)  # Real Server configuration
real_server 192.168.2.67 {
weight 1 #权重
TCP_CHECK {
connection_timeout 10 #(10秒无响应超时)
nb_get_retry
delay_before_retry
}
} # Real Server configuration
real_server 192.168.2.66 {
weight
TCP_CHECK {
connection_timeout
nb_get_retry
delay_before_retry
}
}
}
}

  
  1.配置vrrp   ip为68(启动keepalived之后可使用ip addr可查看)

  lvs + keepalived + nginx + tomcat高可用负载反向代理服务器配置(二)  LVS+Keepalived

  2.配置real_server   此处是67和66的一个集群

五、启动并查看keepalived

#service keepalived start
查看 keepalived状态
# ps -ef | grep keepalived
root : ? :: keepalived -D
root : ? :: keepalived -D
root : pts/ :: grep keepalived
如果未能启动,请使用sudo启动或者检查配置文件 # tail -f /var/log/syslog
Feb :: localhost Keepalived_vrrp: Registering gratutious ARP shared channel
Feb :: localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Feb :: localhost Keepalived_vrrp: Configuration is using : Bytes
Feb :: localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Feb :: localhost Keepalived_vrrp: VRRP sockpool: [ifindex(), proto(), fd(,)]
Feb :: localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb :: localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Feb :: localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Feb :: localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.2.68
Feb :: localhost avahi-daemon[]: Registering new address record for 192.168.2.68 on eth0. 可以看到VIP已经在主服务器上开启

到此lvs+keepalived就已经完成了,接下来就是nginx.

Append: 使用Keepalived管理nginx

! Configuration File for keepalived

global_defs {
# notification_email {
# henryhe@starhubdev.com
# }
# notification_email_from happy@starhubdev.com
# smtp_server 127.0.0.1
# smtp_connect_timeout
router_id LVS_DEVEL
} vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval
weight
} vrrp_instance VI_1 {
state BACKUP
interface eno1
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass starhub
}
virtual_ipaddress {
172.31.34.92
}
track_script {
chk_nginx
}
} #virtual_server 172.31.34.92 {
# delay_loop
# lb_algo rr
# lb_kind DR
# nat_mask 255.255.255.248
# persistence_timeout
# protocol TCP
# sorry_server 172.31.34.94
# real_server 172.31.34.93 {
# weight
# notify_down /usr/local/bin/keepalived_notify.sh
# TCP_CHECK {
# connect_timeout
# nb_get_retry
# delay_before_retry
# connect_port
# }
# }
}

check_nginx.sh

!/bin/bash
status=$(ps -C nginx --no-heading|wc -l)
if [ "${status}" = "" ]; then
/bin/systemctl start nginx.service #redhat7
/etc/init.d/nginx start #centos
status2=$(ps -C nginx --no-heading|wc -l)
if [ "${status2}" = "" ]; then
/bin/systemctl stop keepalived.service #redhat7
/etc/init.d/keepalived start #centos
fi
fi