一、ansible簡介
ansible是2012年出現的自動運維工具,基於python開發,集合了衆多工具的優點,可以實現批量系統配置、批量程序部署、批量運行命令、批量配置文件修改等功能。最主要的是ansible是基於多模塊工作的,而且ansible是無需客戶端安裝就可以基於ssh實現管理節點的,是輕量級的自動化運維工具,ansible是個框架,主要包括以下幾個組件:
(1)、連接插件connection plugins:負責和被監控端實現通信;
(2)、host inventory:設置管理的節點,可以在一個配置文件中定義;
(3)、各種模塊:core modules、customer modules、自定義模塊(可自行使用python開發);
(4)、plugins:藉助於插件完成記錄email、logging、other;
(5)、playbook:劇本執行多個任務時,可以讓管理節點一次性運行多個任務,可使用yaml語言編寫。
二、ansible基本用法介紹
2.1、介紹基本用法,首選安裝ansible程序包,這裏介紹ansible-1.9.2版本,yum源要指向epel源
# yum install ansible //安裝ansible # rpm -ql ansible //查看ansible安裝後生成的所有文件(這裏只介紹幾個常用的) /etc/ansible/hosts //設置要管理的節點 /usr/bin/ansible //ansible的二進制程序 /usr/bin/ansible-doc // 可通過它查看ansible支持的模塊及對應模塊的參數 /usr/bin/ansible-playbook //劇本(playbook)執行的二進制程序
2.2、基本用法及模塊的介紹
2.2.1、首先要配置/etc/ansible/hosts文件來設置要管理的節點
# vim /etc/ansible/hosts //這裏所做的配置,是爲下面介紹高可用集羣做的準備工作 [ha] //類似ini,定義管理節點名 172.16.116.231 //各管理節點的ip地址或主機名 172.16.116.232 172.16.116.233
2.2.2、ansible使用的基本語法
# man ansible //查看ansible工具的man文檔 ansible <host-pattern> [-f forks] [-m module_name] [-a args] host-pattern: 是指那些在/etc/ansible/hosts中定義的管理節點 -f forks: 是指ansible一批可以管理的節點數量,默認爲5個 -m module_name : 是指使用ansible工具時使用的模塊名稱 -a args : 使用模塊對應的參數
2.2.3、ansible常用的模塊介紹(這裏只介紹幾種,其它的靠大家去研究了)
介紹之前先介紹2個命令如下:
# ansible-doc -l //可以列出ansible支持的所有模塊 # ansible-doc -s module_name //可以查看模塊的對應參數
(1)ping ping模塊,用法很簡單
(2)command 命令模塊
(3)shell shell模塊
注:command有可能不支持特殊的命令行處理,如管道echo "pass" | passwd --stdin user
我個人理解是:使用command的地方,都可以使用shell來代替,避免出現問題
(4)user 用戶管理模塊
# ansible-doc -s user //查看user模塊支持的參數 介紹幾個常用的 comment //用戶描述 group //用戶所屬組 home //用戶家目錄 name= //用戶名,這個最關鍵 password //給用戶設置密碼,但是寫入到/etc/shadow中的密碼爲明文的,有可能不讓登錄(別用) shell //默認shell state={present,absent} //創建,刪除用戶 system={yes,no} //是否爲系統用戶,默認爲no uid //用戶的id號
(5)group 組管理模塊
# ansible-doc -s group gid //組的id號 name= //組名 state={present,absent} //創建,刪除 system={yes,no} //是否爲系統組
(6)cron 計劃任務模塊
# ansible-doc -s cron minute //分 (0-59) hour //時 (0-23) day //日 (1-31) month //月 (1-12) weekday //周 (0-6 for Sunday-Saturday) name= //任務計劃的名稱 job= //定義任務計劃使用的命令 state={present,absent} //創建,刪除任務計劃 user //給哪個用戶的任務計劃 例:# ansbile ha -m cron -a 'name="sync time from 172.16.0.1" minute="*/5" job="/usr/sbin/ntpdate 172.16.0.1 &> /dev/null"' //同步時間的計劃任務,每5分鐘執行一次 [root@node1 ~]# crontab -l //查看管理節點上的計劃任務 #Ansible: sync time from 172.16.0.1 */5 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null
(7)copy 複製模塊
# ansible-doc -s copy dest= //目標目錄,要使用絕對路徑 src //源文件 mode //文件的權限 owner //文件的屬主 group //文件的屬組 force=yes //是否強制覆蓋
(8)file 文件管理模塊
# ansible-doc -s file group //屬組 mode //權限 owner //屬主 path= //目標路徑;管理節點的對應文件路徑 src //源文件 state={directory,link,present,absent} //目錄,鏈接,創建,刪除
(9)yum 安裝軟件包模塊
# ansible-doc -s yum name= //要安裝的軟件包名 state={present,latest,absent} //安裝,最新的,刪除
(10)service 服務管理模塊
# ansible-doc -s service name= //服務名 state={started,stopped,restarted,reloaded} //啓動,停止,重啓,重載 enabled={yes,no} //是否開啓啓動,默認爲no runlevel //運行級別
(11)script 腳本模塊
# ansible-doc -s script //沒有參數,直接在-a "腳本名(不用設置x權限)"
[root@localhost ~]# vim hello.sh #!/bin/bash echo "$(hostname):hello,ansible"
至此,幾種常用模塊的使用方法介紹完畢!
2.3、playbook簡單介紹
playbook是由一個或多個“play”組成的列表。play的主要功能在於將事先歸併爲一組的主機裝扮成事先通過ansible中的tasks定義好的角色。
- hosts: all //管理節點,可以爲多個 remote_user: root //執行操作的用戶 tasks: //定義的任務 - name: ensure apache latest version //安裝httpd軟件包 yum: state=latest name=httpd - name: apache configure file //複製httpd的配置文件到管理的節點 copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes notify: //如果配置文件改變,執行httpd重啓 - restart httpd handlers: //定義處理器 - name: restart httpd //重啓httpd服務 service: name=httpd state=restarted
三、corosync
corosync是集羣管理套件的一部分,它可以監控和傳遞集羣各節點的心跳信息及集羣事務信息,工作在高可用集羣的messaging layer層,是從OpenAIS中分離出來的組件
四、pacemaker
pacemaker是高可用集羣的資源管理器(CRM),利用集羣基礎構件(OpenAIS或heartbeat)提供的消息和管理能力來探測並從節點或資源級別的故障中恢復,以實現羣集服務(亦稱資源)的最大可用性。可結合corosync來實現高可用服務。
五、使用ansible自動化,基於pcs實現httpd的高可用
測試環境:centos 7 ,使用pcs/pcsd來完成corosync+pacemaker對http服務的高可用
規劃:定義3個roles
(1)initial:要管理節點的初始化工作,同步時間,主機名解析
(2)pcs:相關的設置工作,如安裝corosync+pacemaker,定義集羣的資源
(3)httpd:安裝httpd服務,定義不同節點的index.html,爲測試使用,生產環境不需要
(1)先看initial角色的設置
其中tasks/main.yml內容: - name: install ntpdate packages //安裝ntpdate包 yum: name=ntpdate state=present - name: cron sync time from 172.16.0.1 cron: name="sync time from 172.16.0.1" minute="*/5" job="/usr/sbin/ntpdate 172.16.0.1 &> /dev/null" //設置計劃任務 - name: copy /initial/template/hosts to node1 node2 node3 template: src=hosts dest=/etc/hosts //使用模板,主機名解析 templates/hosts內容: 172.16.116.231 node1 172.16.116.232 node2 172.16.116.233 node3
(2)pcs角色設置
查看files/pcs.sh 的內容,是個腳本,在創建完高可用集羣后執行定義資源的操作 #!/bin/bash # set stonith_enabled=false pcs property set stonith-enabled=false //不使用stonith設備 # define webip resource pcs resource create webip ocf:heartbeat:IPaddr ip="172.16.16.40" op monitor interval=20s timeout=10s //定義webip資源 # define webserver resource //定義httpd資源 pcs resource create webserver systemd:httpd op monitor interval=20s timeout=20s # define group resource pcs resource group add webservice webip webserver //將webip和httpd定義爲同一個組資源 #define constraint #pcs constraint colocation add webserver with webip //定義排列約束,如果不定義組,可以使用 pcs constraint order start webip then webserver //定義順序約束,webip先啓動 #define constraint pcs constraint order webip then webserver //定義順序約束 查看tasks/main.yml內容 - name: install pcs | pacemaker | corosync | packages //安裝pcs yum: name=pcs state=present - name: service pcsd start service: name=pcsd state=started enabled=yes //開機自動啓動pcsd - name: passwd hacluster shell: echo 'hapassword' | passwd --stdin hacluster //給hacluster設置密碼 - name: delete corosync configure file shell: rm -rf /etc/corosync/corosync.conf - name: delete pacemaker cib.xml file shell: rm -rf /var/lib/pacemaker/cib/cib.xml - name: auth corosync shell: pcs cluster auth node1 node2 node3 -u hacluster -p hapassword //集羣節點認證 - name: create a ha_cluster shell: pcs cluster setup --name hac node1 node2 node3 //將node[1:3]創建爲高可用集羣 - name: start cluster shell: pcs cluster start --all //啓動集羣 - name: define cluster resources //使用上面的腳本定義集羣資源 script: pcs.sh when: ansible_hostname == "node1" //當主機名爲node1的時候執行pcs.sh腳本 tags: resource //設定標籤,如果資源改變,只需執行這步
(3)httpd角色相關設置;定義的還是比較簡單的,比如httpd配置文件的模板應該提供等,大家在這個基礎上做相應的完善即可
- name: install httpd package //安裝httpd軟件包 yum: name=httpd state=present - name: define node1 index.html //給不同節點設置不同主頁,以方便測試使用 shell: echo {{ ansible_hostname }} > /var/www/html/index.html when: ansible_hostname == "node1" - name: define node2 index.html shell: echo {{ ansible_hostname }} > /var/www/html/index.html when: ansible_hostname == "node2" - name: define node3 index.html shell: echo {{ ansible_hostname }} > /var/www/html/index.html when: ansible_hostname == "node3" - name: enabled service httpd //不啓動httpd,只是將httpd設置爲開機自啓 service: name=httpd enabled=yes //centos7只能這樣操作,否則找不到systemd.httpd
(4) roles設置完成,最後定義playbook
- hosts: ha //執行此操作的節點 remote_user: root //執行的用戶,當然可以使用具有管理員權限的sudo用戶 roles: //上面的3個roles - initial - httpd - pcs
(5)執行playbook並測試
[root@localhost corosync]# ansible-playbook corosync1.yml PLAY [ha] ********************************************************************* GATHERING FACTS *************************************************************** ok: [172.16.116.231] ok: [172.16.116.232] ok: [172.16.116.233] TASK: [initial | install ntpdate packages] ************************************ ok: [172.16.116.232] ok: [172.16.116.231] ok: [172.16.116.233] TASK: [initial | cron sync time from 172.16.0.1] ****************************** ok: [172.16.116.231] ok: [172.16.116.232] ok: [172.16.116.233] TASK: [initial | copy /initial/template/hosts to node1 node2 node3] *********** ok: [172.16.116.232] ok: [172.16.116.231] ok: [172.16.116.233] TASK: [httpd | install httpd package] ***************************************** ok: [172.16.116.231] ok: [172.16.116.233] ok: [172.16.116.232] TASK: [httpd | define node1 index.html] *************************************** skipping: [172.16.116.232] skipping: [172.16.116.233] changed: [172.16.116.231] TASK: [httpd | define node2 index.html] *************************************** skipping: [172.16.116.231] skipping: [172.16.116.233] changed: [172.16.116.232] TASK: [httpd | define node3 index.html] *************************************** skipping: [172.16.116.231] skipping: [172.16.116.232] changed: [172.16.116.233] TASK: [httpd | enabled service httpd] ***************************************** ok: [172.16.116.231] ok: [172.16.116.232] ok: [172.16.116.233] TASK: [pcs | install pcs | pacemaker | corosync | packages] ******************* ok: [172.16.116.231] ok: [172.16.116.233] ok: [172.16.116.232] TASK: [pcs | service pcsd start] ********************************************** ok: [172.16.116.232] ok: [172.16.116.231] ok: [172.16.116.233] TASK: [pcs | passwd hacluster] ************************************************ changed: [172.16.116.232] changed: [172.16.116.231] changed: [172.16.116.233] TASK: [pcs | delete corosync configure file] ********************************** changed: [172.16.116.232] changed: [172.16.116.231] changed: [172.16.116.233] TASK: [pcs | delete pacemaker cib.xml file] *********************************** changed: [172.16.116.231] changed: [172.16.116.233] changed: [172.16.116.232] TASK: [pcs | auth corosync] *************************************************** changed: [172.16.116.233] changed: [172.16.116.231] changed: [172.16.116.232] TASK: [pcs | create a ha_cluster] ********************************************* changed: [172.16.116.231] changed: [172.16.116.232] changed: [172.16.116.233] TASK: [pcs | start cluster] *************************************************** changed: [172.16.116.231] changed: [172.16.116.232] changed: [172.16.116.233] TASK: [pcs | define cluster resources] **************************************** skipping: [172.16.116.232] skipping: [172.16.116.233] changed: [172.16.116.231] PLAY RECAP ******************************************************************** 172.16.116.231 : ok=16 changed=8 unreachable=0 failed=0 172.16.116.232 : ok=15 changed=7 unreachable=0 failed=0 172.16.116.233 : ok=15 changed=7 unreachable=0 failed=0
#將node1節點設置爲備用,查看集羣服務是否可用
可以看到服務還可以正常訪問,至此基於ansible自動化使用pcs/pcsd配置corosync+pacemaker實現httpd的過程演示完成!
下面的是自動生成的corosync的配置文件
[root@node1 ~]# vim /etc/corosync/corosync.conf totem { version: 2 //版本 secauth: off // cluster_name: hac //集羣名 transport: udpu //使用udpu協議 } nodelist { //集羣節點列表,3個 node { ring0_addr: node1 nodeid: 1 } node { ring0_addr: node2 nodeid: 2 } node { ring0_addr: node3 nodeid: 3 } } quorum { //投票系統 provider: corosync_votequorum } logging { //日誌 to_syslog: yes }
ok!!介紹完畢!O(∩_∩)O謝謝~~