ansible部署kafka集群

ansible部署kafka集群

其中一台作为Ansible的母机并命名为ansible,另外三台云主机命名为node1、node2、node3,通过附件中的/ansible/ansible.tar.gz软件包在ansible节点安装Ansible服务;使用这一台母机,编写Ansible脚本(在/root目录下创建example目录作为Ansible工作目录,部署的入口文件命名为cscc_install.yaml),编写Ansible脚本使用roles的方式对其他三台云主机进行安装kafka集群的操作(zookeeper和kafka的安装压缩包在gpmall-single.tar.gz压缩包中,将zookeeper和kafka的压缩包解压到node节点的/opt目录下进行安装)。

自行配置/etc/hosts文件(根据实际情况修改)

10.196.110.10 node1
10.196.110.20 node2
10.196.110.30 node3
第一种方式
mkdir -p example/roles/kafka/{files,handlers,meta,tasks,templates,vars}

第二种方式
ansible-galaxy init kafka  #初始化

下载需要的文件

curl -O http://10.0.0.1/ansible.tar.gz
tar xf ansible.tar.gz 
yum install -y ansible/packages/*

curl -O http://10.0.0.1/gpmall-single.tar.gz
tar xf gpmall-single.tar.gz
cp gpmall-single/zookeeper-3.4.14.tar.gz /root/example/roles/kafka/files
cp gpmall-single/kafka_2.11-1.1.1.tgz /root/example/roles/kafka/files

写一个yum源文件安装java
cat >> /root/example/roles/kafka/files/yum.repo <
vim /etc/ansible/hosts
最后加上
[node]
node1  node_id=1
node2  node_id=2
node3  node_id=3

在example文件下面编写cscc_install.yaml文件

---
- hosts: node
  remote_user: root
  roles:
    - kafka

在example/roles/kafka/tasks/下面编写main.yaml

---
    - shell: rm -f /etc/yum.repos.d/*
    - copy: src=yum.repo dest=/etc/yum.repos.d/yum.repo
    - yum: name=java* state=installed
    - unarchive: src=zookeeper-3.4.14.tar.gz dest=/opt/
    - file: path=/tmp/zookeeper/  state=directory
    - shell: echo "{{node_id}}" > /tmp/zookeeper/myid
    - template: src=zoo.cfg.j2 dest=/opt/zookeeper-3.4.14/conf/
    - shell: cd /opt/zookeeper-3.4.14/bin && ./zkServer.sh restart
    - shell: cd /opt/zookeeper-3.4.14/bin && ./zkServer.sh status
    - unarchive: src=kafka_2.11-1.1.1.tgz dest=/opt/
    - template: src=server.properties.j2  dest=/opt/kafka_2.11-1.1.1/config
/server.properties
    - shell: /opt/kafka_2.11-1.1.1/bin/kafka-server-start.sh -daemon /opt/k
afka_2.11-1.1.1/config/server.properties

zookeeper模板

vim example/roles/kafka/templates/zoo.cfg.j2
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper
clientPort=2181
{% for host in ansible_play_batch %}
server.{{ loop.index }}={{ host }}:2888:3888
{% endfor %}

kafka模板

vim example/roles/kafka/templates/server.properties.j2

broker.id={{node_id}}  
listeners=PLAINTEXT://{{inventory_hostname}}:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect={% for host in ansible_play_batch %}{{ host }}:2181,{% endfor %}
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0

树形结构

example/
├── cscc_install.yaml
├── group_vars
└── roles
    └── kafka
        ├── files
        │   ├── kafka_2.11-1.1.1.tgz
        │   ├── yum.repo
        │   └── zookeeper-3.4.14.tar.gz
        ├── handlers
        ├── meta
        ├── tasks
        │   └── main.yaml
        ├── templates
        │   ├── server.properties.j2
        │   └── zoo.cfg.j2
        └── vars

运行

ansible-playbook  cscc_install.yaml 

你可能感兴趣的:(ansible,kafka,ansible,分布式)