微服务自动化部署(ansible playbook)干货之--zookeeper部署

前提:

zookeeper版本:3.4.12

安装包获取:通过http下载拉取;如何搭建http介质源,稍后分享出来。通过httpd服务放置安装介质能够最大程度的将代码和介质包分离开来,这种做法在大型的开发项目中是非常必要的,因为如果介质过大,每一次移动都是时间成本。

全局变量:

master_ip: 192.168.100.141

#zookeeper vars
zoo_port1: 2888
zoo_port2: 3888
zoo_data_dir: /usr/local/zookeeper-3.4.12/zookeeperData
zoo_listen_port: 2181

roles目录结构:

微服务自动化部署(ansible playbook)干货之--zookeeper部署_第1张图片

 

handler main.yml内容:

---
- name: start zookeeper
  shell: /bin/bash /usr/local/zookeeper-3.4.12/bin/zkServer.sh restart

tasks main.yml内容:

---
- name: Check Idle port for zookeeper
  shell: ss -ntl|grep -e {{ zoo_listen_port }} -e {{ zoo_port2 }}
  ignore_errors: True
  register: zoo_port

- name: WARN for port check
  debug:
    msg: "WARNING:Port {{ zoo_listen_port }} or {{ zoo_port2 }}is not idle, Service zookeeper will not be deploy"
  when: zoo_port|succeeded

- name: Download zookeeper pkgs in all hosts
  get_url:
    url: http://{{ master_ip }}/basesource/zookeeper-3.4.12.tar.gz
    dest: /tmp/zookeeper-3.4.12.tar.gz
    mode: 0755
  when: zoo_port|failed

- name: Unzip zookeeper pkgs in all hosts
  unarchive:
    src: /tmp/zookeeper-3.4.12.tar.gz
    dest: /usr/local
    remote_src: yes
    mode: 0755
  when: zoo_port|failed

- name: Create a data_dir
  file:
    path: "{{ zoo_data_dir }}"
    state: directory
    mode: 0755
  when: zoo_port|failed

- name: Config zookeeper myid
  template:
    src: myid.j2
    dest: "{{ zoo_data_dir }}/myid"
    mode: 0755
  when: zoo_port|failed

- name: Config zookeeper service
  template:
    src: zoo.cfg.j2
    dest: /usr/local/zookeeper-3.4.12/conf/zoo.cfg
    mode: 0755
  notify: start zookeeper
  when: zoo_port|failed
main.yml

templates myid.j2 内容:

{{ host_id }}

templates zoo.cfg.j2 内容:

tickTime=2000
initLimit=10
syncLimit=5
dataDir={{ zoo_data_dir }}
#dataDir=/usr/local/zookeeper-3.4.12/zookeeperData
clientPort={{ zoo_listen_port }}

{% for host in groups['zookeeper_svcs'] %}
server.{{ hostvars[host]['host_id'] }}={{ host }}:{{ zoo_port1 }}:{{ zoo_port2 }}
{% endfor %}

 

转载于:https://www.cnblogs.com/yuzichen/p/10251270.html

你可能感兴趣的:(微服务自动化部署(ansible playbook)干货之--zookeeper部署)