Yellow 就怕你碌碌无为,还安慰自己平凡可贵 -------------yolo

ansible 部署redis集群(三台主机6节点3主3备)

ansible 部署 redis集群

由于文件比较大直接放百度盘
链接:https://pan.baidu.com/s/1PadPEXDerpE2ocTQMtwzxQ
提取码:s8xt

运行方式

  ansible-playbook ./site.yml  -i ./hosts 
  运行之前需要修改2个地方
  1  hosts 修改对应的主机IP 以及密码
  2  group_vars/redis.yml 里面的 redis_cluster_ip 

目录结构

目录结构

文件详解

cat ./site.yml 
---
- hosts: redis
  gather_facts: yes
  roles:
    - redis
    首先看入口文件
    --  hosts主机为redis集群
    -- gather_factes: 是否手机远程主机的信息 这里肯定要收集因为后期会用到

进入roles/redis/tasks
查看main.yml

cat ./main.yml 
- include: ruby.yml
- include: install_redis.yml

发现先运行ruby.yml 然后运行 install_redis.yml

仅需查看ruby.yml

cat ./ruby.yml 
---

##第一步 先把文件拷贝到对应的目录下
- name: unarchive_ruby
  unarchive:
    src: rvm.tar.gz
    dest: /usr/local/
  when: master is defined
  tags:
    - unarchive_ruby
##第二部 删除老的ruby
- name: remove_old_ruby
  yum:
    name: ruby
    state: absent
  when: master is defined
  tags:
    - remove_old_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

#第四步 创建gem的软连接
- name: link_gem
  file:
    src: /usr/local/rvm/archives/rvm-1.29.8/rubies/ruby-2.4.6/bin/gem
    dest: /usr/bin/gem
    state: link
  when: master is defined
  tags:
    - link_gem
cat ./install_redis.yml 
---
#创建redis用户
- name: add_redis_user
  user:
    comment: "add redis user"
    name: redis
    create_home: no
    shell: /sbin/nologin
    state: present
  tags:
    - add_redis_user

#创建目录 变了在group_vars里面
- 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

# 拷贝二进制文件到对应的bin下面
- 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

#拷贝配置文件
- 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


# 加入系统启动项
- name: add_redis_systemd
  template:
    src: redis.service.j2
    dest: /etc/systemd/system/redis-{{ item.port }}.service
  with_items:
    - "{{ redis_list }}"
  tags:
    - add_redis_systemd


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

#ruby安装redis
- name: gem_install_redis
  shell: gem install redis
  when: master is defined
  tags:
    - gem_install_redis

#拷贝添加集群脚本
- 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


#安装expect命令
- name: install_expect
  yum: 
    name: expect
    state: latest
  when: master is defined
  tags: 
    - install_expect


# 初始化redis集群
- name: init_redis_cluster
  shell: /tmp/add_cluster.sh
  when: master is defined
  tags:
    - init_redis_cluster


# 如果安装失败。删除目录
#- name: remove|dir
#  file:
#    path: /data
#    state: absent
#  tags:
#    - remove_dir
#

查看需要的原文件

[root@mongodb1 files]# ll
total 158744
-rw-r--r-- 1 root root     57344 Jul 31  2019 redis-4.1.2.gem
-rw-r--r-- 1 root root   2451064 Jul 31  2019 redis-benchmark
-rw-r--r-- 1 root root   5774912 Jul 31  2019 redis-check-aof
-rw-r--r-- 1 root root   5774912 Jul 31  2019 redis-check-rdb
-rw-r--r-- 1 root root   2617152 Jul 31  2019 redis-cli
-rw-r--r-- 1 root root   5774912 Jul 31  2019 redis-sentinel
-rw-r--r-- 1 root root   5774912 Jul 31  2019 redis-server
-rw-r--r-- 1 root root     65991 Jul 31  2019 redis-trib.rb
-rw-r--r-- 1 root root 134250866 Aug  3 02:13 rvm.tar.gz

查看hosts文件。 里面的信息为需要安装redis的主机信息 master=true 要定义 后续需要用

cat  hosts 
[redis]
wyl01   ansible_ssh_host=192.168.190.130   ansible_ssh_pass=123456  master=true
wyl02   ansible_ssh_host=192.168.190.131   ansible_ssh_pass=123456 
wyl03   ansible_ssh_host=192.168.190.132   ansible_ssh_pass=123456

[redis:vars]
mport=7000
sport=7001

查看变量情况

cat  redis_inner3_cluster/group_vars/redis.yml 
---
##需要注意这个用法,其实- name 这里只是注释而且 当然也可以被调用,主要是用下面的port
redis_list:
  - name: redis_master_info
    port: 7000
  - name: redis_slave_info
    port: 7001
  
redis_dir:
  - bin
  - conf
  - pid
  - data
  - log


redis_bin:
  - redis-benchmark
  - redis-check-aof
  - redis-check-rdb
  - redis-trib.rb
  - redis-cli
  - redis-sentinel
  - redis-server

redis_cluster_ip:
 - 192.168.79.128
 - 192.168.79.129
 - 192.168.79.130
posted @ 2020-08-03 21:47  zfno11  阅读(1420)  评论(0编辑  收藏  举报