mysql环形复制加HA_mysql复制+keepalived+haproxy配置(负载均衡)

双主+keepalived+haproxy配置(负载均衡)

实验系统:CentOS 6.5_x86_64

实验前提:防火墙和selinux都关闭

实验软件:keepalived-1.2.13 haproxy-1.8.13 mysql—5.7.21

主1 ip:192.168.226.134

主2 ip:192.168.226.135

vip 192.168.226.150

一、安装mysql

获取mysql安装包:wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

安装mysql过程省略

二、配置主主复制

1 命令 service iptables stop

2 检查:service iptables status

3 关闭vi /etc/selinux/config

4 SELINUX=disabled

两台服务器上都要执行:

创建复制用户

1 grant replication slave on *.* to 'repl'@'%' identified by '123';

进入从服务器mysql

命令: # mysql -uroot -p

关闭slave

命令:stop slave;

开始配置:

输入下面代码即可:

1 CHANGE MASTER TO MASTER_HOST='192.168.226.135', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=681;

先在从服务器配置完成,启动从服务器:

命令: start slave;

反向再配置一次

三、安装haproxy(两台节点都要安装)

1 tar -zxvxf haproxy-1.8.13.tar.gz

2 cd haproxy-1.8.13

3 make TARGET=linux2628 //根据自己主机设定

4 make install

提供启动脚本

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 vi /etc/init.d/haproxy

2

3 #!/bin/sh

4 #

5 # haproxy

6 #

7 # chkconfig: - 85 15

8 # description: HAProxy is a free, very fast and reliable solution \

9 # offering high availability, load balancing, and \

10 # proxying for TCP and HTTP-based applications

11 # processname: haproxy

12 # config: /etc/haproxy/haproxy.cfg

13 # pidfile: /var/run/haproxy.pid

14

15 # Source function library.

16 . /etc/rc.d/init.d/functions

17

18 # Source networking configuration.

19 . /etc/sysconfig/network

20

21 # Check that networking is up.

22 [ "$NETWORKING" = "no" ] && exit 0

23

24 exec="/usr/local/sbin/haproxy"

25 prog=$(basename $exec)

26

27 [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

28

29 cfgfile=/etc/haproxy/haproxy.cfg

30 pidfile=/var/run/haproxy.pid

31 lockfile=/var/lock/subsys/haproxy

32

33 check() {

34 $exec -c -V -f $cfgfile $OPTIONS

35 }

36

37 start() {

38 $exec -c -q -f $cfgfile $OPTIONS

39 if [ $? -ne 0 ]; then

40 echo "Errors in configuration file, check with $prog check."

41 return 1

42 fi

43

44 echo -n $"Starting $prog: "

45 # start it up here, usually something like "daemon $exec"

46 daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS

47 retval=$?

48 echo

49 [ $retval -eq 0 ] && touch $lockfile

50 return $retval

51 }

52

53 stop() {

54 echo -n $"Stopping $prog: "

55 # stop it here, often "killproc $prog"

56 killproc $prog

57 retval=$?

58 echo

59 [ $retval -eq 0 ] && rm -f $lockfile

60 return $retval

61 }

62

63 restart() {

64 $exec -c -q -f $cfgfile $OPTIONS

65 if [ $? -ne 0 ]; then

66 echo "Errors in configuration file, check with $prog check."

67 return 1

68 fi

69 stop

70 start

71 }

72

73 reload() {

74 $exec -c -q -f $cfgfile $OPTIONS

75 if [ $? -ne 0 ]; then

76 echo "Errors in configuration file, check with $prog check."

77 return 1

78 fi

79 echo -n $"Reloading $prog: "

80 $exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)

81 retval=$?

82 echo

83 return $retval

84 }

85

86 force_reload() {

87 restart

88 }

89

90 fdr_status() {

91 status $prog

92 }

93

94 case "$1" in

95 start|stop|restart|reload)

96 $1

97 ;;

98 force-reload)

99 force_reload

100 ;;

101 check)

102 check

103 ;;

104 status)

105 fdr_status

106 ;;

107 condrestart|try-restart)

108 [ ! -f $lockfile ] || restart

109 ;;

110 *)

111 echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"

112 exit 2

113 esac

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 chkconfig --add haproxy

2 chkconfig haproxy on

3 chmod +x /etc/init.d/haproxy

提供配置文件

1 mkdir /etc/haproxy

2 mkdir /var/lib/haproxy

3 useradd -r haproxy

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 vi /etc/haproxy/haproxy.cfg

2

3 global

4

5 log 127.0.0.1 local2

6

7 chroot /var/lib/haproxy

8 pidfile /var/run/haproxy.pid

9 maxconn 4000

10 user haproxy

11 group haproxy

12 daemon

13

14 stats socket /var/lib/haproxy/stats

15

16 defaults

17 mode tcp //haproxy运行模式

18 log global

19 option dontlognull

20 option redispatch

21 retries 3

22 timeout http-request 10s

23 timeout queue 1m

24 timeout connect 10s

25 timeout client 1m

26 timeout server 1m

27 timeout http-keep-alive 10s

28 timeout check 10s

29 maxconn 600 //最大连接数

30

31 listen stats //配置haproxy状态页

32 mode http

33 bind :6677 //找一个比较特殊的端口

34 stats enable

35 stats hide-version //隐藏haproxy版本号

36 stats uri /haproxyadmin?stats //一会用于打开状态页的uri

37 stats realm Haproxy\ Statistics //输入账户密码时的提示文字

38 stats auth admin:admin //用户名:密码

39 stats admin if TRUE //开启状态页的管理功能

40

41 frontend main

42 bind *:80

43 default_backend mysql //后端服务器组名

44

45 backend mysql

46 balance leastconn //使用最少连接方式调度

47 server m1 192.168.226.134:80 check port 80 maxconn 300

48 server m2 192.168.226.135:80 check port 80 maxconn 300

48304ba5e6f9fe08f3fa1abda7d326ab.png

启动日志:

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 vi /etc/rsyslog.conf

2

3 # Provides UDP syslog reception //去掉下面两行注释,开启UDP监听

4 $ModLoad imudp

5 $UDPServerRun 514

6

7 local2.* /var/log/haproxy.log //添加此行

8

9 service rsyslog restart

48304ba5e6f9fe08f3fa1abda7d326ab.png

启动测试haproxy:

1 service haproxy start

2 netstat -tnlp

四、安装keepalived (两台服务器都要执行)

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 yum install -y keepalived

2

3 chkconfig --add keepalived

4 chkconfig keepalived on

5

6 mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

7 vi /etc/keepalived/keepalived.conf

48304ba5e6f9fe08f3fa1abda7d326ab.png

粘贴如下内容

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 ! Configuration File for keepalived

2 global_defs {

3 router_id Mysql_ha

4 }

5

6 vrrp_script chk_mysql {

7 script "/etc/keepalived/check_mysql.sh"

8 interval 2

9 weight 5

10 }

11

12 vrrp_script chk_haproxy {

13 script "/etc/keepalived/chk.sh"

14 interval 2

15 weight 5

16 }

17

18 vrrp_instance mysql-instance {

19 state MASTER #另一台为BACKUP

20 interface enp4s0 #与网卡名称对应

21 virtual_router_id 11 #每一个IP唯一,另一台绑定相同IP要与整个ID一致

22 priority 10 #另一台为9

23 advert_int 1

24 authentication {

25 auth_type PASS

26 auth_pass password321

27 }

28 track_script {

29 chk_mysql

30 }

31 virtual_ipaddress {

32 192.168.226.150/24

33 }

34 }

35

36

37 vrrp_instance mysql-ha {

38 state MASTER

39 interface enp4s0

40 virtual_router_id 13

41 priority 10

42 advert_int 1

43 # nopreempt

44 authentication {

45 auth_type PASS

46 auth_pass password321

47 }

48 track_script {

49 chk_haproxy

50 }

51 virtual_ipaddress {

52 192.168.226.14/24

53 }

54 notify_backup "/etc/init.d/haproxy restart"

55 notify_fault "/etc/init.d/haproxy stop"

56 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

在两台机器上创建chk.sh文件:

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 vi /etc/keepalived/chk.sh

2

3 #!/bin/bash

4 #

5 if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then

6 /etc/init.d/keepalived stop

7 fi

8

9 chmod +x /etc/keepalived/chk.sh

10

11 vi /etc/keepalived/check_mysql.sh

12 #!/bin/bash

13 MYSQL_PORT=:3306

14 alive=$(netstat -apn |grep $MYSQL_PORT|grep LISTEN)

15 if [ -z "$alive" ]

16 then

17 exit 1

18 else

19 exit 0

20 fi

48304ba5e6f9fe08f3fa1abda7d326ab.png

五、在两台机器上测试

.在浏览器打开http://192.168.226.150:6677/haproxyadmin?stats,打开haproxy状态页:

23fd97f87ef19bc67a33bfb470fa38d7.png

bcd9edddf49e641dd4325d80691f1edf.png


版权声明:本文为weixin_39955781原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。