使用Ansible playbooks自动升级,备份和扩展。
在OpenStack上管理应用程序给系统管理员带来了许多挑战,找到降低复杂性和产生一致性的方法是取得成功的关键因素。通过使用无代理IT自动化技术Ansible,系统管理员可以创建Ansible playbooks,提供一致性并降低复杂性。
OpenStack提供了丰富的API来管理资源,从而创建了许多可以轻松适应任何自动化工作流程的Ansible模块。结合在OpenStack实例中自动执行任务的能力,运维可以在内部和外部工作,以协调针对环境的复杂操作。
本文讨论:
升级将如何发生?
如何维护备份?
环境如何随着需求而扩大?
Ansible可以轻松处理这些用例。
例如,考虑一组需要升级的Web服务器,它们都位于OpenStack负载均衡器后面。由于能够管理VM本身内的基础架构和任务,运维可以确保执行的事件序列始终按特定顺序发生。以下是执行滚动升级的剧本的简单示例:
- hosts: web
gather_facts: true
user: centos
serial: 1 # ensures only one server will update/reboot at a time
tasks:
- name: check for pending updates
yum:
list: updates
register: yum_update # check if there are updates before going any further
- block:
- name: remove web server from pool
os_member:
state: absent
name: '{{ ansible_hostname }}'
pool: weblb_80_pool
delegate_to: localhost
- name: update packages
package:
name: '*'
state: latest
become: true
- name: reboot server
shell: sleep 5 && reboot &
async: 1
poll: 0
- name: wait for server
wait_for_connection:
connect_timeout: 20
sleep: 5
delay: 5
timeout: 600
become: true
- name: put server back in pool
os_member:
state: present
name: '{{ ansible_hostname }}'
pool: weblb_80_pool
address: '{{ ansible_default_ipv4.address }}'
protocol_port: 80
delegate_to: localhost
when:
- yum_update.results | length > 0 # only execute the block if there are updates
这个playbook首先检查是否有任何更新要申请。如果是,playboo则从池中删除节点,应用更新,然后重新启动节点。一旦节点重新联机,它就会被添加回池中。Ansible playbook使用serial关键字确保一次只从池中删除一个节点。
如果数据库在OpenStack云中运行,则有时必须还原备份,要么刷新一些测试数据,要么可能在发生数据损坏事件时。使用Ansible可以轻松完成数据库服务器和Cinder之间的编排任务:
- hosts: db
gather_facts: true
user: centos
tasks:
- name: stop database
systemd:
name: mongod
state: stopped
become: true
- name: unmount db volume
mount:
path: /var/lib/mongodb
state: unmounted
become: true
- name: detach volume from server
os_server_volume:
state: absent
server: db0
volume: dbvol
delegate_to: localhost
- name: restore cinder backup
command: openstack volume backup restore dbvol_backup dbvol
delegate_to: localhost
register: vol_restore
failed_when:
- vol_restore.rc > 0
- "'VolumeBackupsRestore' not in vol_restore.stderr"
- name: wait for restore to finish
command: openstack volume show -c status -f value dbvol
register: restore_progress
until: restore_progress.stdout is search("available")
retries: 60
delay: 5
delegate_to: localhost
- name: reattach volume to server
os_server_volume:
state: present
server: db0
volume: dbvol
device: /dev/vdb
delegate_to: localhost
- name: mount db volume
mount:
path: /var/lib/mongodb
state: mounted
src: LABEL=dbvol
fstype: xfs
become: true
- name: start database
systemd:
name: mongod
state: started
become: true
仔细查看playbook,你可能已经注意到还原是通过OpenStack命令行完成的,而不是正确的Ansible模块。在某些情况下,任务的模块可能不存在,但Ansible足够灵活,允许在开发模块之前调用剧本中的任意命令。
原文链接:
https://opensource.com/article/18/10/manage-your-openstack-cloud-ansible