1、結合圖形描述LVS的工作原理;
LVS (Linux Virtual Server)是一種集群(Cluster)技術,采用IP負載均衡技術和基于內容請求分發技術。調度器具有很好的吞吐率,將請求均衡地轉移到不同的服務器上執行,且調度器自動屏蔽掉服務器的故障,從而將一組服務器構成一個高性能的、高可用的虛擬服務器。整個服務器集群的結構對客戶是透明的,而且無需修改客戶端和服務器端的程序。
為此,在設計時需要考慮系統的透明性、可伸縮性、高可用性和易管理性。一般來說,LVS集群采用三層結構,其體系結構如圖所示:
LVS主要的組成部分為負載調度器,它是整個集群對外面的前端機,負責將客戶的請求發送到一組服務器上執行,而客戶認為服務是來自一個IP地址上的;服務器池,是一組真正執行客戶請求的服務器,執行的服務一般有WEB、MAIL、FTP和DNS等;共享存儲,它為服務器池提供一個共享的存儲區,這樣很容易使得服務器池擁有相同的內容,提供相同的服務。 LVS負載均衡分為三種方式,分別是NAT、TUN、DR。下面我分別進行介紹:
1、NAT是一種最簡單的方式,所有的RealServer只需要將自己的網關指向Director即可。客戶端可以是任意操作系統,但此方式下,一個Director能夠帶動的RealServer比較有限。在VS/NAT的方式下,Director也可以兼為一臺RealServer。VS/NAT的體系結構如下圖所示:
2、IP隧道(IP tunneling)是將一個IP報文封裝在另一個IP報文的技術,這可以使得目標為一個IP地址的數據報文能被封裝和轉發到另一個IP地址。IP隧道技術亦稱為IP封裝技術(IP encapsulation)。IP隧道主要用于移動主機和虛擬私有網絡(Virtual Private Network),在其中隧道都是靜態建立的,隧道一端有一個IP地址,另一端也有唯一的IP地址。它的連接調度和管理與VS/NAT中的一樣,只是它的報文轉發方法不同。調度器根據各個服務器的負載情況,動態地選擇一臺服務器,將請求報文封裝在另一個IP報文中,再將封裝后的IP報文轉發給選出的服務器;服務器收到報文后,先將報文解封獲得原來目標地址為 VIP 的報文,服務器發現VIP地址被配置在本地的IP隧道設備上,所以就處理這個請求,然后根據路由表將響應報文直接返回給客戶,如下圖所示:
3、DR方式是通過改寫請求報文中的MAC地址部分來實現的。Director和RealServer必需在物理上有一個網卡通過不間斷的局域網相連。 RealServer上綁定的VIP配置在各自Non-ARP的網絡設備上(如lo或tunl),Director的VIP地址對外可見,而RealServer的VIP對外是不可見的。RealServer的地址即可以是內部地址,也可以是真實地址,如下圖所示:
2、搭建一套LVS-DR模型的高性能集群,并實現以下功能:
(1)、wordpress程序通過nfs共享給各個realserver;
(2)、后端realserver中的nginx和php分離
一、安裝Nginx
先把iptables或者selinux關閉了,再把各個節點的服務器時間同步一下,命令# ntpdate cn.ntp.org.cn 然后再把C++和zlib、openssl、openssl-devel、pcre、pcre-devel等包先裝好,以免過程中出錯,用yum安裝就可以 [root@realserver1 ~]# groupadd www [root@realserver1 ~]# useradd -r -g www www -s /sbin/nologin #運行nginx的用戶和用戶組 [root@realserver1 nginx-1.8.1]# mkdir -pv /opt/application/nginxmkdir: created directory `/opt/application'mkdir: created directory `/opt/application/nginx' [root@realserver1 tools]# tar -xf nginx-1.8.1.tar.gz [root@realserver1 tools]# cd nginx-1.8.1 [root@realserver1 nginx-1.8.1]# ./configure --prefix=/opt/application/nginx/ --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-http_realip_module --with-ipv6 # --with-http_stub_status_module 啟用nginx狀態監控 # --with-http_ssl_module 啟用HTTPS加密 # --with-http_spdy_module 啟用spdy支持,縮短為網頁加載時間 # --with-http_gzip_static_module 啟用靜態壓縮 # --with-http_realip_module 做代理時獲取客戶端真實IP # --with-ipv6 支持ipv6 [root@realserver1 nginx-1.8.1]# make ;make install
創建nginx啟動腳本
[root@realserver1 ~]# vim /etc/rc.d/init.d/nginx [root@realserver1 ~]# chmod 755 /etc/rc.d/init.d/nginx [root@realserver1 ~]# service nginx start Starting nginx: [ OK ] [root@realserver1 ~]# chkconfig --add nginx [root@realserver1 ~]# chkconfig nginx on [root@realserver1 ~]# chkconfig --list nginx nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
測試成功:
[root@realserver1 ~]# curl -I http://192.168.2.212 HTTP/1.1 200 OK Server: nginx/1.8.1 Date: Thu, 11 Aug 2016 20:26:13 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Thu, 11 Aug 2016 19:21:49 GMT Connection: keep-alive ETag: "57acd04d-264" Accept-Ranges: bytes
Nginx腳本如下:
#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/opt/application/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/opt/application/nginx/conf/nginx.conf"lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
192.168.2.213(realserver2) :如同realserver1配置。
二、安裝Mysql
192.168.2.215(mysql-server):
[root@mysql-server ~]# wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.6/mysql-5.6.31.tar.gz [root@mysql-server ~]# tar -zxf mysql-5.6.31.tar.gz [root@mysql-server mysql-5.6.31]# groupadd mysql [root@mysql-server mysql-5.6.31]# useradd -r -g mysql mysql -s /sbin/nologin [root@mysql-server mysql-5.6.31]# cmake . \ -DCMAKE_INSTALL_PREFIX=/opt/application/mysql/ \ -DMYSQL_DATADIR=/opt/application/mysql/data \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DMYSQL_TCP_PORT=3306 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci #編譯時間很長,耐心等待... #DCMAKE_INSTALL_PREFIX 安裝根目錄 #DMYSQL_DATADIR 數據存儲目錄 #DMYSQL_UNIX_ADDR 連接數據庫socket路徑 #DSYSCONFDIR 配置文件(my.cnf)目錄 #DMYSQL_TCP_PORT mysql啟用的TCP/IP端口 #DWITH_MYISAM_STORAGE_ENGINE 啟用MYISAM引擎支持 #DWITH_INNOBASE_STORAGE_ENGINE 啟用INNOBASE引擎支持 #DWITH_MEMORY_STORAGE_ENGINE 啟用Memory引擎支持 #DWITH_READLINE 快捷鍵功能 #DENABLED_LOCAL_INFILE 允許從本地導入數據 #DWITH_PARTITION_STORAGE_ENGINE 安裝支持數據庫分區 #DEXTRA_CHARSETS 安裝所有的字符集 #DDEFAULT_CHARSET 默認字符集 #DDEFAULT_COLLATION 默認編碼 Mysql官方參考文件:http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html # make -j 4 # make install # chown -R mysql.mysql /opt/application/mysql #改變目錄所有者 # /opt/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/application/mysql --datadir=/opt/application/mysql/data #初始化數據庫 [root@mysql-server support-files]# cp /opt/application/mysql/support-files/my-default.cnf /etc/my.cnf [root@mysql-server support-files]# cp /opt/application/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql [root@mysql-server support-files]# chkconfig --add mysql [root@mysql-server support-files]# chkconfig mysql on #添加開機啟動 [root@mysql-server support-files]# service mysql start #啟動mysql服務 Starting MySQL. SUCCESS! [root@mysql-server support-files]# netstat -tunlpa |grep 3306 tcp 0 0 :::3306 :::* LISTEN 24982/mysqld [root@mysql-server ~]# vim /etc/bashrc #把mysql的bin命令添加到path PATH=/opt/application/mysql/bin:$PATH export PATH
三、安裝PHP
[root@php-server ~]# groupadd www [root@php-server ~]# useradd -r -g www www -s /sbin/nologin #運行php-fpm的用戶和用戶組 [root@php-server ~]# wget http://cn.php.net/distributions/php-5.6.24.tar.gz [root@php-server ~]# tar -zxf php-5.6.24.tar.gz [root@php-server ~]# cd php-5.6.24 [root@php-server php-5.6.24]# mkdir -pv /opt/application/php mkdir: created directory `/opt/application' mkdir: created directory `/opt/application/php' [root@php-server php-5.6.24]# ./configure \ --prefix=/opt/application/php \ --with-config-file-path=/etc \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-fpm \ --enable-ftp \ --enable-zip \ --enable-soap \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-exif \ --enable-pcntl \ --enable-opcache \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-sockets \ --enable-gd-native-ttf \ --enable-inline-optimization \ --with-zlib \ --with-curl \ --with-gd \ --with-mcrypt \ --with-openssl \ --with-mhash \ --with-xmlrpc \ --with-gettext \ --with-iconv-dir \ --with-freetype-dir \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-libxml-dir=/usr \ --with-pdo-mysql=mysqlnd \ --disable-rpath \ --disable-ipv6 \ --disable-debug \ --disable-fileinfo \
#這里可能報錯:configure: error: mcrypt.h not found. Please reinstall libmcrypt,解決方法如下:
1、安裝第三方yum源wget http://www.atomicorp.com/installers/atomicsh ./atomic 2、使用yum命令安裝yum install php-mcrypt libmcrypt libmcrypt-devel
四、修改nginx和php的配置文件
192.168.2.212(realserver1):如下圖;
192.168.2.213(realserver2):參照192.168.2.212(realserver1)
[root@realserver1 ~]# cp /opt/application/nginx/conf/nginx.conf /opt/application/nginx/conf/nginx.confbak #備份 [root@realserver1 ~]# vim /opt/application/nginx/conf/nginx.conf location / { root /data/www;#第1處修改 index index.html index.htm; } location ~ \.php$ { root /data/www;#第2處修改 fastcgi_pass 192.168.2.214:9000; #第3處修改 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
五、安裝NFS掛載wordpress
192.168.2.214(php-server):
[root@php-server ~]# yum install -y nfs-utils.x86_64 nfs-utils-lib.x86_64 nfs-utils-lib-devel.x86_64 # yum安裝nfs程序 [root@php-server ~]# /etc/init.d/rpcbind start Starting rpcbind: [ OK ] [root@php-server ~]# /etc/init.d/nfs start Starting NFS services: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@php-server ~]# netstat -tulnp |grep -E '(rpc|nfs)' tcp 0 0 0.0.0.0:48254 0.0.0.0:* LISTEN 65830/rpc.mountd tcp 0 0 0.0.0.0:54949 0.0.0.0:* LISTEN 65830/rpc.mountd tcp 0 0 0.0.0.0:39822 0.0.0.0:* LISTEN 65830/rpc.mountd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 65785/rpcbind tcp 0 0 :::37923 :::* LISTEN 65830/rpc.mountd tcp 0 0 :::47075 :::* LISTEN 65830/rpc.mountd tcp 0 0 :::111 :::* LISTEN 65785/rpcbind tcp 0 0 :::46353 :::* LISTEN 65830/rpc.mountd udp 0 0 0.0.0.0:664 0.0.0.0:* 65785/rpcbind udp 0 0 0.0.0.0:59076 0.0.0.0:* 65830/rpc.mountd udp 0 0 0.0.0.0:59472 0.0.0.0:* 65830/rpc.mountd udp 0 0 0.0.0.0:49502 0.0.0.0:* 65830/rpc.mountd udp 0 0 0.0.0.0:111 0.0.0.0:* 65785/rpcbind udp 0 0 :::664 :::* 65785/rpcbind udp 0 0 :::36008 :::* 65830/rpc.mountd udp 0 0 :::54987 :::* 65830/rpc.mountd udp 0 0 :::44906 :::* 65830/rpc.mountd udp 0 0 :::111 :::* 65785/rpcbind [root@php-server data]# mkdir -pv /data/www mkdir: created directory `/data' mkdir: created directory `/data/www' [root@php-server data]# cat /etc/exports /data/www 192.168.2.0/24(rw,no_root_squash) [root@php-server data]# ll -d /data/www/ drwxrwxrwx 2 root root 4096 Aug 13 04:02 /data/www/ [root@php-server data]# service nfs restart shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory Shutting down NFS daemon: [ OK ] Shutting down NFS mountd: [ OK ] Shutting down RPC idmapd: [ OK ] Starting NFS services: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@php-server data]# showmount -e 192.168.2.214 Export list for 192.168.2.214: /data/www 192.168.2.0/24 #下載wordpress到php-server并解壓縮 [root@php-server www]# chown -R www.www *
192.168.2.211(realserver1):
#安裝nfs [root@realserver1 html]# yum install -y nfs-utils.x86_64 nfs-utils-lib.x86_64 nfs-utils-lib-devel.x86_64 #把192.168.2.214上的程序掛載到realserver1、realserver2對應目錄上 [root@realserver1 html]# mount -t nfs 192.168.2.214:/data/www/ /data/www/ #查看realserver1的mount信息 [root@realserver1 html]# mount /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0") /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 192.168.2.214:/data/www/ on /data/www/ type nfs (rw,vers=4,addr=192.168.2.214,clientaddr=192.168.2.212)
192.168.2.212(realserver2): 參考192.168.2.211(realserver1),具體操作這里忽略;
192.168.2.214(mysql-server):
root@mysql-server ~]# mysql mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci; mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.2.214' IDENTIFIED BY '123456'; mysql> flush privileges;
瀏覽器輸入:http://192.168.2.212;
六、LVS安裝
192.168.2.211:
[root@lvs-dr ~]# yum install -y ipvsadm [root@lvs-dr ~]# ifconfig eth0:0 192.168.2.210/24 broadcast 192.168.2.210 up [root@lvs-dr ~]# route add -host 192.168.2.210 dev eth0:0 [root@lvs-dr ~]# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:3d:6a:9d brd ff:ff:ff:ff:ff:ff inet 192.168.2.211/24 brd 192.168.2.255 scope global eth0 inet 192.168.2.210/24 brd 192.168.2.210 scope global secondary eth0:0 inet6 fe80::20c:29ff:fe3d:6a9d/64 scope link valid_lft forever preferred_lft forever [root@lvs-dr ~]#
192.168.2.212(realserver1):
[root@realserver1 ~]# echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore [root@realserver1 ~]# echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore [root@realserver1 ~]# echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce [root@realserver1 ~]# echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce [root@realserver1 ~]# ifconfig lo:0 192.168.2.210 netmask 255.255.255.255 broadcast 192.168.2.210 [root@realserver1 ~]# route add -host 192.168.2.210 dev lo:0 [root@realserver1 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B8:03:1C inet addr:192.168.2.212 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb8:31c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:9435 errors:0 dropped:0 overruns:0 frame:0 TX packets:6582 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2278123 (2.1 MiB) TX bytes:2171329 (2.0 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:144 errors:0 dropped:0 overruns:0 frame:0 TX packets:144 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:12523 (12.2 KiB) TX bytes:12523 (12.2 KiB) lo:0 Link encap:Local Loopback inet addr:192.168.2.210 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:65536 Metric:1 [root@realserver1 ~]#
192.168.2.213(realserver2):參考192.168.2.212
192.168.2.211(lvs-dr):
[root@lvs-dr ~]# ipvsadm -a -t 192.168.2.210:80 -r 192.168.2.112 -g -w 1 [root@lvs-dr ~]# ipvsadm -a -t 192.168.2.210:80 -r 192.168.2.213 -g -w 2 [root@lvs-dr ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.2.210:80 rr -> 192.168.2.112:80 Route 1 0 0 -> 192.168.2.213:80 Route 2 0 0
3、基于heartbeat v2 crm實現HA LAMP組合;要求,部署wordpress,用于編輯的文章中的任何數據在節點切換后都能正常訪問;
[root@php-server www]service iptables stop [root@php-server www]chkconfig iptables off [root@php-server www]getenforce 0 安裝并同步各個節點的時間,例如 [root@lvs-dr ~]# yum install -y ntpdate.x86_64 [root@lvs-dr ~]# ntpdate cn.ntp.org.cn 一、修改主機名 復制代碼 [root@node1 ~]# more /etc/hosts hosts hosts.allow hosts.deny [root@node1 ~]# more /etc/hosts 127.0.0.1 localhost node1 node1.test.com localhost4 localhost4.localdomain4 ::1 localhost node1 node1.test.com localhost6 localhost6.localdomain6 [root@node1 ~]# more /etc/sysconfig/network NETWORKING=yes HOSTNAME=node1 復制代碼 二、安裝apahce 復制代碼 [root@node1 ~]# yum install -y httpd [root@node1 ~]# vim /var/www/html/index.html [root@node1 ~]# service httpd restart [root@node1 ~]# curl -I http://192.168.2.216 HTTP/1.1 200 OK Date: Sat, 13 Aug 2016 16:32:38 GMT Server: Apache/2.2.15 (CentOS) Last-Modified: Sat, 13 Aug 2016 16:30:22 GMT ETag: "260270-f-539f683fbfea1" Accept-Ranges: bytes Content-Length: 15 Connection: close Content-Type: text/html; charset=UTF-8 三、SSH免密鑰登錄 [root@node1 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: 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: f0:33:40:f4:f1:83:3d:7d:1e:5b:67:a2:a8:08:44:b3 root@node1.test.com The key's randomart image is: +--[ RSA 2048]----+ | o.o . | | . + . = . | | E o o = . + +| | . + + + *.| | . S . . o | | . . + | | . . | | | | | +-----------------+ [root@node1 ~]# ssh-copy-id 192.168.2.217 The authenticity of host '192.168.2.217 (192.168.2.217)' can't be established. RSA key fingerprint is bc:71:16:97:d8:ce:a0:3e:8b:4a:b9:da:ca:bd:dd:07. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.2.217' (RSA) to the list of known hosts. root@192.168.2.217's password: Now try logging into the machine, with "ssh '192.168.2.217'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@node1 ~]# ssh root@192.168.2.217 Last login: Sun Aug 14 00:24:50 2016 from 192.168.2.106 [root@node2 ~]# 四、安裝heartbeat [root@node1 ~]# wget http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@node1 ~]# rpm -ivh epel-release-6-8.noarch.rpm warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY Preparing... ########################################### [100%] 1:epel-release ########################################### [100%] #直接安裝rpm heartbeat會報依賴錯誤,這里我們先解決依賴關系 [root@node1 ~]#yum -y install perl-TimeDate net-snmp-libs libnet PyXML #再安裝就可以了 [root@node1 heartbeat]# rpm -ivh *.rpm --nodeps --force warning: heartbeat-2.1.4-11.el5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 217521f6: NOKEY warning: heartbeat-pils-2.1.4-9.el5.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 192a7d7d: NOKEY Preparing... ########################################### [100%] 1:heartbeat-pils ########################################### [ 25%] 2:heartbeat-stonith ########################################### [ 50%] 3:heartbeat warning: /etc/ha.d/shellfuncs created as /etc/ha.d/shellfuncs.rpmnew ########################################### [ 75%] 4:heartbeat-devel ########################################### [100%] [root@node1 heartbeat]# ll /etc/ha.d/ total 24 -rwxr-xr-x 1 root root 745 Mar 21 2010 harc drwxr-xr-x 2 root root 4096 Aug 14 17:14 rc.d -rw-r--r-- 1 root root 692 Mar 21 2010 README.config drwxr-xr-x 2 root root 4096 Aug 14 17:14 resource.d -rw-r--r-- 1 root root 7864 Mar 21 2010 shellfuncs [root@node1 ha.d]# cp -r /usr/share/doc/heartbeat-2.1.4/ /etc/ha.d/ [root@node1 ha.d]# ll total 28 -rwxr-xr-x 1 root root 745 Mar 21 2010 harc drwxr-xr-x 2 root root 4096 Aug 14 04:24 heartbeat-2.1.4 drwxr-xr-x 2 root root 4096 Aug 14 04:20 rc.d -rw-r--r-- 1 root root 692 Mar 21 2010 README.config drwxr-xr-x 2 root root 4096 Aug 14 04:20 resource.d -rw-r--r-- 1 root root 7864 Mar 21 2010 shellfuncs [root@node1 ha.d]# cp ./heartbeat-2.1.4/{ha.cf,authkeys,haresources} ./ [root@node1 ha.d]# chmod 600 authkeys /etc/ha.d目錄下: #ha.cf :主配置文件,定義各節點上的heartbeat HA集群的基本屬性; #authkeys :集群內節點間彼此傳遞消息時使用加密算法及密鑰; #haresources :為heartbeat v1的提供資源管理器配置接口: v1版本專用的配置接口 [root@node1 ha.d]# ll total 52 -rw------- 1 root root 666 Aug 14 04:27 authkeys -rw-r--r-- 1 root root 10770 Aug 14 04:29 ha.cf -rwxr-xr-x 1 root root 745 Mar 21 2010 harc -rw-r--r-- 1 root root 5970 Aug 14 04:54 haresources drwxr-xr-x 2 root root 4096 Aug 14 04:24 heartbeat-2.1.4 drwxr-xr-x 2 root root 4096 Aug 14 22:39 rc.d -rw-r--r-- 1 root root 692 Mar 21 2010 README.config drwxr-xr-x 2 root root 4096 Aug 14 22:39 resource.d -rw-r--r-- 1 root root 7864 Mar 21 2010 shellfuncs 這里要注意的是:如何在網絡中我們期望的節點集群成為我們所需要的節點,在集群中信息不能隨便傳遞,而心跳節點是基于組播地址傳遞的,如果別人也裝了heartbeat也連接到這個組播地址上來,這都不安全,基于這種情況,我們各節點這間信息傳遞是需要認證的,這種認證基于HMAC 配置heartbeat,它的配置文件在/etc/ha.d/的目錄下,但是安裝完程序之后這個目錄下沒有這個配置文件,只有/usr/share/doc/heartbeat-2.1.4/目錄下有ha.cf的主配置文件樣本,復制到/etc下修改配置文件即可使用;還有一個authkeys的認證文件,這個文件就是我們各節點認證時所保存的認證密碼和認證機制,所以這個文件的權限至關重要,必須是600,否則啟動不了服務;第三個haresources,定義資源時需要資源管理器來讀取這個文件,所以這個也得有; [root@node1 ha.d]# vim authkeys auth 2 2 sha1 be45617e827e7a7d [root@node1 ha.d]# vim ha.cf debugfile /var/log/ha-debug keepalive 2 deadtime 30 warntime 10 udpport 694 mcast eth0 225.10.0.1 694 1 0 auto_failback on node node1.test.com node node2.test.com crm on ping 192.168.2.217 compression bz2 compression_threshold 2 #logfile /var/log/ha-log #日志文件,正常日志信息記錄到哪去的 #keepalive 2 #每隔2s發送一次心跳信息的,單位是秒,毫秒用ms #deadtime 30 #隔多長時間探測到對方不在線就kill掉的時間間隔 #warntime 10 #警告時間 #udpport 694 #mcast eth0 225.10.0.1 694 1 0 #定義組播地址 #auto_failback on #開啟故障轉回功能 #node node1.test.com #定義兩個節點 #node node2.test.com #crm on #啟用crm功能 #ping 192.168.2.218 #ping節點 #compression bz2 #壓縮格式 #compression_threshold 2 #表示小于2K時不壓縮傳輸 [root@node1 ha.d]# scp -p ha.cf haresources authkeys node2:/etc/ha.d/ The authenticity of host 'node2 (192.168.2.217)' can't be established. RSA key fingerprint is bc:71:16:97:d8:ce:a0:3e:8b:4a:b9:da:ca:bd:dd:07. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'node2' (RSA) to the list of known hosts. ha.cf 100% 11KB 10.5KB/s 00:00 haresources 100% 5970 5.8KB/s 00:00 authkeys 100% 666 0.7KB/s 00:00 [root@node1 ha.d]# node1.test.com 192.168.2.218/24/eth0/192.168.255.255 httpd [root@node1 ha.d]# service heartbeat start [root@node1 ha.d]# service heartbeat start logd is already running Starting High-Availability services: Done. [root@node1 ~]# Message from syslogd@node1 at Aug 15 01:32:04 ... heartbeat: [1439]: EMERG: Rebooting system. Reason: /usr/lib64/heartbeat/cibatu Connection closed by foreign host. 網上有說改hearbeat文件的用戶和組,沒用~~,后注釋ha.cf的#crm on 恢復。 [root@node1 ha.d]# service heartbeat restart Stopping High-Availability services: Done. Waiting to allow resource takeover to complete: Done. Starting High-Availability services: 2016/08/14_18:08:55 INFO: Resource is stopped Done. 檢測運行狀態,所有資源在node1上 ,node2 上只有heartbeat服務 [root@node1 ha.d]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:46:B1:B7 inet addr:192.168.2.216 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe46:b1b7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2522 errors:0 dropped:0 overruns:0 frame:0 TX packets:4289 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:320135 (312.6 KiB) TX bytes:2902096 (2.7 MiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:46:B1:B7 inet addr:192.168.2.218 Bcast:10.10.10.254 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@node1 ha.d]# ps -ef |grep httpd root 2635 1 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2640 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2641 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2642 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2643 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2644 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2645 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2646 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd apache 2647 2635 0 18:10 ? 00:00:00 /usr/sbin/httpd root 2900 1592 0 18:12 pts/0 00:00:00 grep httpd [root@node1 ha.d]# pa aux |grep heartbeat -bash: pa: command not found [root@node1 ha.d]# ps aux |grep heartbeat root 2338 0.0 1.4 69608 14364 ? SLs 18:10 0:00 heartbeat: master control process nobody 2341 0.0 0.7 62876 7632 ? SL 18:10 0:00 heartbeat: FIFO reader nobody 2342 0.0 0.7 63004 7760 ? SL 18:10 0:00 heartbeat: write: mcast eth0 nobody 2343 0.0 0.7 63004 7760 ? SL 18:10 0:00 heartbeat: read: mcast eth0 nobody 2344 0.0 0.7 63004 7760 ? SL 18:10 0:00 heartbeat: write: ping 192.168.2.217 nobody 2345 0.0 0.7 63004 7760 ? SL 18:10 0:00 heartbeat: read: ping 192.168.2.217 root 2904 0.0 0.0 103312 876 pts/0 S+ 18:13 0:00 grep heartbeat [root@node2 ha.d]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:2D:5B:EC inet addr:192.168.2.217 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2d:5bec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11766 errors:0 dropped:0 overruns:0 frame:0 TX packets:5494 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3603815 (3.4 MiB) TX bytes:837732 (818.0 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:355 errors:0 dropped:0 overruns:0 frame:0 TX packets:355 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:49398 (48.2 KiB) TX bytes:49398 (48.2 KiB) [root@node2 ha.d]# netstat -nlptu|grep 80 tcp 0 0 :::80 :::* LISTEN 3198/httpd [root@node2 ha.d]# ps -ef |grep httpd root 3198 1 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3200 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3201 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3202 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3203 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3204 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3205 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3206 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd apache 3207 3198 0 18:11 ? 00:00:00 /usr/sbin/httpd root 3217 1811 0 18:14 pts/0 00:00:00 grep httpd [root@node2 ha.d]# [root@node2 ha.d]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:2D:5B:EC inet addr:192.168.2.217 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2d:5bec/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:12689 errors:0 dropped:0 overruns:0 frame:0 TX packets:6338 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3701251 (3.5 MiB) TX bytes:980188 (957.2 KiB) eth0:0 Link encap:Ethernet HWaddr 00:0C:29:2D:5B:EC inet addr:192.168.2.218 Bcast:192.168.2.254 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:413 errors:0 dropped:0 overruns:0 frame:0 TX packets:413 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:57576 (56.2 KiB) TX bytes:57576 (56.2 KiB) [root@node2 ha.d]# ps -ef |grep httpd root 4452 1 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4454 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4455 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4456 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4457 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4458 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4459 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4460 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd apache 4461 4452 0 18:21 ? 00:00:00 /usr/sbin/httpd root 4468 1811 0 18:21 pts/0 00:00:00 grep httpd [root@node2 ha.d]# ps -ef |grep heartbeat root 4022 1 0 18:20 ? 00:00:00 heartbeat: master control process nobody 4024 4022 0 18:20 ? 00:00:00 heartbeat: FIFO reader nobody 4025 4022 0 18:20 ? 00:00:00 heartbeat: write: mcast eth0 nobody 4026 4022 0 18:20 ? 00:00:00 heartbeat: read: mcast eth0 nobody 4027 4022 0 18:20 ? 00:00:00 heartbeat: write: ping 192.168.2.216 nobody 4028 4022 0 18:20 ? 00:00:00 heartbeat: read: ping 192.168.2.216 root 4470 1811 0 18:21 pts/0 00:00:00 grep heartbeat [root@node1 ha.d]# tail -f /var/log/ha-debug heartbeat[2338]: 2016/08/14_18:16:23 info: killing HBWRITE process 2342 with signal 15 heartbeat[2338]: 2016/08/14_18:16:23 info: killing HBREAD process 2343 with signal 15 heartbeat[2338]: 2016/08/14_18:16:23 info: killing HBWRITE process 2344 with signal 15 heartbeat[2338]: 2016/08/14_18:16:23 info: killing HBREAD process 2345 with signal 15 heartbeat[2338]: 2016/08/14_18:16:23 info: Core process 2342 exited. 5 remaining heartbeat[2338]: 2016/08/14_18:16:23 info: Core process 2341 exited. 4 remaining heartbeat[2338]: 2016/08/14_18:16:23 info: Core process 2345 exited. 3 remaining heartbeat[2338]: 2016/08/14_18:16:23 info: Core process 2344 exited. 2 remaining heartbeat[2338]: 2016/08/14_18:16:23 info: Core process 2343 exited. 1 remaining heartbeat[2338]: 2016/08/14_18:16:23 info: node1.test.com Heartbeat shutdown complete. [root@node2 ha.d]# tail -f /var/log/ha-debug IPaddr[4294]:2016/08/14_18:21:13 INFO: Success INFO: Success ResourceManager[4196]:2016/08/14_18:21:13 debug: /etc/ha.d/resource.d/IPaddr 192.168.2.218/24/eth0/192.168.2.254 start done. RC=0 ResourceManager[4196]:2016/08/14_18:21:13 info: Running /etc/init.d/httpd start ResourceManager[4196]:2016/08/14_18:21:13 debug: Starting /etc/init.d/httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using node2.test.com for ServerName [ OK ] ResourceManager[4196]:2016/08/14_18:21:13 debug: /etc/init.d/httpd start done. RC=0 heartbeat[4022]: 2016/08/14_18:21:23 info: Local Resource acquisition completed. (none) heartbeat[4022]: 2016/08/14_18:21:23 info: local resource transition completed. 安裝 heartbeat CRM LAMP WordPress [root@node1 ha.d]# yum install mysql-server php php-mysql -y [root@node1 ha.d]# vim /var/www/html/info.php [root@node1 ha.d]# service httpd restart Stopping httpd: [ OK ] Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using node1.test.com for ServerName [ OK ] 打卡node1節點httpd,已經配置好php了 [root@node1 ha.d]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> nfs服務器上: [root@php-server ~]# mkdir /data/mysql node1上: [root@node1 ha.d]# more /etc/my.cnf [mysqld] datadir=/data/ mount -t nfs 192.168.2.216:/data/mysql /data /usr/bin/mysql_install_db --user=mysql --datadir=/data/ socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [root@node1 ha.d]# service mysqld start Starting mysqld: [ OK ] [root@node1 ha.d]#mysql mysql> CREATE DATABASE `wordpress` CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | wordpress | +--------------------+ 4 rows in set (0.00 sec) 安裝 heartbeat-gui [root@node1 ~]# yum -y install pygtk2-libglade xorg-x11-xauth [root@node1 ~]# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm [root@node1 ~]# echo "password" | passwd --stdin hacluster
原創文章,作者:oranix,如若轉載,請注明出處:http://www.www58058.com/76632