相信大家在从事运维工作当中,都会使用到zookeeper中间件,其中就包括安装部署、使用及维护。此篇文章只针对如何使用ansible快速自动化部署一个zk集群进行展开说明。废话不多说,往下看。
目录结构
playbook/
├── host
├── zk.yml
├── mysql.yml
└── roles
├── jdk
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
├── mysql
│ ├── files
│ ├── handlers
│ ├── tasks
│ ├── templates
│ └── vars
└── zk
├── files
├── handlers
├── tasks
├── templates
└── vars
本次采用的是ansible-playbook进行自动化部署的,其中有涉及ansible、yaml以及jinja2相关的知识可以参考下面的链接 ansible学习文档: ansible yaml语法: yaml JinJa2语法: JinJa2
部署清单
# cat host
[jdk]
192.168.252.1
192.168.252.2
192.168.61.150
[zk]
192.168.252.1
192.168.252.2
192.168.61.150
# cat zk.yml
---
- hosts: zk
gather_facts: false
roles:
- jdk
- zk
# cat jdk.sh
# JAVA
JAVA_HOME=/opt/payment/soft/jdk1.8
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH
# jdk变量
# cat vars/main.yml
soft_name: jdk
jdk_install_path: /opt/payment/soft
jdk_link: jdk1.8
# cat playbook/roles/jdk/tasks/main.yml
---
- import_tasks: copy.yml
- import_tasks: unarchive.yml
- import_tasks: link.yml
- import_tasks: source.yml
- import_tasks: delete.yml
---
# copy.yml
- name: "1、拷贝{{soft_name}}安装包"
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: 0755
with_items:
- { src: "jdk-8u281-linux-x64.tar.gz",dest: "/tmp/" }
- { src: "jdk.sh",dest: "/etc/profile.d/" }
---
# delete.yml
- name: "5、删除临时文件"
file:
path: '/tmp/jdk-8u281-linux-x64.tar.gz'
state: absent
---
# link.yml
- name: "3、建立{{soft_name}}软链接"
file:
src: "{{ jdk_install_path }}/jdk1.8.0_281"
dest: "{{ jdk_install_path }}/{{ jdk_link }}"
state: link
---
# source.yml
- name: "4、加载{{soft_name}}环境"
shell: source /etc/profile
---
# unarchive.yml
- name: "2、解压{{soft_name}}源码包"
unarchive:
src: "/tmp/jdk-8u281-linux-x64.tar.gz"
dest: "{{ jdk_install_path }}"
remote_src: yes
# cat playbook/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 }}/{{ soft_name }}/data/myid
{% endif %}
{% endfor %}
# cat playbook/roles/zk/templates/zoo.cfg.j2
tickTime=2000
initLimit=100
syncLimit=10
clientPort={{ client_port }}
maxClientCnxns=10000
dataDir={{ install_path }}/{{ soft_name }}/data
dataLogDir={{ install_path }}/{{ soft_name }}/logs
{% for ip in groups[zk_hosts] %}
server.{{ loop.index - 1 }}={{ ip }}:{{ leader_port }}:{{vote_port }}
{% endfor %}
# cat playbook/roles/zk/vars/main.yml
#Soft Name
soft_name: zookeeper
#Install Path
install_path: /opt/payment/soft
# Zk user
user: weblogic
group: weblogic
#Zk Port
leader_port: 12888
vote_port: 13888
client_port: 12181
# Zk Host
zk_hosts: zk
# ls /root/playbook/roles/zk/tasks/
install.yml main.yml pre_install.yml
# cat /root/playbook/roles/zk/tasks/*
# main.yml
- import_tasks: pre_install.yml
- import_tasks: install.yml
---
# install.yml
- name: "1、创建{{ group }}组"
group:
name: "{{ group }}"
system: yes
state: present
- name: "2、创建{{ user }}用户"
user:
name: "{{ user }}"
group: "{{ group }}"
shell: /bin/bash
- name: "3、拷贝{{soft_name}}安装包"
copy:
src: "apache-zookeeper-3.6.1-bin.tar.gz"
dest: "/tmp"
- name: "4、创建安装目录"
file:
path: "{{ install_path }}"
owner: "{{ user}}"
group: "{{ group }}"
recurse: yes
state: directory
- name: "5、解压{{soft_name}}源码包"
unarchive:
src: "/tmp/apache-zookeeper-3.6.1-bin.tar.gz"
dest: "{{ install_path }}"
remote_src: yes
- name: "6、改变安装目录"
shell: mv {{ install_path }}/apache-zookeeper-3.6.1-bin {{ install_path }}/{{ soft_name }}
- name: "7、初始化{{soft_name}}安装目录"
file:
path: "{{ item }}"
owner: "{{ user}}"
group: "{{ group }}"
recurse: yes
state: directory
with_items:
- "{{ install_path }}/{{ soft_name }}/data"
- "{{ install_path }}/{{ soft_name }}/logs"
---
# pre_install.yml
- name: "8、修改{{soft_name}}配置文件"
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: 0755
with_items:
- { src: 'setid.sh.j2', dest: '{{ install_path }}/{{ soft_name }}/bin/setid.sh' }
- { src: 'zkServer.sh.j2', dest: '{{ install_path }}/{{ soft_name }}/bin/zkServer.sh' }
- { src: 'zoo.cfg.j2', dest: '{{ install_path }}/{{ soft_name }}/conf/zoo.cfg' }
- name: "9、写入myid"
shell: "source {{ install_path }}/{{ soft_name }}/bin/setid.sh"
- name: "10、目录赋权"
command: chown -R {{ user }}:{{ group }} {{ install_path }}
- name: "11、启动{{soft_name}}集群"
command: su - {{ user }} -c "{{ install_path }}/{{ soft_name }}/bin/zkServer.sh start"
- name: "12、hold on"
command: sleep 120
- name: "13、删除临时文件"
file:
path: '/tmp/apache-zookeeper-3.6.1-bin.tar.gz'
state: absent
- name: "14、获取{{soft_name}}集群状态"
command: "{{ install_path }}/{{ soft_name }}/bin/zkServer.sh status"
register: status
- name: "15、打印{{soft_name}}集群状态"
debug: var=status.stdout_lines
---
部署
# ansible-playbook -i host zk.yml
PLAY [zk] ***********************************************************
TASK [jdk : 1、拷贝jdk安装包] ***********************************************************
changed: [192.168.61.150] => (item={u'dest': u'/tmp/', u'src': u'jdk-8u281-linux-x64.tar.gz'})
changed: [192.168.252.1] => (item={u'dest': u'/tmp/', u'src': u'jdk-8u281-linux-x64.tar.gz'})
changed: [192.168.252.2] => (item={u'dest': u'/tmp/', u'src': u'jdk-8u281-linux-x64.tar.gz'})
ok: [192.168.61.150] => (item={u'dest': u'/etc/profile.d/', u'src': u'jdk.sh'})
ok: [192.168.252.2] => (item={u'dest': u'/etc/profile.d/', u'src': u'jdk.sh'})
ok: [192.168.252.1] => (item={u'dest': u'/etc/profile.d/', u'src': u'jdk.sh'})
TASK [jdk : 2、解压jdk源码包] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [jdk : 3、建立jdk软链接] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [jdk : 4、加载jdk环境] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [jdk : 5、删除临时文件] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 1、创建weblogic组] ***********************************************************
ok: [192.168.61.150]
ok: [192.168.252.2]
ok: [192.168.252.1]
TASK [zk : 2、创建weblogic用户] ***********************************************************
ok: [192.168.61.150]
ok: [192.168.252.2]
ok: [192.168.252.1]
TASK [zk : 3、拷贝zookeeper安装包] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
ok: [192.168.252.1]
TASK [zk : 4、创建安装目录] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.1]
changed: [192.168.252.2]
TASK [zk : 5、解压zookeeper源码包] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 6、改变安装目录] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 7、初始化zookeeper安装目录] ***********************************************************
changed: [192.168.61.150] => (item=/opt/payment/soft/zookeeper/data)
changed: [192.168.252.1] => (item=/opt/payment/soft/zookeeper/data)
changed: [192.168.252.2] => (item=/opt/payment/soft/zookeeper/data)
changed: [192.168.61.150] => (item=/opt/payment/soft/zookeeper/logs)
changed: [192.168.252.1] => (item=/opt/payment/soft/zookeeper/logs)
changed: [192.168.252.2] => (item=/opt/payment/soft/zookeeper/logs)
TASK [zk : 8、修改zookeeper配置文件] ***********************************************************
changed: [192.168.61.150] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/setid.sh', u'src': u'setid.sh.j2'})
changed: [192.168.252.2] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/setid.sh', u'src': u'setid.sh.j2'})
changed: [192.168.252.1] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/setid.sh', u'src': u'setid.sh.j2'})
changed: [192.168.61.150] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/zkServer.sh', u'src': u'zkServer.sh.j2'})
changed: [192.168.61.150] => (item={u'dest': u'/opt/payment/soft/zookeeper/conf/zoo.cfg', u'src': u'zoo.cfg.j2'})
changed: [192.168.252.2] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/zkServer.sh', u'src': u'zkServer.sh.j2'})
changed: [192.168.252.1] => (item={u'dest': u'/opt/payment/soft/zookeeper/bin/zkServer.sh', u'src': u'zkServer.sh.j2'})
changed: [192.168.252.2] => (item={u'dest': u'/opt/payment/soft/zookeeper/conf/zoo.cfg', u'src': u'zoo.cfg.j2'})
changed: [192.168.252.1] => (item={u'dest': u'/opt/payment/soft/zookeeper/conf/zoo.cfg', u'src': u'zoo.cfg.j2'})
TASK [zk : 9、写入myid] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 10、目录赋权] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 11、启动zookeeper集群] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 12、hold on] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 13、删除临时文件] ***********************************************************
changed: [192.168.61.150]
changed: [192.168.252.2]
changed: [192.168.252.1]
TASK [zk : 14、获取zookeeper集群状态] ***********************************************************
changed: [192.168.252.2]
changed: [192.168.61.150]
changed: [192.168.252.1]
TASK [zk : 15、打印zookeeper集群状态] ***********************************************************
ok: [192.168.252.1] => {
"status.stdout_lines": [
"/usr/bin/java",
"Client port found: 12181. Client address: localhost.",
"Mode: follower"
]
}
ok: [192.168.252.2] => {
"status.stdout_lines": [
"/opt/payment/soft/jdk1.8/bin/java",
"Client port found: 12181. Client address: localhost.",
"Mode: follower"
]
}
ok: [192.168.61.150] => {
"status.stdout_lines": [
"/opt/payment/soft/jdk1.8/bin/java",
"Client port found: 12181. Client address: localhost.",
"Mode: leader"
]
}
PLAY RECAP ***********************************************************
192.168.252.1 : ok=20 changed=16 unreachable=0 failed=0
192.168.252.2 : ok=20 changed=17 unreachable=0 failed=0
192.168.61.150 : ok=20 changed=17 unreachable=0 failed=0
从以上输出结果可以看出zk集群已成功部署完成,192.168.61.150为leader,其余两节点为follower。
登录节点验证
工欲善其事必先利其器,通过观察,整改安装过程只需大概要不到5分钟的时间。相当nice,hahaha... 下一篇分享通过ansible安装一键部署mysql5.7主从复制
如果文章对您有帮助,还想了解更过关于k8s相关的实战经验,请微信关注“IT运维图谱”公众号或着通过微信搜一搜关注公众号。