主機的ip地址分配
html
hostname | IP address | role |
master | 192.168.1.250/24 | ansible |
node2.mictiger.com | 192.168.1.120/24 | heartbeat,httpd |
node3.mictiger.com | 192.168.1.130/24 | heartbeat,httpd |
1)修改hostnamenode
在node2和node3的/etc/hosts加入以下信息web
192.168.1.120 node2.mictiger.com node2 192.168.1.130 node3.mictiger.com node3
2)創建master到node2,node3的節點互信vim
生成密鑰對bash
[root@master ~]# ssh-keygen -t rsa -P '' 生成一對密鑰 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: dc:a5:7d:fc:ff:42:8c:8a:55:38:73:87:35:9a:f5:6a root@CentOS The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | | | + | | ..* o | | . .++*.. .| | S o=.+o. | | . ..E. | | o . o .| | . . . .| | .+| +-----------------+
將生成的公鑰傳到目標主機dom
[root@master ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.120 [root@master ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.130 [root@master ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.150
在master上驗證是否與node2,node3創建互信成功ssh
[root@master ~]# ssh 192.168.1.130 'date';ssh 192.168.1.120 'date';date Tue Nov 18 22:39:55 CST 2014 Tue Nov 18 22:40:03 CST 2014 Tue Nov 18 22:40:04 CST 2014
3)安裝配置ansiblecurl
[root@master ~]# yum install ansible [root@master ~]# vim /etc/ansible/hosts 只須要配置這文件配置要管理的主機 [HA] 192.168.1.150 192.168.1.120 192.168.1.130 [web] 192.168.1.120 192.168.1.130 [master] 192.168.1.150
驗證ansible是否配置成功ide
[root@master ~]# ansible all -a 'date' 192.168.1.120 | success | rc=0 >> Tue Nov 18 13:40:21 CST 2014 192.168.1.150 | success | rc=0 >> Tue Nov 18 13:40:21 CST 2014 192.168.1.130 | success | rc=0 >> Tue Nov 18 13:40:21 CST 2014
4)利用ansible更新時間,高可用羣集必須配置同樣的時間ui
[root@master ~]# ansible web -a 'ntpdate 202.118.1.81' 192.168.1.130 | success | rc=0 >> 18 Nov 22:50:54 ntpdate[21487]: step time server 202.118.1.81 offset 1.531700 sec 192.168.1.120 | success | rc=0 >> 18 Nov 22:50:54 ntpdate[13616]: adjust time server 202.118.1.81 offset 0.313219 sec 將時間更新寫入cron [root@master ~]# ansible all -m cron -a 'name="sync time" minute="*/5" job="/usr/sbin/ntpdate 202.118.1.81 &> /dev/null"' 192.168.1.130 | success >> { "changed": true, "jobs": [ "sync time" ] } 192.168.1.120 | success >> { "changed": true, "jobs": [ "sync time" ] } 192.168.1.150 | success >> { "changed": true, "jobs": [ "sync time" ] }
5) 安裝並配置httpd程序
[root@master~]# ansible web -m yum -a 'name=httpd state=present' [root@master ~]# ansible web -a 'service httpd start' 192.168.1.130 | success | rc=0 >> Starting httpd: [ OK ]httpd: apr_sockaddr_info_get() failed for CentOS httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName 192.168.1.120 | success | rc=0 >> Starting httpd: [ OK ]httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName [root@master ~]# touch index.html [root@master ~]# echo heartbeatV2 > index.html [root@master ~]# scp index.html root@192.168.1.130:/var/www/html/ index.html 100% 16 0.0KB/s 00:00 [root@master ~]# scp index.html root@192.168.1.120:/var/www/html/ index.html 100% 16 0.0KB/s 00:00
驗證httpd是否安裝成功
[root@master ~]# ansible master -a 'curl 192.168.1.{120,130}' 192.168.1.150 | success | rc=0 >> --_curl_--192.168.1.120 heartbeatV2 --_curl_--192.168.1.130 heartbeatV2 [1/2]: 192.168.1.120 --> <stdout> % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 16 0 16 0 0 5072 0 --:--:-- --:--:-- --:--:-- 8000 [2/2]: 192.168.1.130 --> <stdout> 0 16 0 16 0 0 10349 0 --:--:-- --:--:-- --:--:-- 10349
將httpd程序stop並不讓它開機自動啓動
[root@www ~]# ansible web -a 'service httpd stop' 192.168.1.120 | success | rc=0 >> Stopping httpd: [ OK ] 192.168.1.130 | success | rc=0 >> Stopping httpd: [ OK ] [root@www ~]# ansible web -a 'chkconfig httpd off' 192.168.1.120 | success | rc=0 >> 192.168.1.130 | success | rc=0 >>
6)安裝heatbeatV2須要處理依賴關係因此沒有用ansible
[root@node2 ~]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat-pils ########################################### [ 25%] 2:heartbeat-stonith ########################################### [ 50%] 3:heartbeat ########################################### [ 75%] 4:heartbeat-gui ########################################### [100%]
7)配置heartbeat
[root@node2 ~]# cp /usr/share/doc/heartbeat-2.1.4/{authkeys,ha.cf} /etc/ha.d/ 配置authkeys [root@node2 ha.d]# openssl rand -hex 16 生成16的隨機數做爲heartbeat傳遞心跳信息的驗證 701782e0e6872f444edd0bbb726871fb auth 2 #1 crc #2 sha1 HI! #3 md5 Hello! 2 sha1 701782e0e6872f444edd0bbb726871f [root@CentOS ha.d]# chmod 600 authkeys 修改authkeys的權限不讓其餘人訪問 配置heartbeat的主配置文件ha.cf大概配置以下信息(根據我的需求而定) logfile /var/log/ha-log 日誌文件位置 mcast eth0 225.10.10.10 694 1 0 使用組播地址通告和端口 auto_failback on 自動添加down了又從新上線的節點到羣集 node node2.mictiger.com 配置節點信息 node node3.mictiger.com ping 192.168.1.1 網關地址 配置haresources添加以下信息 node2.mictiger.com 192.168.1.250/24/eth0 httpd 主節點和VIP綁定的網卡,管理的資源 複製node2 的配置信息到node3 [root@localhost ~]# scp -r /etc/ha.d/* root@192.168.1.130:/etc/ha.d/
8)啓動heartbeat
[root@master ~]# ansible web -a 'service heartbeat start' 192.168.1.130 | success | rc=0 >> Starting High-Availability services: Done.2014/11/18_23:36:19 INFO: Resource is stopped 192.168.1.120 | success | rc=0 >> Starting High-Availability services: Done.2014/11/18_23:36:19 INFO: Resource is stopped
查看heartbeat運行在哪一個節點上
[root@master ~]# ansible web -a 'ifconfig eth0:0'
192.168.1.130 | success | rc=0 >>
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:8F:A0:EF
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
192.168.1.120 | success | rc=0 >>
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:57:5C:BB
inet addr:192.168.1.250 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@master ~]# curl 192.168.1.250
heartbeatV2
關閉node2驗證是否可以訪問驗證高可用性
[root@node2 ~]# service heartbeat stop
Stopping High-Availability services:
Done.