1. Preface
The previous section implemented a technical solution based on LVS (DR) + keepalived cluster. However, in terms of usage and maintenance, it was found that it is more difficult to maintain and implement than the haproxy + keepalived technical solution, the haproxy + keepalived cluster for Web applications is sufficient. Therefore, we recommend the haproxy + keepalived cluster technical solution to you today.
2. Environment
CentOS6 (Linux Kernel 2.6.32-71.el6.i686) haproxy 1.4.19 keepalived 1.1.17
3. Required IP configuration
Master Ip: 10.0.0.8 backup IP: 10.0.0.6 RealServer IP: 10.0.0.5, 10.0.0.6 (and)
4. Install keepalived on the master machine.
[Root @ rserver2/] # mkdir/soft [root @ rserver2/] # cd/soft [root @ rserver2 soft] # wget ‑soft] # tar-zxvf keepalived-1.1.17.tar.gz [root @ rserver2 # cd keepalived-1.1.17 [root @ rserver2 soft] #. /configure [root @ rserver2 keepalived-1.1.17] # Make; make install smoothly on these steps, if there is an error prompt, then according to the specific error processing, generally possible errors: 1) OpenSSL, the prompt may be as follows !!! OpenSSL is not properly installed on your system .!!!!!! Can not include OpenSSL headers files. solution: run the Yum-y installopenssl-devel2) and prompt that there is no GCC compiler solution: run Yum installncurses-devel GCC gcc-C ++ make rpm-Build [root @ rserver2 keepalived-1.1.17] # cp/usr/local/etc/rc. d/init. d/keepalived/etc/init. d/keepalived [root @ rserver2 keepalived-1.1.17] # cp/usr/local/sbin/keepalived/usr/sbin/[root @ rserver2 keepalived-1.1.17] # cp/usr/local/etc/sysconfig /keepalived/etc/sysconfig/[root @ rserver2 keepalived-1.1.17] # mkdir-P/etc/keepalived/[root @ rserver2 keepalived-1.1.17] # cp/usr/local/etc/keepalived. conf/etc/keepalived. conf [root @ rserver2 keepalived-1.1.17] # chmod + x/etc/init. d/keepalived
5. edit/etc/keepalived. conf on the master machine:
Open the editor, such as VI or gedit/etc/keepalived. conf, and modify the content of the program as follows. # Configure/etc/keepalived on the master server. confglobal_defs {icationication_email {leekexi@gmail.com # can be multiple addresses} Small leekexi@gmail.com smtp_server route 30 router_id lvs_devel} vrrp_instance vi_1 {state master # indicates status as master backup machine for backup interface eth0 virtual_router_id 51 priority 100 # Master privilege is higher than backup. For example, backup is 99 advert_int 1 authentication {auth_type pass # Master/Slave server authentication method auth_pass 1111} virtual_ipaddress {10.0.0.8 # multiple virtual IP addresses are allowed, line feed }}# virtual_server 10.0.0.8 80 {delay_loop 6 lb_algo RR protocol TCP real_server 10.0.0.5 80 {weight 5 tcp_check {connect_timeout 10 limit 3 limit 3 connect_port 80} real_server 10.0.0.6 80 {weight 5 tcp_check {connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 }}}
6. Edit and install haproxy on the master machine:
: Http://haproxy.1wt.eu/download/1.4/src/ select the appropriate version, generally choose a newer version will be ideal. Let's assume that the # tar zcvf haproxy-1.4.19.tar.gz # cd haproxy-1.4.19 # Make target = linux26 prefix =/usr/local/haproxy # make install prefix =/usr/local/haproxy in the/soft directory the installation directory/usr/local/haproxy creates the conf folder and then creates haproxy in the folder. use VI or gedit to open haproxy In the CFG configuration file. the configuration file content of the CFG configuration file defines global log 127.0.0.1 local0 maxconn 4096 chroot/usr/local/haproxy uid 501 GID 501 daemon nbproc 1 pidfile/usr/local/haproxy/logs/haproxy. PID logs local3 mode HTTP option httplog option comment option dontlognull option forwardfor option redispatch retries 2 maxconn 2000 balance roundrobin stats uri/haproxy-Stats contimeout 5000 clitimeout 50000 limit 50000 listen web_proxy restart: 80 server website10.0.0.6 10.0.0.6: 80 cookie app1inst1 check inter 2000 rise 2 fall 5 Server web2_10.0.0.5 10.0.0.6: 80 cookie app1inst2 check inter 2000 rise 2 fall 5 cookie 1 indicates serverid 1, check inter 1500 is the heartbeat detection frequency rise 2 is twice correct think that the server is available fall 3 is three failed think that the server is not available Weight represents the weight plus log support edit/etc/syslog. add local3. */var/log/haproxy at the bottom of conf. loglocal0. */var/log/haproxy. log edit/etc/sysconfig/syslog modify: syslogd_options = "-r-m 0" restart Log Service # service syslog restart
7. Differences between the backup machine and the master machine:
The backup and master installation are basically the same, but some parameters are slightly changed. The changed parameters are as follows: 1. keepalived. in the conf file, the original state master # indicates that the master backup machine is in the backup state backup # indicates that the status is changed to the original backup state: priority 100 # Master privilege is higher than backup. For example, if backup is 99 priority 99 # Master privilege is higher than backup, and Master is 100, backup is slightly less than, for example, 99
8. Start haproxy at startup:
To facilitate system loading during startup, you can also create a STARTUP script: # Vim/etc/rc. d/init. d/haproxy :#! /Bin/shset-e Path =/sbin:/bin:/usr/sbin:/usr/bin: /usr/local/haproxy/sbinprogdir =/usr/local/haproxyprogname = haproxydaemon = $ progdir/sbin/$ prognameconfig = $ progdir/CONF/$ progname. confpidfile = $ progdir/run/$ progname. piddesc = "haproxy daemon" scriptname =/etc/init. d/$ progname # gracefully exit if the package has been removed. test-x $ daemon | exit 0 start () {echo-n "Starting $ Desc: $ progname" $ daemon-F $ Config echo ". "} STOP () {echo-n" Stopping $ Desc: $ progname "haproxy_pid = cat $ pidfile kill $ haproxy_pid echo ". "} restart () {echo-n" Restarting $ Desc: $ progname "$ daemon-F $ config-p $ pidfile-SF $ (cat $ pidfile) echo ". "} case" $1 "in START) Start; stop) Stop; restart) restart; *) echo" Usage: $ scriptname {START | stop | restart} "> & 2 exit 1; esac exit 0 save and grant executable permissions # chmod + x/etc/rc. d/init. d/ Haproxy can use service haproxy START | stop | restart to control the start, stop, and restart of a service. Run the following command to load the service to the startup list # chkconfig -- add haproxy
8. Maintain haproxy and keepalived:
1) keepalived maintenance start:/etc/init. d/keepalived start stop:/etc/init. d/keepalived stop restart:/etc/init. d/keepalived restart 2), haproxy MaintenanceStart: Service haproxy start stop: Service haproxy stop restart: Service haproxy restart