搭建高可用redis-cluster集群
配置截图
[root@redis-s redis]# ls
6001 6002 6003 6004 6005 6006
其中一个节点的conf文件
[root@redis-s redis]# cat 6001/conf/redis.conf
bind 0.0.0.0
protected-mode no
port 6001
daemonize no
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
requirepass redis
masterauth redis
3、检查 redis 启动情况
[root@redis-s ~]# sh start.redis.sh
[root@redis-s ~]# netstat -tnlp |grep redis
tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 88031/redis-server
tcp 0 0 0.0.0.0:6002 0.0.0.0:* LISTEN 88034/redis-server
tcp 0 0 0.0.0.0:6003 0.0.0.0:* LISTEN 88041/redis-server
tcp 0 0 0.0.0.0:6004 0.0.0.0:* LISTEN 88048/redis-server
tcp 0 0 0.0.0.0:6005 0.0.0.0:* LISTEN 88055/redis-server
tcp 0 0 0.0.0.0:6006 0.0.0.0:* LISTEN 88061/redis-server
tcp 0 0 0.0.0.0:16001 0.0.0.0:* LISTEN 88031/redis-server
tcp 0 0 0.0.0.0:16002 0.0.0.0:* LISTEN 88034/redis-server
tcp 0 0 0.0.0.0:16003 0.0.0.0:* LISTEN 88041/redis-server
tcp 0 0 0.0.0.0:16004 0.0.0.0:* LISTEN 88048/redis-server
tcp 0 0 0.0.0.0:16005 0.0.0.0:* LISTEN 88055/redis-server
tcp 0 0 0.0.0.0:16006 0.0.0.0:* LISTEN 88061/redis-server
4、创建集群
[root@redis-s redis]#./redis-cli --cluster create 192.168.182.120:6001 192.168.182.120:6002 192.168.182.120:6003 192.168.182.120:6004 192.168.182.120:6005 192.168.182.120:6006 --cluster-replicas 1 -a redis
查看redis集群状态
127.0.0.1:6001> CLUSTER NODES
5b140543ba736c7429783086a4698ce23f5ea009 192.168.182.120:6004@16004 slave e40ed46e827115fabf18fe71a527d8b9279761c0 0 1593782850256 4 connected
e40ed46e827115fabf18fe71a527d8b9279761c0 192.168.182.120:6003@16003 master - 0 1593782849000 3 connected 10923-16383
dbb64e167721771fb57acd37f0c4510eb23dc9b5 192.168.182.120:6001@16001 myself,master - 0 1593782850000 1 connected 0-5460
a703d5e2a4a47627d56d964c4ab524542d5a9c66 192.168.182.120:6002@16002 master - 0 1593782849000 2 connected 5461-10922
b8081e72a84b826abc33801aeae397deb8810e60 192.168.182.120:6005@16005 slave dbb64e167721771fb57acd37f0c4510eb23dc9b5 0 1593782849037 5 connected
e2908aaae69ed7f239927d2a62977877cafb0b65 192.168.182.120:6006@16006 slave a703d5e2a4a47627d56d964c4ab524542d5a9c66 0 1593782849247 6 connected
127.0.0.1:6001> set name haitao
-> Redirected to slot [5798] located at 192.168.182.120:6002
OK
验证
127.0.0.1:6006> get name
-> Redirected to slot [5798] located at 192.168.182.120:6002
"haitao"
[root@ha1 ~]# cat /etc/keepalived/keepalived.conf
[root@ha2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id HA1
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.200
}
track_script {
check_haproxy
}
}
[root@ha2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id HA2
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.182.200
}
track_script {
check_haproxy
}
}
[root@ha2 ~]# cat /etc/keepalived/check_haproxy.sh
#!/bin/bash
/usr/bin/curl -I http://127.0.0.1:1314/haproxystats &>/dev/null
if [ $? -ne 0 ];then
/usr/bin/systemctl stop keepalived
fi
[root@ha2 ~]# ip a |grep eth
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:13:2a:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.182.110/24 brd 192.168.182.255 scope global noprefixroute eth0
inet 192.168.182.200/32 scope global eth0
[root@ha2 ~]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:1314
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 #{ tcp|http|health } 设定启动的实例的协议类型,此处要选择tcp,因为redis是基于tcp协议运行的
balance roundrobin
server redis1 192.168.182.120:6001 check
server redis2 192.168.182.120:6002 check
server redis3 192.168.182.120:6003 check
server redis4 192.168.182.120:6004 check
server redis5 192.168.182.120:6005 check
server redis6 192.168.182.120:6006 check
http://192.168.182.200:1314/haproxystats
--------------------当你发现自己突然嫉妒起其他人时,就请安静下来学习吧!--------------------