Redis 5.0.7 的安装与群集部署
一、安装
(1) 下载源码包
[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
(2) 解压安装包,并进行编译安装
[root@localhost ~]# tar -xf redis-5.0.7.tar.gz [root@localhost ~]# yum install -y gcc gcc-c++ automake make [root@localhost ~]# cd redis-5.0.7/ [root@localhost redis-5.0.7]# yum install -y gcc gcc-c++ automake make [root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis [root@localhost redis-5.0.7]# make install
(3) 创建配置文件目录与其它相关目录,并将源码包中的配置文件复制到配置文件目录中
[root@localhost redis-5.0.7]# mkdir -v /usr/local/redis/{etc,log,data,run} [root@localhost redis-5.0.7]# cp redis.conf /usr/local/redis/etc/
(4) 创建服务启停脚本(如果需要)
[root@localhost redis-5.0.7]# cp ./utils/redis_init_script /etc/init.d/redisd [root@localhost redis-5.0.7]#sed -i 's#CONF="/etc/redis/${REDISPORT}.conf"#CONF="/usr/local/redis/etc/redis.conf"#g' /etc/init.d/redisd [root@localhost redis-5.0.7]# server redisd start
(5) 给命令创建软链接
[root@localhost redis-5.0.7]# ln -sv /usr/local/redis/bin/redis-* /usr/bin/
(6) 修改内核参数(解决启动时的WARNING)
[root@localhost redis-5.0.7]# echo "net.core.somaxconn = 512" >> /etc/sysctl.conf [root@localhost redis-5.0.7]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf [root@localhost redis-5.0.7]# sysctl -p net.core.somaxconn = 512 vm.overcommit_memory = 1 [root@localhost redis-5.0.7]# echo never > /sys/kernel/mm/transparent_hugepage/enabled #以上配置重启主机后会丢失,永久生效的方式如下: [root@localhost redis-5.0.7]# vim /etc/rc.local #添加以下行 echo never > /sys/kernel/mm/transparent_hugepage/enabled [root@localhost redis-5.0.7]# chmod +x /etc/rc.local
(7) 启动
[root@localhost redis-5.0.7]# service redisd start #需第4步配置脚本 #或以下启动方式,默认前台启动,要以守护进程启动需修改配置文件中daemonize no为daemonize yes [root@localhost redis-5.0.7]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf [root@localhost redis-5.0.7]# redis-server #需第5步创建软链接
二、群集部署
2.1 单机多实例
(1) 修改主配置文件 redis.conf
[root@localhost ~]# cd /usr/local/redis/ [root@localhost redis]# grep -Ev "^$|^#" /usr/local/redis/etc/redis.conf bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /usr/local/redis/run/redis_6379.pid loglevel notice logfile "/usr/local/redis/log/redis-6379.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ masterauth 123456 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 requirepass 123456 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
(2) 创建集群目录与配置文件
[root@localhost redis]# mkdir -pv cluster/{7000,7001,7002,7003,7004}/{etc,log,run} #复制配置文件 [root@localhost redis]# cp /usr/local/redis/etc/redis.conf /usr/local/redis/cluster/7000/etc/ #修改配置文件 [root@localhost redis]# grep -Ev "^$|^#" /usr/local/redis/cluster/7000/etc/redis.conf bind 0.0.0.0 protected-mode yes port 7000 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile /usr/local/redis/cluster/7000/run/redis_7000.pid loglevel notice logfile "/usr/local/redis/cluster/7000/log/redis-7000.log" databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ masterauth 123456 replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 requirepass 123456 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 cluster-enabled yes cluster-config-file nodes-7000.conf cluster-node-timeout 15000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
(3) 复制与修改另外的几个配置文件
[root@localhost redis]# pwd /usr/local/redis [root@localhost redis]# cp cluster/7000/etc/redis.conf cluster/7001/etc/ [root@localhost redis]# cp cluster/7000/etc/redis.conf cluster/7002/etc/ [root@localhost redis]# cp cluster/7000/etc/redis.conf cluster/7003/etc/ [root@localhost redis]# cp cluster/7000/etc/redis.conf cluster/7004/etc/ [root@localhost redis]# sed -i 's/7000/7001/g' cluster/7001/etc/redis.conf [root@localhost redis]# sed -i 's/7000/7002/g' cluster/7002/etc/redis.conf [root@localhost redis]# sed -i 's/7000/7003/g' cluster/7003/etc/redis.conf [root@localhost redis]# sed -i 's/7000/7004/g' cluster/7004/etc/redis.conf
(4) 制作启动脚本
[root@localhost redis]# vim start-redis-cluster.sh #!/bin/bash /usr/bin/redis-server /usr/local/redis/etc/redis.conf cd /usr/local/redis/cluster cd 7000 && /usr/bin/redis-server etc/redis.conf cd ../7001 && /usr/bin/redis-server etc/redis.conf cd ../7002 && /usr/bin/redis-server etc/redis.conf cd ../7003 && /usr/bin/redis-server etc/redis.conf cd ../7004 && /usr/bin/redis-server etc/redis.conf
[root@localhost redis]# chmod +x start-redis-cluster.sh
(5) 启动服务
[root@localhost redis]# ./start-redis-cluster.sh
(6) 创建集群
[root@localhost redis]# redis-cli --cluster create 192.168.145.57:6379 192.168.145.57:7000 192.168.145.57:7001 192.168.145.57:7002 192.168.145.57:7003 192.168.145.57:7004 --cluster-replicas 1 -a 123456
(7) 查看集群
[root@localhost redis]# redis-cli -c -p 6379 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> cluster nodes 6f2f3438916f241f487c4033fe9cd64d2960f24c 192.168.145.57:6379@16379 myself,master - 0 1594029981000 1 connected 0-5460 b46b2150673121f904283c52af8ea7184e0507cd 192.168.145.57:7004@17004 slave 6f2f3438916f241f487c4033fe9cd64d2960f24c 0 1594029981716 6 connected bf0976700450e98b540a87676776200be92c1b32 192.168.145.57:7003@17003 slave 49fb5881a98b6feb6bbc3ddd29a0dbc842d317ef 0 1594029980000 5 connected 684c45749d14bddfd99ac1ea8da9dbbdf7140726 192.168.145.57:7000@17000 master - 0 1594029979000 2 connected 5461-10922 49fb5881a98b6feb6bbc3ddd29a0dbc842d317ef 192.168.145.57:7001@17001 master - 0 1594029979000 3 connected 10923-16383 79a5972228b0383f383123fb028b3c75a386cbea 192.168.145.57:7002@17002 slave 684c45749d14bddfd99ac1ea8da9dbbdf7140726 0 1594029980709 4 connected
(8) 集群停止
[root@localhost redis]# kill -9 `ps -ef|grep -v grep|grep redis|awk '{print $2}'` #或下面这种方式 [root@localhost redis]# for i in 6379 7000 7001 7002 7003 7004; do redis-cli -p $i -a 123456 shutdown; done
(9) 集群重建
[root@localhost redis]# rm -rf nodes-6379.conf dump.rdb [root@localhost redis]# for((i=0;i<5;i++));do rm -rf cluster/700${i}/nodes-700${i}.conf; done [root@localhost redis]# for((i=0;i<5;i++));do rm -rf cluster/700${i}/dump.rdb; done