Ansible-playbook三分钟部署一个zookeeper集群

Ansible-playbook三分钟部署一个zookeeper集群

不要说话音乐:陈奕迅 - 不想放手

相信大家在从事运维工作当中,都会使用到zookeeper中间件,其中就包括安装部署、使用及维护。此篇文章将使用ansible配置管理工具快速自动化部署一个zk集群。

Ansible是近年来越来越火的一款轻量级运维自动化工具,主要功能为帮助运维实现运维工作的自动化、降低手动操作的失误、提升运维工作效率。常用于自动化部署软件、自动化配置、自动化管理,支持playbook编排。配置简单,无需安装客户端,被管理端主机通过ssh协议通信。Ansible本身不具有自动化部署功能,全交由其丰富的模块实现。

部署目标:

1、三分钟内部署一个三节点的zookeeper集群

2、部署过程中可以自定义软件安装目录及服务监听端口号

部署清单目录结构

[root@Ansible opt]# tree playbookplaybook├── group_vars│   └── all.yml├── hosts├── roles│   ├── jdk│   │   ├── files│   │   │   └── jdk-8u281-linux-x64.tar.gz│   │   ├── tasks│   │   │   └── main.yml│   │   └── templates│   │       └── jdk.sh.j2│   └── zk│       ├── files│       │   └── apache-zookeeper-3.6.1-bin.tar.gz│       ├── tasks│       │   └── main.yml│       └── templates│           ├── setid.sh.j2│           ├── zkServer.sh.j2│           ├── zoo.cfg.j2│           └── zookeeper.service.j2└── zk.yml
10 directories, 12 files

清单内容

[root@Ansible playbook]# cat hosts [jdk]192.168.91.110192.168.91.120192.168.91.130
[zk]192.168.91.110192.168.91.120192.168.91.130[root@Ansible playbook]# cat zk.yml ---- hosts: zk gather_facts: false vars_prompt: - name: install_path prompt: 请输入软件安装目录 default: "/opt/soft" private: no - name: leader_port prompt: 请输入zk服务的Leader-Port default: 12888 private: no - name: vote_port prompt: 请输入zk服务的Vote-Port default: 13888 private: no - name: client_port prompt: 请输入zk服务的Client-Port default: 12181 private: no roles: - jdk - zk
[root@Ansible playbook]# cat group_vars/all.yml #Soft Namejdk_soft_name: jdkzk_soft_name: zookeeper#Install Pathinstall_path: /opt/soft#Zk Portleader_port: 12888vote_port: 13888client_port: 12181# Zk Hostzk_hosts: zk
[root@Ansible playbook]# cat roles/jdk/templates/jdk.sh.j2 #!/usr/bin/env bash# JAVAJAVA_HOME={{ install_path }}/jdkJRE_HOME=$JAVA_HOME/jrePATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/binCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/libexport PATH[root@Ansible playbook]# cat roles/jdk/tasks/main.yml ---- name: "0、创建{{ install_path }}目录"  file:    path: "{{ install_path }}"    state: directory
- name: "1、解压{{ jdk_soft_name }}二进制包" unarchive: src: "jdk-8u281-linux-x64.tar.gz" dest: "{{ install_path }}"
- name: "2、建立{{ jdk_soft_name }}软链接" file: src: "{{ install_path }}/jdk1.8.0_281" dest: "{{ install_path }}/jdk" state: link
- name: "3、分发环境变量" template: src: jdk.sh.j2 dest: /etc/profile.d/jdk.sh
- name: "4、加载{{ jdk_soft_name }}环境" shell: bash -c 'source /etc/profile.d/jdk.sh'
[root@Ansible playbook]# cat roles/zk/templates/setid.sh.j2 #!/usr/bin/env bash
{% for ip in groups[zk_hosts] %}{% if ip == inventory_hostname %}echo {{ loop.index - 1 }} > {{ install_path }}/{{ zk_soft_name }}/data/myid{% endif %}{% endfor %}[root@Ansible playbook]# cat roles/zk/templates/zoo.cfg.j2 tickTime=2000initLimit=100syncLimit=10clientPort={{ client_port }}maxClientCnxns=10000dataDir={{ install_path }}/{{ zk_soft_name }}/datadataLogDir={{ install_path }}/{{ zk_soft_name }}/logs
{% for ip in groups[zk_hosts] %}server.{{ loop.index - 1 }}={{ ip }}:{{ leader_port }}:{{vote_port }}{% endfor %}[root@Ansible playbook]# cat roles/zk/templates/zookeeper.service.j2 [Unit]# 服务描述Description=zookeeper# 在网络服务启动后运行After=network.target[Service]Type=forking# jdk环境变量Environment=JAVA_HOME={{ install_path }}/jdk ZOO_LOG_DIR={{ install_path }}/zookeeper/logs# 启动命令ExecStart={{ install_path }}/zookeeper/bin/zkServer.sh start# 停止命令ExecStop={{ install_path }}/zookeeper/bin/zkServer.sh stop# 重载命令ExecReload={{ install_path }}/zookeeper/bin/zkServer.sh restart[Install]WantedBy=multi-user.target

以上清单中涉及的ansible模块比较多,同时还涉及jinja2相关的知识及服务配置文件的渲染。需要对此有一定的了解。下面我提供了一些相关知识的链接,供大家学习参考:

ansible学习文档: https://ansible-tran.readthedocs.io/en/latest/

yaml语法: https://www.runoob.com/w3cnote/yaml-intro.html

JinJa2语法: https://docs.jinkan.org/docs/jinja2/

部署

部署之前,需要配置Ansible主机到被管理节点的ssh免密登录;如未配置免密登录,需要额外在资产清单中配置ssh登录密码。

[root@Ansible playbook]# ansible-playbook -i hosts zk.yml请输入软件安装目录 [/opt/soft]: /opt/software     请输入zk服务的Leader-Port [12888]: 请输入zk服务的Vote-Port [13888]: 请输入zk服务的Client-Port [12181]: 22181
PLAY [zk] *****************************************************************************************************************************************************************
TASK [jdk : 0、创建/opt/software目录] *************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.120]changed: [192.168.91.110]
TASK [jdk : 1、解压jdk二进制包] *******************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.120]changed: [192.168.91.110]
TASK [jdk : 2、建立jdk软链接] *********************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.110]changed: [192.168.91.120]
TASK [jdk : 3、分发环境变量] **********************************************************************************************************************************************ok: [192.168.91.130]ok: [192.168.91.120]ok: [192.168.91.110]
TASK [jdk : 4、加载jdk环境] ***********************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.120]changed: [192.168.91.110]
TASK [zk : 1、创建安装目录] ***********************************************************************************************************************************************ok: [192.168.91.120]ok: [192.168.91.110]ok: [192.168.91.130]
TASK [zk : 2、解压zookeeper源码包] ****************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.110]changed: [192.168.91.120]
TASK [zk : 3、改变安装目录] ***********************************************************************************************************************************************changed: [192.168.91.120]changed: [192.168.91.130]changed: [192.168.91.110]
TASK [zk : 4、初始化zookeeper安装目录] ************************************************************************************************************************************changed: [192.168.91.130] => (item=/opt/software/zookeeper/data)changed: [192.168.91.120] => (item=/opt/software/zookeeper/data)changed: [192.168.91.110] => (item=/opt/software/zookeeper/data)changed: [192.168.91.120] => (item=/opt/software/zookeeper/logs)changed: [192.168.91.130] => (item=/opt/software/zookeeper/logs)changed: [192.168.91.110] => (item=/opt/software/zookeeper/logs)
TASK [zk : 5、修改zookeeper配置文件] **************************************************************************************************************************************changed: [192.168.91.130] => (item={'src': 'setid.sh.j2', 'dest': '/opt/software/zookeeper/bin/setid.sh'})changed: [192.168.91.110] => (item={'src': 'setid.sh.j2', 'dest': '/opt/software/zookeeper/bin/setid.sh'})changed: [192.168.91.120] => (item={'src': 'setid.sh.j2', 'dest': '/opt/software/zookeeper/bin/setid.sh'})changed: [192.168.91.130] => (item={'src': 'zkServer.sh.j2', 'dest': '/opt/software/zookeeper/bin/zkServer.sh'})changed: [192.168.91.120] => (item={'src': 'zkServer.sh.j2', 'dest': '/opt/software/zookeeper/bin/zkServer.sh'})changed: [192.168.91.110] => (item={'src': 'zkServer.sh.j2', 'dest': '/opt/software/zookeeper/bin/zkServer.sh'})changed: [192.168.91.120] => (item={'src': 'zoo.cfg.j2', 'dest': '/opt/software/zookeeper/conf/zoo.cfg'})changed: [192.168.91.130] => (item={'src': 'zoo.cfg.j2', 'dest': '/opt/software/zookeeper/conf/zoo.cfg'})changed: [192.168.91.110] => (item={'src': 'zoo.cfg.j2', 'dest': '/opt/software/zookeeper/conf/zoo.cfg'})ok: [192.168.91.130] => (item={'src': 'zookeeper.service.j2', 'dest': '/usr/lib/systemd/system/zookeeper.service'})ok: [192.168.91.120] => (item={'src': 'zookeeper.service.j2', 'dest': '/usr/lib/systemd/system/zookeeper.service'})ok: [192.168.91.110] => (item={'src': 'zookeeper.service.j2', 'dest': '/usr/lib/systemd/system/zookeeper.service'})
TASK [zk : 6、写入myid] ***************************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.120]changed: [192.168.91.110]
TASK [zk : 7、启动zookeeper集群] ******************************************************************************************************************************************changed: [192.168.91.120]changed: [192.168.91.130]changed: [192.168.91.110]
TASK [zk : 8、获取zookeeper集群状态] **************************************************************************************************************************************changed: [192.168.91.130]changed: [192.168.91.120]changed: [192.168.91.110]
TASK [zk : 9、打印zookeeper集群状态] **************************************************************************************************************************************ok: [192.168.91.110] => {   ... }ok: [192.168.91.120] => {    ...}ok: [192.168.91.130] => { ...}
PLAY RECAP ****************************************************************************************************************************************************************192.168.91.110 : ok=14 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.91.120 : ok=14 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.91.130 : ok=14 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

部署过程中的一些截图

图片

图片

图片

部署完成后,查看节点状态

[root@Ansible playbook]# ansible zk -i hosts -m shell -a "/opt/software/zookeeper/bin/zkServer.sh status"192.168.91.130 | CHANGED | rc=0 >>/opt/software/jdk/bin/javaClient port found: 22181. Client address: localhost.Mode: leaderZooKeeper JMX enabled by defaultUsing config: /opt/software/zookeeper/bin/../conf/zoo.cfg192.168.91.120 | CHANGED | rc=0 >>/opt/software/jdk/bin/javaClient port found: 22181. Client address: localhost.Mode: followerZooKeeper JMX enabled by defaultUsing config: /opt/software/zookeeper/bin/../conf/zoo.cfg192.168.91.110 | CHANGED | rc=0 >>/opt/software/jdk/bin/javaClient port found: 22181. Client address: localhost.Mode: followerZooKeeper JMX enabled by defaultUsing config: /opt/software/zookeeper/bin/../conf/zoo.cfg

图片

从显示的结果中,可以看到已经部署完成,192.168.91.130为leader节点,其余两个节点为follower节点。

Redhatlh
收录于合集 #ansible
 2
下一篇Ansible Playbook快速部署一主多从MySQL集群
阅读 140
IT运维图谱
10篇原创内容
 
 
posted @ 2023-07-14 08:53  往事已成昨天  阅读(460)  评论(0编辑  收藏  举报