System environment: Ubuntu16.04 (Docker container)
Architecture environment:
Keepalived/HAproxy MASTER: 172.17.0.4
Keepalived/HAproxy BACKUP: 172.17.0.6
MySQL MASTER: 172.17.0.2
MySQL SLAVE: 172.17.0.3
HAproxy version: haproxy-2.0.8.tar.gz
Keepalived版本:keepalived-2.0.19.tar.gz
A mounting HAproxy (MASTER / BACKUP two to be fitted)
1, go to the official website https://www.haproxy.org/ download HAproxy
Decompression, enter haproxy-2.0.8 directory
vim INSTALL; installation instructions
2, compile and install HAproxy
( 1) Compile
报错1:bash: make: command not found
I direct docker easy, make command not installed, install it:
apt-get -y install make
Then execute:
make -j 2 TARGET=generic USE_GZIP=1
Error 2: / bin / sh: 1 : gcc: not found
Not installed gcc, install gcc:
apt-get -y install gcc
Then execute:
make -j 2 TARGET=generic USE_GZIP=1
Not yet given:
( 2) Installation
Direct execution:
make install PREFIX=/usr/local/HAproxy2.0.8
( 3) copy command to / usr / local / sbin / lower
3, add the service startup script file
vim /etc/init.d/haproxy
! # / bin / bash # # chkconfig: 2345 85 15 # descrition: HAProxy LoadBalancer DAEMON = HAProxy PROG_DIR . = / usr / local / HAproxy2 0.8 RETVAL = 0 Success () {# now explore a friend, use the shell itself to achieve; course also be used through the loop for the, short there are many ways for ((I = 0 ; I <= . 5 ; I ++ )) do SLEEP 0.2 echo -n " . " DONE } Start () { PROG_STAT = $ (the netstat - tlnp |grep ${DAEMON}) if [ -z "$PROG_STAT" ]; then $PROG_DIR/sbin/$DAEMON -f $PROG_DIR/conf/${DAEMON}.cfg echo -ne "Starting ${DAEMON}......\t\t\t" && success echo -e "\e[32m[OK]\e[0m" else echo "$DAEMON is already running" RETVAL=65 fi } stop () { PROG_STAT=$(netstat -tlnp | grep ${DAEMON}) if [ -n "$PROG_STAT" ]; then echo -ne "stopping ${DAEMON}......\t\t\t" && success PROG_PID=$(cat $PROG_DIR/run/${DAEMON}.pid) kill $PROG_PID echo -e "\e[32m[OK]\e[0m" else echo "$DAEMON is already stopped" RETVAL=66 fi } restart() { echo -ne "restarting ${DAEMON}......\t\t\t" && success PROG_PID=$(cat $PROG_DIR/run/${DAEMON}.pid) $PROG_DIR/sbin/$DAEMON -f $PROG_DIR/conf/${DAEMON}.cfg -st $PROG_PID echo -e "\e[32m[OK]\e[0m" } status () { PROG_STAT=$(netstat -tlnp | grep ${DAEMON}) if [ -z "$PROG_STAT" ]; then echo "${DAEMON} stopped" else echo "${DAEMON} running" fi } case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage /etc/init.d/$DAEMON {start | stop | restart | status}" RETVAL=67 esac exit $RETVAL
Given execute permissions:
chmod +x /etc/init.d/haproxy
4, add haproxy.conf profile
Create haproxy users and groups:
And then create a profile:
mkdir -p /etc/haproxy
vim /etc/haproxy/haproxy.conf
, Ltd. Free Join log 127.0 . 0.1 local2 info # Set the log file output oriented, info log level for the chroot / usr / local / HAproxy2. 0.8 # Change the current working directory of the PidFile / usr / local / HAproxy2. 0.8 / RUN / haproxy.pid # PID file the position of user haproxy # users and groups group HAProxy daemon # daemon is started, operation and maintenance work for the background mode the maxconn 4000 # maximum connection number # acted upon immediately after the listen block defaults until the next block, the next default replaces acting on a block after the listen defaults log each instance, Ltd. Free Join # start logging events and traffic mode http # {tcp default mode the mODE | HTTP | Health}, is a four-tcp, http is seven, health will return ok retries 3 after # connection to server failure, the number of retries to enable or disable the session in case of connection failure reallocate # redispatch the Option maxconn 4096 Maximum number of connections per process #maxconn available timeout HTTP - Request 10s timeout Queue 1M timeout 10s connect timeout Client 1M timeout Server 1M timeout HTTP -keep- Alive 10s frontend main the bind 0.0 . 0.0 : 3307 default_backend MySQL backend MySQL Balance leastconn # algorithm: least connections Server mysql1 172.17 . 0.2 :3306 check port 3306 maxconn 300 server mysql2 172.17.0.3:3306 check port 3306 maxconn 300
A copy of the configuration file to the directory:
cp -a /etc/haproxy/haproxy.conf /usr/local/HAproxy2.0.8/conf/haproxy.cfg
To start haproxyd Service
/etc/init.d/haproxy start
Displays the service starts successfully, the port also
Second, the installation Keepalived
1, go to the official website to download the required version https://www.keepalived.org/
Also unpack into a directory, see the installation documentation (here ignored)
2, compiled makeup
( 1) Check the environment
./configure --prefix=/usr/local/keepalived-2.0.19
报错1:Can not include OpenSSL headers files
No openssl, run:
apt-get -y install openssl libssl-dev
Note: RedHat and centos is the need openssl and openssl-devel in the ubuntu, openssl-devel be replaced libssl-dev, can be mounted libssl-dev
Re-examine the environment -
ok, every problem, ignore warnings
( 2) compile, compile and install
make && make install
ok, compile and install complete
3, edit the configuration file
cp /usr/local/keepalived-2.0.19/etc/keepalived/keepalived.conf / etc / keepalived / # copy the configuration file
cp /usr/local/keepalived-2.0.19/sbin/keepalived /usr/local/sbin/
cp /usr/local/keepalived-2.0.19/etc/rc.d/init.d/keepalived /etc/init.d/ # File Replication service starts
chmod +x /etc/init.d/keepalived
vim /etc/keepalived/keepalived.conf(MASTER machine)
! The Configuration File for keepalived global_defs { the router_id of the virtual routing # R1 name, master backup and not consistently } vrrp_script defined chk_haproxy {# Check Script Script " /etc/keepalived/chk_haproxy.sh " # script position interval The . 3 # detector once every 3 seconds fall 3 # failure judging 3 # weight - 2 weight after # failed - 2 } vrrp_instance VI_1 { state MASTER #, only the MASTER or the BACKUP interface eth0 # NIC name, note that it must be a card name you're using virtual_router_id 53 # virtual routing id It is the last two digits of mac virtual routing priority100 # priority advert_int . 1 # advertisement interval authentication {# authentication AUTH_TYPE the PASS AUTH_PASS 1111 } track_script above script execution {# chk_haproxy } virtual_ipaddress {#VIP address, network segments 172.17 . 0.253 } # notify_master " /etc/init.d/ Start HAProxy " # when the current node becomes the master, task execution # notify_backup " /etc/init.d/haproxy restart " # when the current node becomes the backup, task execution # notify_fault "STOP /etc/init.d/haproxy " # when the current node fails, perform tasks }
ok, to create a health check script file
vim /etc/keepalived/chk_haproxy.sh
#/bin/bash STAT=`ps -C haproxy --no-header | wc -l` if [[ ! "$STAT" -eq 1 ]];then /etc/init.d/keepalived stop fi
If the process is not, directly off keepalived
chmod + x chk_haproxy.sh # give execute permissions
4, start the service keepalived
Some startup file file does not exist, the need to manually link it ( Ubuntu is troublesome):
ln -s /lib/lsb/init-functions /etc/init.d/functions
mkdir /etc/rc.d
ln -s /etc/init.d /etc/rc.d/
cp /src/keepalived-2.0.19/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
Then install the daemon commands:
apt-get -y install daemon
Note that FIG., Daemon - before keepalived is keepalived daemon , plus two bars
This command is problematic, which -D keepalived supposed to use, but after such a combination is considered parameters daemon command. This causes the service could not be started. If not modified, it will prompt fails to start, but no specific information output.
Note that since I was using docker container mounted keepalived, so I need to save this container into the mirror, and then re-docker run, to add --privileged this parameter in order to display keepalived of VIP (not by vessel installation can be ignored )
docker run -dit --privileged --name ha_keep -p 3308:3307 ha_keep
Start the following services:
/etc/init.d/keepalived start
Execution: ip addr
5, BACKUP machine configuration
MASTER haproxy services in the same machine, keepalived services primarily to modify the main configuration file
Keepalived / BACKUP machine configuration file:
! The Configuration File for keepalived global_defs { router_id r2 # virtual route name, master and backup is not consistent } vrrp_script chk_haproxy {# define checking scripts Script " /etc/keepalived.chk_haproxy.sh " # script location interval The 3 # tested once every 3 seconds fall 3 # failure judging 3 # weight - 2 weight after # failed - 2 } vrrp_instance VI_1 { state the BACKUP #, only the MASTER or the BACKUP interface eth0 # NIC name, note that it must be a card name you're using virtual_router_id 53 # virtual routing id It is the last two digits of mac virtual routing priority 99 # priority advert_int . 1 # advertisement interval authentication {# authentication AUTH_TYPE the PASS AUTH_PASS 1111 } track_script { chk_haproxy } virtual_ipaddress {#VIP address, network segments 172.17 . 0.253 } # notify_master " /etc/init.d/haproxy Start " # when the current node becomes the master, to perform tasks # notify_backup " /etc/init.d/haproxy restart " # when the current node becomes the backup, task execution # notify_fault " /etc/init.d/haproxy STOP " # when the current node fails, perform tasks }
The other configuration, and then start keepalived
6, testing the MASTER machine haproxy service stop, will not automatically turn off keepalived service, will be transferred to the VIP BAKUP machine
Shown below VIP BACKUP machine to the configuration described ok
7, load balancing, high availability is configured, but this architecture is optimized for mysql master-master replication or shared storage servers, personal feeling mysql The Lord prone to problems, not recommended, personal views.
If reproduced please indicate the source