部署集群環(huán)境
1)redis集群搭建至少需要六臺機器,才能組成集惋,
?ip地址規(guī)劃如下:
– redisA? ? ?192.168.4.1 6341
– redisB ? ? 192.168.4.2 6342
– redisC ? ? 192.168.4.3 6343?
?– redisD? ? 192.168.4.4 6344
?– redisE? ? 192.168.4.5 6345
?– redisF? ? 192.168.4.6 6346
2)創(chuàng)建集群環(huán)境
在六臺主機做如下配置,并運行redis服務
#yum -y install gcc gcc-c++
#tar -zxvf redis-4.0.8.tar.gz
#cd redis-4.0.8/
#make
#make install
#./utils/install_server.sh
注意:以上操作必須在六臺主機上都要進行
3)? 修改配置文件
#/etc/init.d/redis_6379 stop ?//在修改配置文件之前摄杂,先把redis服務關閉
#vim /etc/redis/redis.conf ? ? //修改redis主配置文件
bind IP 地址 ? ? ? ? ? ? ?// 只寫物理接口 IP 地址砚蓬,即網(wǎng)卡的真實Ip地址
daemonize yes ? ? ? ? // 守護進程方式運行
port xxxx ? ? ? ? ? ? ? ? ? ? // 端口號不要使用默認的 6379
cluster-enabled yes? // 開啟集群
cluster-config-file 6379.conf ? ? // 集群的配置文件不要使用默認的名稱,把6379.conf改為xx.conf,不要使用默認的即可
cluster-node-timeout 5000 ? ? ? ? // 請求超時 5 秒
4)修改redis啟動.停止腳本
# vim ?/etc/init.d/redis_6379?
REDISPORT="6379" ? //腳本第八行费变,首先更改端口號,默認6379圣贸,需要改為與redis主配置文件里面對應的端口號?
$CLIEXEC -p $REDISPORT ?shutdown ? ? ? ?//腳本第43行挚歧,因為我們對redis主配置文件的默認端口號和ip地址都進行了更改,所以這里需要增加修改后的ip地址吁峻,端口已經(jīng)在上面的腳本變量修改滑负,這里就不需要再指定
修改后:$CLIEXEC -p $REDISPORT -h 192.168.4.1 shutdown
保存退出
注意:以上操作在每一臺機器上都要進行
4)創(chuàng)建集群
選好需要當redis主服務器的機器,并在上面執(zhí)行創(chuàng)建集群的腳本
#yum -y install ruby rubygems
#rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm? ? #忽略依賴關系安裝ruby-devel
#gem install redis-3.2.1.gem
# cd redis-4.0.8/
# cd src
# cp redis-trib.rb /usr/local/sbin/ ? ?#把ruby腳本拷貝到系統(tǒng)命令路徑下
#ls /usr/local/sbin/
?redis-trib.rb? ? ? #腳本綠色字體表示有執(zhí)行權限
#./redis-trib.rb create ?--replicas 1 \ ? ? ?//開始創(chuàng)建集群 ?--replicas 1 表示 自動為每一個 master 節(jié)點分配一個 slave 節(jié)點
192.168.4.1:6341 \
192.168.4.2:6342 \
192.168.4.3:6343 \
192.168.4.4:6344 \
192.168.4.5:6345 \
192.168.4.6:6346?
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.1:6343
192.168.4.2:6344
192.168.4.3:6345
Adding replica 192.168.4.5:6347 to 192.168.4.3:6343
Adding replica 192.168.4.6:6348 to 192.168.4.4:6344
Adding replica 192.168.4.4:6346 to 192.168.4.5:6345
M: c834c9ae1b0330a53f35af0cffd311d7ef23c83e 192.168.4.1:6343
? slots:0-5460 (5461 slots) master
M: 424e6d161b90819cc8a986e4b3143e3b82d89825 192.168.4.2:6344
? slots:5461-10922 (5462 slots) master
M: 7daf622d9d59c194b9abc185679f4dcc8960a80b 192.168.4.3:6345
? slots:10923-16383 (5461 slots) master
S: 1f8333b9eea0210a5c2c0c2896dfbf5b9e9259d9 192.168.4.4:6346
? replicates 7daf622d9d59c194b9abc185679f4dcc8960a80b
S: 6bb3781bfb002938fa3462ed37631073f6ad6036 192.168.4.5:6347
? replicates c834c9ae1b0330a53f35af0cffd311d7ef23c83e
S: d46a4985853f7430835f61a02da3375ffa7f2eec 192.168.4.6:6348
? replicates 424e6d161b90819cc8a986e4b3143e3b82d89825
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 192.168.4.3:6343)
M: c834c9ae1b0330a53f35af0cffd311d7ef23c83e 192.168.4.3:6343
? slots:0-5460 (5461 slots) master
? 1 additional replica(s)
M: 7daf622d9d59c194b9abc185679f4dcc8960a80b 192.168.4.5:6345
? slots:10923-16383 (5461 slots) master
? 1 additional replica(s)
S: 6bb3781bfb002938fa3462ed37631073f6ad6036 192.168.4.7:6347
? slots: (0 slots) slave
? replicates c834c9ae1b0330a53f35af0cffd311d7ef23c83e
M: 424e6d161b90819cc8a986e4b3143e3b82d89825 192.168.4.4:6344
? slots:5461-10922 (5462 slots) master
? 1 additional replica(s)
S: 1f8333b9eea0210a5c2c0c2896dfbf5b9e9259d9 192.168.4.6:6346
? slots: (0 slots) slave
? replicates 7daf622d9d59c194b9abc185679f4dcc8960a80b
S: d46a4985853f7430835f61a02da3375ffa7f2eec 192.168.4.8:6348
? slots: (0 slots) slave
? replicates 424e6d161b90819cc8a986e4b3143e3b82d89825
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. ? ? ?
#redis-cli -c -h 192.168.4.1:6341 ? //登陸查看用含,-c 表示登陸集群