一、方案规划

申请 vip 绑定到hostServerHA11,hostServerHA12
hostServerHA11 192.168.126.11 #安装haproxy,keepalived, vip 192.168.126.10, centos7
hostServerHA12 192.168.126.12 #安装haproxy,keepalived, vip 192.168.126.10, centos7

hostServerRedis1 192.168.126.21 #开启两个端口 6380, 6381, centos7
hostServerRedis2 192.168.126.22 #开启两个端口 6380, 6381, centos7
hostServerRedis3 192.168.126.23 #开启两个端口 6380, 6381, centos7

搭建一个3master,3slave的主从集群,采用redis6.2.6版本

二、安装redis软件

在hostServerRedis1,hostServerRedis2, hostServerRedis3上面安装redis软件

  1. 进入/etc/yum.repos.d, vim remi.epol,写入如下内容
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Repository: http://rpms.remirepo.net/
# Blog:       http://blog.remirepo.net/
# Forum:      http://forum.remirepo.net/

[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror
enabled=0
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
  1. 执行如下安装命令:
1
yum --enablerepo=remi install redis
  1. 创建redis配置文件
1
2
cd /etc/redis;touch redis6380.conf redis6381.conf;
写入redis配置
  1. 创建服务启动文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
cd /usr/lib/systemd/system;
touch redis6380.service;
touch redis6381.service;

写入如下内容:
[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/redis-server /etc/redis/redis6380.conf --daemonize yes --supervised systemd
ExecStop=/usr/libexec/redis-shutdown redis6380
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
  1. 修改目录权限
1
2
3
4
5
chown -R redis:redis /etc/redis;
mkdir -p /var/lib/redis;
mkdir -p /var/log/redis;
chown -R redis:redis /var/lib/redis;
chown -R redis:redis /var/log/redis;
  1. 启动redis服务
1
2
systemctl start redis6380;
systemctl start redis6381;
  1. 开通防火墙,重启防火墙
1
2
3
4
5
-A RH-Firewall-1-INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 6380  -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 16380 -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 6381  -j ACCEPT
-A RH-Firewall-1-INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 16381 -j ACCEPT
systemctl restart iptables #重启防火墙
  1. 创建集群
1
2
3
4
5
redis-cli --cluster create \
    192.168.126.21:6380 192.168.126.21:6381 \
    192.168.126.22:6380 192.168.126.22:6381 \
    192.168.126.23:6380 192.168.126.23:6381 \
    --cluster-replicas 1 -a xxxx
  1. 连接集群
1
2
3
redis-cli -h 192.168.126.21 -p 6380 -c -a xxxxx 
cluster nodes #查看集群节点信息
cluster info  #查看集群信息
  1. 增加master节点操作
1
2
redis-cli --cluster add-node [加入node的ip:port] [被加入的集群的任意node的ip:port] -a xxx
redis-cli --cluster add-node 192.168.126.25:6380 192.168.126.21:6380
  1. 重新分区slot
1
2
redis-cli --cluster reshard [集群任意节点IP:port]
redis-cli --cluster reshard 192.168.126.21:6380
  1. 增加slaver节点操作
1
2
redis-cli --cluster add-node [待加入节点的ip:port] [集群中的任意节点node的IP:port]  --cluster-slave --cluster-master-id [主节点的id]
redis-cli --cluster add-node 192.168.126.25:6381 192.168.126.21:6380 --cluster-slave --cluster-master-id b01dfefa0b75cf9c04582bdebfe095bff5443c9c
  1. 移除slave节点
redis-cli --cluster del-node [集群中的任意node:port] [node ID]
redis-cli --cluster del-node 192.168.126.21:6380 f98ac1bcc47e90f9b0b9df104750ba07eb3b609c
  1. 移除master节点,要先移除master节点上吗的slot和数据,进行reshard操作
redis-cli --cluster del-node [集群中的任意node:port] [node ID]
redis-cli --cluster del-node 192.168.126.21:6380 3290302920329320103923902390230920920fdab
  1. 创建redis用户和组
1
groupadd redis;useradd -d /var/lib/redis -c "Redis Database Server" -g redis redis -s /sbin/nologin 

三、redis常用配置说明

参考资料

centos7安装redis6.2.6
Remi源
centos7源码编译安装redis6.2.6
Redis 6.2.6 Cluster搭建过程
部署Redis Cluster 6.0 集群并开启密码认证

1
2
3
4
5
安装最新版的 redis ,需要安装Remi的软件源,
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum --enablerepo=remi install redis(过程中输入 y)#安装redis
输入 systemctl status redis 查看 redis 状态
输入 systemctl start redis 启动 redis ,并且执行 systemctl status redis