高可用負載均衡架構(2)

1     高可用Mysql安裝配置

1.1  實驗環境

服務器角色java

服務器IPmysql

VIPlinux

192.168.8.200sql

Mycat1數據庫

192.168.8.22vim

Mycat2後端

192.168.8.23bash

Mysql1(Master1)服務器

192.168.8.24ide

Mysql2(Master2)

192.168.8.25

Mysql3(Slave)

192.168.8.26

 

1.2  Mysql安裝配置

(192.168.8.24,192.168.8.25和192.168.8.26)三臺服務器操做

1.2.1     Mysql安裝

到官網下載mysql的yum源

https://www.mysql.com/

安裝yum源rpm軟件

rpm-ivh mysql57-community-release-el7-11.noarch.rpm

 

yum安裝mysql

yuminstall mysql-community-server

1.2.2     Mysql初始化

啓動mysql

systemctlstart mysqld

獲取root密碼

cat/var/log/mysqld.log | grep "temporary password"

2017-07-06T03:23:46.053467Z1 [Note] A temporary password is generated for root@localhost: iGUl/j*_r1*z

修改root密碼

mysql–uroot –p

SETPASSWORD=PASSWORD('newpassword');

1.3  Mysql主主配置

(192.168.8.24和192.168.8.25)兩臺服務器操做

1.3.1     Mysql配置

192.168.8.24配置:

vi/etc/my.cnf

增長以下內容:

[mysqld]

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

server_id  = 1

log-bin=mysql-bing

log_bin_trust_function_creators=TRUE

log-slave-updates  

sync_binlog=1  

auto_increment_offset=1  

auto_increment_increment=2  

slave-skip-errors=all

lower_case_table_names=1

character_set_server=utf8

skip-name-resolve

 

[mysql]

default-character-set=utf8

[mysqld_safe]

default-character-set=utf8

[mysql.server]

default-character-set=utf8

[client]

default-character-set=utf8

 

192.168.8.25配置:

vi/etc/my.cnf

增長以下內容:

symbolic-links=0

 

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

server_id  = 2

log-bin=mysql-bing

log_bin_trust_function_creators=TRUE

log-slave-updates

sync_binlog=1

auto_increment_offset=1

auto_increment_increment=2

slave-skip-errors=all

lower_case_table_names=1

character_set_server  = utf8

skip-name-resolve

 

[mysql]

default-character-set=utf8

[mysqld_safe]

default-character-set=utf8

[mysql.server]

default-character-set  = utf8

[client]

default-character-set  = utf8

 

服務器1(192.168.8.24)和服務器2(192.168.8.25)重啓服務:

systemctlrestart mysqld

 

1.3.2     Sql配置

服務器1(192.168.8.24)操做:

mysql–u root –p

mysql>GRANT REPLICATION SLAVE ON *.* to 'repluser'@'192.168.8.25' identified by'123456';

mysql>flush privileges;

mysql>flush tables with read lock;            #防止進入新的數據

#查看bin文件和position,以後須要用到

mysql>SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

|File             | Position |Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000004 |      106 |              |                  |

+------------------+----------+--------------+------------------+

 

服務器2(192.168.8.25)操做:

mysql–u root –p

mysql>GRANT REPLICATION SLAVE ON *.* to 'repluser'@'192.168.8.24' identified by'123456';

mysql>flush privileges;

mysql>flush tables with read lock;            #防止進入新的數據

#查看bin文件和position,以後須要用到

mysql>SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

|File             | Position |Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000004 |      350 |              |                  |

+------------------+----------+--------------+------------------+

 

服務器1(192.168.8.24)操做:

mysql>CHANGE MASTER TO MASTER_HOST='192.168.8.25',MASTER_USER='repluser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=350,MASTER_PORT=3306;

 

服務器2(192.168.8.25)操做:

mysql>CHANGE MASTER TOMASTER_HOST='192.168.8.24',MASTER_USER='repluser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=106,MASTER_PORT=3306;

 

服務器1(192.168.8.24)和服務器2(192.168.8.25)分別操做:

mysql> unlock tables;

mysql> start slave;

mysql> show slave status\G;

*************************** 1.row ***************************

主要關注如下 2 個參數:

...

...

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

...

...

 

1.3.3     測試

服務器1(192.168.8.24)新增長庫:

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

+--------------------+

 

mysql>create database tom;

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

|tom                |

+--------------------+

 

服務器2(192.168.8.25)查看自動新增長庫:

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

|tom                |

+--------------------+

 

服務器2(192.168.8.25)將新增長庫刪除:

mysql>drop database tom;

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

+--------------------+

 

服務器1(192.168.8.24)將自動刪除:

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

+--------------------+

至此說明雙方同步成功

1.4  Mysql主從配置

(192.168.8.24和192.168.8.26)兩臺服務器操做

1.4.1     Mysql配置

192.168.8.24配置在上節已經完成。略過

192.168.8.26配置:

vi/etc/my.cnf

增長以下內容:

[mysqld]

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

server_id  = 3

log-bin=mysql-bing

log_bin_trust_function_creators=TRUE

log-slave-updates

sync_binlog=1

auto_increment_offset=1

auto_increment_increment=2

slave-skip-errors=all

lower_case_table_names=1

character_set_server  = utf8

skip-name-resolve

 

[mysql]

default-character-set=utf8

[mysqld_safe]

default-character-set=utf8

[mysql.server]

default-character-set  = utf8

[client]

default-character-set  = utf8

 

重啓服務

systemctlrestart mysqld

 

1.4.2     Sql配置

服務器1(192.168.8.24)操做:

mysql–u root –p

mysql>GRANT REPLICATION SLAVE ON *.* to 'repluser'@'192.168.8.26' identified by'123456';

mysql>flush privileges;

mysql>flush tables with read lock;            #防止進入新的數據

#查看bin文件和position,以後須要用到

mysql>SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

|File             | Position |Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000004 |      106 |              |                  |

+------------------+----------+--------------+------------------+

 

服務器2(192.168.8.26)操做:

mysql>CHANGE MASTER TO MASTER_HOST='192.168.8.24',MASTER_USER='repluser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=106,MASTER_PORT=3306;

 

服務器1(192.168.8.24)操做:

mysql>unlock tables;

服務器2(192.168.8.26)操做:

mysql>start slave;

mysql>show slave status\G;

***************************1. row ***************************

主要關注如下 2 個參數:

...

...

Slave_IO_Running:Yes

Slave_SQL_Running:Yes

...

...

 

1.4.3     測試

服務器1(192.168.8.24)新增長庫:

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

+--------------------+

 

mysql>create database tom;

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

|tom                |

+--------------------+

 

服務器2(192.168.8.26)查看自動新增長庫:

mysql>show databases;

+--------------------+

|Database           |

+--------------------+

|information_schema |

|mysql              |

|test               |

|tom                |

+--------------------+

 

 

1.5  Mycat安裝配置

(192.168.8.22和192.168.8.23)兩臺服務器操做

1.5.1     Mycat安裝

安裝java

yuminstall java-1.8.0-openjdk

下載mycat:

http://dl.mycat.io/

 

解壓安裝包

tarzxvf Mycat-server-1.7.0-DEV-20170416134921-linux.tar.gz

mvMycat  /usr/local/mycat

 

配置環境變量

vi/etc/profile,在系統環境變量文件中增長 MYCAT_HOME=/usr/local/mycat

 

1.5.2     Mycat配置

cd/usr/local/mycat/conf

viserver.xml

修改以下配置

      <user name="root">

                <property  name="password">Test@123</property>

                <property  name="schemas">tom,test(有多個數據庫能夠添加多個邏輯庫)

</property>

 

                <!-- 表級 DML 權限設置 -->

                <!--

                <privileges  check="false">

                        <schema  name="TESTDB" dml="0110" >

                                <table  name="tb01" dml="0000"></table>

                                <table name="tb02"  dml="1111"></table>

                        </schema>

                </privileges>

                 -->

        </user>

 

        <user name="user">

                <property  name="password">user</property>

                <property  name="schemas">tom,test</property>

                <property  name="readOnly">true</property>

        </user>

 

vischema.xml

只保留如下配置讀寫分離,其餘刪除

<?xml  version="1.0"?>

<!DOCTYPE  mycat:schema SYSTEM "schema.dtd">

<mycat:schema  xmlns:mycat="http://io.mycat/">

 

        <schema name="tom"  checkSQLschema="false" sqlMaxLimit="100"  dataNode="dn1">

        </schema>

        <schema name="test"  checkSQLschema="false" sqlMaxLimit="100"  dataNode="dn2">

        </schema>

        <dataNode name="dn1"  dataHost=" localhost1" database="tom" />

        <dataNode name="dn2"  dataHost=" localhost1" database="test" />

        <dataHost name="  localhost1" maxCon="10000" minCon="10"  balance="1"

                           writeType="0" dbType="mysql"  dbDriver="native" switchType="1"  slaveThreshold="100">

                <heartbeat>select  user()</heartbeat>

                <writeHost host="hostM1"  url="192.168.8.24:3306" user="root"

                                   password="Test@123">

                           <readHost  host="hostS2" url="192.168.8.26:3306"  user="root" password="Test@123" />

                </writeHost>

                <writeHost host="hostS1"  url="192.168.8.25:3306" user="root"

                                   password="Test@123"  />

        </dataHost>

</mycat:schema>

啓動mycat

/usr/local/mycat/bin/mycatstart

 

1.6  keepalived安裝配置

1.6.1         實驗環境

服務器角色

服務器IP

VIP1

192.168.8.200

LVS1

192.168.8.10

LVS2

192.168.8.11

 

1.6.2     安裝keepalived

yuminstall -y keepalived ipvsadm

1.6.3     配置keepalived

vim/etc/keepalived/keepalived.conf

LVS-DR-Master上,其配置以下(192.168.8.10操做):

!  Configuration File for keepalived

 

global_defs  {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from  Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance  VI_1 {

    state BACKUP

    interface ens160

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456

    }

    virtual_ipaddress {

        192.168.8.100

        192.168.8.200

    }

}

 

virtual_server  192.168.8.100 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.12 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

 

    real_server 192.168.8.13 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

 

virtual_server  192.168.8.100 21 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.12 21 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 21

        }

    }

 

    real_server 192.168.8.13 21 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 21

        }

}

}

 

virtual_server 192.168.8.200 8066 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.22  8066 {

        weight 1

        TCP_CHECK {   

            connect_timeout  3

            nb_get_retry 3

             delay_before_retry 3

            connect_port 8066

        }

    }

 

    real_server 192.168.8.23  8066 {

        weight 1

        TCP_CHECK {

            connect_timeout  3

            nb_get_retry 3

             delay_before_retry 3

            connect_port  8066

        }

    }

}

LVS-DR-Backup上,其配置以下(192.168.8.11操做):

!  Configuration File for keepalived

 

global_defs  {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from  Alexandre.Cassen@firewall.loc

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance  VI_1 {

    state BACKUP

    interface ens160

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456

    }

    virtual_ipaddress {

        192.168.8.100

        192.168.8.200

    }

}

 

virtual_server  192.168.8.100 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.12 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

 

    real_server 192.168.8.13 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

 

virtual_server  192.168.8.100 21 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.12 21 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 21

        }

    }

 

    real_server 192.168.8.13 21 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

            connect_port 21

        }

}

}

 

virtual_server 192.168.8.200 8066 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 192.168.8.22  8066 {

        weight 1

        TCP_CHECK {   

            connect_timeout  3

            nb_get_retry 3

            delay_before_retry  3

            connect_port  8066

        }

    }

 

    real_server 192.168.8.23  8066 {

        weight 1

        TCP_CHECK {

            connect_timeout  3

            nb_get_retry 3

             delay_before_retry 3

            connect_port  8066

        }

    }

}

 

1.6.4         虛擬IP腳本

mycat服務器(192.168.8.22和192.168.8.23)操做

chmod+x /etc/rc.d/init.d/functions

vi/usr/local/bin/realserver.sh

#!/bin/bash

#description:  Config realserver

 

VIP=192.168.8.200

 

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

 

case  "$1" in

start)

       /sbin/ifconfig lo:0 $VIP netmask  255.255.255.255 broadcast $VIP

       /sbin/route add -host $VIP dev lo:0

       echo "1"  >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "2"  >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "1"  >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "2"  >/proc/sys/net/ipv4/conf/all/arp_announce

       sysctl -p >/dev/null 2>&1

       echo "RealServer Start OK"

       ;;

stop)

       /sbin/ifconfig lo:0 down

       /sbin/route del $VIP >/dev/null  2>&1

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "0"  >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "0"  >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "0"  >/proc/sys/net/ipv4/conf/all/arp_announce

       echo "RealServer Stoped"

       ;;

*)

       echo "Usage: $0  {start|stop}"

       exit 1

esac

 

exit  0

啓動腳本

/usr/local/bin/realserver.shstart

1.6.5         啓動keepalived

LVS-DR-Master(192.168.8.22)和LVS-DR-Backup(192.168.8.13)分別操做:

/etc/init.d/keepalivedstart

經過ipvsadm -L命令查看VIP是否能成功映射到後端服務。若是失敗了,可經過/var/log/messages日誌定位keepalived啓動失敗的緣由。

IPVirtual Server version 1.2.1 (size=4096)

ProtLocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  123.com:8066 rr

  -> 192.168.8.22:8066           Route   1     0          0        

  -> 192.168.8.23:8066           Route   1     0          0         

相關文章
相關標籤/搜索