Ansible部署系列续:(一)安装zookeeper
2021-06-13 本文已影响0人
Anson_1f2a
1. 安装zookeeper
1.1. 说明
由于之前的项目用到Kafka,旧版的Kafka需要用到zookeeper,在这里也记录一下。
myid从1开始
ansiblehosts
文件
[zookeeperservers]
sz-1 zk_myid=1
sz-2 zk_myid=2
vars.yml
添加参数
ZOOKEEPER_DATA_DIR: /home/data/zookeeper/data
1.2. yml脚本
---
- hosts: zookeeperservers
remote_user: root
vars_files:
- ../vars.yml
tasks:
- name: copy and unzip zookeeper
unarchive:
src: "{{ PLAYBOOK_DIR }}/files/apache-zookeeper-3.6.1-bin.tar.gz"
dest: "{{ INSTALL_DIR }}"
- name: mkdir ditectory for zookeeper data
file:
dest: "{{ ZOOKEEPER_DATA_DIR }}"
mode: 0755
state: directory
owner: root
group: root
- name: Copy myid file
template:
src: "{{ PLAYBOOK_DIR }}/zookeeper/templates/myid.j2"
dest: "{{ ZOOKEEPER_DATA_DIR }}/myid"
owner: root
group: root
mode: 0755
- name: install properties file for zookeeper
template:
src: "{{ PLAYBOOK_DIR }}/zookeeper/templates/zoo.cfg.j2"
dest: "{{ INSTALL_DIR }}/apache-zookeeper-3.6.1-bin/conf/zoo.cfg"
owner: root
group: root
mode: 0755
- name: firewarld add 2888
firewalld:
port: 2888/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: firewarld add 3888
firewalld:
port: 3888/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: start zookeeper
shell: "{{ INSTALL_DIR }}/apache-zookeeper-3.6.1-bin/bin/zkServer.sh start"
tags:
- start zookeeper
1.3. 模板文件
因为搭建的是集群环境,因此都需要配置节点
1.3.1 zoo.cfg.j2
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir={{ ZOOKEEPER_DATA_DIR }}
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
{% for host in groups['zookeeperservers'] %}
{%- if hostvars[host].inventory_hostname == ansible_default_ipv4.address -%}
server.{{ hostvars[host].zk_myid }}=0.0.0.0:2888:3888
{% else %}
server.{{ hostvars[host].zk_myid }}={{ hostvars[host].inventory_hostname }}:2888:3888
{% endif %}
{% endfor %}
1.3.2 myid.j2
{{ zk_myid }}
1.4. 运行ansible-playbook
ansible-playbook -i /etc/ansible/hosts zookeeper/main.yml