一、准备环境
1、准备机器
本次以192.168.190.155,192.168.190.156,192.168.190.157,192.168.190.158四台虚机为例,本文操作全在管理机中。
机器IP | 节点 | CPU&内存 | 系统盘 | 存储 |
192.168.190.155 | Ansible管理机 | 2C4G | 50GB | 50GB |
192.168.190.158 | Master/Worker(被管理机) | 2C4G | 50GB | 50GB |
192.168.190.157 | Master/Worker(被管理机) | 2C4G | 50GB | 50GB |
192.168.190.156 | Worker(被管理机) | 2C4G | 50GB | 50GB |
本次主要操作都在管理机:192.168.190.155上完成
二、详细步骤
1、配置ansible的hosts,定义变量:
vi /etc/ansible/hosts
内容:
[zookeeper_nodes] zookeeper1 ansible_host=192.168.190.158 zookeeper2 ansible_host=192.168.190.156 zookeeper3 ansible_host=192.168.190.157 [zookeeper] zookeeper1 myid=1 zookeeper2 myid=2 zookeeper3 myid=3 |
2、在管理机上创建安装zookeeper的必要的目录
mkdir -p /etc/ansible/roles/{zookeeper,java}/{defaults,tasks,templates} |
3、编辑配置java剧本
目录结构图:
①、配置defaults目录下的配置文件(设置变量):main.yml
vi /etc/ansible/roles/java/defaults/main.yml
内容:
--- JAVA_VERSION: 1.8.2_192 JAVA_HOME: /data/java JAVA_TAR: jdk-8u192-linux-x64.tar.gz |
②、配置tasks目录下的配置文件(剧本):java.yml
vi /etc/ansible/roles/java/tasks/java.yml
内容:
--- - name: Deploy Java-JDK-1.8-192 hosts: all become: yes roles: - java tasks: - name: Copy Jdk copy: src: /{{JAVA_HOME}}/{{JAVA_VERSION2}} dest: /tmp/{{JAVA_VERSION2}} - name: Create Kafka logs directory ansible.builtin.file: path: /data/java state: directory - name: Extrack Jdk unarchive: src: /tmp/{{JAVA_VERSION2}} dest: /data/java/ remote_src: yes - name: Add JAVA_HOME to /etc/profile.d/ blockinfile: path: /etc/profile.d/java.sh create: yes mode: '0644' block: | export JAVA_HOME=/{{JAVA_HOME}}/jdk-{{JAVA_VERSION}} export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar marker: "# {mark} ANSIBLE MANAGED BLOCK - JAVA_HOME" |
4、编辑配置zookeeper剧本
目录结构图:
①、配置defaults目录下的配置文件(设置变量):main.yml
vi /etc/ansible/roles/zookeeper/defaults/main.yml
内容:
--- ZOOKEEPER_VERSION: 3.7.2 ZOOKEEPER_HOME: /data/zookeeper/apache-zookeeper-3.7.2-bin ZOOKEEPER_TAR: apache-zookeeper-3.7.2-bin.tar.gz ZOOKEEPER_INSTALL_DIR: /data/zookeeper ZOOKEEPER_INSTALL_DIR_USR: /usr/local/zookeeper |
②、配置tasks目录下的配置文件(剧本):zookeeper.yml
vi /etc/ansible/roles/zookeeper/tasks/zookeeper.yml
内容:
--- - name: Deploy zookeeper-3.7.2 hosts: all become: yes roles: - zookeeper vars: zookeeper_conf_dir: /{{ ZOOKEEPER_HOME }}/conf tasks: - name: Copy zookeeper copy: src: /{{ ZOOKEEPER_INSTALL_DIR }}/{{ ZOOKEEPER_TAR }} dest: /tmp/{{ ZOOKEEPER_TAR }} - name: Create zookeeper ansible.builtin.file: path: /data/zookeeper/data state: directory - name: Create logs ansible.builtin.file: path: /data/zookeeper/apache-zookeeper-3.7.2/logs state: directory - name: Extrack zookeeper unarchive: src: /tmp/{{ ZOOKEEPER_TAR }} dest: /{{ ZOOKEEPER_INSTALL_DIR }} remote_src: yes - name: Ln shell: | ln -s /{{ ZOOKEEPER_HOME }} /{{ ZOOKEEPER_INSTALL_DIR_USR }} - name: Ensure Kafka configuration directory exists ansible.builtin.file: path: "{{ zookeeper_conf_dir }}" state: directory - name: Render zookeeper configuration file ansible.builtin.template: src: /etc/ansible/roles/zookeeper/templates/zookeeper.j2 dest: "{{ zookeeper_conf_dir }}/zoo.cfg" - name: Ensure Zookeeper data directory exists ansible.builtin.file: path: "/{{ ZOOKEEPER_INSTALL_DIR }}/data-{{ ansible_hostname }}" state: directory - name: Create myid file from template ansible.builtin.template: src: /etc/ansible/roles/zookeeper/templates/zookeeper1.j2 dest: "/data/zookeeper/data/myid" - name: Deploy Zookeeper systemd service file ansible.builtin.template: src: /etc/ansible/roles/zookeeper/templates/zookeeper.service.j2 dest: /etc/systemd/system/zookeeper.service - name: reload systemd manager and restart zookeeper ansible.builtin.systemd: name: zookeeper state: restarted daemon_reload: yes |
③、编辑配置jinja2文件(配合剧本使用,自动创建zookeeper配置文件):
a、zookeeper1.j2
vi /etc/ansible/roles/zookeeper/templates/zookeeper1.j2
内容:
{{ myid }} |
b、zookeeper.j2
vi /etc/ansible/roles/zookeeper/templates/zookeeper.j2
内容:
#The number of milliseconds of each tick #The number of ticks that the initial #synchronization phase can take #The number of ticks that can pass between #sending a request and getting an acknowledgement #the directory where the snapshot is stored. #do not use /tmp for storage, /tmp here is just #example sakes. #the port at which the clients will connect tickTime=2000 initLimit=10 syncLimit=5 dataDir={{ ZOOKEEPER_INSTALL_DIR }}/data dataLogDir={{ ZOOKEEPER_HOME }}/logs clientPort=2181 server.1=192.168.190.158:2341:2351 server.2=192.168.190.156:2342:2352 server.3=192.168.190.157:2343:2353 #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 |
c、zookeeper.service.j2
vi /etc/ansible/roles/zookeeper/templates/zookeeper.service.j2
内容:
[Unit] Description=ZooKeeper Service After=network.target [Service] Type=simple User=root Group=root Environment=JAVA_HOME=/data/java/jdk1.8.0_192 WorkingDirectory=/data/zookeeper/apache-zookeeper-3.7.2-bin/ ExecStart=/data/zookeeper/apache-zookeeper-3.7.2-bin/bin/zkServer.sh start-foreground ExecStop=/data/zookeeper/apache-zookeeper-3.7.2-bin/bin/zkServer.sh stop Restart=on-abnormal [Install] WantedBy=multi-user.target |
5、执行ansible-playbook指令即可
ansible-playbook zookeeper.yml ansible-playbook java.yml |
三、被管理机操作
1、执行systemctl语句
systemctl start zookeeper.service ##启动zookeeper systemctl stop zookeeper.service ##启停zookeeper systemctl status zookeeper.service ##查看zookeeper状态 |