天天看點

redis叢集

實驗環境 

centos6.9_x64

redis_master  192.168.0.14    6379/6380/6381/   虛拟節點

reids_slave     192.168.0.15    6382/6383/6384/   虛拟節點

實驗軟體

redis-4.0.8.tar.gz

軟體安裝

yum install  -y wget lrzsz make gcc gcc-c++

yum install centos-release-scl-rh

ls /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo

/etc/yum.repos.d/CentOS-SCLo-scl-rh.repo

cp -pv /etc/sysctl.conf /etc/sysctl.conf.bak

echo net.ipv4.tcp_syncookies = 1 >> /etc/sysctl.conf

echo net.ipv4.tcp_tw_reuse = 1 >> /etc/sysctl.conf

echo net.ipv4.tcp_tw_recycle = 1 >> /etc/sysctl.conf

echo net.ipv4.tcp_fin_timeout = 30 >> /etc/sysctl.conf

echo "vm.max_map_count=262144"  >>  /etc/sysctl.conf

sysctl -p

yum install -y rh-ruby23 

scl enable  rh-ruby23 bash    

ruby -v

ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-linux-gnu]

gem install redis           redis服務端操作

tar zxvf /root/redis-4.0.8.tar.gz  

cd /root/redis-4.0.8

make -j6  && make install  PREFIX=/usr/local/redis 

mkdir -pv /usr/local/redis/cluster

cp -pv /root/redis-4.0.8/src/redis-trib.rb /usr/local/redis/

cp -pv /root/redis-4.0.8/redis.conf /usr/local/redis/cluster/6379-6381.conf        redis-master  6379-6381相同配置 

echo > /usr/local/redis/cluster/6379.conf   6379-6381相同配置  

touch /var/log/redis.log

mkdir -pv /usr/local/redis/data

echo 511 > /proc/sys/net/core/somaxconn

sed -i 's/vm.swappiness = 0/vm.swappiness = 1/g' /etc/sysctl.conf 

cat /usr/local/redis/cluster/6379.conf    6379/6380/6381配置檔案修改内容

bind  127.0.0.1 192.168.0.14

protected-mode yes

port 6379

daemonize yes

appendonly yes

appendfsync everysec

requirepass 123456      群集密碼

maxclients 128

dir /usr/local/redis/data

pidfile /var/run/redis_6379.pid

appendfilename appendonly.aof

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

cluster-enabled yes

cluster-config-file nodes-6379.conf

cluster-node-timeout 5000

cluster-slave-validity-factor 10

#cluster-migration-barrier 1

cluster-require-full-coverage yes

logfile "/var/log/redis.log"

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6379.conf    

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6380.conf     

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6381.conf     啟動服務

netstat -tuplna | grep redis

tcp        0      0 192.168.0.14:6379           0.0.0.0:*                   LISTEN 9989/redis-server 1

tcp        0      0 192.168.0.14:6380           0.0.0.0:*                   LISTEN 9994/redis-server 1

tcp        0      0 192.168.0.14:6381           0.0.0.0:*                   LISTEN 9999/redis-server 1

ps -ef | grep redis

root       9989      1  0 14:58 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.14:6379 [cluster]

root       9994      1  0 14:58 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.14:6380 [cluster]

root       9999      1  0 14:58 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.14:6381 [cluster]    以上操作為redis_master端操作

scp /root/redis-4.0.8.tar.gz [email protected]:/root/                                          

scp /usr/local/redis/cluster/6379.conf [email protected]:/usr/local/redis/cluster/6382.conf  6382/6382/6384相同配置

make  -j4 && make install PREFIX=/usr/local/redis 

mkdir -pv /usr/local/redis/cluster && mkdir  -pv /usr/local/redis/data

cp -pv /root/redis-4.0.8/redis.conf /usr/local/redis/cluster/6382.conf

echo > /usr/local/redis/cluster/6381.conf

echo > /usr/local/redis/cluster/6382.conf

cat /usr/local/redis/cluster/6382.conf    6382-6384相同配置

bind 192.168.0.15

port 6382

requirepass 123456

pidfile /var/run/redis_6382.pid

cluster-config-file nodes-6382.conf

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6382.conf   

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6383.conf  

/usr/local/redis/bin/redis-server /usr/local/redis/cluster/6384.conf     啟動服務

tcp        0      0 192.168.0.15:6382           0.0.0.0:*                   LISTEN 7165/redis-server 1

tcp        0      0 192.168.0.15:6383           0.0.0.0:*                   LISTEN 7158/redis-server 1

tcp        0      0 192.168.0.15:6384           0.0.0.0:*                   LISTEN 7170/redis-server 1

root       7158      1  0 20:55 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.15:6383 [cluster]

root       7165      1  0 20:56 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.15:6382 [cluster]

root       7170      1  0 20:56 ?        00:00:00 /usr/local/redis/bin/redis-server 192.168.0.15:6384 [cluster]

/usr/local/redis/redis-trib.rb create --replicas 1  192.168.0.14:6379 192.168.0.14:6380 192.168.0.14:6381 192.168.0.15:6382  192.168.0.15:6383 192.168.0.15:6384     redis_master端操作,建立群集少于6個節點會報錯             

>>> Creating cluster

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

192.168.0.14:6379

192.168.0.15:6382

192.168.0.14:6380

Adding replica 192.168.0.15:6384 to 192.168.0.14:6379

Adding replica 192.168.0.14:6381 to 192.168.0.15:6382

Adding replica 192.168.0.15:6383 to 192.168.0.14:6380

M: 083354570b0596e14e474554d28a6f1cb2e567c8 192.168.0.14:6379

   slots:0-5460 (5461 slots) master

M: ce932aef7c6fbeb6571898c6923c55032067b43b 192.168.0.14:6380

   slots:10923-16383 (5461 slots) master

S: 168f68a61b035be5fab4dfe9a109a2e1c472c7da 192.168.0.14:6381

   replicates c4305af229ddeb679c4783da856bfb2f6f1d4b38

M: c4305af229ddeb679c4783da856bfb2f6f1d4b38 192.168.0.15:6382

   slots:5461-10922 (5462 slots) master

S: b3e868782e1c5142c9c30317f214741aa380bfdd 192.168.0.15:6383

   replicates ce932aef7c6fbeb6571898c6923c55032067b43b

S: 7897e6cd7f5de220fa7f9248d617254405e408dc 192.168.0.15:6384

   replicates 083354570b0596e14e474554d28a6f1cb2e567c8

[OK] All 16384 slots covered.

/usr/local/redis/bin/redis-cli  -c -h 192.168.0.14    redis_master端操作,檢視群集狀态

192.168.0.14:6379> cluster nodes

168f68a61b035be5fab4dfe9a109a2e1c472c7da 192.168.0.14:6381@16381 slave c4305af229ddeb679c4783da856bfb2f6f1d4b38 0 1570777795825 4 connected

b3e868782e1c5142c9c30317f214741aa380bfdd 192.168.0.15:6383@16383 slave ce932aef7c6fbeb6571898c6923c55032067b43b 0 1570777795826 5 connected

ce932aef7c6fbeb6571898c6923c55032067b43b 192.168.0.14:6380@16380 master - 0 1570777795321 2 connected 10923-16383

7897e6cd7f5de220fa7f9248d617254405e408dc 192.168.0.15:6384@16384 slave 083354570b0596e14e474554d28a6f1cb2e567c8 0 1570777795523 6 connected

083354570b0596e14e474554d28a6f1cb2e567c8 192.168.0.14:6379@16379 myself,master - 0 1570777795000 1 connected 0-5460

c4305af229ddeb679c4783da856bfb2f6f1d4b38 192.168.0.15:6382@16382 master - 0 1570777794315 4 connected 5461-10922

192.168.0.14:6379>  

/usr/local/redis/redis-trib.rb add-node --slave 192.168.0.15:6388 192.168.0.15:6389 添加新slave節點

/usr/local/redis/redis-trib del-node 192.168.0.15:6388   移除節點 

touch /etc/init.d/redis && chmod +x /etc/init.d/redis

cat /etc/init.d/redis 

#!/bin/sh

# chkconfig:   2345 90 10

# Simple Redis init.d script conceived to work on Linux systems

# as it does use of the /proc filesystem.

REDISPORT=6379

EXEC=/usr/local/redis/bin/redis-server 

CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid

CONF="/usr/local/redis/redis.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 ]

                echo "$PIDFILE does not exist, process is not running"

                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"

    *)

        echo "Please use start or stop as first argument"

esac

/etc/init.d/redis start  &&  chkconfig --add redis

繼續閱讀