使用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