keepalive实现mysql主从自动切换

环境说明
mysql master端192.168.56.201
mysql slave端 192.168.56.211
虚拟IP地址 192.168.56.200
1.安装keepalived
在master和slave节点分别解压缩后执行./config&&make&&make install
   
   
[root@mysql1-slave keepalived-1.2.19]# ./configure
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for a BSD-compatible install... /usr/bin/install -c
checking for strip... strip
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking syslog.h usability... yes
checking syslog.h presence... yes
checking for syslog.h... yes
checking for unistd.h... (cached) yes
checking sys/ioctl.h usability... yes
checking sys/ioctl.h presence... yes
checking for sys/ioctl.h... yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking openssl/ssl.h usability... yes
checking openssl/ssl.h presence... yes
checking for openssl/ssl.h... yes
checking openssl/md5.h usability... yes
checking openssl/md5.h presence... yes
checking for openssl/md5.h... yes
checking openssl/err.h usability... yes
checking openssl/err.h presence... yes
checking for openssl/err.h... yes
checking whether ETHERTYPE_IPV6 is declared... yes
checking for crypt in -lcrypt... yes
checking for MD5_Init in -lcrypto... yes
checking for SSL_CTX_new in -lssl... yes
checking for nl_socket_alloc in -lnl-3... no
checking for nl_socket_modify_cb in -lnl... yes
checking for kernel version... 2.6.32
checking for IPVS syncd support... yes
checking for kernel macvlan support... yes
checking whether SO_MARK is declared... yes
checking for an ANSI C-conforming const... yes
checking for pid_t... yes
checking whether time.h and sys/time.h may both be included... yes
checking whether gcc needs -traditional... no
checking for working memcmp... yes
checking return type of signal handlers... void
checking for gettimeofday... yes
checking for select... yes
checking for socket... yes
checking for strerror... yes
checking for strtol... yes
checking for uname... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating genhash/Makefile
config.status: creating keepalived/core/Makefile
config.status: creating lib/config.h
config.status: creating keepalived.spec
config.status: creating keepalived/Makefile
config.status: creating lib/Makefile
config.status: creating keepalived/vrrp/Makefile
config.status: creating keepalived/check/Makefile
config.status: creating keepalived/libipvs-2.6/Makefile
 
Keepalived configuration
------------------------
Keepalived version : 1.2.19
Compiler : gcc
Compiler flags : -g -O2 -DFALLBACK_LIBNL1
Extra Lib : -lssl -lcrypto -lcrypt -lnl
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : Yes
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No
[root@mysql1-slave keepalived-1.2.19]# make
make -C lib || exit 1;
make[1]: Entering directory `/root/keepalived-1.2.19/lib'
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c memory.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c utils.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c notify.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c timer.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c scheduler.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c vector.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c list.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c html.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c parser.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c signals.c
gcc -I. -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_WITHOUT_SNMP_ -c logger.c
make[1]: Leaving directory `/root/keepalived-1.2.19/lib'
make -C keepalived
make[1]: Entering directory `/root/keepalived-1.2.19/keepalived'
make[2]: Entering directory `/root/keepalived-1.2.19/keepalived/core'
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c main.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c daemon.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c pidfile.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c layer4.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c smtp.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c global_data.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c global_parser.c
make[2]: Leaving directory `/root/keepalived-1.2.19/keepalived/core'
make[2]: Entering directory `/root/keepalived-1.2.19/keepalived/check'
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_daemon.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_data.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_parser.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_api.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_tcp.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_http.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_ssl.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_smtp.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c check_misc.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c ipwrapper.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_ -D_WITHOUT_SNMP_ -D_WITH_SO_MARK_ -c ipvswrapper.c
make[2]: Leaving directory `/root/keepalived-1.2.19/keepalived/check'
make[2]: Entering directory `/root/keepalived-1.2.19/keepalived/vrrp'
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_daemon.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_print.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_data.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_parser.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_notify.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_scheduler.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_sync.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_index.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_netlink.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_arp.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_if.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_track.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_ipaddress.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_iproute.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_ipsecah.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_ndisc.c
gcc -I../include -I../../lib -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_HAVE_VRRP_VMAC_ -D_WITHOUT_SNMP_ -c vrrp_vmac.c
make[2]: Leaving directory `/root/keepalived-1.2.19/keepalived/vrrp'
make[2]: Entering directory `/root/keepalived-1.2.19/keepalived/libipvs-2.6'
gcc -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -DLIBIPVS_USE_NL -Wall -Wunused -c -o libipvs.o libipvs.c
gcc -g -O2 -DFALLBACK_LIBNL1 -I/usr/src/linux/include -I/usr/src/linux/include -DLIBIPVS_USE_NL -Wall -Wunused -c -o ip_vs_nl_policy.o ip_vs_nl_policy.c
ar rv libipvs.a libipvs.o ip_vs_nl_policy.o
ar: creating libipvs.a
a - libipvs.o
a - ip_vs_nl_policy.o
rm libipvs.o ip_vs_nl_policy.o
make[2]: Leaving directory `/root/keepalived-1.2.19/keepalived/libipvs-2.6'
Building ../bin/keepalived
strip ../bin/keepalived
 
Make complete
make[1]: Leaving directory `/root/keepalived-1.2.19/keepalived'
make -C genhash
make[1]: Entering directory `/root/keepalived-1.2.19/genhash'
gcc -I../lib -g -O2 -DFALLBACK_LIBNL1 -D_WITH_SO_MARK_ -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -c -o main.o main.c
gcc -I../lib -g -O2 -DFALLBACK_LIBNL1 -D_WITH_SO_MARK_ -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -c -o sock.o sock.c
gcc -I../lib -g -O2 -DFALLBACK_LIBNL1 -D_WITH_SO_MARK_ -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -c -o layer4.o layer4.c
gcc -I../lib -g -O2 -DFALLBACK_LIBNL1 -D_WITH_SO_MARK_ -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -c -o http.o http.c
gcc -I../lib -g -O2 -DFALLBACK_LIBNL1 -D_WITH_SO_MARK_ -I/usr/src/linux/include -I/usr/src/linux/include -Wall -Wunused -Wstrict-prototypes -c -o ssl.o ssl.c
Building ../bin/genhash
strip ../bin/genhash
 
Make complete
make[1]: Leaving directory `/root/keepalived-1.2.19/genhash'
 
Make complete
[root@mysql1-slave keepalived-1.2.19]#
[root@mysql1-slave keepalived-1.2.19]# make install
make -C keepalived install
make[1]: Entering directory `/root/keepalived-1.2.19/keepalived'
install -d /usr/local/sbin
install -m 700 ../bin/keepalived /usr/local/sbin/
install -d /usr/local/etc/rc.d/init.d
install -m 755 etc/init.d/keepalived.init /usr/local/etc/rc.d/init.d/keepalived
install -d /usr/local/etc/sysconfig
install -m 644 etc/init.d/keepalived.sysconfig /usr/local/etc/sysconfig/keepalived
install -d /usr/local/etc/keepalived/samples
install -m 644 etc/keepalived/keepalived.conf /usr/local/etc/keepalived/
install -m 644 ../doc/samples/* /usr/local/etc/keepalived/samples/
install -d /usr/local/share/man/man5
install -d /usr/local/share/man/man8
install -m 644 ../doc/man/man5/keepalived.conf.5 /usr/local/share/man/man5
install -m 644 ../doc/man/man8/keepalived.8 /usr/local/share/man/man8
make[1]: Leaving directory `/root/keepalived-1.2.19/keepalived'
make -C genhash install
make[1]: Entering directory `/root/keepalived-1.2.19/genhash'
install -d /usr/local/bin
install -m 755 ../bin/genhash /usr/local/bin/
install -d /usr/local/share/man/man1
install -m 644 ../doc/man/man1/genhash.1 /usr/local/share/man/man1
make[1]: Leaving directory `/root/keepalived-1.2.19/genhash'
 
安装完成之后默认的keepalived可执行文件放在/usr/local/sbin下,默认的配置文件存放在/usr/local/etc/下。

2.将keepalived添加到服务中,并且设置为自动启动;拷贝配置文件到/etc/keepalived中
   
   
[root@mysql1-slave init.d]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@mysql1-slave init.d]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@mysql1-slave init.d]# mkdir /etc/keepalived
[root@mysql1-slave init.d]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@mysql1-slave init.d]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@mysql1-slave init.d]# chkconfig --add keepalived
[root@mysql1-slave init.d]# chkconfig --level 345 keepalived on
3.配置master端keepalived
在/etc/keepalived/下配置keepalived.conf。
master会执行vvr_script check_run来检查mysql进程是否正常
Keepalived在转换状态时会依照状态来呼叫:
 当进入Master状态时会呼叫notify_master
 当进入Backup状态时会呼叫notify_backup
 当发现异常情况时进入Fault状态呼叫notify_fault
 当Keepalived程序终止时则呼叫notify_stop
   
   
[root@mysql1 keepalived]# cat keepalived.conf
global_defs {
router_id KeepAlive_Mysql
}
 
vrrp_script check_run {
script "/root/shell/mysql_check.sh"
interval 10
}
 
vrrp_sync_group VG1 {
group {
VI_1
}
}
 
vrrp_instance VI_1 {
state BACKUP !开始状态都是BACKUP防止vip抢占漂移,只有两张状态MASTER/BACKUP必须是大些
interface eth0
virtual_router_id 51
priority 100 !优先级
advert_int 1
nopreempt !不抢占,在优先级高的上设置即可
authentication { !认证两边必须一致
auth_type PASS
auth_pass 1111
}
track_script { !执行上面定义check mysql脚本
check_run
}
notify_master /root/shell/master.sh
notify_backup /root/shell/backup.sh
notify_stop /root/shell/stop.sh
 
 
 
virtual_ipaddress { !虚拟ip地址,会在mater和backup中间漂移
192.168.56.200
}
}
其中有4个shell脚本:
mysql_check.sh脚本检查mysql是否正常,如果mysql不健康则停止master端的keepalived:
   
   
[root@mysql1 shell]# cat mysql_check.sh 
#!/bin/bash
 
. /root/.bash_profile
 
count=1
 
while true
do
 
mysql -e "show status;" > /dev/null 2>&1
i=$?
ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
j=$?
if [ $i = 0 ] && [ $j = 0 ]
then
exit 0
else
if [ $i = 1 ] && [ $j = 0 ]
then
exit 0
else
if [ $count -gt 5 ]
then
break
fi
let count++
continue
fi
fi
 
done
 
/etc/init.d/keepalived stop
master.sh脚本表示状态改变为master后需要执行的脚本,先判断同步复制是否执行完成,如果未执行完成等待1分钟后强制停止同步复制,并且更改业务帐号admin的密码和权限,并且记录切换后的日志和pos:
   
   
[root@mysql1 shell]# cat master.sh
#!/bin/bash
 
. /root/.bash_profile
 
Master_Log_File=$(mysql -e "show slave status\G" | grep -w Master_Log_File | awk -F": " '{print $2}')
Relay_Master_Log_File=$(mysql -e "show slave status\G" | grep -w Relay_Master_Log_File | awk -F": " '{print $2}')
Read_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}')
Exec_Master_Log_Pos=$(mysql -e "show slave status\G" | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}')
 
i=1
 
while true
do
 
if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ]
then
echo "ok"
break
else
sleep 1
 
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done
 
mysql -e "stop slave;"
mysql -e "set global innodb_support_xa=0;"
mysql -e "set global sync_binlog=0;"
mysql -e "set global innodb_flush_log_at_trx_commit=0;"
mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';flush privileges;"
mysql -e "show master status;" > /tmp/master_status_$(date "+%y%m%d-%H%M").txt
backup.sh表示状态改变为backup后执行的脚本:
   
   
#!/bin/bash
 
. /root/.bash_profile
 
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';flush privileges;"
mysql -e "set global event_scheduler=0;"
mysql -e "set global innodb_support_xa=0;"
mysql -e "set global sync_binlog=0;"
mysql -e "set global innodb_flush_log_at_trx_commit=0;"

stop.sh表示keepalived停止后执行的脚本,首先更改admin密码,然后设置参数保证数据不丢失,最后查看是否还有写操作,最后1分钟后退出:
   
   
[root@mysql1 shell]# cat stop.sh
#!/bin/bash
 
. /root/.bash_profile
 
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';flush privileges;"
mysql -e "set global innodb_support_xa=1;"
mysql -e "set global sync_binlog=1;"
mysql -e "set global innodb_flush_log_at_trx_commit=1;"
 
M_File1=$(mysql -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position1=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')
sleep 1
M_File2=$(mysql -e "show master status\G" | awk -F': ' '/File/{print $2}')
M_Position2=$(mysql -e "show master status\G" | awk -F': ' '/Position/{print $2}')
 
i=1
 
while true
do
 
if [ $M_File1 = $M_File1 ] && [ $M_Position1 -eq $M_Position2 ]
then
echo "ok"
break
else
sleep 1
 
if [ $i -gt 60 ]
then
break
fi
continue
let i++
fi
done

4.配置slave端的keepalived.conf
   
   
[root@mysql1-slave shell]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id KeepAlive_Mysql
}
 
vrrp_script check_run {
script "/root/shell/mysql_check.sh"
interval 10
}
 
vrrp_sync_group VG1 {
group {
VI_1
}
}
 
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
notify_master /root/shell/master.sh
notify_backup /root/shell/backup.sh
notify_stop /root/shell/stop.sh
 
 
 
virtual_ipaddress {
192.168.56.200
}
}
5.keepalived接管和调用脚本流程
一、停止master的mysql
master检测到mysql停止会停止keepalived,然后调用执行stop.sh
slave端会切换为master端,然后调用master.sh

二、启动原master的mysql和keepalived
此时原来master端并不会变成master,也就是说不会自动漂移,此时会调用backup.sh


三、关闭slave端的keepalived
原master会再变成master端,此时调用master.sh
停止slave端,所以调用stop.sh

四、打开slave端的keepalived
slave端再次变成backup,所以会再次执行backup.sh

6.管理keepalived
启动停止service keepalived stop/start/restart
日志在系统日志中/var/log/messages,里面有vip的漂移说明和mater-backup的切换记录
   
   
Jan 11 00:50:13 mysql1 Keepalived_vrrp[6862]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan 11 00:50:13 mysql1 Keepalived_vrrp[6862]: VRRP_Group(VG1) Syncing instances to MASTER state
Jan 11 00:50:14 mysql1 Keepalived_vrrp[6862]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan 11 00:50:14 mysql1 Keepalived_vrrp[6862]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan 11 00:50:14 mysql1 Keepalived_vrrp[6862]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.56.200
Jan 11 00:50:14 mysql1 Keepalived_healthcheckers[6861]: Netlink reflector reports IP 192.168.56.200 added
Jan 11 00:50:19 mysql1 Keepalived_vrrp[6862]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.56.200
通过 ip a可以查看keepalived所持有的虚拟ip地址!



















  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值