Redis部署资源
资源文件 | 说明 |
---|---|
redis-5.0.12.tar.gz | redis部署资源包 |
gcc-4.8.5.tar.gz | gcc安装包,如果非离线环境,可以不需要 |
redis安装资源包下载:https://download.redis.io/rel...
集群部署
架构:单节点 三主三从 伪集群
1、创建redis安装用户
执行以下代码
groupadd redis
useradd -g redis -d /home/redis -s /bin/bash -m redis
passwd redis
2、检查gcc版本
[redis@ATS-HOST ~]$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
如果小于4.8.5,安装gcc 4.8.5版本,命令如下
[redis@ATS-HOST ~]$ tar -zxvf gcc.tar.gz
解压gcc.tar.gz后得到gcc目录,切换至root用户后,安装gcc
su - root
cd /home/redis/gcc/
rpm -ivh *.rpm --nodeps --force
ps: 如果环境可连网,可以使用yum等软件管理工具进行下载或更新
yum install -y redis
3、使用redis用户,解压redis-5.0.12.tar.gz
1)解压:在/home/redis目录执行
tar -zxvf redis-5.0.12.tar.gz
2)编译:进入redis-5.0.12目录,执行
make MALLOC=libc
编译后检查redis-5.0.12中会有对应的src文件夹。
3)安装:进行src目录下,执行
make PREFIX=/home/redis/redis-5.0.12 install
安装成功后,在redis-5.0.12目录下会生成bin文件夹
4、创建并配置redis伪集群目录
1)在/home/redis目录下,创建redis-cluster目录
cd /home/redis && mkdir redis-cluster
2)进入到redis-cluster目录下,创建10001、10002、... 、 10006共6个文件目录以及data目录。
cd redis-cluster
mkdir 10001 && mkdir 10001/data
mkdir 10002 && mkdir 10002/data
mkdir 10003 && mkdir 10003/data
mkdir 10004 && mkdir 10004/data
mkdir 10005 && mkdir 10005/data
mkdir 10006 && mkdir 10006/data
3)将/home/redis/redis-5.0.12/redis.conf
复制到/home/redis/redis-cluster/10001
目录下,修改配置。
cp /home/redis/redis-5.0.12/redis.conf /home/redis/redis-cluster/10001/
vi /home/redis/redis-cluster/10001/redis.conf
4)修改成以下配置后保存
port 10001(每个节点的端口号)
daemonize yes
bind 10.8.4.187(绑定当前机器 IP)
dir /home/redis/redis-cluster/10001/data/(数据文件存放位置)
logfile "/home/redis/redis-cluster/10001/redis-10001.log"
pidfile /var/run/redis_10001.pid(pid 9001和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes10001.conf(9001和port要对应)
cluster-node-timeout 15000
appendonly yes
5)将/home/redis/redis-cluster/10001/redis.conf复制到其他伪节点目录下
复制到10002伪节点
cp /home/redis/redis-cluster/10001/redis.conf /home/redis/redis-cluster/10002/redis.conf
6)将/home/redis/redis-cluster/10002/redis.conf中的10001
全部替换成10002
方式一
vi /home/redis/redis-cluster/10002/redis.conf 输入 ':%s/10001/10002/g'
方式二
sed -i "s/10001/10002/g" /home/redis/redis-cluster/10002/redis.conf
其他9003~9006伪节点同理。
5、启动各个伪节点
cp -r /home/redis/redis-5.0.12/bin redis-cluster/bin
cd /home/redis/redis-cluster/
./bin/redis-server 10001/redis.conf
./bin/redis-server 10002/redis.conf
./bin/redis-server 10003/redis.conf
./bin/redis-server 10004/redis.conf
./bin/redis-server 10005/redis.conf
./bin/redis-server 10006/redis.conf
启动后可以通过ps -ef|grep redis进行检查
[redis@ATS-HOST redis-cluster]$ ps -ef|grep redis
root 2959 2855 0 17:33 pts/4 00:00:00 su - redis
redis 2960 2959 0 17:33 pts/4 00:00:00 -bash
redis 4199 1 0 18:19 ? 00:00:00 ./bin/redis-server 10.8.4.187:10001 [cluster]
redis 4221 1 0 18:20 ? 00:00:00 ./bin/redis-server 10.8.4.187:10002 [cluster]
redis 4223 1 0 18:20 ? 00:00:00 ./bin/redis-server 10.8.4.187:10003 [cluster]
redis 4225 1 0 18:20 ? 00:00:00 ./bin/redis-server 10.8.4.187:10004 [cluster]
redis 4227 1 0 18:20 ? 00:00:00 ./bin/redis-server 10.8.4.187:10005 [cluster]
redis 4241 1 0 18:20 ? 00:00:00 ./bin/redis-server 10.8.4.187:10006 [cluster]
redis 4245 2960 0 18:20 pts/4 00:00:00 ps -ef
redis 4246 2960 0 18:20 pts/4 00:00:00 grep --color=auto redis
6、启动集群
cd /home/redis/redis-cluster/bin
./redis-cli --cluster create 10.8.4.187:10001 10.8.4.187:10002 10.8.4.187:10003 10.8.4.187:10004 10.8.4.187:10005 10.8.4.187:10005 --cluster-replicas 1
第一次创建的时候会出现选项:
[redis@ATS-HOST redis-cluster]$ cd /home/redis/redis-cluster/bin
[redis@ATS-HOST bin]$ ./redis-cli --cluster create 10.8.4.187:10001 10.8.4.187:10002 10.8.4.187:10003 10.8.4.187:10004 10.8.4.187:10005 10.8.4.187:10005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.8.4.187:10005 to 10.8.4.187:10001
Adding replica 10.8.4.187:10005 to 10.8.4.187:10002
Adding replica 10.8.4.187:10004 to 10.8.4.187:10003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 74e819afbdc22eab958b1c9ada3d60a2ed20a214 10.8.4.187:10001
slots:[0-5460] (5461 slots) master
M: 8c1b0f4e0d513b28b5548caaa93bb25b84cbaaa9 10.8.4.187:10002
slots:[5461-10922] (5462 slots) master
M: 6618c9cdd0d668cde157910193c1db5ba0aea4ee 10.8.4.187:10003
slots:[10923-16383] (5461 slots) master
S: 9d9a65e98a59c22dc2e7b7a23583fd0a977d6c38 10.8.4.187:10004
replicates 6618c9cdd0d668cde157910193c1db5ba0aea4ee
S: b9c2b5525019eb070bbc35384772dad0be868e8c 10.8.4.187:10005
replicates 74e819afbdc22eab958b1c9ada3d60a2ed20a214
S: b9c2b5525019eb070bbc35384772dad0be868e8c 10.8.4.187:10005
replicates 8c1b0f4e0d513b28b5548caaa93bb25b84cbaaa9
Can I set the above configuration? (type 'yes' to accept):
选择:yes
命令解释:调用 ruby创建集群,-replicas 1 表示主从复制比例为 1:1,一个主节点对应一个从节点;并分配好主从节点对应关系,及 solt大小。集群中有 16383 个 solt ,默认会平均分配。
7、验证
/home/redis/redis-cluster/bin/redis-cli -c -h 10.8.4.187 -p 10001
cluster info
cluster nodes
[redis@ATS-HOST bin]$ /home/redis/redis-cluster/bin/redis-cli -c -h 10.8.4.187 -p 10001
10.8.4.187:10001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:5
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:59
cluster_stats_messages_pong_sent:60
cluster_stats_messages_sent:119
cluster_stats_messages_ping_received:56
cluster_stats_messages_pong_received:59
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:119
10.8.4.187:10001> cluster nodes
9d9a65e98a59c22dc2e7b7a23583fd0a977d6c38 10.8.4.187:10004@20004 slave 6618c9cdd0d668cde157910193c1db5ba0aea4ee 0 1618568730999 4 connected
74e819afbdc22eab958b1c9ada3d60a2ed20a214 10.8.4.187:10001@20001 myself,master - 0 1618568731000 1 connected 0-5460
6618c9cdd0d668cde157910193c1db5ba0aea4ee 10.8.4.187:10003@20003 master - 0 1618568732001 3 connected 10923-16383
b9c2b5525019eb070bbc35384772dad0be868e8c 10.8.4.187:10005@20005 slave 8c1b0f4e0d513b28b5548caaa93bb25b84cbaaa9 0 1618568731000 5 connected
8c1b0f4e0d513b28b5548caaa93bb25b84cbaaa9 10.8.4.187:10002@20002 master - 0 1618568729999 2 connected 5461-10922
10.8.4.187:10001>
不管是单节点伪集群,还是多节点集群,部署操作都是一样的。
哨兵部署
部署架构:一主一从一哨兵,生产上建议:一主两从三哨兵
1、创建redis安装用户
执行以下代码
groupadd redis
useradd -g redis -d /home/redis -s /bin/bash -m redis
passwd redis
2、检查gcc版本
[redis@ATS-HOST ~]$ gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
目标:x86_64-redhat-linux
配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
线程模型:posix
gcc 版本 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
如果小于4.8.5,安装gcc 4.8.5版本,命令如下
[redis@ATS-HOST ~]$ tar -zxvf gcc.tar.gz
解压gcc.tar.gz后得到gcc目录,切换至root用户后,安装gcc
su - root
cd /home/redis/gcc/
rpm -ivh *.rpm --nodeps --force
3、使用redis用户,解压redis-5.0.12.tar.gz
1)解压:在/home/redis目录执行
tar -zxvf redis-5.0.12.tar.gz
编译:进入redis-5.0.12目录,执行
make MALLOC=libc
编译后检查redis-5.0.12中会有对应的src文件夹。
安装:进行src目录下,执行
make PREFIX=/home/redis/redis-5.0.12 install
安装成功后,在redis-5.0.12目录下会生成bin文件夹
4、创建并配置redis伪集群目录
1)在/home/redis目录下,创建redis-cluster目录
cd /home/redis && mkdir redis-sentinel
2)进入到redis-sentinel目录下,创建10001、10002、... 、 10006共6个文件目录以及data目录。
cd redis-sentinel
#创建主节点目录
mkdir 11001 && mkdir 11001/data
#创建从节点目录
mkdir 11002 && mkdir 11002/data
#创建哨兵目录
mkdir 12001 && mkdir 12001/data
3)将/home/redis-sentinel/redis-5.0.12/redis.conf
复制到/home/redis-sentinel/redis-sentinel/11001
目录下,修改配置。
cp /home/redis-sentinel/redis-5.0.12/redis.conf /home/redis-sentinel/redis-sentinel/11001
vi /home/redis-sentinel/redis-sentinel/11001/redis.conf
4)修改成以下配置后保存
port 10001(每个节点的端口号)
daemonize yes
bind 10.8.4.187(绑定当前机器 IP)
logfile "/home/redis-sentinel/redis-sentinel/11001/redis-11001.log"
dir /home/redis-sentinel/redis-sentinel/11001/data/(数据文件存放位置)
pidfile /var/run/redis_11001.pid(pid 9001和port要对应)
5)将/home/redis-sentinel/redis-sentinel/11001/redis.conf复制到11002从节点
cp /home/redis-sentinel/redis-sentinel/11001/redis.conf /home/redis-sentinel/redis-sentinel/11002/redis.conf
6)将/home/redis-sentinel/redis-sentinel/11002/redis.conf中的11001
全部替换成11002
方式一
vi /home/redis-sentinel/redis-sentinel/11002/redis.conf 输入 ':%s/11001/11002/g'
方式二
sed -i "s/11001/11002/g" /home/redis-sentinel/redis-sentinel/10002/redis.conf
另外,需要在11002从节点配置文件的底部添加一行
#表示当前节点为10.8.4.187:11001主节点的从节点
slaveof 10.8.4.187 11001
7)将/home/redis-sentinel/redis-5.0.12/sentinel.conf
复制到/home/redis-sentinel/redis-sentinel/12001
目录下,修改配置。
cp /home/redis-sentinel/redis-5.0.12/sentinel.conf /home/redis-sentinel/redis-sentinel/12001
vi /home/redis-sentinel/redis-sentinel/12001/sentinel.conf
8)将sentinel.conf修改成以下配置后保存
// Sentinel节点的端口
port 12001
dir /home/redis-sentinel/redis-sentinel/12001/data/
logfile "/home/redis-sentinel/redis-sentinel/12001/redis-12001.log"
// 当前Sentinel节点监控 10.8.4.187:11001 这个主节点
// 1代表判断主节点失败至少需要2个Sentinel节点节点同意
// mymaster是主节点的别名
sentinel monitor mymaster 10.8.4.187 11001 1
//每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒且没有回复,则判定不可达
sentinel down-after-milliseconds mymaster 30000
//当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1,如果sentinel的节点数为3,可以将这个值修改为2
sentinel parallel-syncs mymaster 1
//故障转移超时时间为180000毫秒
sentinel failover-timeout mymaster 180000
5、启动各个伪节点
cp -r /home/redis-sentinel/redis-5.0.12/bin /home/redis-sentinel/redis-sentinel/bin
cd /home/redis-sentinel/redis-sentinel/
# 启动主节点
./bin/redis-server 11001/redis.conf
# 启动从节点
./bin/redis-server 11002/redis.conf
# 启动哨兵节点
./bin/redis-sentinel 12001/sentinel.conf
启动后可以通过ps -ef|grep redis进行检查
[redis@ATS-HOST redis-cluster]$ ps -ef|grep redis
redis-s+ 19130 1 0 10:21 ? 00:00:02 ./bin/redis-server 10.8.4.187:11002
redis-s+ 19164 1 0 10:24 ? 00:00:03 ./redis-sentinel *:12001 [sentinel]
redis-s+ 19195 1 0 10:29 ? 00:00:01 ./redis-server 10.8.4.187:11001
6、验证主从关系
连接主节点:10.8.4.187:11001,查看Replication信息
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 11001
10.8.4.187:11001> INFO Replication
# Replication
role:master ## 当前角色为主节点
connected_slaves:1
slave0:ip=10.8.4.187,port=11002,state=online,offset=56,lag=1
master_replid:8a536c99fd306e0290aca8fe6668c742014ab23a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
10.8.4.187:11001>
连接从节点:10.8.4.187:11002,查看Replication信息
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 11002
10.8.4.187:11002> INFO Replication
# Replication
role:slave ## 当前角色为从节点,下边有主节点的一些信息,比如ip,端口,上次io间隔
master_host:10.8.4.187
master_port:11001
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8a536c99fd306e0290aca8fe6668c742014ab23a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
7、验证哨兵关系
连接哨兵:10.8.4.187:12001,查看Sentinel信息
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 12001
10.8.4.187:12001> INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.8.4.187:11001,slaves=1,sentinels=1 # 当前哨兵正监控主节点
8、验证故障转移
1)干掉主节点进程10.8.4.187:11001
[redis@ATS-HOST redis-cluster]$ ps -ef|grep redis
redis-s+ 19130 1 0 10:21 ? 00:00:02 ./bin/redis-server 10.8.4.187:11002
redis-s+ 19164 1 0 10:24 ? 00:00:03 ./redis-sentinel *:12001 [sentinel]
redis-s+ 19195 1 0 10:29 ? 00:00:01 ./redis-server 10.8.4.187:11001
[redis@ATS-HOST redis-cluster]$ kill -9 19195
2)连接哨兵:10.8.4.187:12001,查看Sentinel信息,发现主节点的状态已经为odown
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 12001
10.8.4.187:12001> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=odown,address=10.8.4.187:11001,slaves=1,sentinels=1 #主节点的状态已经为odown
3)重启10.8.4.187:11001
./redis-server ../11001/redis.confg
4)连接哨兵:10.8.4.187:12001,查看Sentinel信息,发现主节点已经切换为10.8.4.187:11002
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.8.4.187:11002,slaves=1,sentinels=1 #主节点已经切换为10.8.4.187:11002
5)查看10.8.4.187:11002的状态,节点状态已经变更为mater
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 11002
10.8.4.187:11002> info replication
# Replication
role:master #节点状态已经变更为mater
connected_slaves:0
master_replid:92e5e2b84c159cf7f1216f9d4035c1bbd40e61c8
master_replid2:ed926b0ae26af6826ace85e835e3aa527edba8ef
master_repl_offset:35504
second_repl_offset:5154
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:35504
6)查看10.8.4.187:11001的状态,节点状态已经变更为slave
[redis-sentinel@ATS-HOST bin]$ ./redis-cli -h 10.8.4.187 -p 11001
10.8.4.187:11001> info replication
# Replication
role:slave
master_host:10.8.4.187
master_port:11002
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:41239
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:92e5e2b84c159cf7f1216f9d4035c1bbd40e61c8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:41239
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:41081
repl_backlog_histlen:159