LNMP+SSL+LVS-DR+ipvsadm+LVS Persistence實現同一種服務的不同端口定向到同一臺realserver
說明:本實驗是在VMware虛擬機中實現:
共需三個虛擬linux主機,三臺虛擬linux主機的網卡模式都爲橋接,
本文以web服務的http與https爲例
三臺虛擬主機分別爲:director,RS1,RS2,其中RS1和RS2爲realserver
director: dip eth0 172.16.22.1
vip eth0:1 172.16.22.2
RS1 rip eth0 172.16.22.10 網關:172.16.0.1
vip lo:0 172.16.22.2 子網掩碼:255.255.255.255
RS2 rip eth0 172.16.22.11 網關:172.16.0.1
vip lo:0 172.16.22.2 255.255.255.255
確保windows的防火牆以及虛擬機裏的linux防火牆完全關閉
圖示如下:
前提:RS1和RS2已經搭建好LNMP平臺,具體實施細節請看:
http://lihuan.blog.51cto.com/4391550/809395
這裏不再贅述。
實現過程:
1.在director上安裝ipvsadm,開啓轉發功能並配置好ip地址
- # grep –i “vs” /boot/config-2.8.18-164.e15
- # yum install ipvsadm –y
- # rpm –ql ipvsadm
- # sysctl –w net.ipv4.ip_forward=1
- # setenforce=0
- # ifconfig eth0 172.16.22.1 255.255.0.0
- # ifconfig eth0:1 172.16.22.2 broadcast 172.16.22.2 netmask 255.255.255.255 up
- # route –add –host 172.16.22.2 dev eth0:1
2.在RS1上配置IP與arp規則:
- # ifconfig eth0 172.16.22.10 255.255.0.0
- # ifconfig lo down
- # ifconfig lo up
- # echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
- # echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
- # echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
- # echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
- # ifconfig lo:0 172.16.22.2 broadcast 172.16.22.2 netmask 255.255.255.255 up
- # route add –host 172.16.22.2 dev lo:0
- #echo
- “rs1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111” > /var/www/html/index.html
- # setenforce=0
- # service httpd restart
- # elinks -dump http://172.16.22.10
此時說明本機可以正常訪問web服務了。
3.在RS2上配置IP與arp規則:
- # ifconfig eth0 172.16.22.11 255.255.0.0
- # ifconfig lo down
- # ifconfig lo up
- # echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
- # echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
- # echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
- # echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
- # ifconfig lo:0 172.16.22.2 broadcast 172.16.22.2 netmask 255.255.255.255 up
- # route add –host 172.16.22.2 dev lo:0
- #echo
- “rs2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222” > /var/www/html/index.html
- # setenforce=0
- # service httpd restart
- # elinks -dump http://172.16.22.11
此時說明本機可以正常訪問web服務了。
4.爲RS1和RS2準備證書文件與私鑰文件
(1).在RS1上:
# cd /etc/pki/tls
# vim openssl.cnf
[ CA_default ]
dir = ../../CA # Where everything is kept
把上面”=../../CA”改爲”=/etc/pki/CA”
- # cd ..
- # cd CA
- # mkdir certs crl newcerts
- # touch index.txt
- # echo 01 > serial
- # echo 01 > crlnumber
- # (umask 077;openssl genrsa 2048 > private/cakey.pem) #生成CA自己的私鑰
- # openssl req –new –x509 –key private/cakey.pem –out cacert.pem –days 3650
依次輸入”CN、Henan、ZZ、M、Tech 、 、 ” 這是CA爲自己簽發證書
- # mkdir /etc/nginx/ssl
- # cd /etc/nginx/ssl
- # (umask 077;openssl genrsa 1024 > nginx.key) #生成nginx自己的私鑰
- # openssl req –new –key nginx.key –out nginx.csr
依次輸入”CN、Henan、ZZ、M、Tech 、 、 ”,這是生成nginx的證書籤發請求
# openssl ca –in nginx.csr –out nginx.crt #CA爲nginx頒發證書
(2).在RS2上:
- # mkdir /etc/nginx/ssl
- # cd /etc/nginx/ssl
- # (umask 077;openssl genrsa 1024 > nginx.key) #生成nginx自己的私鑰
- # openssl req –new –key nginx.key –out nginx.csr
依次輸入”CN、Henan、ZZ、M、Tech 、 、 ”,這是生成nginx的證書籤發請求
# scp nginx.csr [email protected]:/root #此時需要輸入RS1的root用戶的密碼
把RS2上nginx的證書籤發請求拷貝到RS1上,由RS1的CA簽發證書
在RS1上:
# cd
# openssl ca –in nginx.csr –out nginx.crt #CA爲nginx頒發證書
# scp nginx.crt [email protected]:/etc/nginx/ssl #此時需要輸入RS2的root用戶的密碼
5.更改RS1和RS2的nginx配置文件,以支持https協議
在RS1上:
# vim /etc/nginx/nginx.conf
修改:
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}爲:
# HTTPS server
#
server {
listen 443;
server_name www.lihuan.com;
ssl on;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
說明:RS2上修改配置文件支持https協議的方法同RS1,這裏不再贅述。
6.在director上定義ipvsadm規則與持久防火牆標記,以實現同一個服務的不同端口定向到同一臺realserver中
# iptables –t mangle –A PREROUTING –i eth0 –p tcp –d 172.16.22.2 –dport 80 –j MARK –set-mark 20
# iptables –t mangle –A PREROUTING –i eth0 –p tcp –d 172.16.22.2 –dport 443 –j MARK –set-mark 20
# ipvsadm –A –f 20 –s wlc –p 2000
# ipvsadm –a –f 20 –r 172.16.22.10 –g –w 5
# ipvsadm –a –f 20 –r 172.16.22.11 –g –w 4
說明:上面幾條語句中綠色的字體的範圍可以是0-99的任意整數,此處選爲20,一旦選爲20,這幾條語句必須都是用20才能保證同一服務的不同端口定向到同一臺realserver中
7.測試
前提:把RS1上的CA證書導入到windows物理機的瀏覽器中。
在瀏覽器輸入:
http://172.16.22.2,出現:
此時發現http和https的結果是一樣的,說明同一服務的的不同端口定向到
同一臺realserver中(有可能是RS1或者RS2,這個是不確定的)