綜合專案:Keepalived+Redis+Haproxy實現主從熱備、負載均衡、秒級切換
Keepalived+Redis+Haproxy 群集
一、部署 Redis 群集 1.編譯安裝 Redis 1)建立群集目錄 2)編輯配置檔案 3)複製配置檔案到每個例項 4)啟動 Redis 例項 2.配置 Ruby 環境 1)安裝 Ruby 工具 2)使用指令碼安裝 Redis 群集 3)檢視群集狀態 二、部署 Keepalived 實現主從熱備、秒級切換 1.主排程器配置 2.備排程器配置 3.編寫 Haproxy 狀態檢測指令碼 4.開啟服務,驗證 VIP 三、部署 Haproxy 實現負載均衡 1.編譯安裝 Haproxy 2.配置 Haproxy 主配置檔案 3.建立自啟動指令碼 4.配置 Haproxy 日誌管理 四、訪問驗證
準備工作:
主機名 作業系統 IP地址 擔任角色
master CentOS7 192.168.1.1 Redis-主
slave 192.168.1.2 Redis-從
haproxy1 192.168.1.3 Keepalived-主,Haproxy
haproxy2 192.168.1.4 Keepalived-從,Haproxy
實驗所需軟體包從這個連結下載:https://pan.baidu.com/s/1tbyUzeBI6vhYHWk92EGUwg
提取碼:3wh6
一、部署 Redis 群集
1.編譯安裝 Redis
master slave 操作一致
anaconda-ks.cfg redis-3.2.9.tar.gz
[[email protected] ~]# tar zxf redis-3.2.9.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/redis-3.2.9/
[[email protected] redis-3.2.9]# make && make install
[[email protected] redis-3.2.9]# cd utils/
[[email protected] utils]# ./install_server.sh
1
2
3
4
5
6
7
8
在這裡插入圖片描述
1)建立群集目錄
master 上操作
[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# mkdir -p /redis_cluster/{6001…6003}
1
2
在這裡插入圖片描述
slave 上操作
[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# mkdir -p /redis_cluster/{6004…6006}
1
2
在這裡插入圖片描述
2)編輯配置檔案
master 和 slave 不同之處在於監聽地址不同
[[email protected] ~]# vim redis.conf
bind 192.168.1.1 #slave 需要將 IP 地址改為 192.168.1.2
protected-mode yes
port 6001
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /redis_cluster/redis_6001.pid
loglevel notice
logfile /redis_cluster/6001/redis_6001.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /redis_cluster/6001
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
3)複製配置檔案到每個例項
master 上操作
[[email protected] ~]# cp redis.conf /redis_cluster/6001
[[email protected] ~]# cp redis.conf /redis_cluster/6002
[[email protected] ~]# cp redis.conf /redis_cluster/6003
[[email protected] ~]# sed -i ‘s/6001/6002/g’ /redis_cluster/6002/redis.conf
[[email protected] ~]# sed -i ‘s/6001/6003/g’ /redis_cluster/6003/redis.conf
1
2
3
4
5
在這裡插入圖片描述
slave 上操作
[[email protected] ~]# cp redis.conf /redis_cluster/6004
[[email protected] ~]# cp redis.conf /redis_cluster/6005
[[email protected] ~]# cp redis.conf /redis_cluster/6006
[[email protected] ~]# sed -i ‘s/6001/6004/g’ /redis_cluster/6004/redis.conf
[[email protected] ~]# sed -i ‘s/6001/6005/g’ /redis_cluster/6005/redis.conf
[[email protected] ~]# sed -i ‘s/6001/6006/g’ /redis_cluster/6006/redis.conf
1
2
3
4
5
6
在這裡插入圖片描述
4)啟動 Redis 例項
master 上操作
[[email protected] ~]# redis-server /redis_cluster/6001/redis.conf
[[email protected] ~]# redis-server /redis_cluster/6002/redis.conf
[[email protected] ~]# redis-server /redis_cluster/6003/redis.conf
1
2
3
在這裡插入圖片描述
slave 上操作
[[email protected] ~]# redis-server /redis_cluster/6004/redis.conf
[[email protected] ~]# redis-server /redis_cluster/6005/redis.conf
[[email protected] ~]# redis-server /redis_cluster/6006/redis.conf
1
2
3
在這裡插入圖片描述
檢視 Redis 的程序和埠
[[email protected] ~]# ps -ef | grep redis | grep cluster
[[email protected] ~]# netstat -anpt | grep redis
1
2
在這裡插入圖片描述
在這裡插入圖片描述
2.配置 Ruby 環境
1)安裝 Ruby 工具
master 上操作
[[email protected] ~]# yum -y install ruby rubygems
上傳軟體 redis-3.2.0.gem
[[email protected] ~]# gem install redis --version 3.2.0
1
2
3
2)使用指令碼安裝 Redis 群集
[[email protected] ~]# cd /usr/src/redis-3.2.9/src/
[[email protected] src]# ./redis-trib.rb create --replicas 1
192.168.1.1:6001 192.168.1.1:6002 192.168.1.1:6003
192.168.1.2:6004 192.168.1.2:6005 192.168.1.2:6006
…
Can I set the above configuration? (type ‘yes’ to accept): yes
…
1
2
3
4
5
6
7
3)檢視群集狀態
[[email protected] ~]# redis-cli -h 192.168.1.1 -p 6001 -c
192.168.1.1:6001> set xingming zhangsan
OK
192.168.1.1:6001> exit
[[email protected] ~]#
[[email protected] ~]# redis-cli -h 192.168.1.2 -p 6004 -c
192.168.1.2:6004> get xingming
-> Redirected to slot [1657] located at 192.168.1.1:6001
“zhangsan”
192.168.1.1:6001> exit
1
2
3
4
5
6
7
8
9
10
在這裡插入圖片描述
二、部署 Keepalived 實現主從熱備、秒級切換
1.主排程器配置
[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id Haproxy_1
}
vrrp_script chk_haproxy {
script “/etc/keepalived/check_haproxy.sh”
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.1.188/24
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2.備排程器配置
[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id Haproxy_2
}
vrrp_script chk_haproxy {
script “/etc/keepalived/check_haproxy.sh”
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.1.188/24
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
3.編寫 Haproxy 狀態檢測指令碼
haproxy1 haproxy2 操作一致
[[email protected] ~]# vim /etc/keepalived/check_haproxy.sh
#!/bin/bash
ps aux | grep haproxy
if [ $? -ne 0 ]
then
/usr/bin/systemctl stop keepalived
fi
[[email protected] ~]# chmod +x /etc/keepalived/check_haproxy.sh
1
2
3
4
5
6
7
8
4.開啟服務,驗證 VIP
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ip a
1
2
在這裡插入圖片描述
三、部署 Haproxy 實現負載均衡
haproxy1 haproxy2操作一致
1.編譯安裝 Haproxy
掛光碟,並配置yum源
[[email protected] ~]# yum -y install pcre-devel bzip2-devel #安裝依賴包
[[email protected] ~]# ls
anaconda-ks.cfg haproxy-1.5.19.tar.gz
[[email protected] ~]# tar zxf haproxy-1.5.19.tar.gz -C /usr/src/ #解壓到/usr/src/目錄
[[email protected] ~]# cd /usr/src/haproxy-1.5.19/
[[email protected] haproxy-1.5.19]# make TARGET=linux26 && make install #安裝 64 位系統
1
2
3
4
5
6
在這裡插入圖片描述
2.配置 Haproxy 主配置檔案
[[email protected] ~]# mkdir /etc/haproxy #建立配置檔案目錄
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info
log /dev/log local1 notice
maxconn 4096
uid 99
gid 99
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:8888
stats enable
stats hide-version
stats uri /haproxystats
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
listen redis
bind *:6379
mode tcp
balance roundrobin
server redis1 192.168.1.1:6001 check inter 2000 fall 3
server redis2 192.168.1.1:6002 check inter 2000 fall 3
server redis3 192.168.1.1:6003 check inter 2000 fall 3
server redis4 192.168.1.2:6004 check inter 2000 fall 3
server redis5 192.168.1.2:6005 check inter 2000 fall 3
server redis6 192.168.1.2:6006 check inter 2000 fall 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
3.建立自啟動指令碼
[[email protected] ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[[email protected] ~]# chmod +x /etc/init.d/haproxy #新增可執行許可權
[[email protected] ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/ #軟連結,優化執行路徑
[[email protected] ~]# /etc/init.d/haproxy restart #啟動 haproxy
1
2
3
4
在這裡插入圖片描述
4.配置 Haproxy 日誌管理
[[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == ‘haproxy’ and KaTeX parse error: Expected 'EOF', got '&' at position 72: …proxy-info.log &̲ ~ if (programname == ‘haproxy’ and $syslogseverity-text == ‘notice’) then -/var/log/haproxy/haproxy-notice.log
& ~
[[email protected] ~]# systemctl restart rsyslog
[[email protected] ~]# /etc/init.d/haproxy restart
1
2
3
4
5
6
7
在這裡插入圖片描述
四、訪問驗證
使用瀏覽器訪問:http://192.168.1.188:8888/haproxystats
在這裡插入圖片描述