Note: This experiment for the two-node Nginx two Apache server to provide load balancing, this article is not to do LVs, so realserver is not configured in keepalived.conf but in the Nginx configuration file upstream.
Issues to consider in this architecture:
1) master does not hang, then Master occupies VIP and Nginx runs on master
2) master hangs up, then backup preemption VIP and runs Nginx service on backup
3) If the Nginx service on the master server is hung, the VIP resource is transferred to the backup server
4) Check the health status of the backend server
Master and backup on both sides of the Nginx service, regardless of master or backup, when one of the keepalived service stopped, the VIP will drift to the keepalived service is still on the node, if you want to make nginx service hanging, VIP also drift to another node, you must use the script or in the configuration file with the shell command to control.
The configuration steps are as follows
1. Initialize 4 test server, the off
[Email protected] ~]# vim/etc/hosts192.168.1.200 ng-vip192.168.1.101 ng-master192.168.1.102 ng-slave192.168.1.161 web1192.168.1.162 web2[[email protected] ~]# yum clean all[[email protected] ~]# Systemctl stop Firewalld.service[[email protected] ~]# systemctl disable firewalld.service[[email protected] ~]# sed-i "s /selinux=enforcing/selinux=disabled/g "/etc/selinux/config
2. Configure WEB1,WEB2 Apache service, two identical methods
[[email protected] ~]# yum-y install httpd[[email protected] ~]# systemctl start httpd[[email protected] ~]# systemctl en Able httpdln-s '/usr/lib/systemd/system/httpd.service '/etc/systemd/system/multiuser.target.wants/httpd.service ' [[email protected] ~]# Cat/var/www/html/index.htmlhello this lvs-web1[[email protected] ~]# yum-y install Httpd[[email p Rotected] ~]# systemctl start httpd[[email protected] ~]# systemctl enable Httpdln-s '/usr/lib/systemd/system/httpd.serv Ice '/etc/systemd/system/multiuser.target.wants/httpd.service ' [[email protected] ~]# cat/var/www/html/ Index.htmlhello this LVS-WEB2
3. Install the Nginx node through the Yum installation, two identical methods
[[email protected] ~]# Vim/etc/yum.repos.d/nginx.repo[nginx]name=nginx repobaseurl=http://nginx.org/packages /centos/7/x86_64/gpgcheck=0enabled=1[[email protected] ~]# yum clean all[[email protected] ~]# yum-y Install nginx[[email protected] ~]# vim/usr/share/nginx/html/index.html 4. Install keepalived on the primary nginx server and configure the Nginx service Health detection script
[[email protected] conf.d]# yum-y install keepalived[[email protected] conf.d]# cd/etc/keepalived/[[email& Nbsp;protected] keepalived]# CP keepalived.conf keepalived.conf.1[[email protected] keepalived]# vim keepalived.confglobal_defs {notification_email {[email protected]} Notification_email_from [email p Rotected] Smtp_server smtp.mail.com smtp_connect_timeout router_id ha_master1 #表示运行keepalived服务器的一个标识, e-mail message is displayed in the main The information in the}vrrp_script chk_http_port {script "/usr/local/keepalived/nginx.sh" # # # #检测nginx状态的脚本链接interval 2weight 2}vrrp_ Instance vi_2 {#vrrp实例 state MASTER #MASTER/backup Interface eno16777736 # # #HA monitoring network Interface Virtual_router_i D #虚拟路由标识, is a number, the same VRRP instance uses a unique identity, master and backup to be the same priority #用于主从模式, the precedence of the master is higher than 100, from less than Advert_int 1 #主备之间的通告间隔秒数 Authentication {#认证用于主从模式, Mater and Backup Configuration Auth_type PASS # # # #主备切换时的验证 Auth_pass 1111 #密码}track_script {chK_http_port # # # Execution Monitoring service} virtual_ipaddress {192.168.1.200/24 dev eno16777736 label eno16777736:1 ########## #虚拟ip }}[[email protected] keepalived]# mkdir-p/usr/local/keepalived[[email protected] keepalived]# vim/usr/ Local/keepalived/nginx.sh#!/bin/bashif [' ps-c nginx--no-header |wc-l '-eq 0];thenkillall KEEPALIVEDFI[[EMAIL&NBSP;PR Otected] keepalived]# chmod 755/usr/local/keepalived/nginx.sh[[email protected] keepalived]# systemctl start Keepalived[[email protected] keepalived]# ifconfig-aeno16777736:flags=4163<up,broadcast,running, Multicast> MTU inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255 Inet6 fe80::20c:2 9FF:FEFE:6F3 prefixlen ScopeID 0x20<link> ether 00:0c:29:fe:06:f3 txqueuelen (Ethernet) eno1677773 6:1: flags=4163<up,broadcast,running,multicast> MTU inet 192.168.1.200 netmask 255.255.255.0 broadcas T 0.0.0.0 ether 00:0c:29:fe:06:f3 Txqueuelen 10XX (Ethernet) lo:flags=73<up,loopback,running> MTU 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 Prefixlen ScopeID 0x10 5. Install keepalived on the standby server and configure the Nginx service Health Check script, which is slightly different from the master
[[email protected] conf.d]# yum-y install keepalived[[email protected] conf.d]# cd/etc/keepalived/[[email& Nbsp;protected] keepalived]# CP keepalived.conf keepalived.conf.1[[email protected] keepalived]# vim keepalived.confglobal_defs {notification_email {[email protected]} Notification_email_from [email p Rotected] Smtp_server smtp.mail.com smtp_connect_timeout router_id ha_master1 #表示运行keepalived服务器的一个标识, e-mail message is displayed in the main The information in the}vrrp_script chk_http_port {script "/usr/local/keepalived/nginx.sh" # # # #检测nginx状态的脚本链接interval 2weight 2}vrrp_ Instance vi_2 {#vrrp实例 state BACKUP #MASTER/backup Interface eno16777736 # # #HA monitoring network Interface Virtual_router_i D #虚拟路由标识, is a number, the same VRRP instance using a unique identity, master and backup to be the same priority #用于主从模式, the precedence of the master is higher than 100, from less than Advert_int 1 #主备之间的通告间隔秒数 Authentication {#认证用于主从模式, Mater and Backup Configuration Auth_type PASS # # # #主备切换时的验证 Auth_pass 1111 #密码}track_script {chk_http_port # # # Execution Monitoring service} virtual_ipaddress {192.168.1.200/24 dev eno16777736 label eno16777736:1 ########## #虚拟ip} }[[email protected] keepalived]# mkdir-p/usr/local/keepalived[[email protected] keepalived]# vim/usr/ Local/keepalived/nginx.sh#!/bin/bashif [' ps-c nginx--no-header |wc-l '-eq 0];thenkillall KEEPALIVEDFI[[EMAIL&NBSP;PR Otected] keepalived]# chmod 755/usr/local/keepalived/nginx.sh[[email protected] keepalived]# systemctl start Keepalived[[email protected] keepalived]# ifconfig-aeno16777736:flags=4163<up,broadcast,running, Multicast> MTU inet 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255 Inet6 fe80::20c:2 9ff:fe87:fd0e prefixlen ScopeID 0x20<link> ether 00:0c:29:87:fd:0e txqueuelen (Ethernet) lo:flags =73<up,loopback,running> MTU 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 Prefixlen Scopei D 0x106. Test: Access the test http://192.168.1.200/through the browser to discover that traffic jumps between Web1 and web2.
6.1 Test closes the keepalived server on the main Nginx node, and the bundled VIP disappears at the primary node
[[email protected] keepalived]# systemctl stop keepalived.service [[email protected] keepalived]# ifconfig- Aeno16777736:flags=4163<up,broadcast,running,multicast> MTU inet 192.168.1.101 netmask 255.255.255.0 Broadcast 192.168.1.255 Inet6 fe80::20c:29ff:fefe:6f3 prefixlen-ScopeID 0x20<link> ether 00:0c:2 9:fe:06:f3 Txqueuelen (Ethernet) lo:flags=73<up,loopback,running> MTU 65536 inet 127.0.0.1 netmask 2 55.0.0.0 Inet6:: 1 prefixlen scopeid 0x106.2 Start the Keepalived service again for the master node and discover that the VIP is re-drifting to the master node
[Email protected] keepalived]# systemctl start Keepalived.service [[email protected] keepalived]# ifconfig- Aeno16777736:flags=4163<up,broadcast,running,multicast> MTU inet 192.168.1.101 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::20c:29ff:fefe:6f3 prefixlen- ScopeID 0x20< link> ether 00:0c:29:fe:06:f3 txqueuelen (Ethernet) eno16777736:1: Flags=4163<up,broadcast ,running,multicast> MTU inet 192.168.1.200 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:fe:06:f3 txqueuelen (Ethernet) Access test http://192.168.1.200/through the browser, It is found that traffic is still jumping between web1 and web2.
6.3 The Nginx service on the Nginx master node is closed, the VIP disappears from the primary node, the Keepalived service shuts down, and the VIP appears on the standby node.
[Email protected] keepalived]# systemctl stop nginx.service [[email protected] keepalived]# Ifconfig-aeno16777736:flag S=4163<up,broadcast,running,multicast> MTU inet 192.168.1.101 netmask 255.255.255.0 Broadcast 192.168.1.255 inet6 fe80::20c:29ff:fefe:6f3 prefixlen- scopeid 0x20<link> ether 00:0c:29:fe:06:f3 Txqueuelen (Ethernet) lo:flags=73<up,loopback,running> MTU 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6:: 1 prefixlen scopeid 0x10loaded:loaded (/usr/lib/systemd/system/keepalived.service; disabled) active:inactive (dead) Access test HTTP via browser ://192.168.1.200/, you can find that traffic is still jumping between web1 and web2.
6.4 After starting the Nginx and keepalived service of the master node again, the VIP floats back to the master node.
[Email protected] keepalived]# systemctl start nginx.service [[email protected] keepalived]# systemctl start keepalived Pass The browser accesses the test http://192.168.1.200/to discover that traffic is still jumping between web1 and web2.
Reference:
Http://www.linuxdiyf.com/linux/12955.html
Http://nginx.org/en/linux_packages.html
http://blog.csdn.net/e421083458/article/details/30086413
http://my.oschina.net/u/1458120/blog/208740
Dual-node (nginx+keepalived) provides load balancing for two Apache servers