keepalived+nginx+mysql achieve high availability and load balancing


Recently, I have fiddled with the high availability scheme of the mysql database. With the help of mysql's official InnoDB Cluster and nginx+keepalived. can be done easily. The effect and stability are satisfactory.


Foreword:

First of all, by default you have installed the mysql cluster and the cluster is available. If you will not install it, you can refer to another blog post: http://blog.csdn.net/kokjuis/article/details/78401022

The installation of mysql  InnoDB Cluster is not introduced here, only the high availability configuration of mysql through nginx+keepalived is introduced.

In fact, it should be highly available for mysql-router. Because mysql  InnoDB Cluster itself is a high-availability solution for mysql. It's just that there may be a single point of problem with mysql-router in it.

mysql-router itself does not provide a high availability solution, but we can achieve load balancing and high availability through nginx, and the high availability of nginx can be achieved through the keepalived active and standby mode.


install nginx


First install the dependencies required by nginx

1. gcc installation


To install nginx, you need to compile the source code downloaded from the official website first. The compilation depends on the gcc environment. If there is no gcc environment, you need to install:

yum install gcc-c++


Two. PCRE pcre-devel installation
PCRE (Perl Compatible Regular Expressions) is a Perl library, including perl compatible regular expression library. The http module of nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux. pcre-devel is a secondary development library developed with pcre. nginx also requires this library. Order:


yum install -y pcre pcre-devel


3. zlib Installing
the zlib library provides a variety of compression and decompression methods. nginx uses zlib to gzip the content of the http package, so the zlib library needs to be installed on Centos.


yum install -y zlib zlib-devel


4. OpenSSL Installation
OpenSSL is a powerful secure socket layer cryptographic library, including major cryptographic algorithms, commonly used key and certificate encapsulation management functions and SSL protocols, and provides a wealth of applications for testing or other purposes.
Nginx supports not only the http protocol, but also https (that is, transmitting http over the ssl protocol), so you need to install the OpenSSL library on Centos.


yum install -y openssl openssl-devel

install nginx

Go to the official website to download nginx directly. Download .tar.gzthe installation package directly, address: https://nginx.org/en/download.html

Note: proxy mysql needs to support tcp. Only nginx1.9 and above can support the tcp protocol, just download the latest version from the official website;


Install:

tar -zxvf nginx-1.13.7.tar.gz
cd nginx-1.13.7

Compile nginx:

Notice:

Nginx implements the TCP proxy function in two ways :

One is to use the nginx_tcp_proxy_module module, which is generally used in earlier versions of Nginx .

One is to use the official ngx_stream_core_module module for 1.9 and later.

It is recommended to use the official ngx_stream_core_module.

只有在configure时使用了--with-stream参数,编译出来的nginx程序才支持stream方式实现TCP代理。

./configure --prefix=/usr/nginx  --with-stream --with-stream_ssl_module
make
#已经在使用的nginx则不需make install,否则就覆盖了
make install

配置tcp代理功能

nginx.conf配置文件中添加

stream配置块以及其中的server配置块。

stream {

upstream mysql {

hash $remote_addr consistent;
#mysql-router连接地址
server 172.30.12.14:7001 weight=5 max_fails=3 fail_timeout=30s;
server 172.30.12.17:7001 weight=5 max_fails=3 fail_timeout=30s;

}

server {

listen 7777;
#建立连接超时时间
proxy_connect_timeout 5s;
#proxy_timoute指定Nginx与客户端,以及Nginx与被代理的主机之间在两个相邻的读或写数据的操作之间的最大时间间隔。超过此时间间隔而没有数据读或写操作发生,则断开连接。
proxy_timeout 5m;

proxy_pass mysql;

}

}

As for other nginx configurations, I won't talk about it here. many online


Installation and configuration of Keepalived:

Download the latest version of keepalived from the official website .

Installation steps for keepalived


#decompress
tar -zxvf keepalived-1.2.24.tar.gz
# enter the directory
cd keepalived-1.2.24
#compile
./configure --prefix=/usr/keepalived
#create folder
mkdir /etc/keepalived


#copy configuration
cp /usr/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/keepalived-1.3.9/keepalived/etc/init.d/keepalived /usr/keepalived/etc/rc.d/init.d/keepalived
cp /usr/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/keepalived/etc/sysconfig/keepalived /etc/sysconfig/




#authorization
chmod +x /etc/rc.d/init.d/keepalived


#Add soft link
ln -s /usr/sbin/keepalived/usr/sbin/
ln -s /usr/keepalived/sbin/keepalived/sbin/
#Add to service
chkconfig --add /etc/init.d/keepalived
chkconfig keepalived on


#View service
chkconfig --list keepalived

Note:

cp /usr/keepalived-1.3.9/keepalived/etc/init.d/keepalived /usr/keepalived/etc/rc.d/init.d/keepalived
The new version may not exist in keepalived, just copy it from /keepalived-1.3.9/keepalived/etc/init.d/ directory.

system prompt warning

 *** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
安装:
yum -y install libnl libnl-devel


The system prompts an error 

configure: error: libnfnetlink headers missing

Install
yum install -y libnfnetlink-devel


Configure keepalived.conf

host:

global_defs {
# Set the id of the nginx master, which should be unique in a network
    router_id nginx_master
}
vrrp_script chk_http_port {
#Finally execute this script manually to ensure that this script can be executed normally
    script "/etc/keepalived/check_nginx_pid.sh"
	 # (Interval for detecting script execution, in seconds)
    interval 2
    weight 2
	#Detect 2 consecutive failures to determine whether it is a true failure. Will use weight to reduce the priority (between 1-255)
	fall 2
	#Detecting 1 time is successful even if it is successful. but do not modify the priority
	rise 1
}
vrrp_instance VI_1 {
# 指定keepalived的角色,MASTER为主,BACKUP为备
    state MASTER
	# 当前进行vrrp通讯的网络接口卡(当前centos的网卡),通过命令 ip addr 可以查看,必须要填对
    interface enp2s0
	# 虚拟路由编号,主从要一致
    virtual_router_id 66
	# 优先级,数值越大,获取处理请求的优先级越高
    priority 100
	# 检查间隔,默认为1s(vrrp组播周期秒数)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
	#(调用检测脚本)
    chk_http_port
    }
    virtual_ipaddress {
	# 定义虚拟ip(VIP),可多设,每行一个
        172.30.12.200
    }
}


备:

global_defs {
# 设置nginx master的id,在一个网络应该是唯一的
    router_id nginx_slave
}
vrrp_script chk_http_port {
#最后手动执行下此脚本,以确保此脚本能够正常执行
    script "/etc/keepalived/check_nginx_pid.sh"
	 #(检测脚本执行的间隔,单位是秒)
    interval 2
    weight 2
	#检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
	fall 2
	#检测1次成功就算成功。但不修改优先级
	 rise 1
}
vrrp_instance VI_1 {
# 指定keepalived的角色,MASTER为主,BACKUP为备
    state BACKUP
	# 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
    interface enp2s0
	# 虚拟路由编号,主从要一致
    virtual_router_id 66
	# 优先级,数值越大,获取处理请求的优先级越高
    priority 99
	# 检查间隔,默认为1s(vrrp组播周期秒数)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
	#(调用检测脚本)
    chk_http_port
    }
    virtual_ipaddress {
	# 定义虚拟ip(VIP),可多设,每行一个
        172.30.12.200
    }
}


nginx detection script:
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
      /usr/nginx/sbin/nginx                #重启nginx
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx restart fails, stop the keepalived service and perform VIP transfer
              killall keepalived                    
      be
be

#Start service
service keepalived start
#Out of service
service keepalived stop


Started successfully:

Note: When keepalived starts, it will not check whether the syntax of the configuration file is correct, so we must be very careful when writing the configuration file, do not write it wrong, otherwise there will be some unexpected phenomena.


Use the ip addr command to check that the virtual IP has taken effect .

Note: Only the master service takes effect at the same time, and the vip will be shifted to the slave only after the master server goes down. After the host is restored, the vip will be automatically shifted to the host again.




Then you can access the database through vip (172.30.12.200:7777), and high availability and load balancing have been achieved








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325393016&siteId=291194637