阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS7下Redis3.2.3 Cluster配置

176次阅读
没有评论

共计 7648 个字符,预计需要花费 20 分钟才能阅读完成。

配置 redis cluster,以两台机器,6 个实例为环境。

操作系统 CentOS Linux release 7 64 位

安装依赖包:单独下载 redis-3.3.1.gem 这个包要对应 os 版本
yum -y install tcl   
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis 

解压安装:

cd /usr/local/
tar zxvf redis-3.2.3.tar.gz
cd redis-3.2.3
make && make install

在 172.16.25.220 的 /usr/local/webapp 下创建 bin、redis6379、redis6380、redis6381 四个目录
mkdir bin
mkdir redis6379
mkdir redis6380
mkdir redis6381

在 172.16.25.72 的 /usr/local/webapp 下创建 bin、redis6379、redis6380、redis6381 四个目录
mkdir bin
mkdir redis6379
mkdir redis6380
mkdir redis6381

分别在两台机器上创建配置文件,并启动:
cd /usr/local/redis-3.2.3/src/
cp mkreleasehdr.sh redis-benchmark redis-sentinel redis-server redis-trib.rb redis-cli /usr/local/webapp/bin
cd /usr/local/webapp/redis6379/
touch redis-6379.conf
cp /usr/local/webapp/redis6379/redis-6379.conf /usr/local/webapp/redis6380/redis-6380.conf
cp /usr/local/webapp/redis6379/redis-6379.conf /usr/local/webapp/redis6381/redis-6381.conf
sed -i ‘s/6379/6380/g’ /usr/local/webapp/redis6380/redis-6380.conf
sed -i ‘s/6379/6381/g’ /usr/local/webapp/redis6381/redis-6381.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6379/redis-6379.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6380/redis-6380.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6381/redis-6381.conf

创建集群(在其中一台服务器上执行即可)
cd /usr/local/webapp/bin/
./redis-trib.rb create –replicas 1 172.16.25.220:6379 172.16.25.72:6379 172.16.25.220:6380 172.16.25.72:6380 172.16.25.220:6381 172.16.25.72:6381

集群状态查看:
./redis-cli -c -h 172.16.25.220 -p 6380
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master – 0 1478249255421 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master – 0 1478249256423 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249254919 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master – 0 1478249254419 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249255921 7 connected

给 redis cluster 集群加上认证功能:
登陆集群后配置密码(我这里测试不生效)
./redis-cli -c -h 172.16.25.72 -p 6379
config set masterauth mypasswd
config set requirepass mypasswd
config rewrite
 
修改配置文件:加入下面两行
requirepass “mypasswd”
masterauth “mypasswd”
 
停 redis
./redis-cli -c -h 172.16.25.220 -p 6379 shutdown
./redis-cli -c -h 172.16.25.220 -p 6380 shutdown
./redis-cli -c -h 172.16.25.220 -p 6381 shutdown
./redis-cli -c -h 172.16.25.72 -p 6381 shutdown 
./redis-cli -c -h 172.16.25.72 -p 6380 shutdown
./redis-cli -c -h 172.16.25.72 -p 6379 shutdown
 
修改
修改 gem 的 redis 工具下的一个文件,我这里是默认安装,路径如下:/usr/local/share/gems/gems/redis-3.3.1/lib/redis/client.rb
vim /usr/local/share/gems/gems/redis-3.3.1/lib/redis/client.rb
 
启动 redis
service redis6379 start
service redis6380 start
service redis6381 start
 
登陆
./redis-cli -c -h 172.16.25.220 -p 6380
172.16.25.220:6380> cluster nodes
NOAUTH Authentication required.
172.16.25.220:6380> auth mypasswd
OK
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master – 0 1478249211338 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master – 0 1478249211839 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249210836 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master – 0 1478249212341 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249210334 7 connected
172.16.25.220:6380> 
 
./redis-cli -c -h 172.16.25.220 -p 6380 -a mypasswd
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master – 0 1478249255421 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master – 0 1478249256423 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249254919 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master – 0 1478249254419 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249255921 7 connected
172.16.25.220:6380> 
 
停止有密码认证的集群
./redis-cli -c -a “mypasswd” -h 172.16.25.220 -p 6379 shutdown

配置自启动:
如果配置成自启动,那相关的权限会变成 root 权限,请慎重考虑。下面给出自启动的脚本,仅供参考。
在 /etc/init.d 下编写脚本 redis6379,并给 + x 权限,然后配置启动级别
chkconfig –add redis6379
chkconfig –level 345 redis6379 on
脚本如下:
cat /etc/init.d/redis6379
#!/bin/bash
#chkconfig:345 61 61
 
PATH=/usr/local/bin:/sbin:/usr/bin:/bin 
     
REDISPORT=6379 
EXEC=/usr/local/bin/redis-server 
REDIS_CLI=/usr/local/bin/redis-cli 
     
PIDFILE=/usr/local/webapp/redis6379/redis_6379.pid 
CONF=”/usr/local/webapp/redis6379/redis-6379.conf” 
     
case “$1” in 
    start) 
        if [-f $PIDFILE] 
        then 
                echo “$PIDFILE exists, process is already running or crashed” 
        else 
                echo “Starting Redis server…” 
                $EXEC $CONF 
        fi 
        if [“$?”=”0”]   
        then 
              echo “Redis is running…” 
        fi 
        ;; 
    stop) 
        if [! -f $PIDFILE] 
        then 
                echo “$PIDFILE does not exist, process is not running” 
        else 
                PID=$(cat $PIDFILE) 
                echo “Stopping …” 
                $REDIS_CLI -p $REDISPORT SHUTDOWN 
                while [-x ${PIDFILE} ] 
              do 
                    echo “Waiting for Redis to shutdown …” 
                    sleep 1 
                done 
                echo “Redis stopped” 
        fi 
        ;; 
  restart|force-reload) 
        ${0} stop 
        ${0} start 
        ;; 
  *) 
    echo “Usage: /etc/init.d/redis {start|stop|restart|force-reload}” >&2 
        exit 1 
esac 
##############################

redis 的配置文件如下:
bind 172.16.25.220
protected-mode yes
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /usr/local/webapp/redis6379/redis_6379.pid
loglevel notice
logfile /usr/local/webapp/redis6379/redis_6379.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/webapp/redis6379
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
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
lua-time-limit 5000
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
requirepass “mypasswd”
masterauth “mypasswd”

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis 集���明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137014.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计7648字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中