目錄
實驗環(huán)境及要求
實驗一
1.1:準備NFS
1.2:準備Tengine
1.2.1:編譯安裝Tengine
1.2.2:掛載NFS
1.2.3:配置Tengine站點
1.2.4:客戶端訪問測試
實驗二
2.1:準備Haproxy
2.1.1:編譯安裝Hapoxy
2.1.2:根據(jù)代理轉(zhuǎn)發(fā)要求讶请,定義Haproxy配置文件
2.1.3:啟動Haproxy
2.2:準備測試靜態(tài)資源
2.3:客戶端訪問測試
實驗環(huán)境及要求
實驗一:
- 192.168.43.73和192.168.43.74編譯安裝Tengine夺溢,均可以代理站點api.yqc.com:9001;
- 192.168.43.75配置NFS存儲风响,供73和74共享靜態(tài)資源。
實驗二:
- 192.168.43.219為Haproxy状勤,為后端兩臺Tengine提供七層代理(api.yqc.com:9001);
- 192.168.43.73和192.168.43.74為Tengine服務器持搜,經(jīng)Haproxy代理轉(zhuǎn)發(fā)后焙矛,73負責響應路徑為/a的請求,74負責響應路徑為/b的請求贫导。
實驗一
1.1:準備NFS
在192168.43.75上安裝nfs抛猫,并進行相應配置:
~]# yum install nfs-utils -y
~]# mkdir /data/tengine -p
~]# vim /etc/exports
/data/tengine *(rw,no_root_squash)
~]# systemctl start nfs
~]# systemctl enable nfs
1.2:準備Tengine
1.2.1:編譯安裝Tengine
tengine是基于nginx開發(fā)的孩灯,官網(wǎng)下載地址:http://tengine.taobao.org/download.html
這里使用Tengine-2.3.0峰档,在192.168.43.73和74上分別編譯安裝Tengine:
執(zhí)行下方命令準備編譯基礎環(huán)境:
~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
下載解壓tengine:
~]# cd /usr/local/src
src]# wget http://tengine.taobao.org/download/tengine-2.3.0.tar.gz
src]# tar zxvf tengine-2.3.0.tar.gz
src]# cd tengine-2.3.0/
進行編譯安裝:
#進行編譯
tengine-2.3.0]# ./configure --prefix=/usr/local/tengine --user=tengine --group=tengine --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-http_perl_module
#安裝
tengine-2.3.0]# make && make install
添加tengine用戶:
~]# useradd tengine -s /sbin/nologin -u 2001
創(chuàng)建nginx命令軟鏈接:
~]# ln -sv /usr/local/tengine/sbin/nginx /usr/bin/nginx
驗證:
~]# nginx -v
Tengine version: Tengine/2.3.0 (nginx/1.15.9)
~]# nginx
~]# ps -ef | grep nginx
root 19949 1 0 12:10 ? 00:00:00 nginx: master process nginx
tengine 19950 19949 0 12:10 ? 00:00:00 nginx: worker process
~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 19949 root 6u IPv4 58815 0t0 TCP *:http (LISTEN)
nginx 19950 tengine 6u IPv4 58815 0t0 TCP *:http (LISTEN)
1.2.2:掛載NFS
為兩臺Tengine服務器分別安裝nfs面哥,用于掛載NFS存儲:
~]# yum install nfs-utils -y
~]# showmount -e 192.168.43.75
Export list for 192.168.43.75:
/data/tengine *
掛載NFS存儲:
~]# mkdir /data/tengine -p
~]# mount -t nfs 192.168.43.75:/data/tengine/ /data/tengine/
~]# df -TH
192.168.43.75:/data/tengine nfs4 52G 1.4G 50G 3% /data/tengine
配置開機掛載:
~]# vim /etc/fstab
192.168.43.75:/data/tengine /data/tengine nfs defaults,_netdev 0 0
#加上_netdev參數(shù)后,啟動系統(tǒng)掛載分區(qū)時尚卫,會有一個超時時間,避免了無法訪問網(wǎng)絡存儲而一直卡在掛載那里無法啟動系統(tǒng)吱涉。
驗證掛載參數(shù):
~]# umount /data/tengine
~]# mount -a
~]# df -TH
192.168.43.75:/data/tengine nfs4 52G 1.4G 50G 3% /data/tengine
1.2.3:配置Tengine站點
此為模仿api調(diào)用,這里配置tengine監(jiān)聽在9001端口特石,server_name為api.yqc.com鳖链。
在兩臺Tengine上都進行如下配置:
~]# cd /usr/local/tengine/
tengine]# mkdir conf.d
tengine]# vim conf/nginx.conf
……
http {
include /usr/local/tengine/conf.d/*.conf;
……
}
tengine]# vim conf.d/api_yqc.conf
server {
listen 9001;
server_name api.yqc.com;
location / {
root /data/tengine;
}
}
~]# nginx -t
~]# nginx -s reload
~]# vim /data/tengine/index.html
page api.yqc.com:9001
1.2.4:客戶端訪問測試
~]# vim /etc/hosts
192.168.43.73 api.yqc.com
~]# curl api.yqc.com:9001
page api.yqc.com:9001
~]# vim /etc/hosts
192.168.43.74 api.yqc.com
~]# curl api.yqc.com:9001
page api.yqc.com:9001
實驗二
2.1:準備Haproxy
2.1.1:編譯安裝Hapoxy
192.168.43.219上操作
安裝基礎依賴包:
~]# yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y
編譯安裝haproxy-1.8.20:
~]# cd /usr/local/src/
src]# ll haproxy-1.8.20.tar.gz
-rw-r--r--. 1 root root 2083917 Jun 17 17:00 haproxy-1.8.20.tar.gz
src]# tar zxvf haproxy-1.8.20.tar.gz
src]# cd haproxy-1.8.20/
#編譯
haproxy-1.8.20]# make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
#USE_CPU_AFFINITY=1為開啟haproxy進程與CPU核心綁定
#USE_SYSTEMD=1為支持使用-Ws參數(shù)(systemd-aware master-worker 模式)啟動Haproxy,從而實現(xiàn)單主進程多子進程運行模式逞敷。
#安裝
haproxy-1.8.20]# make install PREFIX=/usr/local/haproxy
haproxy-1.8.20]# cp haproxy /usr/sbin/
準備啟動腳本文件:
~]# vim /lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
驗證版本:
~]# haproxy -v
HA-Proxy version 1.8.20 2019/04/25
Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>
2.1.2:根據(jù)代理轉(zhuǎn)發(fā)要求灌侣,定義Haproxy配置文件
實驗要求192.168.43.73負責響應路徑為/a的請求,192.168.43.74負責響應路徑為/b的請求牛柒,這里通過Haproxy的ACL中的path_beg,來匹配請求URL的開頭皮壁,實現(xiàn)不同路徑的請求調(diào)度符喝。
~]# mkdir /etc/haproxy/conf -p
~]# vim /etc/haproxy/haproxy.cfg
global
maxconn 100000
chroot /usr/local/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
pidfile /usr/local/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen api
bind 192.168.43.219:9001
mode http
acl a_request path_beg -i /a
use_backend a_server if a_request
acl b_request path_beg -i /b
use_backend b_server if b_request
backend a_server
server a1 192.168.43.73:9001 weight 1 check port 9001 inter 3s fall 3 rise 5
backend b_server
server b1 192.168.43.74:9001 weight 1 check port 9001 inter 3s fall 3 rise 5
2.1.3:啟動Haproxy
~]# systemctl daemon-reload
~]# systemctl start haproxy
~]# systemctl enable haproxy
~]# ps -ef | grep haproxy
root 37810 1 0 17:10 ? 00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
nobody 37813 37810 0 17:10 ? 00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
nobody 37814 37810 0 17:10 ? 00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf -p /run/haproxy.pid
2.2:準備測試靜態(tài)資源
準備/a和/b路徑的靜態(tài)資源,以驗證配置結(jié)果(因為NFS是共享的畏腕,無法從訪問結(jié)果上直觀看出到底是哪臺Tengine響應了請求,所以這里先把NFS的分區(qū)卸載描馅,在兩臺Tengine本地分別創(chuàng)建/a和/b的訪問頁面):
192.168.43.73:
~]# umount /data/tengine
~]# mkdir /data/tengine/a
~]# vim /data/tengine/a/index.html
a page(192.168.43.73)
192.168.43.74:
~]# umount /data/tengine
~]# mkdir /data/tengine/b
~]# vim /data/tengine/b/index.html
b page(192.168.43.74)
2.3:客戶端訪問測試
~]# vim /etc/hosts
192.168.43.219 api.yqc.com
~]# curl api.yqc.com:9001/a/index.html
a page(192.168.43.73)
~]# curl api.yqc.com:9001/b/index.html
b page(192.168.43.74)