ansible部署redis内置集群3个节点

 ansible部署redis三主三从内置集群:

 目录文件解释:

redis_inner3_cluster/
├── group_vars
│   └── redis.yml  # 存的全局变量
├── hosts
├── README.txt
├── roles           # 角色
│   └── redis  
│       ├── files    #redis编译后的一些文件
│       │   ├── redis-benchmark
│       │   ├── redis-check-aof
│       │   ├── redis-check-rdb
│       │   ├── redis-cli
│       │   ├── redis-sentinel
│       │   ├── redis-server
│       │   ├── redis-trib.rb
│       │   └── rvm.tar.gz     #rvm安装ruby后打的包
│       ├── tasks
│       │   ├── install_redis.yml
│       │   ├── main.yml
│       │   └── ruby.yml
│       └── templates             #部署中所需要的模板
│           ├── add_cluster.sh
│           ├── redis.conf.j2
│           └── redis.service.j2
├── site.retry
└── site.yml

6 directories, 19 files

执行顺序:

先找入口文件site.yml

---
- hosts: redis
  gather_facts: yes
  roles:
    - redis

 然后执行redis的角色下面的main.yml

- include: ruby.yml
- include: install_redis.yml

main.yml文件中包括2个任务,一是我们都知道在安装redis集群的时候需要先安装ruby,然后才是安装redis。

我们先看ruby.yml文件

 这里采用的rvm方式安装ruby,之前已经安装过,我们把这个包给打包,到新的服务器上解压即可,配置软连接即可。

---
- name: unarchive_ruby
  unarchive:
    src: rvm.tar.gz
    dest: /usr/local/
  when: master is defined
  tags:
    - unarchive_ruby
    - install_ruby



- name: link_ruby
  file:
    src: /usr/local/rvm/archives/rvm-1.29.8/rubies/ruby-2.4.6/bin/ruby
    dest: /usr/bin/ruby
    state: link
  when: master is defined
  tags:
    - link_ruby
    - install_ruby

然后就是安装redis的部分了

[root@wyl01 redis_inner3_cluster]# cat roles/redis/tasks/install_redis.yml 
---
- name: add_redis_user
  user:
    comment: "add redis user"
    name: redis
    create_home: no
    shell: /sbin/nologin
    state: present
  tags:
    - add_redis_user
    - install_redis_cluster



- name: mkdir_redis_dir
  file:
    path: /data/redis-{{item.0.port}}/{{ item.1 }}
    state: directory
    owner: redis
    group: redis
    mode: 0755
  with_nested:
    - "{{ redis_list }}"
    - "{{ redis_dir }}"
  tags:
    - mkdir_redis_dir
    - install_redis_cluster



- name: copy_redis_bin
  copy:
    src: "{{ item.1 }}"
    dest: /data/redis-{{ item.0.port }}/bin
    owner: redis
    group: redis
    mode: 0755
  with_nested:
    - "{{ redis_list }}"
    - "{{ redis_bin }}"
  tags:
    - copy_redis_bin
    - install_redis_cluster



- name: copy_redis_conf
  template:
    src: redis.conf.j2
    dest: /data/redis-{{ item.port }}/conf/redis.conf
    owner: redis
    group: redis
  with_items:
    - "{{ redis_list }}"
  tags:
    - copy_redis_conf
    - install_redis_cluster



- name: redis | service
  template:
    src: redis.service.j2
    dest: /etc/systemd/system/redis-{{ item.port }}.service
  with_items:
    - "{{ redis_list }}"
  tags:
    - install_redis
    - install_redis_service



- name: start_redis_service
  systemd:
    name: redis-{{ item.port }}
    daemon_reload: yes
    enabled: yes
    state: restarted
  with_items:
    - "{{ redis_list }}"
  tags:
    - start_redis_service
    - install_redis_service



- name: copy_redis_gem
  copy: 
    src: "redis-4.1.2.gem"
    dest: /tmp
    mode: 0755
  when: master is defined
  tags:
    - copy_redis_gem
    - install_redis_service



- name: gem_install_redis
  shell: gem install /tmp/redis-4.1.2.gem
  when: master is defined
  tags:
    - gem_install_redis
    - install_redis_service


- name: copy_script_file
  template:
    src: add_cluster.sh
    dest: /tmp
    owner: redis
    group: redis
    mode: '0755'
  when: master is defined
  tags:
    - copy_script_file
    - install_redis_service



- name: init_redis_cluster
  shell: /tmp/add_cluster.sh
  when: master is defined
  tags:
    - init_redis_cluster
    - install_redis_service



#- name: remove|dir
#  file:
#    path: /data
#    state: absent
#  tags:
#    - remove_dir
#

 

执行结果: 

[root@wyl01 redis_inner3_cluster]# ansible-playbook  -i hosts -t remove_dir site.yml 

PLAY [redis] ***************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [wyl03]
ok: [wyl01]
ok: [wyl02]

TASK [redis : remove|dir] **************************************************************************************************************
changed: [wyl02]
changed: [wyl01]
changed: [wyl03]

PLAY RECAP *****************************************************************************************************************************
wyl01                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
wyl02                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
wyl03                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@wyl01 redis_inner3_cluster]# ansible-playbook  -i hosts site.yml 

PLAY [redis] ***************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************
ok: [wyl01]
ok: [wyl03]
ok: [wyl02]

TASK [redis : unarchive_ruby] **********************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
changed: [wyl01]

TASK [redis : link_ruby] ***************************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
ok: [wyl01]

TASK [redis : add_redis_user] **********************************************************************************************************
ok: [wyl03]
ok: [wyl01]
ok: [wyl02]

TASK [redis : mkdir_redis_dir] *********************************************************************************************************
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'bin'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'bin'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'bin'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'conf'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'conf'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'conf'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'pid'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'pid'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'data'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'data'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'pid'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'log'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'log'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'data'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'bin'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'bin'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'log'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'conf'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'conf'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'pid'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'pid'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'bin'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'data'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'data'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'conf'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'log'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'log'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'pid'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'data'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'log'])

TASK [redis : copy_redis_bin] **********************************************************************************************************
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-benchmark'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-benchmark'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-benchmark'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-aof'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-aof'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-aof'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-rdb'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-rdb'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-check-rdb'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-trib.rb'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-trib.rb'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-cli'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-cli'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-trib.rb'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-sentinel'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-sentinel'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-cli'])
changed: [wyl03] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-server'])
changed: [wyl02] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-server'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-sentinel'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-benchmark'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-benchmark'])
changed: [wyl01] => (item=[{u'name': u'redis_master_info', u'port': 7000}, u'redis-server'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-aof'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-aof'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-benchmark'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-rdb'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-rdb'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-trib.rb'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-trib.rb'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-aof'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-cli'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-cli'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-check-rdb'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-sentinel'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-sentinel'])
changed: [wyl03] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-server'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-trib.rb'])
changed: [wyl02] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-server'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-cli'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-sentinel'])
changed: [wyl01] => (item=[{u'name': u'redis_slave_info', u'port': 7001}, u'redis-server'])

TASK [redis : copy_redis_conf] *********************************************************************************************************
changed: [wyl03] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl02] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl01] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl02] => (item={u'name': u'redis_slave_info', u'port': 7001})
changed: [wyl03] => (item={u'name': u'redis_slave_info', u'port': 7001})
changed: [wyl01] => (item={u'name': u'redis_slave_info', u'port': 7001})

TASK [redis : redis | service] *********************************************************************************************************
ok: [wyl03] => (item={u'name': u'redis_master_info', u'port': 7000})
ok: [wyl02] => (item={u'name': u'redis_master_info', u'port': 7000})
ok: [wyl01] => (item={u'name': u'redis_master_info', u'port': 7000})
ok: [wyl03] => (item={u'name': u'redis_slave_info', u'port': 7001})
ok: [wyl02] => (item={u'name': u'redis_slave_info', u'port': 7001})
ok: [wyl01] => (item={u'name': u'redis_slave_info', u'port': 7001})

TASK [redis : start_redis_service] *****************************************************************************************************
changed: [wyl03] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl01] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl02] => (item={u'name': u'redis_master_info', u'port': 7000})
changed: [wyl03] => (item={u'name': u'redis_slave_info', u'port': 7001})
changed: [wyl01] => (item={u'name': u'redis_slave_info', u'port': 7001})
changed: [wyl02] => (item={u'name': u'redis_slave_info', u'port': 7001})

TASK [redis : copy_redis_gem] **********************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
ok: [wyl01]

TASK [redis : gem_install_redis] *******************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
changed: [wyl01]

TASK [redis : copy_script_file] ********************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
ok: [wyl01]

TASK [redis : init_redis_cluster] ******************************************************************************************************
skipping: [wyl02]
skipping: [wyl03]
changed: [wyl01]

TASK [redis : remove|dir] **************************************************************************************************************
changed: [wyl02]
changed: [wyl03]
changed: [wyl01]

PLAY RECAP *****************************************************************************************************************************
wyl01                      : ok=14   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
wyl02                      : ok=8    changed=5    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0   
wyl03                      : ok=8    changed=5    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0 

执行完,我们就可以看到集群搭建成功

ansible部署redis内置集群3个节点_第1张图片

上面的项目是在github上,github地址

你可能感兴趣的:(Redis/Codis)