一、操作系统环境准备
1.1 操作系统版本
Red Hat Linux 6(UL1+)、Red Hat Linux 7(UL0+)、CentOS7.2+【当前案例版本7.6】
1.2 操作系统环境
1.2.1 内核参数配置
[root@localhost ~]# echo 1 > /proc/sys/vm/overcommit_memory
[root@localhost ~]# echo 512 > /proc/sys/net/core/somaxconn
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 10000
1.2.2 修改ulimit值
[root@mo ~]# vi /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 16384
* hard nproc 16384
[root@mo ~]# vi /etc/security/limits.d/20-nproc.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 16384
* hard nproc 16384
1.2.3 yum依赖安装
[root@mo ~]# yum install -y gcc gcc-c++ automake make tcl
1.2.4 用户创建
添加redis用户组以及redis用户,默认赋予redis用户/redis目录所有权限
[root@mo ~]# groupadd redis
[root@mo ~]# useradd redis -g redis
二、软件编译安装
1.1 集群模式
1.1.1 软件包存储目录
使用redis用户创建安装包存放目录/redis/sourcefile
[redis@mo ~]$ mkdir -p /redis/sourcefile
上传安装包到安装包存放目录/redis/sourcefile/
[redis@mo sourcefile]$ ls –l
-rw-r--r-- 1 redis redis 1039530 Jun 8 2020 redis-5.0.9.tar.gz
1.1.2 解压编译安装
解压源文件目录中redis-5.0.9.tar.gz
[redis@mo ~]$ cd /redis/sourcefile
[redis@mo sourcefile]$ tar -zxvf redis-5.0.9.tar.gz
执行configure进行预编译
[redis@mo sourcefile]$ cd /redis/sourcefile/redis-5.0.9./configure
[redis@mo redis-5.0.9]$ ./configure
执行make以及make install进行编译以及安装
[redis@mo redis-5.0.9]$ make
[redis@mo redis-5.0.9]$ make PREFIX=/redis/redis install
[redis@mo redis-5.0.9]$ cd /redis/redis
[redis@mo redis]$ mkdir -p /redis/redis/{data,logs,conf,run}
1.1.3 主配置文件
三台主机,三主三从,每台机器一主一从,主从关系两两交叉
[redis@mo redis]$ cp /redis/sourcefile/redis-5.0.9/redis.conf /redis/redis/conf
[redis@mo redis]$ cd /redis/redis/conf
[redis@mo conf]$ mv redis.conf redis-6379.conf
[redis@mo conf]$ vim redis-6379.conf
bind 192.168.1.1
protected-mode yes
port 6379
daemonize yes
pidfile /redis/redis/run/redis_6379.pid
logfile "/redis/redis/logs/redis-6379.log"
dir /redis/redis/data
cluster-enabled yes
cluster-config-file /redis/redis/conf/nodes-6379.conf
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
masterauth 0okm)OKM
requirepass 0okm)OKM
1.1.4 从配置文件
三台主机,三主三从,每台机器一主一从,主从关系两两交叉
[redis@mo redis]$ cd /redis/redis/conf
[redis@mo conf]$ cp redis-6379.conf redis-6380.conf
[redis@mo conf]$ vim redis-6380.conf
bind 192.168.1.1
protected-mode yes
port 6380
daemonize yes
pidfile /redis/redis/run/redis_6380.pid
logfile "/redis/redis/logs/redis-6380.log"
dir /redis/redis/data
cluster-enabled yes
cluster-config-file /redis/redis/conf/nodes-6379.conf
cluster-node-timeout 15000
cluster-migration-barrier 1
cluster-require-full-coverage yes
masterauth 0okm)OKM
requirepass 0okm)OKM
1.1.5 集群创建
依次启动redis所有节点【三台机器上的6379/6380全部启动】
[redis@mo conf]$ cd /redis/redis/bin
[redis@mo bin]$ ./redis-server ../conf/redis-6379.conf
创建集群中三主节点
[redis@mo bin]$ ./redis-cli --cluster create 192.168.1.1:6379 192.168.1.2:6379 192.168.1.3:6379 -a '0okm)OKM'
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 9bd95c948e95393307acf856681496a28ba43a2e 192.168.1.1:6379
slots:[0-5460] (5461 slots) master
M: e9a8de17d091a16ba39f6bcd541ff4d8eab29e32 192.168.1.2:6379
slots:[5461-10922] (5462 slots) master
M: 1c0cb61f861f0557e66139feb7977dd2357581c9 192.168.1.3:6379
slots:[10923-16383] (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 192.168.1.1:6379)
M: 9bd95c948e95393307acf856681496a28ba43a2e 192.168.1.1:6379
slots:[0-5460] (5461 slots) master
M: e9a8de17d091a16ba39f6bcd541ff4d8eab29e32 192.168.1.2:6379
slots:[5461-10922] (5462 slots) master
M: 1c0cb61f861f0557e66139feb7977dd2357581c9 192.168.1.3:6379
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
选中三主中任意一主节点进行客户端连接,通过cluster meet方式添加从节点
[redis@mo bin]$ ./redis-cli -c -h 192.168.1.1 -p 6379 -a '0okm)OKM'
192.168.1.1:6379> cluster meet 192.168.1.1 6380
OK
192.168.1.1:6379> cluster meet 192.168.1.2 6380
OK
192.168.1.1:6379> cluster meet 192.168.1.3 6380
OK
查看此时集群节点信息,节点ID在指定主从时候需要用到
192.168.1.1:6379> cluster nodes
158d5dc2702506566db8571ab631af0b79f9cd87 192.168.1.2:6380@40015 master - 0 1600830128000 5 connected
1c0cb61f861f0557e66139feb7977dd2357581c9 192.168.1.3:6379@40013 master - 0 1600830126793 3 connected 10923-16383
e9a8de17d091a16ba39f6bcd541ff4d8eab29e32 192.168.1.2:6379@40012 master - 0 1600830125792 2 connected 5461-10922
114d637cbbd3097c5388ba34282a33e553975c4a 192.168.1.1:6380@40014 master - 0 1600830127795 4 connected
b6e7a187f98a6b943f7800a3f2739ec70eae8eac 192.168.1.3:6380@40016 master - 0 1600830128799 0 connected
9bd95c948e95393307acf856681496a28ba43a2e 192.168.1.1:6379@40011 myself,master - 0 1600830125000 1 connected 0-5460
为主节点指定对应从节点,保持交叉主从(遵循主从节点不同机原则)
[redis@mo bin]$ ./redis-cli -c -h 192.168.1.1 -p 6380 -a '0okm)OKM'
192.168.1.1:6380> CLUSTER REPLICATE 9bd95c948e95393307acf856681496a28ba43a2e
[redis@mo bin]$ ./redis-cli -c -h 192.168.1.1 -p 6380 -a '0okm)OKM'
192.168.1.2:6380> CLUSTER REPLICATE e9a8de17d091a16ba39f6bcd541ff4d8eab29e32
[redis@mo bin]$ ./redis-cli -c -h 192.168.1.1 -p 6380 -a '0okm)OKM'
192.168.1.3:6380> CLUSTER REPLICATE 1c0cb61f861f0557e66139feb7977dd2357581c9
集群节点信息查看
192.168.1.3:6380> cluster nodes
158d5dc2702506566db8571ab631af0b79f9cd87 192.168.1.2:6380@40015 slave e9a8de17d091a16ba39f6bcd541ff4d8eab29e32 0 1600830435000 5 connected
e9a8de17d091a16ba39f6bcd541ff4d8eab29e32 192.168.1.2:6379@40012 master - 0 1600830436000 2 connected 5461-10922
1c0cb61f861f0557e66139feb7977dd2357581c9 192.168.1.3:6379@40013 master - 0 1600830438094 3 connected 10923-16383
b6e7a187f98a6b943f7800a3f2739ec70eae8eac 192.168.1.3:6380@40016 myself,slave 1c0cb61f861f0557e66139feb7977dd2357581c9 0 1600830437000 0 connected
9bd95c948e95393307acf856681496a28ba43a2e 192.168.1.1:6379@40011 master - 0 1600830437091 1 connected 0-5460
114d637cbbd3097c5388ba34282a33e553975c4a 192.168.1.1:6380@40014 slave 9bd95c948e95393307acf856681496a28ba43a2e 0 1600830436088 4 connected
集群至此创建成功
1.1.6 停止服务
/redis/redis/bin/redis-cli -h 192.168.1.1 -p 6379 -a '0okm)OKM' shutdown
/redis/redis/bin/redis-cli -h 192.168.1.1 -p 6380 -a '0okm)OKM' shutdown
1.1.7 启动服务
/redis/redis/bin/redis-server /redis/redis/conf/redis-6379.conf
/redis/redis/bin/redis-server /redis/redis/conf/redis-6380.conf
1.2 哨兵模式
1.2.1 软件包存储目录
使用redis用户创建安装包存放目录/redis/sourcefile
[redis@mo ~]$ mkdir -p /redis/sourcefile
上传安装包到安装包存放目录/redis/sourcefile/
[redis@mo sourcefile]$ ls –l
-rw-r--r-- 1 redis redis 1039530 Jun 8 2020 redis-5.0.9.tar.gz
1.2.2 解压编译安装
解压源文件目录中redis-5.0.9.tar.gz
[redis@mo ~]$ cd /redis/sourcefile
[redis@mo sourcefile]$ tar -zxvf redis-5.0.9.tar.gz
执行configure进行预编译
[redis@mo sourcefile]$ cd /redis/sourcefile/redis-5.0.9./configure
[redis@mo redis-5.0.9]$ ./configure
执行make以及make install进行编译以及安装
[redis@mo redis-5.0.9]$ make
[redis@mo redis-5.0.9]$ make PREFIX=/redis/redis install
[redis@mo redis-5.0.9]$ cd /redis/redis
[redis@mo redis]$ mkdir -p /redis/redis/{data,logs,conf,run}
1.2.3 主配置文件
三台主机,一主二从
[redis@mo redis]$ cp /redis/sourcefile/redis-5.0.9/redis.conf /redis/redis/conf
[redis@mo redis]$ cd /redis/redis/conf
[redis@mo conf]$ mv redis.conf redis-6379.conf
[redis@mo conf]$ vim redis-6379.conf
bind 192.168.1.1
protected-mode yes
port 6379
daemonize yes
pidfile /redis/redis/run/redis_6379.pid
logfile "/redis/redis/logs/redis-6379.log"
dir /redis/redis/data
masterauth 0okm)OKM
requirepass 0okm)OKM
1.2.4 从配置文件1
[redis@mo redis]$ cd /redis/redis/conf
[redis@mo conf]$ vim redis-6380.conf
bind 192.168.1.1
protected-mode yes
port 6380
daemonize yes
pidfile /redis/redis/run/redis_6380.pid
logfile "/redis/redis/logs/redis-6380.log"
dir /redis/redis/data
replicaof 192.168.1.1 6379
masterauth 0okm)OKM
requirepass 0okm)OKM
1.2.5 从配置文件2
[redis@mo redis]$ cd /redis/redis/conf
[redis@mo conf]$ vim redis-6380.conf
bind 192.168.1.1
protected-mode yes
port 6380
daemonize yes
pidfile /redis/redis/run/redis_6380.pid
logfile "/redis/redis/logs/redis-6380.log"
dir /redis/redis/data
replicaof 192.168.1.1 6379
masterauth 0okm)OKM
requirepass 0okm)OKM
1.2.6 sentinel.conf配置<哨兵配置文件>
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.1.1 6379 2
sentinel auth-pass mymaster 0okm)OKM
port 26379
daemonize yes
pidfile "/redis/redis/run/redis-sentinel.pid"
logfile "/redis/redis/logs/redis-sentinel.log"
dir "/redis/redis/data"
1.2.7 停止服务
/redis/redis/bin/redis-cli -h 192.168.1.1 -p 6379 -a '0okm)OKM' shutdown
/redis/redis/bin/redis-cli -h 192.168.1.1 -p 26379 -a '0okm)OKM' shutdown
1.2.8 启动服务
/redis/redis/bin/redis-server /redis/redis/conf/redis-6379.conf
/redis/redis/bin/redis-sentinel /redis/redis/conf/redis-sentinel.conf