Use HaProxy to achieve high availability of Mycat

Step 1: Install Xinetd

  • throughxinetd.HAProxyYou can usehttpchkTo detectMyCatThe survival state of.
  • xinetdextended internet daemon.xinetdIs a new generation of network daemon services, also called superInternetThe server. Often used to manage multiple lightweightsInternetService.
  • implementationMyCatHeartbeat detection port: 48700

1. If xinetd has not been installed, run the following command to install it:

# yum install xinetd
Copy the code

Conf includedir /etc/xinetd.d includedir /etc/xinetd.d includedir /etc/xinetd.d

# vi /etc/xinetd.conf
Copy the code

3. Check whether the /etc/xinetd.d directory exists

# mkdir /etc/xinetd.d/
Copy the code

4. Added the MyCat survival detection service configuration

# touch /etc/xinetd.d/mycat_status
# vi /etc/xinetd.d/mycat_status
Copy the code

Add the following content (chmod 777 /etc/xinetd.d/mycat_status) :

service mycat_status
{
    flags = REUSE
    socket_type = stream
    port = 48700
    wait = no
    user = root
    server =/usr/local/bin/mycat_status
    log_on_failure += USERID
    disable = no
}
Copy the code

5. Add the /usr/local/bin/mycat_status service script

   # touch /usr/local/bin/mycat_status
    # vi /usr/local/bin/mycat_status
Copy the code

Add the following:

#! /bin/bash #/usr/local/bin/mycat_status.sh # This script checksif a mycat server is healthy running on localhost.
# It will return:
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`/usr/local/mycat/bin/mycat status | grep 'not running' | wc -l`
if [ "$mycat" = "0" ]; then
/bin/echo -e "HTTP / 1.1 200 OK \ r \ n"
else
/bin/echo -e "HTTP / 1.1 503 Service Unavailable \ r \ n"
fi
Copy the code

6. Grant the execute permission to the new script

# chmod 777 /usr/local/bin/mycat_status
Copy the code

Add mycat_status to /etc/services

   # vi /etc/services
Copy the code

Add at the end :(vim uses shift +g to the end of text)

mycat_status 48700/tcp # mycat_status
Copy the code

Save the Settings and restart the xinetd service

# service xinetd restart
Copy the code

8. Verify that the mycat_status service is successfully started

# netstat -antup|grep 48700
Copy the code

Step 2: Install HAProxy (MyCat load balancing)

HAProxy supports TCP (Layer 4) and HTTP (Layer 7) proxy applications. In this lesson, we will use HAProxy to make MyCat load balancing proxy using TCP mode.

In tier 4 mode:

  • HAProxyOnly bidirectional traffic is forwarded between the client and server.
  • HAProxySimple configuration, has very good server health check function, when its proxy back-end server fails,HAProxy

    The server is automatically removed and added to the system after the fault is rectified.

HAProxy installation 1. Download or upload haproxy-1.5.16.tar.gz to /usr/local and decompress the installation package

[root@edu-haproxy-01 src]# tar -zxvf haproxy-1.516..tar.gz
[root@edu-haproxy-01 src]# cd haproxy-1.516.
Copy the code

2. If you need to know the installation precautions, please check the software description of HAProxy

[root@edu-haproxy-01 haproxy-1.516.]# less README
Copy the code

3. Install the dependency packages required for compilation

# yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
Copy the code

4, compile,

# make TARGET=linux2628 ARCH=x86_64 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1PREFIX=/usr/local/haproxy ## TARGET specifies the kernel version, higher than2.628.# uname -r, ARCH specifies the system architecture, openSSL PCre zlib these three packages need to be installed or not supportedCopy the code

5. Create the /usr/local/haproxy installation directory

# mkdir /usr/local/haproxy
Copy the code

6. Perform the installation

[root@edu-haproxy-01 haproxy-1.516.]# make install PREFIX=/usr/local/haproxy
Copy the code

7. Create a configuration file directory

# mkdir -p /usr/local/haproxy/conf
# mkdir -p /etc/haproxy/
Copy the code

8. Copy the configuration file from the configuration file template and add the configuration file soft connection

# cp /usr/local/haproxy-1.516./examples/haproxy.cfg /usr/local/haproxy/conf/
# ln -s /usr/local/haproxy/conf/haproxy.cfg /etc/haproxy/haproxy.cfg
Copy the code

9. Copy the error page and add a directory soft connection (HTTP mode optional)

# cp -r /usr/local/haproxy-1.516./examples/errorfiles /usr/local/haproxy/
# ln -s /usr/local/haproxy/errorfiles /etc/haproxy/errorfiles
Copy the code

10. Copy the startup file and grant the executable permission

# cp /usr/local/haproxy-1.516./examples/haproxy.init /etc/rc.d/init.d/haproxy
# chmod +x /etc/rc.d/init.d/haproxy
Copy the code

11. Add haProxy command script soft connection

# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin
Copy the code

12. Set HAProxy to boot

# chkconfig --add haproxy
# chkconfig haproxy on
Copy the code

Conf /haproxy. CFG:

Parameters in the global configuration are process-level parameters, usually related to the operating system on which the global log is running127.0. 01.Local0 info ## Defines the syslog server globally, at most2The ### local0 is the log device, corresponding to the configuration in /etc/rsyslog.conf, which defaults to the log level of info #log127.0. 01.Local1 info chroot /usr/share/haproxy #group haproxy #group haproxy # uid But here we use the user name daemon ## to set up the haProxy daemon form to run nbProc1## specifies the number of haproxy processes to start. Start by default1Maxconn is generally used only in fields where a single process can open only a few file descriptors4096Set the maximum number of concurrent connections per HaProxy process, which is equivalent to the command line option"-n"."ulimit-n"Automatically computed results are formally referenced from parameter Settings # pidfile /var/run/haproxy.pid ## Process file (default path /var/ run/haproxy. Pid) node edu - haproxy -01## Define the name of the current node, used in HA scenarios where multiple haproxy processes share the same IP address01## defaults: Used to provide default parameters for all other configuration segments, which can be specified by the next"defaults"Log global ## mode HTTP ## mode: specifies the mode in which the file is processed (TCP: layer 4, HTTP: layer 7, health: OK) The instance runs in pure TCP mode and establishes a full-duplex connection between the client and server, ####, with no connection7HTTP: The instance is running in HTTP mode. Client requests are deeply analyzed before being forwarded to the backend server. #### All requests that are not compatible with RFC mode are rejected. The instance runs in health mode and only responds to an inbound request with an "OK" message and closes the connection. #### does not record any log message. This mode is used for option Httplog retries of corresponding external components3Option redispatch ## serverId after the corresponding server fails, force redirection to another healthy server maxCONN2000## Maximum number of concurrent connections for the front-end (default2000### this cannot be used in the Backend section. For large sites, it is possible to increase this value to allow HAProxy to manage the connection queue, ### to avoid failure to respond to user requests. Of course, this maximum cannot exceed the definition in the "global" section. In addition, keep in mind that HaProxy maintains two buffers per connection, each of which is 8KB in size. ### plus other data, each connection will take up approximately 17KB of RAM space, which means that with proper optimization, ### will be maintained with 1GB of available RAM space40000-50000Concurrent connection. ### If you specify a value that is too large, in extreme scenarios it may end up taking up more space than the available memory of the current host. ### This may have unexpected results, so set it to an acceptable value of sensible absolute, which defaults to2000Timeout Client 50000ms server 50000ms ## # HAProxy listen admin_stats bind:48800## select * from 'admin' where user = 'admin'; ## select * from 'admin' where user = 'admin' Httplog ## enable HTTP request ## listen: Listen mycat_Servers bind to define a complete proxy by associating "front end" and "back end", usually only useful for TCP traffic:3388Option tcpka ## Whether to allow sending keepalive option HTTPCHK OPTIONS * HTTP/ to the server and client1.1\r\nHost:\ WWW ## Back-end service status detection ###48700The port (the port value is configured by xinetd on the backend server) sends the OPTIONS request ###, and HAProxy determines whether the backend service is available based on the returned content. Other response codes or no response indicate a server fault. Balance Roundrobin ## Defines the load balancing algorithm that can be used"defaults","listen"and"backend"The default polling mode is Server mycat_01192.16825.100.:8066 check port 48700 inter 2000ms rise 2 fall 3 weight 10
    server    mycat_02 192.16825.101.:8066 check port 48700 inter 2000ms rise 2 fall 3 weight 10## format: server <name> <address>[:[port]] [param*] ### serser declares a server on the backend, which can only be used in the listen and backend sections. ### <name> Specifies the internal name of this server, which will appear in logs and warnings. ### <address> Specifies the IPv4 address of this server. ### [:[port]] specifies the destination port on which the client connection request will be sent to the server. #### weight: indicates the weight. The default value is1, the maximum value is256.0#### backup: Indicates that the server is not involved in load balancing. #### Backup: Indicates that the server is configured as the standby server and cannot be enabled for other servers only in load balancing scenarios #### check: indicates that the monitoring status check of the server is enabled. It can be more elaborate with additional parameters #### inter: Sets the interval for monitoring status checks, in milliseconds, default to2000##### can also be used with Fastinter and downinter to optimize this event delay based on server side topics #### rise: Sets the number of times the server needs to be checked from offline to normal (if this is not set, the default is2#### fall: Sets the number of times that a server needs to check when switching from the normal state to the offline state. If this parameter is not set, the default value is3#### cookie: Sets the cookie value for the specified server. The value specified here will be checked on incoming requests. The server selected for this value for the first time by ##### will be selected for subsequent requests. #### maxconn: specifies the maximum number of concurrent connections that this server can accept. If the number of connections sent to this server is higher than the value specified here, ##### will be placed on the request queue until other connections are releasedCopy the code

Create a path for chroot to run

mkdir /usr/share/haproxy
Copy the code

Enable the log recording function of Haproxy

By default, haProxy does not record logs. To record logs, you need to configure syslog, which is the Rsyslog service in Linux.

# yum install rsyslog 
# vi /etc/rsyslog.conf
Copy the code

Add the following

$ModLoad imudp ## is the module name supporting UDP protocol $UDPServerRun514
$IncludeConfig /etc/rsyslog.d/*.conf 
Copy the code

Enter the loading directory:

# CD/etc/rsyslog. D / # # # touch rsyslog to this directory will be loaded configuration haproxy. Conf # # # to create haproxy log configuration file vi/etc/rsyslog. D/haproxy. ConfCopy the code

Add the following:

local0.* /var/log/haproxy.log
&~
Copy the code

Save the configuration and restart the Rsyslog service

  # service rsyslog restart
Copy the code

Configure the IP packet forwarding function of the system kernel

 # vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
Copy the code

Make the configuration take effect

 # sysctl -p
Copy the code

HaProxy command

  • monitoring:http://192.168.25.100:48800/admin-status(Customized for load balancing port 3388)
  • Start test: / usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy CFG (only by this way the kill off)
  • Connect the test:Mysql -p -h 192.168.25.100 -p 3388
  • Start the:service haproxy start
  • stop:service haproxy stop
  • restart:service haproxy restart