redis集群创建
我这里用了2台机器,创建伪集群,大家可根据自己需要,稍微调整即可
##安装配置redis集群
[[email protected] redis]# cat redis.sls
redis-installed:
archive.extracted:
- name: /ane/soft/
- source: salt://redis/files/redis-3.2.1.tar.gz
- archive_format: tar
- if_missing: /ane/soft/redis-3.2.1
- unless: test -d /ane/redis
pkg.installed:
- pkgs:
- gcc
- tcl
- gcc-c++
cmd.run:
- name: cd /ane/soft/redis-3.2.1 && make && make install
redis-6379conf:
file.managed:
- name: /ane/redis/6379/redis.conf
- source: salt://redis/files/redis.conf
- makedirs: True
- template: jinja
host: {{ grains[‘fqdn_ip4‘][0] }}
port: 6379
redis-6380conf:
file.managed:
- name: /ane/redis/6380/redis.conf
- source: salt://redis/files/redis.conf
- makedirs: True
- template: jinja
host: {{ grains[‘fqdn_ip4‘][0] }}
port: 6380
redis-6381conf:
file.managed:
- name: /ane/redis/6381/redis.conf
- source: salt://redis/files/redis.conf
- makedirs: True
- template: jinja
host: {{ grains[‘fqdn_ip4‘][0] }}
port: 6381
redis-cluster:
pkg.installed:
- pkgs:
- ruby
- ruby-devel
- rubygems
file.managed:
- name: /ane/soft/redis-3.2.1.gem
- source: salt://redis/files/redis-3.2.1.gem
cmd.run:
- name: gem install /ane/soft/redis-3.2.1.gem
redis-service:
file.managed:
- name: /ane/script/startredis.sh
- source: salt://redis/files/startredis.sh
- makedirs: True
- mode: 655
cmd.run:
- name: /ane/script/startredis.sh
上面采用了jinja模版,其中host和port是定义的两个模版变量,可以传不同的值进去
##创建集群
[[email protected] redis]# salt ‘master1‘ cmd.run ‘echo yes|/ane/soft/redis-3.2.1/src/redis-trib.rb create --replicas 1 172.16.77.160:6379 172.16.77.160:6380 172.16.77.160:6381 172.16.77.162:6379 172.16.77.162:6380 172.16.77.162:6381‘
master1:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.77.160:6379
172.16.77.162:6379
172.16.77.160:6380
Adding replica 172.16.77.162:6380 to 172.16.77.160:6379
Adding replica 172.16.77.160:6381 to 172.16.77.162:6379
Adding replica 172.16.77.162:6381 to 172.16.77.160:6380
M: 1ef26e8ddcd56d22f66b1270e0f29cf319b6305c 172.16.77.160:6379
slots:0-5460 (5461 slots) master
M: 0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573 172.16.77.160:6380
slots:10923-16383 (5461 slots) master
S: 07b6124f057e12fc78833be31757413de01f6368 172.16.77.160:6381
replicates e4d5bdc8815126789cc8935cec5be3380f7e68d4
M: e4d5bdc8815126789cc8935cec5be3380f7e68d4 172.16.77.162:6379
slots:5461-10922 (5462 slots) master
S: 6cafd239cff892c4e587b14d5cc5619cb3a97277 172.16.77.162:6380
replicates 1ef26e8ddcd56d22f66b1270e0f29cf319b6305c
S: cb31f41ffe6ff356b65727b1dc3d5fdc14378c45 172.16.77.162:6381
replicates 0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573
Can I set the above configuration? (type ‘yes‘ to accept): >>> 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.77.160:6379)
M: 1ef26e8ddcd56d22f66b1270e0f29cf319b6305c 172.16.77.160:6379
slots:0-5460 (5461 slots) master
M: 0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573 172.16.77.160:6380
slots:10923-16383 (5461 slots) master
M: 07b6124f057e12fc78833be31757413de01f6368 172.16.77.160:6381
slots: (0 slots) master
replicates e4d5bdc8815126789cc8935cec5be3380f7e68d4
M: e4d5bdc8815126789cc8935cec5be3380f7e68d4 172.16.77.162:6379
slots:5461-10922 (5462 slots) master
M: 6cafd239cff892c4e587b14d5cc5619cb3a97277 172.16.77.162:6380
slots: (0 slots) master
replicates 1ef26e8ddcd56d22f66b1270e0f29cf319b6305c
M: cb31f41ffe6ff356b65727b1dc3d5fdc14378c45 172.16.77.162:6381
slots: (0 slots) master
replicates 0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
##测试redis集群
[[email protected] redis]# redis-cli -h 172.16.77.160 -p 6379
172.16.77.160:6379> set hello world
OK
172.16.77.160:6379> get hello
"world"
172.16.77.160:6379>
[[email protected] redis]# redis-cli -h 172.16.77.160 -p 6379
172.16.77.160:6379> set hello world
OK
172.16.77.160:6379> get hello
"world"
172.16.77.160:6379> 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:232
cluster_stats_messages_received:232
172.16.77.160:6379> cluster nodes
1ef26e8ddcd56d22f66b1270e0f29cf319b6305c 172.16.77.160:6379 myself,master - 0 0 1 connected 0-5460
cb31f41ffe6ff356b65727b1dc3d5fdc14378c45 172.16.77.162:6381 slave 0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573 0 1517560583831 6 connected
6cafd239cff892c4e587b14d5cc5619cb3a97277 172.16.77.162:6380 slave 1ef26e8ddcd56d22f66b1270e0f29cf319b6305c 0 1517560578775 5 connected
e4d5bdc8815126789cc8935cec5be3380f7e68d4 172.16.77.162:6379 master - 0 1517560580795 4 connected 5461-10922
07b6124f057e12fc78833be31757413de01f6368 172.16.77.160:6381 slave e4d5bdc8815126789cc8935cec5be3380f7e68d4 0 1517560579785 4 connected
0cd4d5ecdecc538e76c6b3d0cfa689cb40f7e573 172.16.77.160:6380 master - 0 1517560582817 2 connected 10923-16383
172.16.77.160:6379>
原文地址:http://blog.51cto.com/9682938/2134823
时间: 2024-10-11 05:24:59