ansible建立redis集群

整个实验持续时间非常长,踩了好多坑,坑踩多了再遇到问题就能更好处理

准备工作

<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   #hosts定义文件,也可以不使用,在yml文件里直接指定IP;
drwxr-xr-x 6 root root  309 3月  12 01:25 redis-5.0.3  #redis包,编译后(make && make install);
-rw-r--r-- 1 root root  244 3月  12 03:30 redis.conf.j2  #配置文件,使用template功能实现对不同机器的修改;
-rw-r--r-- 1 root root 1547 3月  12 03:59 redis_master.yml #脚本;

文件内容

<root@linux0 /etc/ansible>$ cat hosts
[thosts]  #组
linux0   #组内机器主机名,需配合/etc/hosts文件使用,或用内部DNS;ip是192.168.3.16;
[host2]   #组2
192.168.3.17  #也可直接使用IP来访问机器;
<root@linux0 /etc/ansible>$ cat redis_master.yml   #理论运行一次就会给hosts装上一个redis节点,节点端口在文件里定义,可在hosts上安装多个节点,只需改动一个变量,就是redis_port;
---
- hosts: thosts:192.168.3.17    #主机,组用分号分隔;
  remote_user: root  
  vars:
    redis_port: "7001"    #设置redis节点的端口号;
    IP: "{{ansible_default_ipv4['address']}}"   #从setup模块里取得IP,这个IP是ansible与主机通信的默认IP,使用IP作为参数,用于修改配置文件,生成不同目录,不同文件;
  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    #集群的信息会保存在此文件;
#cluster-node-timeout 10100
appendonly yes  #开启aof;

#
#
#
#
#
#
#
#
#

运行

<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   #同意slot分配;
>>> 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.

#
#
#
#
#
#
#
#
#
#
#

你可能感兴趣的:(linux)