redis集羣

一、環境

系統    CentOS7.0 64位最小化安裝

redis1    172.16.1.46    6379,6380

redis2    172.16.1.47    6379,6380

redis3    172.16.1.47    6379,6380

二、基礎軟件安裝

[root@redis1 ~]# yum install vim wget tree ntp net-tools lsof gcc* -y
[root@redis1 ~]# yum -y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf libxml2* rubygem-nokogiri

三、安裝ruby

[root@redis1 ~]# yum install ruby rubygems ruby-devel -y

#刪除ruby的默認源
[root@redis1 ~]# gem source --remove https://rubygems.org/
https://rubygems.org/ removed from sources

#添加源
[root@redis1 ~]# gem sources -a https://ruby.taobao.org
https://ruby.taobao.org added to sources

#安裝rails
[root@redis1 ~]# gem install rails

#安裝redis和ruby的接口
[root@redis1 ~]# gem install redis

四、安裝redis

#下載redis
[root@redis1 ~]# wget http://download.redis.io/releases/redis-3.0.1.tar.gz
[root@redis1 ~]# tar xf redis-3.0.1.tar.gz -C /usr/local/
[root@redis1 ~]# ln -sv /usr/local/redis-3.0.1 /usr/local/redis
‘/usr/local/redis’ -> ‘/usr/local/redis-3.0.1’
[root@redis1 ~]# cd /usr/local/redis
[root@redis1 redis]# make
[root@redis1 redis]# make install
[root@redis1 ~]# cp /usr/local/redis/redis.conf /usr/local/redis6379.conf

#編輯6379的配置文件
[root@redis1 ~]# vim /usr/local/redis/redis6379.conf
port 6379
pidfile /var/run/redis6379.pid
dbfilename dump6379.rdb
appendfilename "appendonly-6379.aof"
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-enabled yes
appendonly yes
dir /usr/local/redis/data/6379

#編輯6380的配置文件
[root@redis1 ~]# cp /usr/local/redis/redis6379.conf /usr/local/redis/redis6380.conf
[root@redis1 ~]# sed -i 's#6379#6380#g' /usr/local/redis/redis6380.conf

#創建相應的目錄
[root@redis1 ~]# mkdir -p /usr/local/redis/data/{6379,6380}

#創建6379的啓動腳本
[root@redis1 ~]# cat /etc/init.d/redis6379
#!/bin/bash
# chkconfig: 2345 50 30
#
# description: Redis service
#
#Script:Redis command
  
Redisserver=/usr/local/bin/redis-server
Rediscli=/usr/local/bin/redis-cli
Redisconf=/usr/local/redis/redis6379.conf
  
function_start()
{
    printf "start redis-server..."
    $Redisserver $Redisconf &>/dev/null  & 
    if [ $? -eq 0 ];then
        echo "runing"
    fi
}
  
function_stop()
{
    printf "stop redis-server..."
    $Rediscli -p 6379 shutdown
    if [ $? -eq 0 ];then
        echo "stop"
    fi
}
  
function_restart()
{
    function_start
    function_stop
}
  
function_kill()
{
    killall redis-server
}
  
function_status()
{
    a=`ps -A|grep "redis-server\>" -c`
    if [ $a -ge 1 ];then
        echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
    else
        echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
    fi
}
  
case "$1" in
        start)
                function_start
                ;;
        stop)
                function_stop
                ;;
        restart)
                function_stop
                function_start
                ;;
        kill)
                function_kill
                ;;
        status)
                function_status
                ;;
              *)
              echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}"
              
esac
  
exit

#賦予執行權限
[root@redis1 ~]# chmod +x /etc/init.d/redis6379
[root@redis1 ~]# /etc/init.d/redis6379 start
start redis-server...runing
[root@redis1 ~]# netstat -tunlp |grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      5631/redis-server * 

#創建6380的啓動腳本
[root@redis1 ~]# cat /etc/init.d/redis6380 
#!/bin/bash
# chkconfig: 2345 50 30
#
# description: Redis service
#
#Script:Redis command
  
Redisserver=/usr/local/bin/redis-server
Rediscli=/usr/local/bin/redis-cli
Redisconf=/usr/local/redis/redis6380.conf
  
function_start()
{
    printf "start redis-server..."
    $Redisserver $Redisconf &>/dev/null  & 
    if [ $? -eq 0 ];then
        echo "runing"
    fi
}
  
function_stop()
{
    printf "stop redis-server..."
    $Rediscli -p 6380 shutdown
    if [ $? -eq 0 ];then
        echo "stop"
    fi
}
  
function_restart()
{
    function_start
    function_stop
}
  
function_kill()
{
    killall redis-server
}
  
function_status()
{
    a=`ps -A|grep "redis-server\>" -c`
    if [ $a -ge 1 ];then
        echo -e "The Redis is [\e[0;32;5m runing \e[0m]"
    else
        echo -e "The Redis is [\e[0;31;5m not run \e[0m]"
    fi
}
  
case "$1" in
        start)
                function_start
                ;;
        stop)
                function_stop
                ;;
        restart)
                function_stop
                function_start
                ;;
        kill)
                function_kill
                ;;
        status)
                function_status
                ;;
              *)
              echo "Usage: /etc/init.d/redis {start|stop|restart|kill|status}"
              
esac
  
exit

#賦予執行權限並啓動6380
[root@redis1 ~]# chmod +x /etc/init.d/redis6380 
[root@redis1 ~]# /etc/init.d/redis6380 start
start redis-server...runing
[root@redis1 ~]# netstat -tunlp |grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      5646/redis-server * 
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      5652/redis-server * 

#其餘2臺執行同樣的操作

五、配置集羣

#在任意一臺機器上操作
[root@redis1 ~]# /usr/local/redis/src/redis-trib.rb create --replicas 1 172.16.1.46:6379 172.16.1.46:6380 172.16.1.47:6379 172.16.1.47:6380 172.16.1.48:6379 172.16.1.48:6380
>>> Creating cluster
Connecting to node 172.16.1.46:6379: OK
Connecting to node 172.16.1.46:6380: OK
Connecting to node 172.16.1.47:6379: OK
Connecting to node 172.16.1.47:6380: OK
Connecting to node 172.16.1.48:6379: OK
Connecting to node 172.16.1.48:6380: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.1.46:6379
172.16.1.47:6379
172.16.1.48:6379
Adding replica 172.16.1.47:6380 to 172.16.1.46:6379
Adding replica 172.16.1.46:6380 to 172.16.1.47:6379
Adding replica 172.16.1.48:6380 to 172.16.1.48:6379
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379
   slots:0-5460 (5461 slots) master
S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0
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 172.16.1.46:6379)
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 172.16.1.46:6379
   slots:0-5460 (5461 slots) master
M: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   slots: (0 slots) master
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
M: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   slots: (0 slots) master
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
M: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   slots: (0 slots) master
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#檢查集羣狀態
[root@redis1 ~]# /usr/local/redis/src/redis-trib.rb check 127.0.0.1:6379
Connecting to node 127.0.0.1:6379: OK
Connecting to node 172.16.1.48:6379: OK
Connecting to node 172.16.1.48:6380: OK
Connecting to node 172.16.1.47:6379: OK
Connecting to node 172.16.1.47:6380: OK
Connecting to node 172.16.1.46:6380: OK
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 3c2279ff6333b98a233ebf541e2b75981d27751f 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: a0cf20f0ea29760dfdb3e4748417efaefc0c37f0 172.16.1.48:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c6df03cde4424b2b09752c89d236ee1cd4834709 172.16.1.48:6380
   slots: (0 slots) slave
   replicates a0cf20f0ea29760dfdb3e4748417efaefc0c37f0    
M: dd122d63def65b8bc62719cabbb468f7976b1b73 172.16.1.47:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: c8136c436cec8146dbbbd1ff2e5468d1dd5dd195 172.16.1.47:6380
   slots: (0 slots) slave
   replicates 3c2279ff6333b98a233ebf541e2b75981d27751f
S: eb0b79b22797c14172c8f0f117a4839d6825ea1f 172.16.1.46:6380
   slots: (0 slots) slave
   replicates dd122d63def65b8bc62719cabbb468f7976b1b73
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#測試集羣,添加一個值
[root@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379
127.0.0.1:6379> set a a
-> Redirected to slot [15495] located at 172.16.1.48:6379        #可以看到值添加到了172.16.1.48:6379這個節點上
OK
172.16.1.48:6379> get a
"a"

#關閉172.16.1.48:6379這個節點
[root@redis3 redis]# /etc/init.d/redis6379 stop
stop redis-server...stop
[root@redis3 redis]# netstat -tunlp |grep 6379

#再次連接到集羣上
[root@redis1 ~]# /usr/local/bin/redis-cli -c -p 6379
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.16.1.48:6380
"a"            #這裏能看到之前設置的值,從172.16.1.48:6380拿到結果了

#集羣配置到此結束


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章