CentOS7.2 安裝redis 3.0.6叢集
阿新 • • 發佈:2018-11-19
1.環境確認
a.系統版本檢視 [[email protected] ~]$ cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)b.安裝依賴 [[email protected] ~]$ sudo yum -y install tcl ruby tree
2.安裝redis 3.0.6
3.建立節點配置目錄
[[email protected] ~]# mkdir /etc/redis [[email protected] ~]# mkdir /etc/redis/7000
4.建立節點配置檔案 a.配置7000節點,先複製redis-server與初始配置檔案redis.conf,然後修改redis.conf,使其適合叢集 [
# /etc/redis /7000/redis.conf port 7000 daemonize yes pidfile /var/run/redis_7000.pid cluster-enabled yes cluster-config-file nodes.conf logfile "/var/log/redisd7000.log" dir /etc/redis/7000/ cluster-node-timeout 5000 appendonly yes
b.複製其他節點配置檔案 #生成7001配置,其他類似 cp -r 7000 7001 sed -i "s/7000/7001/g" 7001/redis.conf cat 7001/redis.conf |grep 7001
5.配置節點開機啟動 修改redisd7000的啟動指令碼,7001~7005類似
[[email protected] ~]#cp /usr/local/ redis-stable/ utils/ redis_init_script /etc/init.d/redisd7000 [[email protected] ~]#vim /etc/init.d/redisd7000
#!/bin/sh #chkconfig: 2345 90 10 #description: Redis is a persistent key-value database # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the /proc filesystem.
REDISPORT=7000 #EXEC=/usr/local/bin/redis-server EXEC=/etc/redis/${REDISPORT}/redis-server CLIEXEC=/usr/local/bin/redis-cli PIDFILE=/var/run/redis_${REDISPORT}.pid #CONF="/etc/redis/${REDISPORT}.conf" CONF="/etc/redis/${REDISPORT}/redis.conf" 主要修改內容包括如下三點: a.加入前三行即 #!/bin/sh #chkconfig: 2345 90 10 #description: Redis is a persistent key-value database b.修改埠,即 REDISPORT=7000 c.指定 EXEC與CONF即 EXEC=/etc/redis/${REDISPORT}/redis-server CONF="/etc/redis/${REDISPORT}/redis.conf" 其他的不動,略
複製生成其他啟動指令碼 #生成7001指令碼,其他類似 cp /etc/init.d/redisd7000 /etc/init.d/redisd7001
sed -i "s/7000/7001/g" /etc/init.d/redisd7001 cat /etc/init.d/redisd7001 |grep 7001
#設定為開機自啟動伺服器,其他服務類似 [[email protected] ~]#chkconfig redisd7000 on 確認配置檔案,重啟前應該是如下狀態 [ [email protected] ~]# ls /etc/init.d/ |grep redis redisd7000 redisd7001 redisd7002 redisd7003 redisd7004 redisd7005 注:以上檔案是6個服務的自動啟動指令碼 [ [email protected] ~]# tree /etc/redis /etc/redis ├── 7000 │ ├── redis.conf │ └── redis-server ├── 7001 │ ├── redis.conf │ └── redis-server ├── 7002 │ ├── redis.conf │ └── redis-server ├── 7003 │ ├── redis.conf │ └── redis-server ├── 7004 │ ├── redis.conf │ └── redis-server ├── 7005 │ ├── redis.conf │ └── redis-server 注:以上目錄是各伺服器的redis-server可執行程式與其配置檔案,若沒有tree命令,先 yum install -y tree
[[email protected] ~]# reboot
重啟以後服務應該就啟動了,若有需要可以用如下命令來啟動或停止服務了 service redisd7000 start service redisd7000 stop
6.安裝redis介面 #rubygems.org被牆,換ruby.taobao.org gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/ gem install redis
7.建立叢集 [ [email protected] redis-stable]# /usr/local/redis-stable/src/redis-trib.rb create --replicas 1 192.168.1.161:7000 192.168.1.161:7001 192.168.1.161:7002 192.168.1.161:7003 192.168.1.161:7004 192.168.1.161:7005 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.1.161:7000 192.168.1.161:7001 192.168.1.161:7002 Adding replica 192.168.1.161:7003 to 192.168.1.161:7000 Adding replica 192.168.1.161:7004 to 192.168.1.161:7001 Adding replica 192.168.1.161:7005 to 192.168.1.161:7002 M: 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 192.168.1.161:7000 slots:0-5460 (5461 slots) master M: 925478713e7a73c986dc0c07c01dd7bd63c20f47 192.168.1.161:7001 slots:5461-10922 (5462 slots) master M: d81817cb1d01695e552ea2fde2a5ac62772d2a08 192.168.1.161:7002 slots:10923-16383 (5461 slots) master S: 26906db48fba3321374c9d1fa0f3b55730c49ae1 192.168.1.161:7003 replicates 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 S: 94077768fe3ff601722f2970c65b1604c6e714df 192.168.1.161:7004 replicates 925478713e7a73c986dc0c07c01dd7bd63c20f47 S: 9259eff3d7d40449830e2de9561e6c77a490f4e0 192.168.1.161:7005 replicates d81817cb1d01695e552ea2fde2a5ac62772d2a08 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.1.161:7000) M: 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 192.168.1.161:7000 slots:0-5460 (5461 slots) master M: 925478713e7a73c986dc0c07c01dd7bd63c20f47 192.168.1.161:7001 slots:5461-10922 (5462 slots) master M: d81817cb1d01695e552ea2fde2a5ac62772d2a08 192.168.1.161:7002 slots:10923-16383 (5461 slots) master M: 26906db48fba3321374c9d1fa0f3b55730c49ae1 192.168.1.161:7003 slots: (0 slots) master replicates 5f62052fb63b330ae39e35e35fdb5cd5d3f3d013 M: 94077768fe3ff601722f2970c65b1604c6e714df 192.168.1.161:7004 slots: (0 slots) master replicates 925478713e7a73c986dc0c07c01dd7bd63c20f47 M: 9259eff3d7d40449830e2de9561e6c77a490f4e0 192.168.1.161:7005 slots: (0 slots) master replicates d81817cb1d01695e552ea2fde2a5ac62772d2a08 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
8.確認叢集狀態 [ [email protected] ~]# /usr/local/redis-stable/src/redis-trib.rb check 192.168.1.161:7000 >>> Performing Cluster Check (using node 192.168.1.161:7000) M: e3b5e66e28c015552625b7f1b52538da98d16c96 192.168.1.161:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 6dacc2604f22692a9291e3e6e82bee492fbb0817 192.168.1.161:7003 slots: (0 slots) slave replicates e3b5e66e28c015552625b7f1b52538da98d16c96 M: 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 192.168.1.161:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: 8252ac10b98a2f8d78c6f1b488610b8f789548db 192.168.1.161:7005 slots: (0 slots) slave replicates 6aa2e0cb813a6ed451437ef4203aa669d42ac50c S: 1e179fa5c399dba3eed3e592c5855cf600a648a5 192.168.1.161:7004 slots: (0 slots) slave replicates 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc M: 6aa2e0cb813a6ed451437ef4203aa669d42ac50c 192.168.1.161:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
有錯誤的話,修復叢集 [[email protected] ~]# /usr/local/redis-stable/src/redis-trib.rb fix 192.168.1.161:7000 若要確認輸入yes
[ [email protected] ~]# ps -ef | grep redis root 26530 13340 0 12:07 pts/1 00:00:03 ./7000/redis-server *:7000 [cluster] root 26914 26538 0 12:07 pts/3 00:00:03 ./7001/redis-server *:7001 [cluster] root 27430 13649 0 12:10 pts/2 00:00:03 ./7002/redis-server *:7002 [cluster] root 27441 26922 0 12:10 pts/4 00:00:03 ./7003/redis-server *:7003 [cluster] root 27732 27448 0 12:10 pts/5 00:00:03 ./7004/redis-server *:7004 [cluster] root 28029 27740 0 12:11 pts/6 00:00:02 ./7005/redis-server *:7005 [cluster] root 28572 5882 0 12:23 pts/0 00:00:00 redis-cli -c -p 7000 root 30085 28867 0 12:35 pts/7 00:00:00 grep --color=auto redis
[ [email protected] ~]# netstat -tnlp | grep redis tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 26530/./7000/redis- tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 26914/./7001/redis- tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 27430/./7002/redis- tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 27441/./7003/redis- tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 27732/./7004/redis- tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 28029/./7005/redis- tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 26530/./7000/redis- tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 26914/./7001/redis- tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 27430/./7002/redis- tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 27441/./7003/redis- tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 27732/./7004/redis- tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 28029/./7005/redis- tcp6 0 0 :::17000 :::* LISTEN 26530/./7000/redis- tcp6 0 0 :::17001 :::* LISTEN 26914/./7001/redis- tcp6 0 0 :::17002 :::* LISTEN 27430/./7002/redis- tcp6 0 0 :::17003 :::* LISTEN 27441/./7003/redis- tcp6 0 0 :::17004 :::* LISTEN 27732/./7004/redis- tcp6 0 0 :::17005 :::* LISTEN 28029/./7005/redis- tcp6 0 0 :::7000 :::* LISTEN 26530/./7000/redis- tcp6 0 0 :::7001 :::* LISTEN 26914/./7001/redis- tcp6 0 0 :::7002 :::* LISTEN 27430/./7002/redis- tcp6 0 0 :::7003 :::* LISTEN 27441/./7003/redis- tcp6 0 0 :::7004 :::* LISTEN 27732/./7004/redis- tcp6 0 0 :::7005 :::* LISTEN 28029/./7005/redis-
9.開啟防火牆埠 [ [email protected] ~]# firewall-cmd --zone=public --add-port=7000-7005/tcp --permanent success [ [email protected] ~]#firewall-cmd --reload success
10.訪問測試 [ [email protected] redis-stable]#redis-cli -c -p 7000 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:1248 cluster_stats_messages_received:1248 127.0.0.1:7000> cluster nodes 6dacc2604f22692a9291e3e6e82bee492fbb0817 192.168.1.161:7003 slave e3b5e66e28c015552625b7f1b52538da98d16c96 0 1451399924770 4 connected 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 192.168.1.161:7001 master - 0 1451399923768 2 connected 5461-10922 8252ac10b98a2f8d78c6f1b488610b8f789548db 192.168.1.161:7005 slave 6aa2e0cb813a6ed451437ef4203aa669d42ac50c 0 1451399922766 6 connected 1e179fa5c399dba3eed3e592c5855cf600a648a5 192.168.1.161:7004 slave 5aa111c270091b474feaf0f11aa8a2e3ff2a5ccc 0 1451399925772 5 connected e3b5e66e28c015552625b7f1b52538da98d16c96 192.168.1.161:7000 myself,master - 0 0 1 connected 0-5460 6aa2e0cb813a6ed451437ef4203aa669d42ac50c 192.168.1.161:7002 master - 0 1451399926776 3 connected 10923-16383 127.0.0.1:7000>
11.python測試 先安裝 pip install redis-py-cluster 注意在叢集中 pip install redis安裝的redis會報錯!!!
from rediscluster import StrictRedisCluster
startup_nodes = [{"host": "192.168.1.161", "port": "7000"}]
rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
rc.set("blog", "http://blog.csdn.net/dgatiger")
print rc.get('blog')