heartbeartv2實現lamp高可用-week17


3、基于heartbeat v2 crm實現HA LAMP組合;要求,部署wordpress,用于編輯的文章中的任何數據在節點切換后都能正常訪問;

拓撲:
mark

環境: CentOS6.6
NFS: 172.16.0.34 輸出mysql數據目錄
ntp: 172.16.0.31 時間服務器
node1: 172.16.0.32 heartbeart+httpd+php+mysql
node2: 172.16.0.33 heartbeart+httpd+php+mysql
fip: 172.16.0.40 浮動ip

一. 安裝ntp時間服務器

1. 安裝ntp服務器
#查看是否已經安裝
[root@localhost ~]# rpm -qa ntp
#如果沒有就安裝
[root@localhost ~]# yum -y install ntp

2. 配置ntp服務
[root@localhost ~]# vim /etc/ntp.conf
#設置權限
#restrict default kod nomodify notrap nopeer noquery  默認拒絕所有來源的任何訪問
restrict 127.0.0.1    #給于本機所有權限 
restrict 172.16.0.0 mask 255.255.0.0 notrap nomodify   #給于局域網機的機器有同步時間的權限
#設置上層ntp服務器,注釋掉原有上層服務器
server ntp1.aliyun.com prefer   #設置上傳時間服務器,假prefer表示優先
server time.nist.gov
#內部時鐘,用在沒有外網ntp服務器時
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10    #時間服務器的層次

[root@localhost ~]# cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict 172.16.0.0 mask 255.255.0.0 notrap nomodify
server ntp1.aliyun.com prefer
server time.nist.gov
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

3. 啟動ntp
[root@localhost ~]# service ntpd start
[root@localhost ~]# chkconfig  ntpd on

4. 查看并測試
查看ntp端口
[root@localhost ~]# netstat -ulnp | grep ntpd
udp        0      0 172.16.0.31:123             0.0.0.0:*                               1485/ntpd
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1485/ntpd
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1485/ntpd
udp        0      0 fe80::20c:29ff:fe90:d168:123 :::*                                    1485/ntpd
udp        0      0 ::1:123                     :::*                                    1485/ntpd
udp        0      0 :::123                      :::*                                    1485/ntpd

查看ntp服務器有無和上層連通
[root@localhost ~]# ntpstat
synchronised to NTP server (182.92.12.11) at stratum 3
   time correct to within 11 ms
   polling server every 64 s

查看ntp服務器與上層ntp服務器的狀態
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u    5   64  377    3.272    1.288   0.844
xnist1-lnk.binar .ACTS.           1 u  139   64  144  318.446  -44.920   5.038

其中,
remote - 本機和上層ntp的ip或主機名,“+”表示優先,“*”表示次優先
refid - 參考上一層ntp主機地址
st - stratum階層
when - 多少秒前曾經同步過時間
poll - 下次更新在多少秒后
reach - 已經向上層ntp服務器要求更新的次數
delay - 網絡延遲
offset - 時間補償
jitter - 系統時間與bios時間差

二.heartbeart環境準備

(1) 設置主機名

[root@node1 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node1.magedu.com

[root@node2 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node2.magedu.com

(2) 分別配置hosts文件

~]# vim /etc/hosts
172.16.0.32 node1.magedu.com node1
172.16.0.33 node2.magedu.com node2

(3) ssh密鑰認證

[root@node1 ~]# ssh-keygen -t rsa
[root@node1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@node2

[root@node2 ~]# ssh-keygen -t rsa
[root@node2 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@node1

(4) 同步時間

~]# yum -y install ntp
 ~]# vim /etc/ntp.conf
 server 172.16.0.31
 ~]# service  ntpd start
 ~]# chkconfig ntpd on
 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 172.16.0.31     182.92.12.11     3 u   46   64    7    0.219  112.007   0.418
 ~]# ntpstat
synchronised to NTP server (172.16.0.31) at stratum 4
   time correct to within 1072 ms
   polling server every 64 s
   驗證: 
   [root@node1 ~]# date; ssh node2 'date'
Sun May 21 15:57:15 CST 2017
Sun May 21 15:57:15 CST 2017

三.nfs服務器

#創建目錄并授權
[root@localhost data]# mkdir /mydata
[root@localhost data]# groupadd -r -g 306 mysql
[root@localhost data]# useradd -r -g 306 -u 306 mysql
[root@localhost data]# mkdir /mydata/data
[root@localhost data]# chown -R mysql.mysql /mydata/data
[root@localhost data]# ll /mydata/
total 4
drwxr-xr-x. 2 mysql mysql 4096 May 18 18:23 data
#配置共享
[root@localhost data]# vim /etc/exports
/mydata     172.16.0.0/16(rw,no_root_squash)
[root@localhost data]# exportfs -arv
#啟動nfs
[root@localhost ~]# service rpcbind start
[root@localhost ~]# service nfs start
[root@localhost ~]# showmount -e 172.16.0.34
Export list for 172.16.0.34:
/mydata 172.16.0.0/16

四.安裝mysql

在node1和node2分別執行

]# yum -y install nfs-utils
]# mkdir /mydata
]# groupadd -r -g 306 mysql
]# useradd -r -g 306 -u 306 mysql
]# mount -t nfs 172.16.0.34:/mydata /mydata

]# tar xf mariadb-5.5.52-linux-x86_64.tar.gz -C /usr/local/
]# cd /usr/local/
]# ln -sv mariadb-5.5.52-linux-x86_64 mysql
]# cd mysql/
]# chown -R root.mysql ./*
#初始化數據庫(只執行一次)
]# ./scripts/mysql_install_db  --datadir=/mydata/data/ -user=mysql

配置文件
mysql]# mkdir /etc/mysql
mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
mysql]# vim /etc/mysql/my.cnf
[mysqld]
...
datadir =/mydata/data
innodb_file_per_table = on
skip_name_resolve = on

準備啟動腳本,并啟動
mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
mysql]# chkconfig --add mysqld
mysql]# service mysqld start

創建數據庫(只執行一次)
[root@localhost mysql]# /usr/local/mysql/bin/mysql
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all on wpdb.* to 'wpuser'@'172.16.%' identified by '123456';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit

關閉mysqld服務,并設置開機不啟動
]# service mysqld stop
]# chkconfig mysqld off

卸載nfs共享
]# umount /mydata

五. httpd安裝

在node1和node2分別執行

]# yum -y install httpd
]# echo "<h1>node1</h1>" > /var/www/html/index.html    #node1執行
]# echo "<h1>node2</h1>" > /var/www/html/index.html

啟動
]# service httpd start;ssh node2 'service httpd start'  #node1執行

訪問測試
]# curl node{1,2}
<h1>node1</h1>
<h1>node2</h1>

停止服務及開機啟動
]# service httpd stop;ssh node2 'service httpd stop'   #node1執行
]# chkconfig httpd off; ssh node2 'chkconfig httpd off'

查看是否關閉自啟動
~]# chkconfig --list httpd; ssh node2 'chkconfig --list httpd'
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

六.安裝php

在node1和node2分別執行

]# yum -y install php php-mysql

七.安裝wordpress

在node1和node2分別執行

~]# cd /usr/local/src
src]# wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.zip
src]# unzip wordpress-4.7.4-zh_CN.zip
src]# cp -r -p  wordpress/* /var/www/html
src]# cd /var/www/html/
wordpress]# cp wp-config-sample.php wp-config.php         
wordpress]# vim wp-config.php                             
define('DB_NAME', 'wpdb');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', '123456');
define('DB_HOST', '172.16.0.40');     

注意: 頁面安裝時必須使用fip,因為ip地址會寫入數據庫中,造成頁面打開失敗(訪問另一個不可用的ip地址)

測試命令:
手動掛載數據目錄并啟動mysql                         
]# mount -t nfs 172.16.0.34:/mydata /mydata
]# service mysqld start
]# service httpd start
手動關閉httpd,msyqld服務并取消掛載
[root@node1 wordpress]# service httpd stop
[root@node1 wordpress]# service mysqld stop
[root@node1 wordpress]# umount /mydata/

八. 安裝heartbeat

在node1和node2分別執行
(1) 安裝依賴包

~]# yum -y install  net-snmp-libs libnet PyXML  gettext-devel  libtool-ltdl pygtk2-libglade
~]# rpm -ivh libnet-1.1.6-7.el6.x86_64.rpm

(2) 安裝heartbeart及相關包

~]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm

(3) 準備heartbeat配置文件

~]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/
~]# cd /etc/ha.d/
ha.d]# chmod 600 authkeys

(4) 配置authkeys

ha.d]# openssl rand -base64 6
7mA2c+82
ha.d]# vim authkeys
auth 2
#1 crc
2 sha1 7mA2c+82

(5) 配置ha.cf

ha.d]# vim ha.cf
#logfacility    local0
logfile /var/log/heartbeat.log
mcast eth0 225.23.190.1 694 1 0
auto_failback on
node node1.magedu.com
node node2.magedu.com
ping 172.16.0.1
crm on

[root@node1 ha.d]# grep -v "#" ha.cf
logfile /var/log/heartbeat.log
mcast eth0 225.23.190.1 694 1 0
auto_failback on
node node1.magedu.com
node node2.magedu.com
ping 172.16.0.1
crm on

(6) 配置文件拷貝到node2

[root@node1 ha.d]# scp -p  authkeys ha.cf root@node2:/etc/ha.d

(7) 啟動服務

[root@node1 ~]# service heartbeat start; ssh node2 'service heartbeat start'

(8) 查看集群狀態

~]# crm_mon
Refresh in 14s...

============
Last updated: Sun May 21 18:26:53 2017
Current DC: node2.magedu.com (2fe0005c-a582-4e46-9e22-353eed7d91cc)
2 Nodes configured.
0 Resources configured.
============

Node: node2.magedu.com (2fe0005c-a582-4e46-9e22-353eed7d91cc): online
Node: node1.magedu.com (81ae3d5f-02ca-4c97-81cc-6d8b933f716c): online

(9) 設置hacluster密碼
哪個節點使用hb_gui,就在哪個節點創建

[root@node1 ~]# echo "mageedu" | passwd --stdin hacluster
[root@node2 ~]# echo "mageedu" | passwd --stdin hacluster

(10)hb_gui設置
問題: 解決hb_hui無法正常顯示問題

[root@node1 ha.d]# hb_gui &
[root@node1 ha.d]# Traceback (most recent call last):
  File "/usr/bin/hb_gui", line 41, in <module>
    import gtk, gtk.glade, gobject
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
    _init()
  File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
    _gtk.init_check()
RuntimeError: could not open display

#安裝支持xshell xmanager包,否則xshell無法調用x11,安裝后重啟系統
[root@node1 ha.d]# yum -y install xorg-x11-xauth
#重啟后可以彈出窗口,但無法顯示文字,
[root@node1 ~]# hb_gui &
[1] 1276
[root@node1 ~]# /usr/bin/hb_gui:2856: PangoWarning: failed to choose a font, expect ugly output. engine-type='PangoRenderFc', script='latin'
  win_widget.show_all()
/usr/bin/hb_gui:2856: PangoWarning: failed to choose a font, expect ugly output. engine-type='PangoRenderFc', script='common'
  win_widget.show_all()
#安裝字體
[root@node1 ~]#  yum install dejavu-lgc-sans-fonts

配置

[root@node1 ~]# hb_gui &

mark
登錄
mark
mark
解釋:
Node Name : 節點名字
Online: 是否在線
Is it DC:是不是DC
Type: 是不是集群成員
StandBy: 是不是在備用模式
Expected up: 是否啟動為true
Shutdown: 是否shutdown
unclean: 是不是非干凈(一致性)狀態

添加資源ip,nfs,mysqld,httpd
要求:

  1. mysqld要在nfs啟動之后出能啟動
  2. ip地址和mysqld服務之間沒有先后關系
  3. httpd服務需要先啟動ip,因為服務啟動的時候明確需要ip地址的資源
    注意: 分組group已經包含了順序約束和排列約束,要按照順序創建資源
    1) 定義一個組并添加ip
    mark
    mark
    mark
    mark
    mark
    mark

2) 添加nfs文件系統
mark
mark
3) 添加mysql資源
mark
mark
4) 添加web資源
mark
mark
5) 啟動組
保存配置
mark
mark
mark
默認在DC上運行,所以所有資源運行在node2上

(11) 查看節點上運行的資源

服務:
[root@node2 ~]# ss -tnl | egrep "3306|80"
LISTEN     0      50                        *:3306                     *:*
LISTEN     0      128                      :::80                      :::*
進程:
[root@node2 ~]# ps aux | egrep "mysqld_safe|httpd"
root      2811  0.0  0.3  11432  1512 ?        S    22:08   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mydata/data --pid-file=/mydata/data/node2.magedu.com.pid
root      3246  0.0  1.8 252404  9104 ?        Ss   22:08   0:00 /usr/sbin/httpd
apache    3248  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3249  0.0  1.0 252404  5252 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3250  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3251  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3252  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3253  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3254  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
apache    3255  0.0  1.0 252404  5228 ?        S    22:08   0:00 /usr/sbin/httpd
ip地址: 
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:0c:29:b6:b3:39 brd ff:ff:ff:ff:ff:ff
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b6:b3:43 brd ff:ff:ff:ff:ff:ff
    inet 172.16.0.33/24 brd 172.16.0.255 scope global eth0
    inet 172.16.0.40/16 brd 172.16.255.255 scope global eth0
    inet6 fe80::20c:29ff:feb6:b343/64 scope link
       valid_lft forever preferred_lft forever
nfs掛載: 
[root@node2 ~]# mount | grep mydata
172.16.0.34:/mydata on /mydata type nfs (rw,vers=4,addr=172.16.0.34,clientaddr=172.16.0.33)

(12) 訪問測試
當前在node2上

mark
mark
mark
mark
mark
(13) 模擬節點故障,node2切換為備節點standby
mark
頁面訪問寫入均正常
mark
mark
(14) node2切換為active, node2搶回所有資源
mark

問題: 用fip打開頁面時,報連接不到數據庫或者是頁面上的連接跳轉到另一個沒有開啟服務的ip地址
解決方法: 將wp-config.php中的ip設置為fip
原因是ip地址會寫入mysql數據庫
mark

原創文章,作者:hansj,如若轉載,請注明出處:http://www.www58058.com/76779

(0)
hansjhansj
上一篇 2017-05-23 10:14
下一篇 2017-05-23 11:01

相關推薦

  • 關于取路徑名與基名的探討和擴展-20160806

                          關于取路徑名與基名的探討和擴展     這兩天學習grep  ,   egrep  ,  sed  &nb…

    Linux干貨 2016-08-07
  • Linux系統上命令的使用格式

    第一部分:Linux系統上命令的使用格式 命令的語法通用格式:      COMMAND OPTIONS ARGUMENTS     發起一個命令:請求內核將某個二進制程序運行為一個進程;      程序 —-> 進程   &nbs…

    Linux干貨 2016-08-15
  • 從Linux小白到大?!c狼共舞的日子12(上)

    馬哥教育網絡班21期+第12周課程練習 1、請描述一次完整的http請求處理過程; 1)建立或處理連接:客戶端發送http請求報文,服務器端接收或拒絕請求; 2)接收請求:服務器端接收來自客戶端對某些資源的請求; 3)處理請求:服務器端解析客戶端請求報文,獲取客戶端請求的資源及請求方法等信息; 4)訪問資源:服務器端獲取客戶端請求的資源; 5)構建響應報文;…

    Linux干貨 2016-12-26
  • N22網絡班第一周作業

    1、 描述計算機的組成及其功能。 運算器、控制器、存儲器、輸入設備、輸出設備 運算器、控制器 :cpu 運算和邏輯計算 存儲器:緩存和保存數據 輸入設備、輸出設備:用戶和計算機交互設備和界面 2、 按系列羅列Linux的發行版,并描述不同發行版之間的聯系與區別。 Slackware系列: suse   opensuse debian系列: ubun…

    Linux干貨 2016-08-15
  • 11文件查找find和locate

    有些時候我們是想要在系統中查找某個具體的文件,卻不知道路徑在哪里,只是知道其中的某些特性,比如大小或者名字什么的。這時候就要用到查找工具啦。 在文件系統上查找符合條件的文件命令有兩個,locate和find,其中locate是非實時查找即數據庫查找。而find是實時查找 locate: 用法:locate [OPTION]… [PATTERN]&…

    Linux干貨 2016-11-27
  • 免費翻墻 [精]

    本人在hostus上買了一個國外的vps,花了一上午把Google給做好,可以訪問g.abcdocker.com進行搜索,因為是使用nginx代理進行翻墻。網上的文章也很亂,很不好整理。 可以可以使用g.abcdocker.com上Google查閱資料。(無法觀看視頻) www.abcdocker.com

    2017-06-17

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-06-20 09:58

    寫的很好,排版也很漂亮,看來你的博客完全可以搭建出來,希望可以再接再厲,多多寫一個好的博客出來

欧美性久久久久