redis集群搭建
#
灌云网站建设公司成都创新互联公司,灌云网站设计制作,有大型网站制作公司丰富经验。已为灌云上千余家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的灌云做网站的公司定做!
#redis 官网: https://redis.io
#redis 文档:https://redis.io/documentation
#redis 集群:https://redis.io/topics/cluster-tutorial
#redis 常用配置解释:https://yq.aliyun.com/articles/38806;https://github.com/linli8/cnblogs/blob/master/redis%E5%89%AF%E6%9C%AC.conf
#软件下载
[root@node3 ~]# wget http://download.redis.io/releases/redis-3.2.9.tar.gz
#编译
[root@node3 ~]# mkdir /usr/local/redis [root@node3 ~]# mkdir /usr/local/redis/{data,conf,bin} [root@node3 ~]# tar -xf redis-3.2.9.tar.gz [root@node3 ~]# cd redis-3.2.9/ [root@node3 redis-3.2.9]# make
#拷贝编译后的文件
[root@node3 redis-3.2.9]# cd src/ [root@node3 src]# \cp -p mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis/bin/ [root@node3 src]# cd .. [root@node3 redis-3.2.9]# cp -p redis.conf /usr/local/redis/conf/
#创建数据目录
[root@node3 redis-3.2.9]# cd /usr/local/redis/data/ [root@node3 data]# mkdir {7000..7005} [root@node3 data]# ls 7000 7001 7002 7003 7004 7005 [root@node3 data]#
#修改相应端口(大致如下)
[root@node3 conf]# grep '^[a-Z]' redis.conf bind 0.0.0.0 dir /usr/local/redis/data/7000 pidfile /var/run/redis_7000.pid appendfilename "appendonly-7000.aof" port 7000 cluster-config-file nodes-7000.conf cluster-enabled yes cluster-node-timeout 15000 protected-mode no tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no loglevel warning logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum no dbfilename dump.rdb slave-serve-stale-data yes slave-read-only yes repl-diskless-sync yes repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100 appendonly yes 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 100000 slowlog-max-len 128 hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 latency-monitor-threshold 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 64mb 16mb 60 hz 10 aof-rewrite-incremental-fsync yes maxmemory-policy allkeys-lru maxmemory-samples 5 maxclients 10000
#拷贝配置文件
[root@node3 conf]# for i in {7000..7005};do cp redis.conf $i.conf;done [root@node3 conf]# ls 7000.conf 7001.conf 7002.conf 7003.conf 7004.conf 7005.conf redis.conf [root@node3 conf]# rm -f redis.conf #批量修改 [root@node3 conf]# for i in {7000..7005};do sed -i "s/7000/$i/g" "$i".conf;done
#修改脚本(十分粗糙的脚本,不建议采纳,望读者自行编写)
[root@node3 ~]# cp -p redis-3.2.9/utils/redis_init_script /etc/init.d/redis [root@node3 ~]# vim /etc/init.d/redis [root@node3 ~]# cat /etc/init.d/redis #!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem. #REDISPORT=$2 EXEC=/usr/local/redis/bin/redis-server CLIEXEC=/usr/local/redis/bin/redis-cli CONFDIR="/usr/local/redis/conf/" #PIDFILE=/var/run/redis_${REDISPORT}.pid #CONF="$CONFDIR${REDISPORT}.conf" function main(){ REDISPORT=$2 PIDFILE=/var/run/redis_${REDISPORT}.pid CONF="$CONFDIR${REDISPORT}.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 ;; stop) if [ ! -f $PIDFILE ] then echo "$PIDFILE does not exist, process is not running" else PID=$(cat $PIDFILE) echo "Stopping ..." $CLIEXEC -p $REDISPORT shutdown while [ -x /proc/${PID} ] do echo "Waiting for Redis to shutdown ..." sleep 1 done echo "Redis stopped" fi ;; *) echo "Please use start or stop as first parameter,port or all as second parameter." ;; esac } if [ "$2" == "all" ] then for i in `ls $CONFDIR | grep [0-9] | cut -c 1-4` ;do main $1 $i done else main $1 $2 fi
#启动服务
[root@node3 ~]# service redis start all [root@node3 ~]# netstat -lntup | grep redis tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 6735/redis-server 0 tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 6737/redis-server 0 tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 6743/redis-server 0 tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 6747/redis-server 0 tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 6749/redis-server 0 tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 6755/redis-server 0 tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 6735/redis-server 0 tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 6737/redis-server 0 tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 6743/redis-server 0 tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 6747/redis-server 0 tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 6749/redis-server 0 tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 6755/redis-server 0 [root@node3 ~]#
#创建集群
[root@node3 conf]# yum install ruby ruby-devel rubygems rpm-build [root@node3 conf]# gem install redis #如果没有反应,可以更换源 #更换源 参考:http://gems.ruby-china.org/ # gem update --system # 这里请×××一下 #gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ # [root@node3 conf]# cd /usr/local/redis/bin/ [root@node3 bin]# ls mkreleasehdr.sh redis-check-aof redis-cli redis-server redis-benchmark redis-check-rdb redis-sentinel redis-trib.rb [root@node3 bin]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Adding replica 127.0.0.1:7003 to 127.0.0.1:7000 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 M: de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 slots:5461-10922 (5462 slots) master M: 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 slots:10923-16383 (5461 slots) master S: af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 replicates de6c4b00953b0e354ea375106f29bd627ed3ab2e S: 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 replicates fce2cebafefeb540592c624c68b741d465ce095b S: 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 replicates 83474177e39b72145adaad60cad1fe4523a0ee79 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 127.0.0.1:7000) M: de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 slots: (0 slots) slave replicates de6c4b00953b0e354ea375106f29bd627ed3ab2e M: 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 slots: (0 slots) slave replicates fce2cebafefeb540592c624c68b741d465ce095b S: 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 slots: (0 slots) slave replicates 83474177e39b72145adaad60cad1fe4523a0ee79 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
#登陆集群
[root@node3 ~]# /usr/local/redis/bin/redis-cli -c -h 127.0.0.1 -p 7000 127.0.0.1:7000> cluster nodes fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 master - 0 1495358027725 2 connected 5461-10922 de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460 af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 slave de6c4b00953b0e354ea375106f29bd627ed3ab2e 0 1495358029772 4 connected 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 master - 0 1495358025682 3 connected 10923-16383 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 slave fce2cebafefeb540592c624c68b741d465ce095b 0 1495358028748 5 connected 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 slave 83474177e39b72145adaad60cad1fe4523a0ee79 0 1495358026706 6 connected 127.0.0.1:7000> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_sent:77 cluster_stats_messages_received:77 127.0.0.1:7000> cluster nodes fce2cebafefeb540592c624c68b741d465ce095b 127.0.0.1:7001 master - 0 1495358038300 2 connected 5461-10922 de6c4b00953b0e354ea375106f29bd627ed3ab2e 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460 af00ce59424417ab1cfba4edc145223d4b3ac999 127.0.0.1:7003 slave de6c4b00953b0e354ea375106f29bd627ed3ab2e 0 1495358039395 4 connected 83474177e39b72145adaad60cad1fe4523a0ee79 127.0.0.1:7002 master - 0 1495358036106 3 connected 10923-16383 64391b1f359b6bfdfdd5373c71388fb119dc2bb3 127.0.0.1:7004 slave fce2cebafefeb540592c624c68b741d465ce095b 0 1495358033918 5 connected 3509837665c9cfe7a798513b87c84c9b2d3c9f1d 127.0.0.1:7005 slave 83474177e39b72145adaad60cad1fe4523a0ee79 0 1495358040489 6 connected 127.0.0.1:7000>
#redis集群搭建成功
本文标题:redis集群搭建
文章来源:http://scyanting.com/article/gdegds.html