整个实验持续时间非常长,踩了好多坑,坑踩多了再遇到问题就能更好处理
准备工作
<root@linux0 /etc/ansible>$ ls -ld hosts redis-5.0.3 redis.conf.j2 redis_master.yml
-rw-r--r-- 1 root root 37 3月 11 23:58 hosts
drwxr-xr-x 6 root root 309 3月 12 01:25 redis-5.0.3
-rw-r--r-- 1 root root 244 3月 12 03:30 redis.conf.j2
-rw-r--r-- 1 root root 1547 3月 12 03:59 redis_master.yml
文件内容
<root@linux0 /etc/ansible>$ cat hosts
[thosts]
linux0
[host2]
192.168.3.17
<root@linux0 /etc/ansible>$ cat redis_master.yml
---
- hosts: thosts:192.168.3.17
remote_user: root
vars:
redis_port: "7001"
IP: "{{ansible_default_ipv4['address']}}"
tasks:
- name: copy redis packet after make & make install
copy: src=redis-5.0.3.tar.gz dest=/usr/local owner=root group=root
- name: uncompress
unarchive: src=/usr/local/redis-5.0.3.tar.gz dest=/usr/local remote_src=yes
- name: cofig file
template: src=redis.conf.j2 dest=/etc/redis_{{ redis_port }}.conf owner=root group=root mode=0644
- name: make data dir
file: path=/data/redis_data/{{ redis_port }} state=directory mode=755
- name: enter dir
shell: cd /usr/local/redis-5.0.3/src
- name: start the redis
shell: redis-server /etc/redis_{{ redis_port }}.conf
<root@linux0 /etc/ansible>$ cat redis.conf.j2
port {{ redis_port }}
bind {{ IP }}
daemonize yes
pidfile /var/run/redis_{{ redis_port }}.pid
dir /data/redis_data/{{ redis_port }}
cluster-enabled yes
cluster-config-file nodes_{{ redis_port }}.conf
appendonly yes
运行
<root@linux0 /etc/ansible>$ ansible-playbook redis_master.yml
PLAY [thosts:192.168.3.17] *****************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************
ok: [linux0]
ok: [192.168.3.17]
TASK [copy redis packet after make & make install] *****************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]
TASK [uncompress] **************************************************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]
TASK [cofig file] **************************************************************************************************************************************************************
ok: [192.168.3.17]
ok: [linux0]
TASK [make data dir] ***********************************************************************************************************************************************************
changed: [linux0]
changed: [192.168.3.17]
TASK [enter dir] ***************************************************************************************************************************************************************
changed: [192.168.3.17]
changed: [linux0]
TASK [start the redis] *********************************************************************************************************************************************************
changed: [linux0]
changed: [192.168.3.17]
PLAY RECAP *********************************************************************************************************************************************************************
192.168.3.17 : ok=7 changed=3 unreachable=0 failed=0
linux0 : ok=7 changed=3 unreachable=0 failed=0
- redis_port: 7000 运行一次;
- redis_port: 7001 运行一次;
- redis_port: 7002 运行一次;即在两个电脑上,生成了6个redis副本,之后可以将他们集合成集群;
<root@linux0 /etc/ansible>$ /usr/local/redis-5.0.3/src/redis-cli --cluster create 192.168.3.16:7000 192.168.3.17:7000 192.168.3.16:7001 192.168.3.17:7001 192.168.3.16:7002 192.168.3.17:7002 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.3.17:7001 to 192.168.3.16:7000
Adding replica 192.168.3.16:7002 to 192.168.3.17:7000
Adding replica 192.168.3.17:7002 to 192.168.3.16:7001
M: c300df2dd7ffb6054adbbbe89b1e32757eb0b437 192.168.3.16:7000
slots:[0-5460] (5461 slots) master
M: bdf372777b2227133638a263a2c812e4d8227b62 192.168.3.17:7000
slots:[5461-10922] (5462 slots) master
M: 47f0043709c0b0b0903b030bb31a6552bb887326 192.168.3.16:7001
slots:[10923-16383] (5461 slots) master
S: 895491945c0777d5d5782c73f4cfb4d3f4d99862 192.168.3.17:7001
replicates c300df2dd7ffb6054adbbbe89b1e32757eb0b437
S: 2f4d8f944a17c772a21d9fecd52ee35893544fa7 192.168.3.16:7002
replicates bdf372777b2227133638a263a2c812e4d8227b62
S: 3d38a38c49fcc5d12f8ccf0d3ccfee8f5a08d50b 192.168.3.17:7002
replicates 47f0043709c0b0b0903b030bb31a6552bb887326
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.3.16:7000)
M: c300df2dd7ffb6054adbbbe89b1e32757eb0b437 192.168.3.16:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3d38a38c49fcc5d12f8ccf0d3ccfee8f5a08d50b 192.168.3.17:7002
slots: (0 slots) slave
replicates 47f0043709c0b0b0903b030bb31a6552bb887326
M: bdf372777b2227133638a263a2c812e4d8227b62 192.168.3.17:7000
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 895491945c0777d5d5782c73f4cfb4d3f4d99862 192.168.3.17:7001
slots: (0 slots) slave
replicates c300df2dd7ffb6054adbbbe89b1e32757eb0b437
M: 47f0043709c0b0b0903b030bb31a6552bb887326 192.168.3.16:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2f4d8f944a17c772a21d9fecd52ee35893544fa7 192.168.3.16:7002
slots: (0 slots) slave
replicates bdf372777b2227133638a263a2c812e4d8227b62
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.