简介
Ansible是近年来越来越火的一款轻量级运维自动化工具,主要功能为帮助运维实现运维工作的自动化、降低手动操作的失误、提升运维工作效率。常用于自动化部署软件、自动化配置、自动化管理,支持playbook编排。配置简单,无需安装客户端,被管理端主机通过ssh协议通信。Ansible本身不具有自动化部署功能,全交由其丰富的模块实现。
部署目标
- 快速部署一个三节点的zookeeper集群
- 部署过程中可以自定义软件安装目录及服务监听端口号
环境配置
hostname | IP | CPU | RAM | DISK | Network |
master | 192.168.199.201 | 2C | 4G | 40G | NAT |
slave1 | 192.168.199.202 | ||||
slave2 | 192.168.199.203 |
安装基础环境
# All node install
yum install -y wget unzip screen ntpdate net-tools sshpass vim tree telnet
# Only master node install
yum install -y ntp ansible
配置ssh互信
tee >> /etc/hosts <<EOF192.168.199.201 master
192.168.199.202 slave1
192.168.199.203 slave2
EOFssh-keygen -q -t rsa -f /root/.ssh/id_rsa -N ""; \cp -f /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
tee /root/.ssh/config <<-'EOF'
host *StrictHostKeyChecking noUserKnownHostsFile /dev/null
EOF
ssh-copy-id master
ssh-copy-id slave1
ssh-copy-id slave2
部署文件
- jdk:jdk-8u341-linux-x64.tar.gz
- zookeeper:apache-zookeeper-3.8.2-bin.tar.gz
创建目录
[root@master ~]# mkdir -p /opt/playbook/{group_vars,roles/{jdk/{files,tasks,templates},zk/{files,tasks,templates}}}
[root@master ~]# cd /opt/playbook/
部署清单目录结构
[root@master playbook]# tree
.
├── group_vars
│ └── all.yml
├── hosts
├── roles
│ ├── jdk
│ │ ├── files
│ │ │ └── jdk-8u341-linux-x64.tar.gz
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── templates
│ │ └── jdk.sh.j2
│ └── zk
│ ├── files
│ │ └── apache-zookeeper-3.8.2-bin.tar.gz
│ ├── tasks
│ │ └── main.yml
│ └── templates
│ ├── setid.sh.j2
│ ├── zoo.cfg.j2
│ └── zookeeper.service.j2
└── zk.yml10 directories, 11 files
清单内容
[root@master playbook]# cat hosts
[jdk]
192.168.199.201
192.168.199.202
192.168.199.203[zk]
192.168.199.201
192.168.199.202
192.168.199.203
交互式配置文件
[root@master playbook]# cat zk.yml
---
- hosts: zkgather_facts: falsevars_prompt:- name: install_pathprompt: 请输入软件安装目录default: "/opt/soft"private: no- name: leader_portprompt: 请输入zk服务的Leader-Portdefault: 12888private: no- name: vote_portprompt: 请输入zk服务的Vote-Portdefault: 13888private: no- name: client_portprompt: 请输入zk服务的Client-Portdefault: 12181private: noroles:- jdk- zk
声明变量
[root@master playbook]# cat group_vars/all.yml
#Soft Package
jdk_pkg: jdk-8u341-linux-x64.tar.gz
jdk_version: jdk1.8.0_341
zk_pkg: apache-zookeeper-3.8.2-bin.tar.gz
zk_version: apache-zookeeper-3.8.2-bin
#Soft Name
jdk_soft_name: jdk
zk_soft_name: zookeeper
#Install Path
install_path: /opt/soft
#Zk Port
leader_port: 12888
vote_port: 13888
client_port: 12181
# Zk Host
zk_hosts: zk
JDK部署清单
[root@master 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_pkg }}"dest: "{{ install_path }}"- name: "2、建立{{ jdk_soft_name }}软链接"file:src: "{{ install_path }}/{{ jdk_version }}"dest: "{{ install_path }}/{{ jdk_soft_name }}"state: link- name: "3、分发环境变量"template:src: jdk.sh.j2dest: /etc/profile.d/jdk.sh- name: "4、加载{{ jdk_soft_name }}环境"shell: bash -c 'source /etc/profile.d/jdk.sh'
JDK环境变量
[root@master playbook]# cat roles/jdk/templates/jdk.sh.j2
#!/usr/bin/env bash
# JAVA
JAVA_HOME={{ install_path }}/{{ jdk_soft_name }}
JRE_HOME=$JAVA_HOME/jre
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export PATH JAVA_HOME CLASSPATH JRE_HOME
[root@master playbook]# cat roles/zk/tasks/main.yml
- name: "1、创建安装目录"file:path: "{{ install_path }}"state: directory- name: "2、解压{{ zk_soft_name }}源码包"unarchive:src: "{{ zk_pkg }}"dest: "{{ install_path }}"- name: "3、建立{{ zk_soft_name }}软链接"file:src: "{{ install_path }}/{{ zk_version }}"dest: "{{ install_path }}/{{ zk_soft_name }}"state: link- name: "4、创建{{ zk_soft_name }}数据目录"file:path: "{{ item }}"state: directorywith_items:- "{{ install_path }}/{{ zk_soft_name }}/data"- "{{ install_path }}/{{ zk_soft_name }}/logs"- name: "5、修改{{ zk_soft_name }}配置文件"template: '{{ item }}'with_items:- "{'src': 'setid.sh.j2', 'dest': '{{ install_path }}/{{ zk_soft_name }}/bin/setid.sh'}"- "{'src': 'zoo.cfg.j2', 'dest': '{{ install_path }}/{{ zk_soft_name }}/conf/zoo.cfg'}"- "{'src': 'zookeeper.service.j2', 'dest': '/usr/lib/systemd/system/zookeeper.service'}"- name: "6、写入myid"shell: /bin/bash setid.shargs:chdir: "{{ install_path }}/{{ zk_soft_name }}/bin"- name: "7、启动{{ zk_soft_name }}集群"systemd:name: zookeeperstate: starteddaemon_reload: yesenabled: yes- name: "8、获取{{ zk_soft_name }}集群状态"shell: ./zkServer.sh statusargs:chdir: "{{ install_path }}/{{ zk_soft_name }}/bin"register: info- name: "9、打印{{ zk_soft_name }}集群状态"debug:msg: "{{ info.stderr_lines }}, {{ info.stdout_lines }}"
myid模板
[root@master 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 %}
zookeeper配置文件
[root@master playbook]# cat roles/zk/templates/zoo.cfg.j2
tickTime=2000
initLimit=100
syncLimit=10
clientPort={{ client_port }}
maxClientCnxns=10000
dataDir={{ install_path }}/{{ zk_soft_name }}/data
dataLogDir={{ install_path }}/{{ zk_soft_name }}/logs{% for ip in groups[zk_hosts] %}
server.{{ loop.index - 1 }}={{ ip }}:{{ leader_port }}:{{vote_port }}
{% endfor %}
zookeeper服务文件
[root@master 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@master playbook]# ansible-playbook -i hosts zk.yml
部署完成后,从上图的显示结果中可以看到,192.168.199.203为leader节点,其余两个节点为follower节点。
来自: Ansible部署 之 zookeeper集群https://mp.weixin.qq.com/s?__biz=Mzk0NTQ3OTk3MQ==&mid=2247487456&idx=1&sn=1fa0cdb236b82678ca83afaceb4fdc9e&chksm=c31587acf4620eba0594962ad1c4f2f4e3365649fbfdde8c2352a230477db7a6753c443fe508&token=113329682&lang=zh_CN#rd