Keepalived+haproxy High availability
Environment: main Haproxy server 192.168.80.100 keepalived+Haproxy standby Haproxy server 192.168.80.101 keepalived+Haproxyweb server 1 192.168.80.102 httpd (nginx, tomcat) web server 2 192.168.80.103 httpd80.100 and 80.101 need to be connected to the network
-
On the 80.100 virtual machine:
Systemctl stop firewalld / / turn off the firewall setenforce 0 / / turn off monitoring cd / etc/yum.repos.d/ enter the directory cp back/*. / / move the contents of back to the previous directory ls / / view the contents of the directory
Yum install-y epel-releaseyum install ntp-y / / install ntpntpdate time1.aliyun.com / / synchronize network time date
Yum install keepalived-y / / install keepalivedcd / / return to the root directory vi / etc/keepalived/ keepalived.conf` to delete everything and add the following:! Configuration File for keepalivedglobal_defs {route_id haproxy-01} vrrp_script haproxy {define a hot standby script named haproxy script "/ opt/haproxy.sh" # script path to detect haproxy status interval 2 # Detection interval weight 2 # if the condition holds Weight + 2} vrrp_instance VI_1 {state MASTER interface ens32 virtual_router_id 51 priority 150 advert_int 1 authentication {auth_type PASS auth_pass 1111} track_script {# add track_script block to instance configuration block haproxy # check whether the HAProxy service is alive} virtual_ipaddress {192.168.80.188}: wq
Scp / etc/keepalived/keepalived.conf root@192.168.80.101:/etc/keepalived/keepalived.conf / / transfer the keepalived.conf to the yesroot password in 80.101
Yum install lrz*-y / / install the upload software and pull the haproxy package into
Scp haproxy-1.5.15.tar.gz root@192.168.80.101:/root / / transfer the haproxy package to the password in 80.101
Tar xf haproxy-1.5.15.tar.gz-C / opt/ decompress the haproxy package cd / opt/haproxy-1.5.15/ source code compile and install haproxyyum install-y\ pcre-devel\ bzip2-devel\ gcc\ gcc-c++\ makemake TARGET=linux26 PREFIX=/usr/local/haproxy / / Mark 64 create the directory cduseradd-M-s / sbin/nologin haproxyid haproxy for the system make install PREFIX=/usr/local/haproxymkdir / etc/haproxy / /
Cd / opt/haproxy-1.5.15/cp examples/haproxy.cfg / etc/haproxyvi / etc/haproxy/haproxy.cfg modifies the following: # chroot / usr/share/haproxy comments this line uid 1000gid 1000 statistics page module content under the default module listen admin_stats bind 0.0.0.0 stats stats realm Haproxy 8089 stats enable mode http log global stats uri / stats stats realm Haproxy\ Statistics stats auth admin:admin # stats hide -version stats admin if TRUE stats refresh 30sweb server / terminal listen webcluster bind 0.0.0.0 mode http option httpchk GET / index.html log global maxconn 3000 balance roundrobinserver web01 192.168.80.102 balance roundrobinserver web01 80 check inter 2000 fall 5server web02 192.168.80.103 mode http option httpchk GET 80 check inter 2000 fall 5:wq
Cd / opt/ls
Vi haproxy.shemaking binbinapharp [$(ps-C haproxy-- no-header | wc-l)-eq 0]; then / usr/sbin/haproxy-f / etc/haproxy/haproxy.cfg starts the haproxy service fisleep 2 wait 2 seconds if [$(ps-C haproxy-- no-header | wc-l)-eq 0]; then service keepalived stopfi: wq / / save exit
Chmod + x / opt/haproxy.sh / / set permissions to this file cd / opt/haproxy-1.5.15/cp examples/haproxy.init / etc/init.d/haproxychmod + x / etc/init.d/haproxyll / etc/init.d/haproxychkconfig-- add haproxyln-s / usr/local/haproxy/sbin/haproxy / usr/sbin/haproxy / / soft link cat / var/log/messages
Service haproxy start / / start the service
Netstat-anpt | grep haproxy
Enter 192.168.80.100:8089/stats on the web page
Scp haproxy.cfg root@192.168.80.101:/etc/haproxy/ transfer haproxy.cfg to the password on the 80.101 virtual machine
Service haproxy stopnetstat-anpt | grep haproxysystemctl start keepalivedip addr show ens32netstat-anpt | grep haproxy
-
On the 80.101 virtual machine:
Systemctl stop firewalld / / turn off the firewall setenforce 0 / / turn off monitoring cd / etc/yum.repos.d/ enter the directory cp back/*. / / move the contents of back to the previous directory ls / / view the contents of the directory
Yum install-y epel-releaseyum install ntp-y / / install ntpntpdate time1.aliyun.com / / synchronize network time dateyum install keepalived-y / / install keepalivedcd / / return to the root directory
Vi / etc/keepalived/keepalived.conf / / enter into the following:
Modify it to the following:
Lstar xf haproxy-1.5.15.tar.gz-C / opt/cd / opt/haproxy-1.5.15/
Source code compilation and installation haproxyyum install-y\ pcre-devel\ bzip2-devel\ gcc\ gcc-c++\ makemake TARGET=linux26 PREFIX=/usr/local/haproxy / / Mark 64 is the system make install PREFIX=/usr/local/haproxymkdir / etc/haproxyuseradd-s / sbin/nologin-M haproxyid haproxy
Ls / etc/haproxy/cp examples/haproxy.init / etc/init.d/haproxychmod + x / etc/init.d/haproxy / / set permissions for this file chkconfig-- add haproxyln-s / usr/local/haproxy/sbin/haproxy / usr/sbin/haproxy / / soft link
Service haproxy start / / start the service netstat-anpt | grep haproxyservice haproxy stop / / turn off the service netstat-anpt | grep haproxycd / opt/vi haproxy.shename if if $(ip addr | grep 192.168.80.188 shock 32 | grep-v grep | wc-l) if [$A-gt 0] Then service haproxy startelse service haproxy stopfi: wqchmod + x haproxy.sh / / set permissions ll systemctl start keepalived ip addr show ens32netstat-anpt | grep haproxy
-
On the 80.102 virtual machine:
Systemctl stop firewalld / / turn off firewall setenforce 0 / / turn off monitoring yum install httpd-y / / install httpdvi / etc/httpd/conf/httpd.conf to delete the # in front of ServerName www.example.com:80
Cd / var/www/html/ enter this directory echo "server aa" > index.htmlsystemctl start httpd / / start the service and enter 192.168.80.102 on the web page
-
On the 80.103 virtual machine:
Systemctl stop firewalld / / turn off firewall setenforce 0 / / turn off monitoring yum install httpd-y / / install httpdvi / etc/httpd/conf/httpd.conf to delete the # in front of ServerName www.example.com:80
Cd / var/www/html/ enter this directory echo "server bb" > index.htmlsystemctl start httpd / / start the service and enter 192.168.80.103 on the web page