zookeeper集群模式切换至kraft集群模式
拷贝kraft集群配置文件
- name: copy server.properties
template: src=server.kraft.properties.j2 dest=/usr/local/kafka/{{brokers.package_name}}/config/kraft/server.properties
创建kraft集群并且启动服务
- name: mkdir kafka-logs
shell: mkdir -p /usr/local/kafka/{{brokers.package_name}}/kafka-logs
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: set JAVA_HOME
shell: export JAVA_HOME=/usr/local/{{jdk.version}} && export PATH=$JAVA_HOME/bin:$PATH
- name: create meta-data uuId
shell: source /etc/profile && source ~/.bashrc && sh /usr/local/kafka/{{brokers.package_name}}/bin/kafka-storage.sh random-uuid
register: kafka_metadata_uuId
- name: copy init kafka cluster script
template:
src: init_kafka_cluster.j2
dest: /tmp/init_kafka_cluster.sh
owner: root
group: root
mode: 0777
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: init kafka cluster
shell: source /etc/profile && source ~/.bashrc && sh /tmp/init_kafka_cluster.sh
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: clean kafka cluster script
shell: rm -f /tmp/init_kafka_cluster.sh
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: stop old kafka service
shell: service kafka stop
ignore_errors: yes
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: delete old kafka service
shell: rm -f /usr/lib/systemd/system/kafka.service
ignore_errors: yes
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: copy kafka service
template: src=kafka.service.j2 dest=/usr/lib/systemd/system/kafka.service
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: reload systemctl deamon
shell: systemctl daemon-reload
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: enable kafka service
shell: systemctl enable kafka
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: start kafka service
shell: service kafka restart
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
查看kafka启用端口查看kafka集群状态以及topic
- name: check kafka port
shell: netstat -lnpt | grep **
register: port_info
ignore_errors: yes
- name: show port info
debug:
msg: "{{port_info.stdout}}"
- name: check kafka cluster info
shell: grep "cluster.id" /usr/local/kafka/{{brokers.package_name}}/kafka-logs/kraft/meta.properties
register: cluster_info
ignore_errors: yes
- name: show cluster info
debug:
msg: "{{cluster_info.stdout}}"
- name: check kafka topic info
shell: /usr/local/kafka/{{brokers.package_name}}/bin/kafka-topics.sh --bootstrap-server localhost:** --list
register: topic_info
ignore_errors: yes
- name: show topic info
debug:
msg: "{{topic_info.stdout}}"
可以使用如下命令进行kafka集群由zookeeper切换至kraft模式
ansible-playbook -i hosts playbook/middleware.yml --tag kraftConfig
ansible-playbook -i hosts playbook/middleware.yml --tag kraft --limit ipAddress
ansible-playbook -i hosts playbook/middleware.yml --tag check
kraft集群模式切换至zookeeper集群模式
查看zookeeper服务是否启动,未启动尝试启动,启动失败安装zookeeper启动
---
- name: check zookeeper service
shell: "ps -ef|grep zookeeper|grep -v grep |awk '{print $2}'"
register: zookeeper_process_result
- name: try to start zookeeper service
shell: "/usr/local/zookeeper/{{zookeeper.package_name}}/bin/zkServer.sh start"
ignore_errors: true
register: zookeeper_start_process
when: zookeeper_process_result.stdout_lines==[]
- fail:
msg: "try to start zookeeper fail, start setup zookeeper later"
register: zookeeper_setup
ignore_errors: true
when: zookeeper_start_process is not skip and zookeeper_start_process is failure
- name: setup zookeeper
block:
- name: copy packages
copy:
src: "{{copy_path}}/{{zookeeper.package_name}}.tar"
dest: "/usr/local/zookeeper/"
owner: root
group: root
mode: 0755
ignore_errors: yes
- name: unzip zookeeper package
shell: cd /usr/local/zookeeper && tar -xvf {{zookeeper.package_name}}.tar
ignore_errors: true
- name: check jdk version
shell: java -version
register: jdk_check_result
ignore_errors: true
- fail:
msg: "try to get jdk version fail, start setup jdk later"
register: jdk_setup
ignore_errors: true
when: jdk_check_result is not skip and jdk_check_result is failure
- name: copy jdk
copy:
src: "{{copy_path}}/{{jdk.version}}.tar"
dest: "/usr/local"
dest: "/usr/local/"
owner: root
group: root
mode: 0755
ignore_errors: yes
when: jdk_setup is not skip
- name: unzip jdk
shell: cd /usr/local/ && tar -xvf {{jdk.version}}.tar
shell: cd /usr/local/ && tar -xvf {{jdk.version}}.tar
when: jdk_setup is not skip
- name: set JAVA_HOME
lineinfile: dest=/etc/profile insertafter="{{item.position}}" line="{{item.value}}" state=present
with_items:
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"}
when: jdk_setup is not skip
- name: copy jdk to ~/.bashrc
lineinfile: dest=~/.bashrc insertafter="{{item.position}}" line="{{item.value}}" state=present
with_items:
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export JAVA_HOME=/usr/local/{{jdk.version}}"}
- {position: EOF, value: "export PATH=$JAVA_HOME/bin:$PATH"}
when: jdk_setup is not skip
- name: source profile
shell: source /etc/profile && source ~/.bashrc
when: jdk_setup is not skip
- name: copy zoo.cfg
template: src=zoo.cfg.j2 dest=/usr/local/zookeeper/{{zookeeper.package_name}}/conf/zoo.cfg
- name: mkdir /var/zookeeper/data
shell: mkdir -p /var/zookeeper/data
ignore_errors: true
- name: copy myid
template: src=myid.j2 dest=/var/zookeeper/data/myid
- name: start zookeeper service
shell: /usr/local/zookeeper/{{zookeeper.package_name}}/bin/zkServer.sh start
- name: add clean zookeeper snapshot and transaction log corntab
cron:
name: Clean zookeeper snapshot and transaction log
hour: "3"
job: /bin/bash /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/cleanZkOldData-job.sh
user: root
rescue:
- name:
shell: "echo setup end"
拷贝zookeeper集群模式kafka服务并启动kafka
- name: copy kafka service
template: src=kafka.serviceOld.j2 dest=/usr/lib/systemd/system/kafka.service
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: reload systemctl deamon
shell: systemctl daemon-reload
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: enable kafka service
shell: systemctl enable kafka
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
- name: start kafka service
shell: service kafka restart
delegate_to: "{{item}}"
with_items: "{{groups['middleware']}}"
可使用如下命令将kraft集群模式kafka切换至zookeeper集群模式
ansible-playbook -i hosts playbook/middleware.yml --tag zookeeper
ansible-playbook -i hosts playbook/middleware.yml --tag zkKafka