微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Haproxy+Keepalived+MariaDB(Galera集群)

Haproxy+Keepalived+MariaDB(galera集群)

1. 环境

1.1 操作系统:CentOS 7.*
1.2 机器:
  1. 192.16.16.108 haproxy01(haproxy+keepalived)
  2. 192.16.16.109 haproxy02(haproxy+keepalived)
  3. 192.16.16.100 MysqL01(mariadb)
  4. 192.16.16.101 MysqL02(mariadb)

2. 准备工作

2.1 关闭防火墙(所有服务器)

# systemctl stop firewalld.service
# systemctl disable firewalld.service
# setenforce 0
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

2.2 修改/etc/hosts文件

# cat <<END >>/etc/hosts
192.16.16.108  haproxy01
192.16.16.109  haproxy02
192.16.16.100  MysqL01
192.16.16.101  MysqL02
END

3. 配置MariaDB(galera集群)

3.1 运行在MysqL01和MysqL02

# yum install epel-release -y
# yum install centos-release-openstack-newton -y
# yum makecache
# yum install mariadb mariadb-server-galera galera rsync -y
# cd  /etc/my.cnf.d
# cp galera.cnf galera.cnf.bak
# cp mariadb-server.cnf mariadb-server.cnf.bak
# cat <<END >mariadb-server.cnf
[MysqLd]
###认配置###
datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

###新增配置###
bind-address = 192.16.16.100 or 192.16.16.101
default-storage-engine = innodb
innodb_file_per_table
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8
END

# cat <<END >galera.cnf
[MysqLd]
###新增配置###
wsrep_on=ON
binlog_format=ROW
default-storage-engine=innodb
bind-address=192.16.16.100 or 192.16.16.101
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_name="my_wsrep_cluster"
wsrep_cluster_address="gcomm://MysqL01,MysqL02"
wsrep_node_name=MysqL01 or MysqL02
wsrep_node_address=192.16.16.100 or 192.16.16.101
wsrep_sst_method=rsync
wsrep_sst_auth=root:

###认配置###
wsrep_slave_threads=1
innodb_autoinc_lock_mode=2
wsrep_certify_nonPK=1
wsrep_max_ws_rows=131072
wsrep_max_ws_size=1073741824
wsrep_debug=0
wsrep_convert_LOCK_to_trx=0
wsrep_retry_autocommit=1
wsrep_auto_increment_control=1
wsrep_drupal_282555_workaround=0
wsrep_causal_reads=0
wsrep_notify_cmd=
END

3.2 运行在MysqL01上

# galera_new_cluster
# MysqL_secure_installation

3.3 运行在MysqL02上

# systemctl start mariadb.service
# MysqL_secure_installation

3.4 验证galera集群

4. 安装配置keepalived

4.1 运行在haproxy01和haproxy02上

# yum install keepalived -y
# cd /etc/keepalived
# cp keepalived.conf keepalived.conf.bak
# cat <<END >notify.sh
#!/bin/bash

case "$1" in
    master)
        systemctl start haproxy.service
        exit 0
    ;;
    backup)
        systemctl stop haproxy.service
        exit 0
    ;;
    fault)
        systemctl stop haproxy.service
        exit 0
    ;;
    *)
        echo "Usage: `basename $0` {master|backup|fault}"
        exit 1
    ;;
esac
END

4.2 运行在haproxy01上

# cat <<END >keepalived.conf
global_defs {
    router_id haproxy01
}

vrrp_instance VI_1 {
    state MASTER
    virtual_router_id 51
    interface ens33
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass e6895c34dc6b711d
    }

    virtual_ipaddress {  
        192.16.16.10/24
    }

    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"

}
END
# systemctl restart keepalived.service

4.3 运行在haproxy02上

# cat <<END >keepalived.conf
global_defs {
    router_id haproxy02
}

vrrp_instance VI_1 {
    state BACKUP
    virtual_router_id 51
    interface ens33
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass e6895c34dc6b711d
    }

    virtual_ipaddress {
        192.16.16.10/24
    }

    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"

}
END
# systemctl restart keepalived.service

4.4 查看VIP

  • 可以在haproxy01上查看到secondary ip(VIP)
  • 在haproxy01上,执行# systemctl stop keepalived.service,可以查看到haproxy01上VIP消失
  • 登录haproxy02上,可以查看到secondary ip(VIP)
  • 重新启动haproxy01上的keepalived服务,VIP回到haproxy01上

5. 安装配置HAproxy

5.1 运行在haproxy01和haproxy02上

# yum install haproxy -y
# cd /etc/haproxy
# cp haproxy.cfg haproxy.cfg.bak

5.2 运行在haproxy01上

# cat <<END >haproxy.cfg
global
    log         127.0.0.1 local2
    
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    # option                  httplog
    option                  dontlognull
    option http-server-close
    # option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
    bind 192.16.16.10:1080
    mode http
    option httplog
    maxconn 10
    stats refresh 30s
    stats uri /stats
    stats realm XingCloud\ Haproxy
    stats auth admin:admin
    stats hide-version
    stats admin if TRUE

#listen webserver
#    bind 192.16.16.10:8080
#    balance roundrobin
#    mode http
#    option httplog
#    server MysqL01 192.16.16.100:80 check inter 1500 rise 3 fall 3 weight 1
#    server MysqL02 192.16.16.101:80 check inter 1500 rise 3 fall 3 weight 1

listen MysqLdb
    bind 192.16.16.10:3307
    balance roundrobin
    mode tcp
    option tcpka
    option httpchk
    server MysqL01 192.16.16.100:3306 weight 1
    server MysqL02 192.16.16.101:3306 weight 1
END
# systemctl restart haproxy.service

5.6 运行在haproxy02上

# cat <<END >haproxy.cfg
global
    log         127.0.0.1 local2
    
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    # option                  httplog
    option                  dontlognull
    option http-server-close
    # option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
    bind 192.16.16.10:1080
    mode http
    option httplog
    maxconn 10
    stats refresh 30s
    stats uri /stats
    stats realm XingCloud\ Haproxy
    stats auth admin:admin
    stats hide-version
    stats admin if TRUE

#listen webserver
#    bind 192.16.16.10:8080
#    balance roundrobin
#    mode http
#    option httplog
#    server MysqL01 192.16.16.100:80 check inter 1500 rise 3 fall 3 weight 1
#    server MysqL02 192.16.16.101:80 check inter 1500 rise 3 fall 3 weight 1

listen MysqLdb
    bind 192.16.16.10:3307
    balance roundrobin
    mode tcp
    option tcpka
    option httpchk
    server MysqL01 192.16.16.100:3306 weight 1
    server MysqL02 192.16.16.101:3306 weight 1
END

6. 验证haproxy是否生效

  • 首先访问192.16.16.10::1080/stats,admin/admin认证

    image

  • 可以看到MysqLdb中的session处,两个都为0
  • 现在通过客户端,访问192.16.16.10:3307,root/123456,访问4次

    image

  • 此时,可以看到MysqL01和MysqL02都有2个session,总共是4次,即表示两个节点都访问了2次,符合haproxy的roundrobin策略

7. 验证keepalived是否生效

  • 在上个验证的基础上,在haproxy01上执行# systemctl stop keepalived.service命令
  • 再次访问192.16.16.10:1080/stats,admin/admin认证,能成功访问

丶追风 发布了6 篇原创文章 · 获赞 2 · 访问量 7589 私信 关注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐