Redis-5.0.5集群部署

2020-04-30   393 次阅读


  • 环境介绍
    • 系统:CentOS7.6
    • 采用三主三从方式
主机 ip地址 端口
redis-1 10.110.10.3 6379、6380、6381
redis-2 10.110.10.4 6382、6383、6384

1.创建安装目录

mkdir -p /usr/local/redis

2.安装依赖包

yum -y install ruby ruby-devel rubygems rpm-build tcl gcc

3.解压源码包

tar zxvf   redis-5.0.5.tar.gz
cd  redis-5.0.5

4.编译&编译测试

make && make test
All tests passed without errors!
Cleanup: may take some time... OK
没有错误,test 测试ok!

5.redis比较特殊,在这里指定安装路径

[root@redis-1 src]$ make install PREFIX=/usr/local/redis

6.安装后在bin下有如下文件

[root@redis-1 src]$ ls -ahl /usr/local/redis/bin
-rwxr-xr-x. 1 root root 353824 Oct 17 17:09 redis-benchmark  #redis性能测试工具启动,测试redis在你的系统及你的配置下的读写性能
-rwxr-xr-x. 1 root root 4045352 Oct 17 17:09 redis-check-aof #更新aof日志检查
-rwxr-xr-x. 1 root root 4045352 Oct 17 17:09 redis-check-rdb #本地数据库检查
-rwxr-xr-x. 1 root root 794384 Oct 17 17:09 redis-cli        #命令行操作工具(即客户端)
lrwxrwxrwx. 1 root root 12 Oct 17 17:09 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 4045352 Oct 17 17:09 redis-server    #redis服务器的daemon启动程序

7.配置环境变量

[root@redis-1 redis]$ echo 'export PATH=$PATH:/usr/local/redis/bin' >> /etc/profile
[root@redis-1 redis]$ source /etc/profile
[root@redis-1 redis]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/redis/bin

8.查看redis启动方式

[root@redis-1 redis]$ redis-server --help
Usage: ./redis-server [/path/to/redis.conf] [options]
./redis-server - (read config from stdin)
./redis-server -v or --version
./redis-server -h or --help
./redis-server --test-memory

9.拷贝安装包目录下的配置文件到安装目录的conf目录下

[root@redis-1 redis]$ cd /opt/redis-5.0.5
[root@redis-1 redis-5.0.5]$ mkdir -p /usr/local/redis/conf
[root@redis-1 redis-5.0.5]$ cp redis.conf /usr/local/redis/conf

10.创建 Redis 节点

  • 在/usr/local/redis_cluster目录下,创建各节点的目录为6379、6380、6381
[root@redis-1 ~]$ cd /usr/local/redis_cluster/
[root@redis-1 redis_cluster]$ mkdir 6379 6380 6381

11.拷贝配置文件

[root@redis-1 redis_cluster]$ cp /data/software/redis/redis-5.0.5/redis.conf /usr/local/redis_cluster/6379
[root@redis-1 redis_cluster]$ cp /data/software/redis/redis-5.0.5/redis.conf /usr/local/redis_cluster/6380
[root@redis-1 redis_cluster]$ cp /data/software/redis/redis-5.0.5/redis.conf /usr/local/redis_cluster/6381

12.修改配置

port 6379                            #修改端口为6379、6380、6381
bind 0.0.0.0                         #改为其他节点机器可访问的ip,本机ip
daemonize no                         #设置为yes,使redis在后台运行
pidfile /var/run/redis_6379.pid      #修改端口为6379、6380、6381
#cluster-enabled yes                 #开启集群 把注释#去掉
#cluster-config-file nodes-6379.conf #集群的配置,把注释#去掉,并改为对应的6379,6380,6381
cluster-node-timeout 15000           #请求超时,默认15秒,可自行设置
masterauth test@2018                 #设置auth认证密码
requirepass test@2018                #设置auth认证密码,这两个都要写。

13.启动各个节点

[root@redis-1 ~]$ /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6379/redis.conf
12478:C 17 Oct 2019 22:15:41.209 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12478:C 17 Oct 2019 22:15:41.209 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12478, just started
12478:C 17 Oct 2019 22:15:41.209 # Configuration loaded
[root@redis-1 ~]$ /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6380/redis.conf
[root@redis-1 ~]$ /usr/local/redis/bin/redis-server /usr/local/redis_cluster/6381/redis.conf

14.查看进程是否存在

[root@redis-1 redis_cluster]$ ps -ef | grep redis
root 12479 1 0 22:15 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.3:6379 [cluster]
root 12484 1 0 22:15 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.3:6380 [cluster]
root 12503 1 0 22:19 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.3:6381 [cluster]

15.根据以上方法同样部署redis-2,然后启动各个节点

[root@redis-2 redis_cluster]$ ps -ef |grep redis
root 23225 1 0 22:43 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.4:6382 [cluster]
root 23230 1 0 22:43 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.4:6383 [cluster]
root 23235 1 0 22:43 ? 00:00:00 /usr/local/redis/bin/redis-server 10.110.10.4:6384 [cluster]
root 23240 18761 0 22:43 pts/2 00:00:00 grep --color=auto redis

16.创建集群

[root@api-1 redis_cluster]$ redis-cli --cluster  create 10.110.10.3:6379 10.110.10.3:6380 10.110.10.3:6381 10.110.10.4:6382 10.110.10.4:6383 10.110.10.4:6384 --cluster-replicas 1 -a test@2018
Can I set the above configuration? (type 'yes' to accept):yes  #注意这里一定要输入  “yes” 全拼
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 集群搭建结束!
  • 注意事项:
    • 1.关闭redis集群不能直接kill掉进程,或者关机
    • 2.我们要通过命令 redis-cli -a test@2018 -p 6380 shutdown 进行关闭,这样在关闭之前,数据才能够进行保存。

17.查看集群状态

redis-cli -a test@2018 --cluster check 10.110.10.3:6381  #这里可以写任意一个节点
或:
127.0.0.1:6381> cluster info
cluster_state:ok                             #所有slots正常则显示为OK,否则为error
cluster_slots_assigned:16384                 #多少slots被分配了,即多少被master管理了,16384为全部slots
cluster_slots_ok:16384                       #有多少slots是正常的
cluster_slots_pfail:0                        #有多少slots可能处于异常状态,处于这个状态并不表示有问题,仍能继续提供服务
cluster_slots_fail:0                         #有多少slots处于异常状态,需要修复才能服务
cluster_known_nodes:6                        #集群中的节点数
cluster_size:3                               #集群中master个数
cluster_current_epoch:6                      #本地的当前时间变量,用于故障切换时生成独一无二的增量版本号
cluster_my_epoch:2
cluster_stats_messages_ping_sent:251
cluster_stats_messages_pong_sent:240
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:494              #通过集群消息总线发送的消息总数
cluster_stats_messages_ping_received:238
cluster_stats_messages_pong_received:254
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:494          #通过集群消息总线收到的消息总数
127.0.0.1:7004> 
  • 几种常见的redis集群启动警告信息处理
    • 问题一:WARNING overcommit_memory is set to 0!
      • 三种处理方法:
      1. echo "vm.overcommit_memory=1" >>/etc/sysctl.conf && sysctl -p
      2. sysctl vm.overcommit_memory=1
      3. echo "1" > /proc/sys/vm/overcommit_memory
  • 问题二:WARNING you have Transparent Huge Pages (THP) support enabled in your kernel.
    • 处理方法:
      echo "never" > /sys/kernel/mm/transparent_hugepage/enabled 并将其添加到/etc/rc.local,以便在重新启动后保留设置
  • 问题三:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    • 处理方法
      [root@redis-1 ~]$ echo "511" > /proc/sys/net/core/somaxconn
      [root@redis-1 ~]$ echo -e “net.core.somaxconn=1024” >> /etc/sysctl.conf
      [root@redis-1 ~]$ sysctl -p

脑无理想 枉活一世