Build high-availability web and DB clusters

Source: Internet
Author: User
Tags db2 fpm install php pack

1, the Project profile is the topology diagram
This design uses keepalived and LVS to build a scheduler cluster, to achieve the high availability of LVS scheduler, when the LVS scheduler master down, Keepalived will automatically switch to backup.
Dispatching the following Web cluster to achieve load balancing of the Web cluster, the forward scheduler sends the service to the host in the Web cluster in response to the LVS algorithm.
Build the database cluster behind the Web cluster, set the main structure of the database, and add keepalived to achieve high availability.
The specific topology diagram is as follows:

2. Environment preparation
Machine name IP Configuration service role remarks
Lvs1-master 192.168.4.1
(vip1:192.168.4.100) Scheduler master server configuration keepalived
Lvs2-backup 192.168.4.2
(vip1:192.168.4.100) scheduler from server configuration keepalived
Web1 192.168.4.3 back-end Web server to open LNM Web
Web2 192.168.4.4 Backend Web server opens LNM Web
db1 192.168.4.5
(vip2:192.168.4.200) backend MySQL server configuration keepalived
DB2 192.168.4.6
(vip2:192.168.4.200) backend MySQL server configuration keepalived
3, configure Dispatch server
3.1 Pack
[[email protected] ~]# yum-y Install Ipvsadm.x86_64
[[email protected] ~]# yum-y Install keepalived.x86_64
3.2 Modify keepalived configuration file (main scheduler)
1) Global Segment--Set Send message

global_defs {notification_email {[email protected]} Notification_email_from [email protected] Smt P_server 127.0.0.1 smtp_connect_timeout router_id lvs_devel vrrp_skip_check_adv_addr# vrrp_strict vrrp_garp_i Nterval 0 vrrp_gna_interval 0}2) define master-slave and vipvrrp_instance vi_1 {state master//main scheduler interface Eth0 vir TUAL_ROUTER_ID 51//Virtual route IP priority 150//precedence Advert_int 1 Authentication {Auth_typ E PASS auth_pass 1111//Set Password} virtual_ipaddress {192.168.4.100//configure VIP}}3) define scheduling algorithm VI Rtual_server 192.168.4.100 {delay_loop 6 Lb_algo RR//algorithm for polling lb_kind DR//lvs mode for        DR Persistence_timeout the same client in//50s to access the same Web server protocol TCP real_server 192.168.4.3 {weight 1 Tcp_check {connect_timeout 3 nb_get_retry 3 delay_before_retry 3}} Real _server 192.168.4.4 80 {                      There are several Web servers that write several segments weight 1 Tcp_check {connect_timeout 3 Nb_get_ret Ry 3 delay_before_retry 3}}}3.3 configuration files from the server from the server are similar to the primary server configuration file, so direct SCP a copy of the configuration file from the server to backup and the priority Small.

[Email protected] ~]# scp/etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
[Email protected] ~]# vim/etc/keepalived/keepalived.conf
......
State BACKUP
......
Priority 100
......
: Wq

4、配置lnp服务器(web)4.1安装源码包nginx

[Email protected] lnmp_soft]# TAR-XF nginx-1.12.2.tar.gz
[Email protected] lnmp_soft]# CD NGINX-1.12.2/
[Email protected] nginx-1.12.2]# useradd-s/sbin/nologin nginx
[Email protected] nginx-1.12.2]# yum-y install gcc gcc-c++ pcre-devel
[[email protected] nginx-1.12.2]#./configure--prefix=/usr/local/nginx--user=nginx--group=nginx//If an error is made, install the required package
[Email protected] nginx-1.12.2]# yum-y install Zlib-devel
[[email protected] nginx-1.12.2]# make & make Install
[Email protected] sbin]# ln-s/usr/local/nginx/sbin/nginx/sbin/

  4.2 Modify nginx configuration file [email protected] sbin]# vim/usr/local/nginx/conf/nginx.conf1) Modify maximum number of links, increase concurrency events { Worker_connections 65535;}            2) site root path location/{root HTML;        Index index.php index.html index.htm;            }3) do static and dynamic separation location ~ \.php$ {root html;            Fastcgi_pass 127.0.0.1:9000;            Fastcgi_index index.php;        Include fastcgi.conf; }4) Start service: [[email protected] sbin]# nginx-s reload4.3 change ulimit parameter supports greater concurrency [[email protected] sbin]# Ulimit-hn 100000//temporary modification [[email protected] sbin]# ulimit-sn 100000[[email protected] sbin]# vim/etc/secur          ITY/LIMITS.CONF//Permanent modification * Soft nofile 100000* hard nofile 100000 Finally add 4.4 to install PHP to support dynamic parsing of  

[[email protected] sbin]# yum-y install php php-mysql.x86_64
[[email protected] lnmp_soft]# yum-y Install php-fpm-5.4.16-42.el7.x86_64.rpm
[[email protected] lnmp_soft]# systemctl Restart Php-fpm.service
[[email protected] lnmp_soft]# systemctl enable Php-fpm.service

4.5 Configuring the VIP on the lo:0 NIC (native loopback address) [[email protected] lnmp_soft]# cd/etc/sysconfig/network-scripts/[[email  Protected] network-scripts]# CP ifcfg-lo{,:0}[[email protected] network-scripts]# vim ifcfg-lo:0device=lo:0 ipaddr=192.168.4.100netmask=255.255.255.255network=192.168.4.100broadcast=192.168.4.100onboot=yesname=lo:0:wq[    [email protected] network-scripts]# ifup lo:0//start NIC [[email protected] network-scripts]# IP addr Show lo View 4.6 web2 on the same configuration (IBID.) 5, test the previous configuration is correct 1) write a different page on Web1 and WEB2 2) Start the LVS1&LVS2 service on keepalived [[email protected] ~]# systemctl start Keepalived.service [[email protected] ~]# IPVSADM-LN/See if there are any WEB1&WEB2[[EMAIL PR in the rules Otected] ~]# Firefox http://192.168.4.1003) Hang one of the web to see if there are any changes in the rules, and if there is a message 6, configure the db1&db26.1 installation configuration mysql[[email  Protected] ~]# tar-xf Mysql-5.7.17.tar [[email protected] ~]# yum-y Install mysql-community-*[[email  Protected] ~]# systemctl start mysqld//Start service [[email protected] ~]# grep-i PASSWOrd/var/log/mysqld.log//Find initial login secret [[email protected] ~]# mysql-hlocalhost-uroot-p ' H:rqup7phkqi ' mysql> set Glo Bal validate_password_policy=0;mysql> set global validate_password_length=6;mysql> alter User user () identified By "123456"; [[email protected] ~]# vim/etc/my.cnf......validate_password_policy=0validate_password_length=6log-bin= db2server_id=5binlog_format= "Mixed" relay-log=relay-binrelay-log-index= Slave-realy-bin.indexauto-increment-increment=2auto-increment-offset=2 ... Note: MYSQL1 and MySQL only server-id different and auto-increment-offset different, others must be the same. Some of the configuration items are interpreted as follows: binlog_format= mixed: Specifies the format of the Binlog log for MySQL, mixed is a mixed mode. Relay-log: Turn on the trunk log feature Relay-log-index: auto-increment-increment= 2: Indicates the amount of each increment from the growth field, with a default value of 1. Its value should be set to the total number of servers in the entire structure, this case uses two servers, so the value is set to 2. Auto-increment-offset= 2: Used to set the starting point of automatic growth in the database (that is, the initial value), because both of these servers set an autogrow value of 2, so their starting point must be different, in order to avoid the two server data synchronization with primary key conflict [email  protected] ~]# systemctl restart mysqld[[email protected] mysql]# mysql-uroot-p123456mysql> Grant Replication Slave on * * to [emAil protected] '% ' identified by ' 123456 ';mysql> grant all on * * to [email protected] '% ' identified by ' 12345 6 "; 6.2 db1&db2 Mutual master from mysql> change master to master_host=" 192.168.4.5 ", master_user=" Repluser ", master_password=" 123456 ", master_log_file=" db1.000001 ",master_log_pos=154;mysql> start slave;mysql> show slave status\g;mysql > Change Master to master_host= "192.168.4.6", master_user= "Repluser", master_password= "123456", master_log_file= " db2.000002 ",master_log_pos=154;mysql> start slave;mysql> show slave status\g;6.3 db1&db2 on Deployment keepalived1) Pack [ [email protected] network-scripts]# yum-y install keepalived.x86_64 2) modify configuration file [[email protected] ~]# vim/etc/ KEEPALIVED/KEEPALIVED.CONF Global Segment-e-mail
global_defs {   notification_email {     [email protected]   }   notification_email_from [email protected]   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id mysql-1   vrrp_skip_check_adv_addr
#   vrrp_strict   vrrp_garp_interval 0   vrrp_gna_interval 0}

Defining the primary server

vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 53    priority 150    nopreempt      //不抢占优先权    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.4.200    }}

Defining Algorithms and Real_server

virtual_server 192.168.4.200 3306 {    delay_loop 6    lb_algo rr    lb_kind DR    persistence_timeout 50    protocol TCP    real_server 192.168.4.5 3306 {
   weight 1
notify_down/etc/keepalived/bin/mysql.sh

"//If MySQL is down, execute the contents of the script.

        TCP_CHECK {                connect_timeout 3                nb_get_retry 3                delay_before_retry 3                connect_port 3306     //监控健康状态端口。        }        }}3)把主服务器上的配置文件scp一份到从库上,只需修改MASTER/优先级和real server的ip即可。并把nopreempt这一行去掉,这个一般只配置在优先级大的上面。4)启服务[[email protected] ~]# systemctl restart keepalived.service 5)看vip是否到这台主机了。[[email protected] ~]# ip a s eth06.4 db1&db2编写调度keepalived的脚本因为keepalived没有与lvs联用,所以不会像前面的调度器上调度web一样,当一台web挂掉,keepalived会把其从lvs的规则中自动删除,在这一个db集群中,只有db1或者db2上的keepalived程序挂掉时,vip才会从master上跳到backup上。Mysqld挂掉则不会自动跳,这样启不到监控调度的作用,所以我们可以编写个脚本来调度。当监控到db1上3306端口挂掉了,则关闭本机的keepalived服务器。[[email protected] bin]# mkdir /etc/keepalived/bin[[email protected] bin]# vim mysql.sh#!/bin/bashpkill keepalived/sbin/ifdown eth0 && /sbin/ifup eth0:wq6.5 验证调度keepalived的脚本在db1上把mysqld服务stop掉,看vip是否跑到db2这台主机上了。当数据库重新起来之后,要把keepalived服务手动启起来。

Building high-availability web and DB clusters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.