1妇蛀、LVS實(shí)現(xiàn)nat,dr
LVS實(shí)現(xiàn)NAT:
lvs服務(wù)器两疚,搭建兩個(gè)網(wǎng)卡,設(shè)定VIP和DIP,并且開啟路由轉(zhuǎn)發(fā)功能
sysctl -w net.ipv4.ip_forward=1
在后端服務(wù)器搭建http服務(wù)器:兩臺(tái)服務(wù)器的網(wǎng)關(guān)指向DIP
第一臺(tái):
yum install httpd
vim /var/www/html/index.html
<h1>server1</h1>
systemctl start httpd
第二臺(tái):
yum install httpd
vim /var/www/html/index.html
<h1>server2</h1>
systemctl start httpd
在LVS服務(wù)器安裝ipvsadm設(shè)定規(guī)則
yum install ipvsadm
ipvsadm -A -t 192.168.0.114:80 -s wrr
ipvsadm -a -t 192.168.0.114:80 -r 192.168.174.129 -m -w 2
ipvsadm -a -t 192.168.0.114:80 -r 192.168.174.130 -m -w 3
[root@localhost ~]# 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.0.114:80 wrr
? -> 192.168.174.129:80? ? ? ? ? Masq? ? 2? ? ? 0? ? ? ? ? 0? ? ? ?
? -> 192.168.174.130:80? ? ? ? ? Masq? ? 3? ? ? 0? ? ? ? ? 0? ?
在客服端測(cè)試
for i in {1..10}; do curl http://192.168.0.114/index.html; done
LVS實(shí)現(xiàn)DR:
在后端服務(wù)器搭建http服務(wù)器?
第一臺(tái):
yum install httpd
vim /var/www/html/index.html
<h1>server1</h1>
systemctl start httpd
vim setparam.sh? ? ? ? ? ? ? ? ? ##或者換個(gè)腳本
#!/bin/bash
vip='192.168.0.200'
mask='255.255.255.255'
iface='lo:0'
case $1 in
start)
? ? ? ? echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
? ? ? ? echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
? ? ? ? echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
? ? ? ? echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
? ? ? ? ifconfig $iface $vip netmask $mask broadcast $vip up
? ? ? ? route add -host $vip dev $iface
? ? ? ? ;;
stop)
? ? ? ? ifconfig $iface down
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
? ? ? ? ;;
*)
? ? ? ? echo "Usage: $(basename $0) start|stop"
? ? ? ? exit 1
? ? ? ? ;;
esac
bash?setparam.sh start?
第二臺(tái):
yum install httpd
vim /var/www/html/index.html
<h1>server2</h1>
systemctl start httpd
vim setparam.sh? ? ? ? ? ? ? ? ? ##或者換個(gè)腳本
#!/bin/bash
vip='192.168.0.200'
mask='255.255.255.255'
iface='lo:0'
case $1 in
start)
? ? ? ? echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
? ? ? ? echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
? ? ? ? echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
? ? ? ? echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
? ? ? ? ifconfig $iface $vip netmask $mask broadcast $vip up
? ? ? ? route add -host $vip dev $iface
? ? ? ? ;;
stop)
? ? ? ? ifconfig $iface down
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
? ? ? ? echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
? ? ? ? ;;
*)
? ? ? ? echo "Usage: $(basename $0) start|stop"
? ? ? ? exit 1
? ? ? ? ;;
esac
bash?setparam.sh start?
在LVS服務(wù)器安裝ipvsadm設(shè)定規(guī)則
yum install ipvsadm
ifconfig ens33:0 192.168.0.200 netmask 255.255.255.255 broadcast 192.168.0.200 up
[root@localhost ~]# ipvsadm -A -t 192.168.0.200:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.117 -g
[root@localhost ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.108 -g
[root@localhost ~]# 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.0.200:80 rr
? -> 192.168.0.108:80? ? ? ? ? ? Route? 1? ? ? 0? ? ? ? ? 0? ? ? ?
? -> 192.168.0.117:80? ? ? ? ? ? Route? 1? ? ? 0? ? ? ? ? 0? ? ? ?
在客服端測(cè)試
for i in {1..10}; do curl http://192.168.0.114/index.html; done
2钦无、nginx反向代理否纬,虛擬主機(jī)
反向代理:
在后端服務(wù)器搭建http服務(wù)器?
yum install httpd
vim /var/www/html/index.html
<h1>server1</h1>
systemctl start httpd
在前端搭建反代服務(wù)器
yum install nginx?
vim /etc/nginx/conf.d/default.conf
server {
? ? listen? ? ? 80;
? ? server_name? localhost;
location / {
? ? ? ? root? /usr/share/nginx/html;
? ? ? ? index? index.html index.htm;
? ? ? ? proxy_pass http://192.168.0.108:80;
? ? }
}
systemctl start nginx
在客戶端測(cè)試
curl 192.168.0.114?
實(shí)現(xiàn)后端兩臺(tái)服務(wù)器動(dòng)靜分離:
配置server1為后端靜態(tài)web服務(wù)器
yum install -y nginx
mkdir -pv /data/nginx/html? ? ? ? ? ? ? ??? ##創(chuàng)建nginx主頁目錄
mv phpMyAdmin-4.0.10.20-all-languages /data/nginx/html/
cd /data/nginx/html/? ? ? ? ? ? ? ? ? ? ? ? ??
ln -sv phpMyAdmin-4.0.10.20-all-languages pma
vim /etc/nginx/nginx.conf? ? ? ? ? ? ? ? ? ?##編輯nginx配置文件
server{
? ? ?listen? ? 80 ;
? ? ?server_name? ?192.168.45.60;
? ? ?root? ? /data/nginx/html;
nginx -t
systemctl start nginx
配置server2為后端動(dòng)態(tài)web服務(wù)器
yum install -y php-fpm php-mysql php-mbstring php-mcrypt mariadb-server httpd
vim /etc/php-fpm.d/www.conf? ? ? ? ? ? ? ? ##編輯php-fpm配置文件
listen = 0.0.0.0:9000
;listen.allowed_clients = 127.0.0.1
pm.max_children = 150
pm.status_path = /status
ping.path = /ping
mkdir /var/lib/php/session? ? ? ? ? ? ? ? ? ? ? ? ?##創(chuàng)建session目錄
chown apache:apache /var/lib/php/session/? ? ? ? ? ? ##授權(quán)給apache用戶
systemctl start php-fpm.service
systemctl start httpd.service
systemctl start mariadb.service
mkdir /data/apps -pv
vim /data/apps/index.php? ? ? ? ? ? ? ? ? ? ? 編輯php主頁,驗(yàn)證php是否安裝成功
<?php
? ? ? ?phpinfo();
?>
vim /etc/my.cnf? ? ? ? ? ? ? ? ? ? ? ? ? ? ?##編輯mysql配置文件
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
systemctl start mariadb
mysql_secure_installation? ? ? ? ? ? ? ? ? ? ? ? ?##mysql加固
unzip phpMyAdmin-4.0.10.20-all-languages.zip
cp config.sample.inc.php config.inc.php
vim config.inc.php
配置前端服務(wù)器
yum install nginx
vim /etc/nginx/conf.d/nginx.conf? ? ? ? ? ? ? ? ? ? ?##編輯nginx配置
http {
? ? ?fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m? ? ? ? ? ? ? ? ? ? ? ?max_size=2g;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?##定義緩存? ?
? ? server {
? ? ? ? listen 80;
? ? ? ? server_name 192.168.45.57;
? ? ? ? index index.php index.html;
? ? ? ? location / {
? ? ? ? ? ? ? ? root /data/nginx/html;
? ? ? ? ? ? ? ? proxy_pass http://192.168.45.60:80;
? ? ? ? }
? ? ? ? location ~* \.php$ {
? ? ? ? ? ? ? ? fastcgi_pass 192.168.45.59:9000;? ? ? ? ##代理后端動(dòng)態(tài)php主機(jī)地址
? ? ? ? ? ? ? ? fastcgi_index index.php;? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? fastcgi_param SCRIPT_FILENAME? ? /data/apps/$fastcgi_script_name;
? ? ? ? ? ? ? ? include fastcgi_params;
? ? ? ? ? ? ? ? fastcgi_keep_conn? ? on;
? ? ? ? ? ? ? ? fastcgi_cache fcache;? ? ? ? ? ? ? ? ? ? ? ? ? ?##調(diào)用緩存及設(shè)置參數(shù)
? ? ? ? ? ? ? ? fastcgi_cache_key $request_uri;
? ? ? ? ? ? ? ? fastcgi_cache_valid 200 302? 10m;
? ? ? ? ? ? ? ? fastcgi_cache_valid 301? ? ? 1h;
? ? ? ? ? ? ? ? fastcgi_cache_valid any? ? ? 1m;
? ? ? ? }
? ? ? ? location ~* ^/(status|ping)$ {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? include? ? fastcgi_params;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fastcgi_pass? 192.168.45.59:9000;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
? ? ? ? }
? ? }
}
nginx -t
nginx-s reload
驗(yàn)證:
http://192.168.45.57/pma/index.php
實(shí)現(xiàn)upstream負(fù)載均衡
第一臺(tái)http服務(wù)器:
yum install httpd
vim /var/www/html/index.html
<h1>server1</h1>
sysctemctl stop firewalld
systemctl start httpd
第二臺(tái)http服務(wù)器:
yum install httpd
vim /var/www/html/index.html
<h1>server2</h1>
sysctemctl stop firewalld
systemctl start httpd
nginx服務(wù)器:
yum install nginx?
vim /etc/nginx/conf.d/nginx.conf
http {
? ? upstream websrvs {
? ? ? ? server 192.168.0.108;
? ? ? ? server 192.168.0.118;
? ? }
? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? 192.168.0.121;
? ? ? ? root /data/nginx/html;
? ? ? ? location / {
? ? ? ? ? ? index? index.html index.htm index.php;
? ? ? ? ? ? proxy_pass http://websrvs;
? ? ? ? ?}
? ? ?}
}
nginx -t
systemctl start nginx?
nginx -s reload
測(cè)試:
curl 192.168.0.121