1. 程式人生 > >linux 集群及lvs

linux 集群及lvs

查看數據庫 eas install director form 虛擬 localhost remote ech

集群及LVS

集群:
一組通過高速網絡互聯的計算機組,並以單一系統的模式加以管理
價格很多服務器集中起來,提供同一種服務,在客戶端看起來就像只有一個服務器
可以在付出較低成本的情況下獲得在性能,可靠性,靈活性方面的相對較高的收益
任務調度室集群系統中的核心技術

集群目的:
提高性能:如計算密集型應用,如:天氣預報,核試驗模擬
降低成本:相對於百萬美元級的超級計算機,價格便宜
提高可擴展性:只要增加集群節點即可
增強可靠性:多個節點完成相同功能,避免單點失敗

集群分類:
高性能計算集群HPC:通過以集群開發的並行應用程序,解決復雜的科學問題
負載均衡(LB)集群:客戶端負載在計算機集群中盡可能平均分攤
高可用(HA)集群:避免單點故障,當一個系統發生故障時,可以快速遷移

實例:準備LAMP分離結構
環境:準備兩臺web服務器和一臺數據庫服務器
mysql:192.168.4.1
web1:192.168.4.2
web2:192.168.4.3

mysql端
[[email protected] ~]# yum -y install mysql-server
[[email protected] ~]# /etc/init.d/mysqld start ;chkconfig mysqld on
mysql> grant all on *.* to ‘admin‘@‘192.168.4.2‘ identified by ‘123456‘;
mysql> grant all on *.* to ‘admin‘@‘192.168.4.3‘ identified by ‘123456‘;


web端
[[email protected] ~]# yum -y install httpd php php-mysql
[[email protected] ~]# /etc/init.d/httpd start ;chkconfig httpd on
[[email protected] ~]# yum -y install httpd php php-mysql
[[email protected] ~]# /etc/init.d/httpd start ;chkconfig httpd on
在web1安裝Discuz
[[email protected] ~]# unzip Discuz_X3.2_SC_UTF8.zip
[[email protected] ~]# mv upload/ /var/www/html/bbs
[[email protected] ~]# chown -R apache config/ data/ uc_*
[[email protected] ~]# firefox http://192.168.4.2/bbs
數據庫服務器: 192.168.4.1(mysql服務器ip)
數據庫名:hydra(自帶的函數會自動創建次庫)
數據庫用戶名:admin(在mysql服務器授權的用戶)
數據庫密碼:123456
在mysql端查看數據庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hydra |
| mysql |
| test |
+--------------------+

在web端測試註冊一個叫hydra的用戶並發帖,
帖子主題為hydra 內容為 Anonymous!!!!!!

在mysq服務器查看用戶數據,查看發帖內容
mysql> select uid,username,password,email from pre_common_member;
+-----+----------+----------------------------------+----------------+
| uid | username | password | email |
+-----+----------+----------------------------------+----------------+
| 1 | admin | eab19088f6ff65eb97530281c7d49f3e | [email protected] |
| 2 | hydra | f0b02c4f4fe8adf540bf7ed8a7522d03 | [email protected] |
+-----+----------+----------------------------------+----------------+
mysql> select author,subject,message from pre_forum_post;
+--------+---------+-----------------------+
| author | subject | message |
+--------+---------+-----------------------+
| hydra | hydra | Anonymous!!!!!! |
+--------+---------+-----------------------+

在第二臺web2服務器
將第一臺的web1服務器的bbs項目打包拷貝到第二臺web1服務器即可
[[email protected] html]# tar -czf bbs.tar.gz bbs/
[[email protected] html]# scp bbs.tar.gz 192.168.4.3:/var/www/html/
[[email protected] html]# tar -xf bbs.tar.gz
[[email protected] html]# firefox http://192.168.4.3/bbs(在第二臺web服務器測試訪問,並註冊用戶測試)
在mysql服務器查看
mysql> select author,subject,message from pre_forum_post;
+-----------+----------+-----------------------+
| author | subject | message |
+-----------+----------+-----------------------+
| hydra | hydra | Anonymous!!!!!! |
| | | |
| Anonymous | ha ha ha | ha ha ha ha ha |
+-----------+----------+-----------------------+

——————————————————————————————————————————————————————————————————

LVS

LVS項目
linux虛擬服務器(lvs)由章文嵩博士創建
lvs可以實現高可用的,可伸縮的web,mail,cache和media等網絡服務
最終目標是利用linux操作系統和lvs集群軟件實現一個高可用,高性能,低成本的服務器應用集群

負載均衡集群LB
硬件:F5
軟件:lvs,haproxy

lvs集群組成
前端:負載均衡層,由一臺或多臺負載調度器構成
中間:服務器群組層,由一組實際運行應用服務器組成
底端:數據共享存儲層,提供共享存儲空間的存儲區域

lvs術語
Director Server:調度服務器,將負載分發到Real Server的服務器
Real Server:真實服務器,真正提供應用的服務器
VIP:虛擬ip地址,公布給用戶訪問的虛擬ip地址
RIP:真實ip地址,集群節點上使用的ip地址
DIP:調度器連接節點服務器的ip地址

負載均衡調度算法
lvs目前實現了10種調度算法
常用的調度算法有4種
輪詢
加權輪詢
最少連接
加權最少連接


實例:配置LVS NAT模式
環境:準備一臺lvs服務器,修改兩臺web服務器的網關,指向dir
打開調度器的路由轉發功能
eth0:192.168.4.4
eth2:201.1.1.4

web端
[[email protected] ~]# vim /etc/sysconfig/network
GATEWAY=192.168.4.4
[[email protected] ~]# vim /etc/sysconfig/network
GATEWAY=192.168.4.4
調度器端
[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/ip_forward(臨時生效)
[[email protected] ~]# vim /etc/sysctl.conf(永久生效)
net.ipv4.ip_forward = 1
[[email protected] ~]# sysctl -p(生效)
安裝lvs
[[email protected] ~]# vim /etc/yum.repos.d/rhel6.repo (需要添加yum源)
[rhel]
name=Linux NSD
baseurl=file:///root/myiso/LoadBalancer(需要加上這個路徑,yum默認找server這個庫)
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[[email protected] ~]# yum -y install ipvsadm(安裝lvs)

ipvsadm用法
ipvsadm -Ln:查看調度規則
創建虛擬服務器
-A:添加虛擬服務器
-t:設置群集地址(VIP,Virtual IP)
-s:指定負載調度算法
添加,刪除服務器節點
-a:添加真實服務器
-d:刪除真實服務器
-r:指定真實服務器(Real Server)的地址
-m:使用NAT模式,-g,-i分別對應DR,TUN模式
-w:為節點服務器設置權重,默認為1

[[email protected] ~]# ipvsadm -A -t 201.1.1.4:80 -s wrr(wrr加強輪詢算法)
[[email protected] ~]# ipvsadm -a -t 201.1.1.4:80 -r 192.168.4.2 -m -w 2
[[email protected] ~]# ipvsadm -a -t 201.1.1.4:80 -r 192.168.4.3 -m
[[email protected] ~]# firefox http://201.1.1.4/bbs(測試訪問)
[[email protected] ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 201.1.1.4:80 wrr
-> 192.168.4.2:80 Masq 2 0 0
-> 192.168.4.3:80 Masq 1 0 0
[[email protected] jiaobeng]# vim web_start.sh(寫出腳本,掃描服務器,如果有服務器出問題則把次服務器幹掉,沒問題則添加上,死循環 每5秒執行一次)
#!/bin/bash
VIP=201.1.1.4:80
RIP1=192.168.4.2
RIP2=192.168.4.3
while [ 1 ]
for ip in $RIP1 $RIP2
do
web_stat=$(nmap -n -sT $ip -p 80 2> /dev/unll | grep open)
ip_in_lvs=$(ipvsadm -Ln | grep $ip)
if [ -z "$web_stat" -a -n "$ip_in_lvs" ];then
ipvsadm -d -t $VIP -r $ip
elif [ -n "$web_stat" -a -z "$ip_in_lvs" ];then
ipvsadm -a -t $VIP -r $ip -m
fi
done
done
[[email protected] jiaobeng]# chmod +x web_start.sh (加執行權)
[[email protected] ~]# service ipvsadm save(存盤,不保存重啟服務後配置就沒有了)


實例:配置LVS DR模式
環境:先把NAT環境下的LVS連接外網的ip移除,清空lvs nat配置
在lvs上為物理網卡配置vip,
在兩臺real server上為lo配置vip
lvs端
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth2(把IPADDR=201.1.1.4刪除即可)
[[email protected] ~]# /etc/init.d/ipvsadm restart(清空規則)
[[email protected] ~]# ipvsadm -D -t 201.1.1.4:80(存盤後要這樣清空)
[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0(拷貝網卡)
[[email protected] network-scripts]# vim ifcfg-eth0:0
DEVICE=eth0:0(只修改這兩行,其他不變)
IPADDR=192.168.4.100
[[email protected] ~]# /etc/init.d/network restart
[[email protected] ~]# ifconfig
eth0:0 Link encap:Ethernet HWaddr 54:52:01:01:04:01
inet addr:192.168.4.100 Bcast:192.168.4.255 Mask:255.255.255.0
real server端(兩臺都做相同的操作)
[[email protected] network-scripts]# cp ifcfg-lo ifcfg-lo:0
[[email protected] network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.100
NETMASK=255.255.255.255
NETWORK=192.168.4.100
BROADCAST=192.168.4.100
ONBOOT=yes
[[email protected] network-scripts]# /etc/init.d/network restart
配置real server端內核參數(兩臺都做相同的操作)
[[email protected] ~]# sysctl -a | grep arp_i
[[email protected] ~]# echo ‘nat.ipv4.conf.all.arp_ignore = 1‘ >> /etc/sysctl.conf
[[email protected] ~]# echo ‘nat.ipv4.conf.lo.arp_ignore = 1‘ >> /etc/sysctl.conf
[[email protected] ~]# sysctl -a | grep arp_a
[[email protected] ~]# echo ‘net.ipv4.conf.all.arp_announce = 2‘ >> /etc/sysctl.conf
[[email protected] ~]# echo ‘net.ipv4.conf.lo.arp_announce = 2‘ >> /etc/sysctl.conf
[[email protected] ~]# sysctl -p(生效)
lvs端配置
[[email protected] ~]# ipvsadm -A -t 192.168.4.100:80 -s lc
[[email protected] ~]# ipvsadm -a -t 192.168.4.100:80 -r 192.168.4.2
[[email protected] ~]# ipvsadm -a -t 192.168.4.100:80 -r 192.168.4.3 -g -w 2(-g 路由模式 w 2權重為2)
[[email protected] ~]# ipvsadm -Ln(查看規則)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.4.100:80 lc
-> 192.168.4.2:80 Route 1 0 0
-> 192.168.4.3:80 Route 2 0 0
[[email protected] ~]# firefox http://192.168.4.100/bbs(測試訪問)

————————————————————————————————————————————————————————————————————————————————————————

linux 集群及lvs