ARM架構MySql全家桶







一、集群架構圖

MySql數據庫采用主主復制、主從同步架構即兩主兩從。

二、服務器規划

編號 角色 ip地址 版本
1 MySql-master1 192.16.1.109 MySql8.0.25-1.el7
2 MySql-slave1 192.16.1.160 MySql8.0.25-1.el7
3 MySql-master2 192.16.1.208 MySql8.0.25-1.el7
4 MySql-slave2 192.16.1.50 MySql8.0.25-1.el7
5 ShardingSphere-proxy1 192.16.1.7 ShardingSphere-proxy5.0.0
6 ShardingSphere-proxy2 192.16.1.8 ShardingSphere-proxy5.0.0
7 Keepalived+HA(Master) 192.16.1.20 Keepalived+HA
8 Keepalived+HA(Backup) 192.16.2.21 Keepalived+HA
9 VIP 192.16.1.193

三、基礎環境准備

1、安裝jdk

查看jdk版本、保證jdk的版本在1.8以上

java -version

2、禁用NUMA

dmesg|grep -i numa
# vim /etc/default/grub
修改 GRUB_CMDLINE_LINUX="rhgb quiet" 添加 numa=off
修改后內容為 GRUB_CMDLINE_LINUX="rhgb quiet numa=off"
grub2-mkconfig -o /etc/grub2.cfg
#重啟操作系統
reboot 
#重新確認numa被禁用
dmesg|grep -i numa 

3、禁用Selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce  0

4、禁用防火牆

#臨時禁用,重啟系統失效
systemctl stop firewalld
#永久禁用
systemctl disable firewalld

5、修改系統資源限制

vim /etc/security/limits.conf

*   soft   nproc   65535
*   hard   nproc   65535
*   soft   nofile  65535
*   hard   nofile  65535
# vim /etc/systemd/system.conf
DefaultLimitNOFILE=65536
DefaultLimitNPROC=65536

6、配置時間同步

tzselect
#一依次執行以下順序
5-9-1-1>yes
rm /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#重啟服務器
reboot

四、部署MySql集群(主主復制、主從復制)

1、下載MySQL安裝包

官網下載mysql8 Arm版本rpm包mysql-8.0.25-1.el7.aarch64.rpm-bundle.tar

https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.25-1.el8.aarch64.rpm-bundle.tar

2、創建用戶

#創建mysql用戶設置密碼
useradd mysql
passwd fqhOcqOK$61F

3、上傳安裝包

/home/mysql

4、安裝MySQL

1、解壓壓縮包

tar -xvf mysql-8.0.25-1.el7.aarch64.rpm-bundle.tar

2、ll查看,安裝包數量,並解壓安裝包

rpm -ivh mysql-community-common-8.0.25-1.el7.aarch64.rpm ----nodeps –force
rpm -ivh mysql-community-libs-8.0.25-1.el7.aarch64.rpm ----nodeps –force
rpm -ivh mysql-community-client-8.0.25-1.el7.aarch64.rpm ----nodeps –force
rpm -ivh mysql-community-server-8.0.25-1.el7.aarch64.rpm ----nodeps –force

3、查看安裝情況

rpm -qa | grep mysql

4、初始化數據庫

mysqld --initialize;

5、路徑賦權

chown mysql:mysql /var/lib/mysql -R;

6、啟動數據庫

systemctl start mysqld.service;
systemctl enable mysqld;

7、查看數據庫默認密碼

cat /var/log/mysqld.log | grep password
#記錄默認密碼

8、登錄數據庫

mysql -uroot -p

9、修改數據庫密碼

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密碼';

10、建立遠程連接

create user 'root'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'root'@'%' with grant option;

11、刷新數據庫權限

flush privileges;

5、配置雙主雙從

1、MySql-master1配置

修改my.cnf配置文件,vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

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

#主服務器唯一ID (與 Master2,...... MasterN 的不同點)
server-id=1
#啟用二進制日志
log-bin=mysql-bin

#從庫的中繼日志,主庫日志寫到中繼日志,中繼日志再重做到從庫
# relay-log=myslql-relay-bin

# binlog保留時間7天
expire_logs_days=7

# binlog 文件的大小
max_binlog_size=1G

#設置logbin格式。取值:STATEMENT (默認),ROW,MIXED
binlog_format=ROW

# 該從庫是否寫入二進制日志。如果需要成為多主則可啟用。只讀可以不需要
log-slave-updates=1

# 該服務器自增列的初始值。(與 Master2,...... MasterN 的不同點)
auto-increment-offset=1

# 該服務器自增列增量。其默認值是1, 取值范圍是1 .. 65535
auto-increment-increment=2


# 設置不要復制的數據庫(可設置多個)
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

#設置需要復制的數據庫(可選)。
# 如果要配置了此項,則 mysql 只復制下面指定的數據庫。
# 如果不配置此項,則 mysql 默認復制所有的數據庫(不包含 binlog-ignore-db 的數據庫)
#binlog-do-db=需要復制的主數據庫1
#binlog-do-db=需要復制的主數據庫2

2、MySql-master2配置

修改my.cnf配置文件,vim /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

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

#主服務器唯一ID (與 Master2,...... MasterN 的不同點)
server-id=3
#啟用二進制日志
log-bin=mysql-bin

#從庫的中繼日志,主庫日志寫到中繼日志,中繼日志再重做到從庫
# relay-log=myslql-relay-bin

# binlog保留時間7天
expire_logs_days=7

# binlog 文件的大小
max_binlog_size=1G

#設置logbin格式。取值:STATEMENT (默認),ROW,MIXED
binlog_format=ROW

# 該從庫是否寫入二進制日志。如果需要成為多主則可啟用。只讀可以不需要
log-slave-updates=1

# 該服務器自增列的初始值。(與 Master2,...... MasterN 的不同點)
auto-increment-offset=2

# 該服務器自增列增量。其默認值是1, 取值范圍是1 .. 65535
auto-increment-increment=2


# 設置不要復制的數據庫(可設置多個)
binlog-ignore-db=information_schema
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys

#設置需要復制的數據庫(可選)。
# 如果要配置了此項,則 mysql 只復制下面指定的數據庫。
# 如果不配置此項,則 mysql 默認復制所有的數據庫(不包含 binlog-ignore-db 的數據庫)
#binlog-do-db=需要復制的主數據庫1
#binlog-do-db=需要復制的主數據庫2

3、MySql-slave1配置

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

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

#從服務器唯一ID
server-id=2
#啟用中繼日志
relay-log=mysql-relay

4、MySql-slave2配置

[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

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

#從服務器唯一ID
server-id=4
#啟用中繼日志
relay-log=mysql-relay

5、重啟四台Mysql,使配置生效

systemctl restart mysqld.service

6、雙MySql-master機器上創建賬號,並授權遠程復制

grant replication slave on *.* TO 'slave'@'%' identified by 'fqhOcqOK$61F';

如果報錯,可以采用迂回的方式解決

create user slave identified BY 'fqhOcqOK$61F';

grant select, replication root, replication client on *.* TO 'slave'@'%';

grant all privileges on *.* TO 'slave'@'%';

flush privileges;

因為mysql8的加密方式與mysql5.7的不一樣,在配置主從復制時容易出現Slave_IO_Running的狀態不是YES,為了解決這個問題需要再執行一條命令

ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'fqhOcqOK$61F';

分別查看MySql-master1和MySql-master2的狀態,記錄下File和position的值,執行完此步驟后不要再操作主服務器MYSQL,防止主服務器狀態值變化

show master status;

MySql-master1




MySql-master2

7、雙MySql-slave機器上執行change master

1、change master格式

#復制主機的命令
change master to master_host='主機的IP地址',
master_user='slave',
master_password='123456',
master_log_file='File的內容',
master_log_pos=Position的內容;

2、在MySql-slave1上執行change master

change master to master_host='192.16.1.109',
master_user='dog',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=1264;

3、在MySql-slave2上執行change master

change master to master_host='192.16.1.208',
master_user='root',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=1264;

4、啟動MySql-slave1和MySql-slave2的復制功能

start slave;

查看Slave的啟動狀態

show slave status\G





正常情況下,Slave_IO_Running 、Slave_SQL_Running 兩個參數都是Yes,則說明主從配置成功!如遇到這種情況就需要排查問題了,



5、如想重新配置主從復制關系

1、停止從服務的復制功能

stop slave;


2、重置主從關系,重新配置主從

reset master;

8、配置主主復制關系

因為是雙主雙從,所以這里MySql-master1配置MySql-master2,MySql-master2配置MySql-master1

1、在MySql-master1執行

change master to master_host='192.16.1.208',
master_user='slave',
master_password='密碼',
master_log_file='mysql-bin.000002',master_log_pos=156; ## Master1 的mysql-bin 相應參數

2、在MySql-master2執行

change master to master_host='192.16.1.109',
master_user='slave',
master_password='密碼',
master_log_file='mysql-bin.000002',master_log_pos=156; ## Master1 的mysql-bin 相應參數

3、啟動主從復制功能

start slave;

4、查看從服務器狀態

show slave status\G

9、雙主雙從驗證

在 MySql-master1主機新建庫、新建表、 insert 記錄, 然后在MySql-master2、MySql-slave1、MySql-slave2上查看復制 是否成功

五、部署ShardingSphere-Proxy集群(2台)

1、下載鏡像


官網下載 https://mirrors.bfsu.edu.cn/apache/shardingsphere/5.0.0/apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz 其他版本自行官網下載
將壓縮包上傳到指定的路徑下解壓

mkdir -P /home/shardingsphere
tar -zxvf apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz -C /home/shardingsphere
mv apache-shardingsphere-5.0.0 proxy

2、下載MySql-connect.jar包

下載合適的驅動包,這里的驅動包是指與mysql數據庫jdbc連接的jar包,官網下載:https://repo1.maven.org/maven2/mysql/mysql-connector-java/拷貝合適的驅動包到ShardingSphere-Proxy根目錄的lib下(注:pgsql不需要引入)

/home/shardingsphere/proxy/lib/

3、修改配置文件(注意:每一個配置文件就是一個schema)

1、修改 server.yaml , - root@%:passwd(修改密碼)

<br />
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

######################################################################################################
# 
# If you want to configure governance, authorization and proxy properties, please refer to this file.
# 
######################################################################################################

scaling:
  blockQueueSize: 10000
  workerThread: 40
  clusterAutoSwitchAlgorithm:
    type: IDLE
    props:
      incremental-task-idle-minute-threshold: 30
  dataConsistencyCheckAlgorithm:
    type: DEFAULT
#
#mode:
#  type: Cluster
#  repository:
#    type: ZooKeeper
#    props:
#      namespace: governance_ds
#      server-lists: localhost:2181
#      retryIntervalMilliseconds: 500
#      timeToLiveSeconds: 60
#      maxRetries: 3
#      operationTimeoutMilliseconds: 500
#  overwrite: false
#
rules:
  - !AUTHORITY
    users:
      - root@%:passwd
      - sharding@:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED
  - !TRANSACTION
    defaultType: XA
    providerType: Atomikos

props:
  max-connections-size-per-query: 1
  kernel-executor-size: 16  # Infinite by default.
  proxy-frontend-flush-threshold: 128  # The default value is 128.
  proxy-opentracing-enabled: false
  proxy-hint-enabled: false
  sql-show: false
  check-table-metadata-enabled: false
  show-process-list-enabled: false
    # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
    # The default value is -1, which means set the minimum value for different JDBC drivers.
  proxy-backend-query-fetch-size: -1
  check-duplicate-table-enabled: false
  sql-comment-parse-enabled: false
  proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
    # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
    # if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
  proxy-backend-executor-suitable: OLAP
  proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
  sql-federation-enabled: false

2、修改 config-replica-query.yaml配置文件

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

######################################################################################################
# 
# Here you can configure the rules for the proxy.
# This example is configuration of replica-query rule.
# 
######################################################################################################
#
#schemaName: readwrite_splitting_db
#
#dataSources:
#  primary_ds:
#    url: jdbc:postgresql://127.0.0.1:5432/demo_primary_ds
#    username: postgres
#    password: postgres
#    connectionTimeoutMilliseconds: 30000
#    idleTimeoutMilliseconds: 60000
#    maxLifetimeMilliseconds: 1800000
#    maxPoolSize: 50
#    minPoolSize: 1
#  replica_ds_0:
#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_0
#    username: postgres
#    password: postgres
#    connectionTimeoutMilliseconds: 30000
#    idleTimeoutMilliseconds: 60000
#    maxLifetimeMilliseconds: 1800000
#    maxPoolSize: 50
#    minPoolSize: 1
#  replica_ds_1:
#    url: jdbc:postgresql://127.0.0.1:5432/demo_replica_ds_1
#    username: postgres
#    password: postgres
#    connectionTimeoutMilliseconds: 30000
#    idleTimeoutMilliseconds: 60000
#    maxLifetimeMilliseconds: 1800000
#    maxPoolSize: 50
#    minPoolSize: 1
#
#rules:
#- !READWRITE_SPLITTING
#  dataSources:
#    pr_ds:
#      writeDataSourceName: primary_ds
#      readDataSourceNames:
#        - replica_ds_0
#        - replica_ds_1

######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################

schemaName: nacos

dataSources:
  write_ds:
    url: jdbc:mysql://192.16.1.109:3306/nacos?serverTimezone=UTC&useSSL=false
    username: root
    password: Csxpxc@1234!
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  read_ds_0:
    url: jdbc:mysql://192.16.1.160:3306/nacos?serverTimezone=UTC&useSSL=false
    username: root
    password: Csxpxc@1234!
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  read_ds_1:
    url: jdbc:mysql://192.16.1.50:3306/nacos?serverTimezone=UTC&useSSL=false
    username: root
    password: Csxpxc@1234!
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !READWRITE_SPLITTING
  dataSources:
    pr_ds:
      writeDataSourceName: write_ds
      readDataSourceNames:
        - read_ds_0
        - read_ds_1

3、啟動測試

1、進入bin路徑下,編輯啟動腳本

vim start.sh
修改啟動限制最小為328K

2、啟動腳本

sh start.sh

3、查看啟動日志

4、通過命令驗證是否可以登錄

mysql -uroot -P3307 -p


5、使用Navicate連接測試


六、部署keepalived+HA集群(2台)

1、下載安裝包

官網下載keepalived-1.4.5.tar.gz安裝包,https://www.keepalived.org/software/keepalived-1.4.5.tar.gz
和haproxy-2.2.3.tar.gz ,https://src.fedoraproject.org/repo/pkgs/haproxy/

2、安裝必要的依賴包

1、部署HAproxy

修改內核參數

#查看默認參數
[root@haproxy01 keepalived]# cat /proc/sys/net/ipv4/ip_nonlocal_bind 
0

#修改參數
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind
sysctl -w net.ipv4.ip_nonlocal_bind=1

#永久生效
echo 'net.ipv4.ip_nonlocal_bind=1' >> /etc/sysctl.conf

將haproxy-2.2.3.tar.gz安裝包上傳到對應路徑下解壓、編譯

	cd /software
	tar -xvf haproxy-2.2.3.tar.gz 
	cd /software/haproxy-2.2.3
	make TARGET=linux2628 PREFIX=/usr/local/haproxy
	make install PREFIX=/usr/local/haproxy
	cp -rf /usr/local/haproxy/sbin/haproxy /usr/sbin/
  cp examples/haproxy.init /etc/init.d/haproxy
  #賦權
  chmod 755 /etc/init.d/haproxy
  #查看HAproxy的版本
  haproxy -v

配置rsyslog

  #創建目錄
  mkdir /var/log/haproxy
  #目錄賦權
  chmod a+w /var/log/haproxy

開啟rsyslog記錄HAproxy日志

vim /etc/rsyslog.conf
#將如下兩行得注釋取消
$ModLoad imudp
$UDPServerRun 514

#在該文件添加如下內容:
# Save haproxy log
local3.*                       /var/log/haproxy/haproxy.log

修改“/etc/sysconfig/rsyslog”文件,內容如下

vim /etc/sysconfig/rsyslog
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-r -m 0 -c 2"


創建HAproxy運行用戶
以root用戶身份分別在所有集群節點上執行如下命令創建需要的組和用戶:

groupadd -r haproxy
useradd -g haproxy -M -s /sbin/nologin haproxy

配置HAproxy.cfg文件
以root用戶身份分別在所有集群節點上編輯配置文件
vim /usr/local/haproxy/etc/haproxy.cfg配置haproxy.cfg文件

global
#設置日志
    chroot /usr/local/haproxy
    log 127.0.0.1   local3 info
#用戶與用戶組
    user haproxy
    group haproxy
#定義每個haproxy進程的最大連接數 ,由於每個連接包括一個客戶端和一個服務器端,所以單個進程的TCP會話最大數目將是該值的兩倍。
    maxconn 4096
# 以守護進程的方式運行
    daemon
defaults
log global
#日志中不記錄空連接
option dontlognull
# 定義連接后端服務器的失敗重連次數,連接失敗次數超過此值后將會將對應后端服務器標記為不可用
retries 3
option redispatch
# 設置成功連接到一台服務器的最長等待時間,默認單位是毫秒
timeout connect 5000
# 設置連接客戶端發送數據時的成功連接最長等待時間,默認單位是毫秒
timeout client 50000
# 設置服務器端回應客戶度數據發送的最長等待時間,默認單位是毫秒
timeout server 50000
#統計頁面
listen  admin_stats
        bind 192.16.1.193:48800  
        mode http
#采用http日志格式  
        option httplog
#統計頁面自動刷新時間  
        stats refresh 30s
#統計頁面url
        stats uri /admin_stats
#統計頁面密碼框上提示文本
        stats realm Haproxy Manager
#統計頁面用戶名和密碼設置    
        stats auth admin:admin
#隱藏統計頁面上HAProxy的版本信息
        stats hide-version
listen ShardingSphere_service
# 綁定192.16.1.193:3307:3307端口訪問端口ShardingSphere3307
bind 192.16.1.193:3307
# 定義為tcp模式
      mode tcp
#采用http日志格式
      option tcplog
# 開啟對后端服務器的健康檢測
  option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
# 設置haproxy的調度算法
      balance roundrobin
#根據調度分配到真實的后台地址,參數解釋:port 48700:檢測端口48700, inter 5s:5秒檢測一次,rise 2:檢測成功2次表示服務器可用,fall 3:檢測失敗3次后表示服務器不可用
  server ShardingSphere1 192.16.1.7:3307 check port 48700 inter 5s rise 2 fall 3
  server ShardingSphere2 192.16.1.8:3307 check port 48700 inter 5s rise 2 fall 3
#設置服務器端回應客戶度數據發送的最長等待時間,默認單位是毫秒
 timeout server 20000
5:9066 cookie 2 check port 48700 inter 5s rise 2 fall 3
timeout server 20000

啟動HAproxy

# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg



2、部署keepalived

yum install -y pcre-devel openssl-devel popt-devel
[root@master ~]# tar zxvf keepalived-1.4.5.tar.gz
[root@master ~]# cd keepalived-1.4.5
[root@master ~]#./configure --prefix=/usr/local/keepalived
make && make install

將編譯好的文件拷貝到指定路徑下

[root@master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

3、修改配置文件

Master節點

! Configuration File forkeepalived
global_defs {
script_user root
enable_script_security
notification_email {
test@sina.com
 }
notification_email_from  admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAproxy      #標識,雙主相同
 }
 
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"  ## 檢測haproxy狀態的腳本路徑
    interval 2  ## 檢測時間間隔
    weight 2  ## 如果條件成立,權重+2 
}
 
vrrp_instance VI_1 {
 state MASTER           #兩台都設置BACKUP
 interface eth0
 virtual_router_id 51       #主備相同
 priority 100           #優先級,backup設置90
 advert_int 1
 nopreempt             #不主動搶占資源,只在master這台優先級高的設置,backup不設置
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 
## 將track_script塊加入instance 配置塊
track_script {
 chk_haproxy  ## 檢查HAProxy服務是否存活
 }

 virtual_ipaddress {
 192.16.1.193
 }
}

Backup節點

! Configuration File forkeepalived
global_defs {
script_user root
enable_script_security
notification_email {
test@sina.com
 }
notification_email_from  admin@test.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAproxy      #標識,雙主相同
 }
 
vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"  ## 檢測haproxy狀態的腳本路徑
    interval 2  ## 檢測時間間隔
    weight 2  ## 如果條件成立,權重+2 
}
 
vrrp_instance VI_1 {
 state BACKUP           #兩台都設置BACKUP
 interface eth0
 virtual_router_id 51       #主備相同
 priority 90           #優先級,backup設置90
 advert_int 1             #不主動搶占資源,只在master這台優先級高的設置,backup不設置
 authentication {
 auth_type PASS
 auth_pass 1111
 }
 
## 將track_script塊加入instance 配置塊
track_script {
 chk_haproxy  ## 檢查HAProxy服務是否存活
 }

 virtual_ipaddress {
 192.16.1.193
 }
}

編輯端口探測腳本check_haproxy.sh

# vim /etc/keepalived/check_haproxy.sh  //添加如下內容保存
	#!/bin/bash
	START_HAPROXY="/etc/init.d/haproxy start"
	LOG_FILE="/var/log/keepalived-haproxy-state.log"
	if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
	  echo "`date "+%Y-%m-%d %H:%M:%S"` --- haproxy is down,next will restart haproxy" >> $LOG_FILE
	  echo $START_HAPROXY >> $LOG_FILE
	  $START_HAPROXY &>> $LOG_FILE
	  sleep 2
	  if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
	    echo "start haproxy failed,will killall keepalived" >> $LOG_FILE
	    killall keepalived
	  else
	    echo "`date "+%Y-%m-%d %H:%M:%S"` --- haproxy restart successful" >> $LOG_FILE
	  fi
	fi

給腳本賦權

chmod +x /etc/keepalived/check_haproxy.sh

啟動keepalived

systemctl start keepalived
#查看keepalived運行狀態
systemctl status keepalived
chkconfig keepalived on //設置開機啟動




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM