MySQL高可用集群解决方案之:lvs+keepalived+mysql cluster实现负载均衡

news2024/11/28 14:05:42

一:前言

Mysql-cluster本身实现了高可用,但是其sql节点并没有实现负载均衡。怎样让程序能均衡地访问sql节点呢,程序里写哪个数据库服务器IP呢?

LVS是实现负载均衡作用的,即将客户端的需求采用特定的负载均衡算法分发到后端的Web应用服务器上。

Keepalived是用来实现高可用的,提供健康检查,故障转移,即保证主LVS宕机后,从LVS可以在很短时间顶上,从而保证了系统或网站的稳定性。Keepalived在后端的realserver接收不到主节点的信息之后,keepalived能够自己调用ipvsadm命令生成规则,能够自动实现,将主节点的VIP以及ipvs规则“拿过来”,应用在从节点上,继续为用户服务。

程序里写虚拟IP即可。

拓扑图:
在这里插入图片描述

二:实验环境

已经安装好了mysql cluster
Mysql cluster安装,请参考:https://blog.csdn.net/hualinger/article/details/131092300
在这里插入图片描述

三:实验步骤

3.1:安装ipvsadm

分别在lvs-master(主调度器)和lvs-backup(从调度器)上安装.

#下载ipvsadm

点击这里下载ipvsadm:

http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

#查看内核版本

uname -r

2.6.32-431.el6.x86_64

先进cd /usr/src/kernels/看有没有该文件:

2.6.32-431.el6.x86_64

如果没有,需要:yum -y install kernel-devel

否则,在后面编译的时候回报错:如libipvs.h:14:23: error: net/ip_vs.h: No such file or directory

#创建软连接

ln -s /usr/src/kernels/2.6.32-573.3.1.el6.x86_64/ /usr/src/linux

注意:软连接时,需要提供2.6.32-573.3.1.el6.x86_64的绝对路径,如果直接进入/usr/src/kernels目录,这样软连接是不行的:ln -s 2.6.32-573.3.1.el6.x86_64/ /usr/src/linux

#编译

cd /download
tar -xvf ipvsadm-1.24.tar.gz 
cd ipvsadm-1.24
[root@ser6-70 ipvsadm-1.24]# make
make -C libipvs
make[1]: Entering directory `/download/ipvsadm-1.24/libipvs'
gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include  -DHAVE_NET_IP_VS_H -c -o libipvs.o libipvs.c
ar rv libipvs.a libipvs.o
ar: creating libipvs.a
a - libipvs.o
make[1]: Leaving directory `/download/ipvsadm-1.24/libipvs'
gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include -I.. -I. -DVERSION=\"1.24\" -DSCHEDULERS=\""rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq"\"  -DHAVE_NET_IP_VS_H -c -o ipvsadm.o ipvsadm.c
ipvsadm.c: In function ‘print_largenum’:
ipvsadm.c:1564: warning: field width should have type ‘int’, but argument 2 has type ‘size_t’
gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include -I.. -I. -DVERSION=\"1.24\" -DSCHEDULERS=\""rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq"\"  -DHAVE_NET_IP_VS_H -c -o config_stream.o config_stream.c
gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include -I.. -I. -DVERSION=\"1.24\" -DSCHEDULERS=\""rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq"\"  -DHAVE_NET_IP_VS_H -c -o dynamic_array.o dynamic_array.c
gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -o ipvsadm ipvsadm.o config_stream.o dynamic_array.o libipvs/libipvs.a 
[root@ser6-70 ipvsadm-1.24]# make install
make -C libipvs
make[1]: Entering directory `/download/ipvsadm-1.24/libipvs'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/download/ipvsadm-1.24/libipvs'
if [ ! -d /sbin ]; then mkdir -p /sbin; fi
install -m 0755 -s ipvsadm /sbin
install -m 0755 ipvsadm-save /sbin
install -m 0755 ipvsadm-restore /sbin
[ -d /usr/man/man8 ] || mkdir -p /usr/man/man8
install -m 0644 ipvsadm.8 /usr/man/man8
install -m 0644 ipvsadm-save.8 /usr/man/man8
install -m 0644 ipvsadm-restore.8 /usr/man/man8
if [ -d /etc/rc.d/init.d ]; then \
  install -m 0755 ipvsadm.sh /etc/rc.d/init.d/ipvsadm; \
fi

#验证是否安装成功
[root@ser6-70 ipvsadm-1.24]# /sbin/ipvsadm -v

ipvsadm v1.24 2005/12/10 (compiled with getopt_long and IPVS v1.2.1)

说明安装成功。

3.2:安装keepalived

分别在lvs_master(主调度器)和lvs-backup(从调度器)上安装。

安装keepalived前先要安装popt-devel,openssl :

yum install popt-devel -y
yum install -y openssl openssl-devel

#安装keepalived

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

#解压

tar zxvf keepalived-1.1.19.tar.gz

#初始化

[root@ser6-70 linux]# cd keepalived-1.1.19
[root@ser6-70 keepalived-1.1.19]# ./configure --prefix=/usr/local/keepalived
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
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 for MD5_Init in -lcrypto... yes
checking for SSL_CTX_new in -lssl... yes
checking for poptGetContext in -lpopt... yes
checking for kernel version... 2.6.32
checking net/ip_vs.h usability... yes
checking net/ip_vs.h presence... yes
checking for net/ip_vs.h... yes
checking for IPVS syncd support... 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: WARNING:  'genhash/Makefile.in' seems to ignore the --datarootdir setting
config.status: creating keepalived/core/Makefile
config.status: creating keepalived/include/config.h
config.status: creating keepalived.spec
config.status: creating keepalived/Makefile
config.status: WARNING:  'keepalived/Makefile.in' seems to ignore the --datarootdir setting
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.1.19
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto 
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use Debug flags          : No

以下全部为 YES, 表示配置成功。

Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes

#编译

[root@ser6-70 keepalived-1.1.19]# make
make -C lib || exit 1;
make[1]: Entering directory `/download/keepalived-1.1.19/lib'
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c memory.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c utils.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c notify.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c timer.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c scheduler.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c vector.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c list.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c html.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c parser.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c signals.c
gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes  -c logger.c
make[1]: Leaving directory `/download/keepalived-1.1.19/lib'
make -C keepalived
make[1]: Entering directory `/download/keepalived-1.1.19/keepalived'
make[2]: Entering directory `/download/keepalived-1.1.19/keepalived/core'
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c main.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c daemon.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c pidfile.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c layer4.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c smtp.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c global_data.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_WITH_VRRP_  -c global_parser.c
make[2]: Leaving directory `/download/keepalived-1.1.19/keepalived/core'
make[2]: Entering directory `/download/keepalived-1.1.19/keepalived/check'
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_daemon.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_data.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_parser.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_api.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_tcp.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_http.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_ssl.c
check_ssl.c: In function ‘build_ssl_ctx’:
check_ssl.c:79: warning: assignment discards qualifiers from pointer target type
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_smtp.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c check_misc.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c ipwrapper.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_ -D_WITH_VRRP_  -c ipvswrapper.c
make[2]: Leaving directory `/download/keepalived-1.1.19/keepalived/check'
make[2]: Entering directory `/download/keepalived-1.1.19/keepalived/vrrp'
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_daemon.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_data.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_parser.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_notify.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_scheduler.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_sync.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_index.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_netlink.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_arp.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_if.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_track.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_ipaddress.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_iproute.c
gcc -g -O2  -I/usr/src/linux/include -I../include -I../../lib -Wall -Wunused -Wstrict-prototypes -D_KRNL_2_6_ -D_WITH_LVS_ -D_HAVE_IPVS_SYNCD_  -c vrrp_ipsecah.c
make[2]: Leaving directory `/download/keepalived-1.1.19/keepalived/vrrp'
make[2]: Entering directory `/download/keepalived-1.1.19/keepalived/libipvs-2.6'
gcc -g -O2  -I/usr/src/linux/include -Wall -Wunused   -c -o libipvs.o libipvs.c
ar rv libipvs.a libipvs.o
ar: creating libipvs.a
a - libipvs.o
rm libipvs.o
make[2]: Leaving directory `/download/keepalived-1.1.19/keepalived/libipvs-2.6'
Building ../bin/keepalived
strip ../bin/keepalived
 
Make complete
make[1]: Leaving directory `/download/keepalived-1.1.19/keepalived'
make -C genhash
make[1]: Entering directory `/download/keepalived-1.1.19/genhash'
gcc -g -O2  -I/usr/src/linux/include -I../lib -Wall -Wunused -Wstrict-prototypes   -c -o main.o main.c
gcc -g -O2  -I/usr/src/linux/include -I../lib -Wall -Wunused -Wstrict-prototypes   -c -o sock.o sock.c
gcc -g -O2  -I/usr/src/linux/include -I../lib -Wall -Wunused -Wstrict-prototypes   -c -o layer4.o layer4.c
gcc -g -O2  -I/usr/src/linux/include -I../lib -Wall -Wunused -Wstrict-prototypes   -c -o http.o http.c
gcc -g -O2  -I/usr/src/linux/include -I../lib -Wall -Wunused -Wstrict-prototypes   -c -o ssl.o ssl.c
ssl.c: In function ‘init_ssl’:
ssl.c:50: warning: assignment discards qualifiers from pointer target type
Building ../bin/genhash
strip ../bin/genhash
 
Make complete
make[1]: Leaving directory `/download/keepalived-1.1.19/genhash'
 
Make complete
[root@ser6-70 keepalived-1.1.19]# make install
make -C keepalived install
make[1]: Entering directory `/download/keepalived-1.1.19/keepalived'
install -d /usr/local/keepalived/sbin
install -m 700 ../bin/keepalived /usr/local/keepalived/sbin/
install -d /usr/local/keepalived/etc/rc.d/init.d
install -m 755 etc/init.d/keepalived.init /usr/local/keepalived/etc/rc.d/init.d/keepalived
install -d /usr/local/keepalived/etc/sysconfig
install -m 755 etc/init.d/keepalived.sysconfig /usr/local/keepalived/etc/sysconfig/keepalived
install -d /usr/local/keepalived/etc/keepalived/samples
install -m 644 etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/
install -m 644 ../doc/samples/* /usr/local/keepalived/etc/keepalived/samples/
install -d /usr/local/keepalived/share/man/man5
install -d /usr/local/keepalived/share/man/man8
install -m 644 ../doc/man/man5/keepalived.conf.5 /usr/local/keepalived/share/man/man5
install -m 644 ../doc/man/man8/keepalived.8 /usr/local/keepalived/share/man/man8
make[1]: Leaving directory `/download/keepalived-1.1.19/keepalived'
make -C genhash install
make[1]: Entering directory `/download/keepalived-1.1.19/genhash'
install -d /usr/local/keepalived/bin
install -m 755 ../bin/genhash /usr/local/keepalived/bin/
install -d /usr/local/keepalived/share/man/man1
install -m 644 ../doc/man/man1/genhash.1 /usr/local/keepalived/share/man/man1
make[1]: Leaving directory `/download/keepalived-1.1.19/genhash'
[root@ser6-70 keepalived-1.1.19]# 
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived

3.3:配置keepadlived

3.3.1 lvs-master配置

vi /etc/keepalived/keepalived.conf

添加内容:

global_defs {
notification_email {
               baidandan@gooagoo.com    #email 通知
        }
        notification_email_from baidandan@gooagoo.com
        smtp_server smtp.mxhichina.com 
        smtp_connect_timeout 30
        router_id LVS1          # 设置lvs的id,在一个网络内应该是唯一的
}
vrrp_sync_group test {           #设置vrrp组
group {
        loadbalance
	}
}
 
vrrp_instance loadbalance {
        state MASTER       #设置lvs的状态,报错MASTER和BACKUP两种,必须大写
        interface eth0     #设置对外服务的接口
        lvs_sync_daemon_inteface eth0   #设置lvs监听的接口
        virtual_router_id 51                     #设置虚拟路由表示
        priority 180            #设置优先级,数值越大,优先级越高
        advert_int 5           #设置同步时间间隔
		authentication {                    #设置验证类型和密码
		        auth_type PASS
		        auth_pass 1111
		}
		virtual_ipaddress {                #设置lvs vip
		        192.168.6.202
		}
}
 
virtual_server 192.168.6.202 3306 {
        delay_loop 6          #健康检查时间间隔
        lb_algo rr               #负载均衡调度算法
        lb_kind DR            #负载均衡转发规则
        #persistence_timeout 20  #设置会话保持时间,对bbs等很有用
        protocol TCP                #协议
        real_server 192.168.6.66 3306 {
        weight 3                #设置权重
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306 
}
}
        real_server 192.168.6.67 3306 {
		        weight 3
		        TCP_CHECK {
		        connect_timeout 3
		        nb_get_retry 3
		        delay_before_retry 3
		        connect_port 3306 
		}
	}
}

3.3.2 lvs-backup 配置

vi /etc/keepalived/keepalived.conf

添加:

global_defs {
notification_email {
              baidandan@gooagoo.com 
        }
        notification_email_from baidandan@gooagoo.com 
        smtp_server smtp.mxhichina.com 
        smtp_connect_timeout 30
        router_id LVS2
}
vrrp_sync_group test {
group {
        loadbalance
}
}
 
vrrp_instance loadbalance {
        state BACKUP
        interface eth0
        lvs_sync_daemon_inteface eth0
        virtual_router_id 51
        priority 150
        advert_int 5
authentication {
        auth_type PASS
        auth_pass 1111
}
virtual_ipaddress {
        192.168.6.202
}
}
 
virtual_server 192.168.6.202 3306 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        #persistence_timeout 20
        protocol TCP
        real_server 192.168.6.66 3306 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306 
}
}
        real_server 192.168.6.67 3306 {
        weight 3
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306 
}
}
}

3.3.3 Realserver配置

在所有sql节点上配置:

vi /etc/rc.d/init.d/realserver.sh

添加:

#!/bin/bash
# description: Config realserver lo and apply noarp
 
SNS_VIP=192.168.6.201
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
       /sbin/route add -host $SNS_VIP dev lo:0
       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
 
       ;;
stop)
       ifconfig lo:0 down
       route del $SNS_VIP >/dev/null 2>&1
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
 
exit 0

赋予执行权限

chmod +x /etc/rc.d/init.d/functions
chmod +x /etc/rc.d/init.d/realserver.sh

#启动

/etc/rc.d/init.d/realserver.sh start 

启动后使用ifconfig命令能看到类似下面新增的:

lo:0      Link encap:Local Loopback  

          inet addr:192.168.6.202  Mask:255.255.255.255

          UP LOOPBACK RUNNING  MTU:16436  Metric:1

注意:在sql节点一定要启动这个,否则后面无法连接mysql虚拟ip:

 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.6.202' (110)

.3.4:启动keepalived

在lvs-master,lvs-backup上启动:

service keepalived start

Starting keepalived: [ OK ]

启动keepalived后,虚拟ip就能ping通了。

#查看状态

在lvs-master查看:

[root@ser6-70 keepalived]# ip addr
1:lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3d:fd:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.70/24 brd 192.168.6.255 scope global eth0
    inet 192.168.6.202/32 scope global eth0
    inet6 fe80::5054:ff:fe3d:fd30/64 scope link 
       valid_lft forever preferred_lft forever
       

可以看到虚拟 IP 192.168.6.202.

[root@ser6-70 keepalived]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.6.202:mysql rr
  -> 192.168.6.67:mysql           Route   3      2          0         
  -> 192.168.6.66:mysql           Route   3      0          0 

– 在 lvs-backup 上查看状态

[root@ser6-71 keepalived]# ip addr
 1:lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
 2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3d:fd:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.71/24 brd 192.168.6.255 scope global eth0
    inet6 fe80::5054:ff:fe3d:fd72/64 scope link 
       valid_lft forever preferred_lft forever

这里没看到虚拟 ip ,说明这个是从 lvs.

[root@ser6-71 keepalived]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.6.202:mysql rr
  -> 192.168.6.67:mysql           Route   3      0          0         
  -> 192.168.6.66:mysql           Route   3      0          0      
  

3.4 建允许远程访问的用户

在sql节点上创建允许远程访问的用户:

grant select,update,delete,insert on *.* to 'dandan' identified by 'dandan';

3.5 测试

3.5.1 测试lvs负载均衡

#在客户端192.168.6.51连接虚拟ip

[root@ser6-51 ~]# mysql -u dandan -pdandan -h 192.168.6.202
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3677
Server version: 5.5.35-ndb-7.2.15-cluster-gpl MySQL Cluster Community Server (GPL)
 
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dba                |
| mysql              |
| ndbinfo            |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)
 
mysql> use dba;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> show tables;
+---------------+
| Tables_in_dba |
+---------------+
| t2            |
+---------------+
1 row in set (0.01 sec)
 
mysql> select * from t2;
+------+
| id   |
+------+
|    1 |
|    4 |
|    2 |
|    3 |
|    5 |
|    1 |
+------+
6 rows in set (0.01 sec)
 
mysql> delete from t2 where id = 1;
Query OK, 2 rows affected (0.07 sec)
 
mysql> select * from t2;
+------+
| id   |
+------+
|    3 |
|    5 |
|    2 |
|    4 |
+------+
4 rows in set (0.00 sec)
 
mysql> 

在 lvs-master 上查看:

[root@ser6-70 keepalived]# ipvsadm -lcn
IPVS connection entries
pro expire state       source             virtual            destination
TCP 14:29  ESTABLISHED 192.168.6.51:29569 192.168.6.202:3306 192.168.6.66:3306

看到192.168.6.51通过虚拟ip,连接到了192.168.6.66.

进sql节点192.168.6.66,看是否真的删除了数据。

[root@ser6-66 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3731
Server version: 5.5.35-ndb-7.2.15-cluster-gpl MySQL Cluster Community Server (GPL)
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use dba;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select * from t2;
+------+
| id   |
+------+
|    3 |
|    5 |
|    2 |
|    4 |
+------+
4 rows in set (0.02 sec)

真的删除了,说明客户端成功通过虚拟 ip 连接到了 sql 节点。

#在客户端192.168.6.52上登录vip

[root@ser6-52 ~]# mysql -u dandan -pdandan -h 192.168.6.202
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3739
Server version: 5.5.35-ndb-7.2.15-cluster-gpl MySQL Cluster Community Server (GPL)
 
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> 

在 lvs-master 上查看状态:

[root@ser6-70 keepalived]# ipvsadm -lcn
IPVS connection entries
pro expire state       source             virtual            destination
TCP 12:36  ESTABLISHED 192.168.6.51:29569 192.168.6.202:3306 192.168.6.66:3306
TCP 14:54  ESTABLISHED 192.168.6.52:52348 192.168.6.202:3306 192.168.6.67:3306

显示192.168.6.52连接到了192.168.6.67.说明lvs实现了负载均衡。两个客户端分别连上了不同的sql节点。

注意:只能在lvs-master上输入ipvsadm -lcn进行查看,在lvs-backup上查看是空的:

[root@ser6-71 keepalived]# ipvsadm -lcn
IPVS connection entries
pro expire state       source             virtual            destination

3.5.2 测试keepalived高可用

现在把lvs-master的keepalived关闭,看是否能实现高可用。

[root@ser6-70 keepalived]# service keepalived stop
Stopping keepalived:                                       [  OK  ]

过几秒钟后,在lvs-backup上进行查看:

[root@ser6-71 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3d:fd:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.71/24 brd 192.168.6.255 scope global eth0
    inet 192.168.6.202/32 scope global eth0
    inet6 fe80::5054:ff:fe3d:fd72/64 scope link 
       valid_lft forever preferred_lft forever
       

看到虚拟 IP 已经漂移到了 lvs-backup.

[root@ser6-71 keepalived]# ipvsadm -lcn
IPVS connection entries
pro expire state       source             virtual            destination
TCP 14:25  ESTABLISHED 192.168.6.52:54311 192.168.6.202:3306 192.168.6.67:3306
TCP 14:49  ESTABLISHED 192.168.6.51:29604 192.168.6.202:3306 192.168.6.66:3306

客户端也能够正常访问vip节点,这里不再赘述。

在lvs-master上启动keepalived后,vip会自动漂移到master节点上。

[root@ser6-70 keepalived]# service keepalived start
Starting keepalived:                                       [  OK  ]
[root@ser6-70 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3d:fd:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.70/24 brd 192.168.6.255 scope global eth0
    inet 192.168.6.202/32 scope global eth0
    inet6 fe80::5054:ff:fe3d:fd30/64 scope link 
       valid_lft forever preferred_lft forever
       

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/620076.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

在职考研 | 对于朝九晚五的上班族来说同等学力申硕更轻松!

我们要知道的是&#xff0c;考在职研究生的群体90%以上都是朝九晚五的上班族。 他们工作忙&#xff0c;时间和精力都很有限。 考研本身难度就很大&#xff0c;但是既拿不出足够多的备考时间&#xff0c;也没法到线下去上课。 怎么能考上呢&#xff1f; 在这样的现实条件下&…

yolov5部署到android studio

目录 环境获取demo将pt文件导出为ptl文件修改demo修改PrePostProcessor增加ptl文件并增加类别文件修改MainActivity 大功告成 环境 Ubuntu22.10 Pytorch2.0.1cu117 Android Studio Flamingo | 2022.2.1 Patch 1 获取demo git clone https://github.com/pytorch/android-demo…

self Attention 位置编码的奇偶输入问题

attention机制一直是放在encoder-decoder中进行使用&#xff0c;self-attention是为了解决前者结构无法并行计算&#xff0c;而抽离出的概念&#xff08;前者的编码解码多为时序网络&#xff09;。 但因为缺少时序模型天然的位置编码特点&#xff0c;所以self-attention模型需要…

Docker镜像大小优化

如何缩小镜像的体积大小&#xff1f; 1&#xff09;尽可能的使用小体积的基础镜像 2&#xff09;尽可能减少Dockerfile文件中的指令数量 3&#xff09;构建镜像步骤最后添加清空系统和应用程序的缓存的命令 4&#xff09;使用多级&#xff08;多阶段&#xff09;构建 FROM A…

亚信科技AntDB数据库荣获赛宝优选信创优秀解决方案奖

近日&#xff0c;由工业和信息化部电子第五研究所主办的中国赛宝信创生态合作伙伴大会暨赛宝优选信创优秀解决方案颁奖仪式在北京成功举办&#xff0c;亚信科技AntDB数据库信创解决方案“业务支撑系统核心数据库迁移改造”从千余案例中脱颖而出&#xff0c;荣获赛宝优选信创优秀…

文件描述符表

文章目录 1. 文件描述符1.1 前言1.2 理解文件1.3 文件描述附表1.4 打开文件时1.5 默认打开的三个文件 2. 重定向2.1 瞅瞅2.2 dup22.3 实现原理 3. 一切皆文件 1. 文件描述符 1.1 前言 环境&#xff1a;Linux 2.6 在 Linux 中&#xff0c;有句话叫做「一切皆文件」&#xff0…

Coinbase:如有必要,不惜在最高法院与SEC“一决胜负”!

6日早间&#xff0c;央视财经频道&#xff08;CCTV-2&#xff09;对“币安及其首席执行官被美证交会&#xff08;SEC&#xff09;起诉”进行了报道。 值得一提的是&#xff0c;央视并不常播报加密货币相关新闻&#xff0c;上一次是关于香港监管框架&#xff0c;赵长鹏&#xff…

< 每日算法:一文带你认识 “ 双指针算法 ” >

每日算法&#xff1a;初识双指针算法 &#x1f449; 1. 双指针概念&#xff1a;&#x1f449; 2. 左右指针> 案例一&#xff1a;二分查找> 案例二&#xff1a;双指针 - 移除元素 &#x1f449; 3. 快慢指针> 案例一&#xff1a; 删除排序数组中的重复项 &#x1f449;…

MySQL8安装详细教程

MySQL8安装详细教程 一、下载二、安装三、配置环境 一、下载 官网下载&#xff1a;点击跳转 进入官网后&#xff0c;如下界面 点击Archives可以选择版本进行下载&#xff0c;如下图根据描述操作即可。 百度云下载链接&#xff1a;点击跳转 或者复制到浏览器打开&#xff1a;链…

【1 微信小程序学习-核心思想与体验】

1 小程序的核心技术 .js文件,负责数据处理,函数定义和实现 .wxml文件,组件结构与数据绑定 .wxss文件,组件样式 .json配置信息补充 MVVM架构实现声明式编程 采用MVVM架构,核心是通过数据绑定实现响应式编程(声明式编程). M:数据处理 V:样式结构 VM:数据绑定和事件监听 vue和小程…

程序员的职场危机,能靠技术化解吗?

很多人认为&#xff0c;程序员一定要干到管理层&#xff0c;才会有继续走下去的希望&#xff0c;而踏实做技术的程序员&#xff0c;只会面临淘汰。 事实真是如此吗&#xff1f; △ 截图来源脉脉&#xff0c;如侵删 我们先不说结论&#xff0c;一起看看网友们的看法&#xff1…

Linux | Job control

Linux | Job control 文章目录 Linux | Job control命令直接将命令丢到后台中“执行”的 &将当前工作丢到后台“暂停”&#xff1a;Ctrl z查看后台工作状态&#xff1a;jobs将后台工作拿到前台处理&#xff1a;fg让工作在后台下的状态变成运行中&#xff1a;bg管理后台工作…

Qt6之vc调用qt生成的带ui的dll

受益于跨平台的特性&#xff0c;Qt生成dll的各种语言调用上体现的淋漓尽致&#xff0c;上篇已经演示了qt生成的无ui的dll及vc如何调用它&#xff0c;本篇将演示vc如何调用qt生成的带ui的dll。 传统观点认为Qt生成的带ui的dll受限于 QApplication 的 exec 方法&#xff0c;这样才…

《精通特征工程》学习笔记(6):非线性特征化与k-均值模型堆叠

1.非线性特征 当数据位于一个薄饼状的线性子空间时&#xff0c;PCA 是非常有用的。但如果数据形成了一个更加复杂的形状&#xff0c;情况又将如何呢&#xff1f; 如果线性子空间是一张平展的纸&#xff0c;那么非线性流形的一个简单例子就是卷起来的纸&#xff0c;它有 个非…

《人人健康》期刊简介及投稿邮箱

《人人健康》期刊简介及投稿邮箱 《人人健康》期刊简介&#xff1a; 主管单位&#xff1a;山西出版传媒集团有限责任公司 主办单位&#xff1a;山西三晋报刊传媒集团有限责任公司 国际刊号ISSN&#xff1a;1004-597X 国内刊号CN&#xff1a;14-1033/R 邮发代号&#xff1…

不甘做小弟,JS时间对象又在搞事情!(上)

关注“大前端私房菜”微信公众号&#xff0c;回复暗号【面试宝典】即可免费领取107页前端面试题。 Date Date 是 js 的一个内置对象&#xff0c;也叫内置构造函数。提供了一堆的方法帮助我们更方便的操作时间 创建时间对象&#xff1a;new Date() 获取时间对象&#xff1a;ne…

NTSC和PAL视频格式的区别

1&#xff0c;历史演变 NTSC早期描述的是525I59.94格式视频&#xff0c;1953年确立标准&#xff0c;美国主导。后来为了方便和PAL制式格式的视频转换&#xff0c;于是将60hz的倍数关系的帧率纳入该系统。 PAL描述的是625I50格式视频&#xff0c;1967年确立标准&#xff0c;欧洲…

完全免费白嫖 GPT-4 的终极方案!

原文链接&#xff1a;https://icloudnative.io/posts/completely-free-to-use-gpt4/ GPT-4 目前是世界上最强的多模态大模型&#xff0c;能力甩 GPT-3.5 好几条街。 大家都希望早日用上 GPT-4&#xff0c;不过目前体验 GPT-4 的渠道非常有限&#xff0c;要么就是开通 ChatGPT …

Java编程笔记31:Record

Java编程笔记31&#xff1a;Record 图源&#xff1a;Fotor懒设计 在日常使用的时候&#xff0c;我们往往需要创建一些“仅用于传输数据的类型”&#xff0c;比如Web编程时候的DTO。 将特殊用途的类型限制为“只读”的一个好处是&#xff0c;这些类型可以安全地在多线程之间共享…

uniapp开发小程序-显示左滑删除效果

一、效果图&#xff1a; 二、代码实现&#xff1a; <template><view class"container"><view class"myorderList"><uni-swipe-action><uni-swipe-action-item class"swipe-action-item" :right-options"option…