本文是生產(chǎn)環(huán)境中的一個(gè)案例,主要是為了通過反向代理實(shí)現(xiàn)多條線路容災(zāi)。在原有的基礎(chǔ)上升級(jí)了openssl,openssh,nginx,通過一些模塊來實(shí)現(xiàn)我們的需求。
正常情況下,反向代理會(huì)去請(qǐng)求online下面的主機(jī),使用sticky模塊實(shí)現(xiàn)session粘連。如果online的下面的主機(jī)全部掛掉了,這個(gè)時(shí)候,它會(huì)去出502報(bào)錯(cuò)(或404,具體與你的環(huán)境有關(guān)),此時(shí)它會(huì)啟用下failover下面的主機(jī),以實(shí)現(xiàn)線路容災(zāi)。具體的容災(zāi)方式,你可以選擇多個(gè)。比如使用backup來進(jìn)行標(biāo)識(shí)。使用了nginx_upstream_check_module進(jìn)行后端的主機(jī)健康檢查。
- 重新部署新應(yīng)用nginx
-
- 1、升級(jí)openssh
- 2、升級(jí)nginx
- 3、增加nignx模塊
-
- 本次升級(jí)主要是從容災(zāi)的角度和反向代理安全性的角度考慮.通過升級(jí)openssh,避免一些低版本漏洞。
- 添加nginx的一些常規(guī)支持:
- a、支持多個(gè)SSL證書
- b、支持反端http 健康檢查
- c、支持session 粘滯 nginx-sticky-module //支持sticky+rr ,sticky+weight
- d、通過nginx的權(quán)重+粘滯實(shí)現(xiàn)多線路容災(zāi)
- e、添加geoip模塊支持,未來考慮智能CDN+GeoIP配合(在nginx前端對(duì)來源IP,判斷從哪個(gè)機(jī)房取數(shù)據(jù))
-
-
- 安裝telnet服務(wù)器:
- #yum install -y telnet-server telnet
-
- 編譯托管的服務(wù)
-
- # chkconfig telnet on
- #
- # /etc/init.d/xinetd restart
- Stopping xinetd: [FAILED]
- Starting xinetd: [ OK ]
-
- # netstat -tnlp
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2632/sshd
- tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN 21977/xinetd
-
- //在防火墻中加入自己的IP允許23的規(guī)則
-
- 建立普通用戶進(jìn)行登錄
- # useradd sshinstall
- # echo "123456@sshinstall" | passwd --stdin sshinstall
- Changing password for user sshinstall.
- passwd: all authentication tokens updated successfully.
-
- 將該用戶加入到sudo組里
- echo "sshinstall ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
-
- 開始安裝包了
- # tar -xzf openssl-1.0.1c.tar.gz
- # cd openssl-1.0.1c
- # ./config enable-tl***t --prefix=/usr/local/openssl-1.0.0c
- # make
- # make test
- # make install
-
- # echo /usr/local/openssl-1.0.0c/lib/ >> /etc/ld.so.conf
- # ln -s /usr/local/openssl-1.0.0c/ /usr/local/openssl
-
- echo '
- PATH=/usr/local/openssl/bin:$PATH
- export PATH' >> /etc/profile
-
- # source /etc/profile
- # openssl version -a
- OpenSSL 1.0.1c 10 May 2012
- built on: Fri Jan 4 00:32:23 CST 2013
- platform: linux-x86_64
- options: bn(64,64) rc4(16x,int) des(idx,cisc,16,int) idea(int) blowfish(idx)
- compiler: gcc -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -Wa,--noexecstack -m64 -DL_ENDIAN -DTERMIO -O3 -Wall -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM
- OPENSSLDIR: "/usr/local/openssl-1.0.0c/ssl
-
- 開始刪除openssh
- # rpm -e openssh-server-4.3p2-41.el5 --nodeps
- # rpm -e openssh-4.3p2-41.el5 --nodeps
- # rpm -e openssh-askpass-4.3p2-41.el5 --nodeps
- # rpm -e openssh-clients-4.3p2-41.el5 --nodeps
-
- # rm -rf /etc/ssh/
-
- 開始安裝openssh
- # tar -xzf openssh-6.1p1.tar.gz
- # cd openssh-6.1p1
- # ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-pam --with-ssl-dir=/usr/local/openssl-1.0.0c --with-md5-passwords --mandir=/usr/share/man
- # make
- # make install
-
- 將sshd加入到服務(wù)列表里面去
-
- # cp ./contrib/redhat/sshd.init /etc/init.d/sshd
- # chmod u+x /etc/init.d/sshd
- # chkconfig --add sshd
- # chkconfig sshd on
- # service sshd start
- Starting sshd: OK ]
- # ssh -v
- OpenSSH_6.1p1, OpenSSL 1.0.1c 10 May 2012
-
- 關(guān)掉telnetserver,刪除sshinstall用戶
- # chkconfig telnet off
- # /etc/init.d/xinetd restart
- Stopping xinetd: [ OK ]
- Starting xinetd: [ OK ]
- # netstat -tnlp
- Active Internet connections (only servers)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 29602/sshd
-
- # userdel -r sshinstall
-
- 安裝Nginx
- # tar zxvf libunwind-0.99.tar.gz
- # cd libunwind-0.99/
- # CFLAGS=-fPIC ./configure && make CFLAGS=-fPIC
- # make CFLAGS=-fPIC install
-
- # tar xzf google-perftools-1.6.tar.gz
- # cd google-perftools-1.6
- # ./configure
- # make && make install
-
- # tar -xzf pcre-8.12.tar.gz
- # cd pcre-8.12
- # ./configure && make && make install
-
- 安裝geoip
- # wget http://geolite./download/geoip/api/c/GeoIP.tar.gz
- # tar -xzf GeoIP.tar.gz
- # cd GeoIP-1.4.8/
- # ./configure && make && make install
- # wget http://geolite./download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
- # gunzip GeoIP.dat.gz
- # echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
- # ldconfig
-
- 解壓各個(gè)模塊,在安裝nginx時(shí),加入該模塊
- # unzip nginx_upstream_jvm_route.zip //tomcat session
- Archive: nginx_upstream_jvm_route.zip
- creating: nginx-upstream-jvm-route/
- creating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/
- inflating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/CHANGES
- inflating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/config
- inflating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/jvm_route.patch //補(bǔ)丁文件,需要手工進(jìn)行執(zhí)行
- inflating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c
- inflating: nginx-upstream-jvm-route/nginx_upstream_jvm_route/README
-
- # unzip master.zip //nginx_upsteam check_module
- # tar -xzf nginx-sticky-module-1.1.tar.gz //session
- # tar -xzf nginx-1.2.6.tar.gz
- # cd nginx-1.2.6
- 開始打補(bǔ)丁了
- # patch -p0 < /root/upgrade/nginx-upstream-jvm-route/nginx_upstream_jvm_route/jvm_route.patch
- patching file src/http/ngx_http_upstream.c
- Hunk #1 succeeded at 4117 (offset 380 lines).
- Hunk #3 succeeded at 4249 (offset 380 lines).
- Hunk #5 succeeded at 4348 (offset 380 lines).
- patching file src/http/ngx_http_upstream.h
- Hunk #1 succeeded at 90 (offset 5 lines).
- Hunk #3 succeeded at 118 (offset 5 lines).
-
- # patch -p1 < /root/upgrade/nginx_upstream_check_module-master/check_1.2.6+.patch
- patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
- patching file src/http/modules/ngx_http_upstream_least_conn_module.c
- patching file src/http/ngx_http_upstream_round_robin.c
- patching file src/http/ngx_http_upstream_round_robin.h
-
- # ./configure --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_geoip_module --with-http_ssl_module --with-http_ssl_module --with-openssl=/root/upgrade/openssl-1.0.1c --with-pcre=/root/upgrade/pcre-8.12 --add-module=/root/upgrade/nginx-upstream-jvm-route/nginx_upstream_jvm_route/ --add-module=/root/upgrade/nginx_upstream_check_module-master/ --add-module=/root/upgrade/nginx-sticky-module-1.1/ --with-google_perftools_module
- # make && make install
-
- # /usr/local/nginx/sbin/nginx -v
- nginx version: nginx/1.2.6
-
- 如果你的nginx是正常運(yùn)行的,請(qǐng)對(duì)當(dāng)前nginx進(jìn)行在線升級(jí).
- # ps aux | grep master
- root 13589 0.0 0.0 26772 3884 ? S 2012 0:01 nginx: master process /usr/local/nginx/sbin/nginx
- root 20834 0.0 0.0 61140 768 pts/4 S+ 17:14 0:00 grep master
-
- 進(jìn)程替換
- # kill -USR2 13589
- # ps aux | grep master
- root 13589 0.0 0.0 26772 3884 ? S 2012 0:01 nginx: master process /usr/local/nginx/sbin/nginx
- root 21395 0.5 0.0 40272 3504 ? S 17:16 0:00 nginx: master process /usr/local/nginx/sbin/nginx
- root 21416 0.0 0.0 61140 768 pts/4 S+ 17:16 0:00 grep master
-
- # kill -WINCH 13589 //發(fā)送WINCH信號(hào)到舊的nginx主進(jìn)程以殺掉舊的nginx子進(jìn)程
- # kill -QUIT 13589 // 退出舊的nginx主進(jìn)程
- # ps aux |grep master
- root 21395 0.0 0.0 40272 3504 ? S 17:16 0:00 nginx: master process /usr/local/nginx/sbin/nginx
- root 21749 0.0 0.0 61140 772 pts/4 S+ 17:16 0:00 grep master
-
- 刪除老的版本
- # rm -rf /usr/local/nginx/sbin/nginx.old
- 查看當(dāng)前版本
- # /usr/local/nginx/sbin/nginx -v
- nginx version: nginx/1.2.6
-
- 完成所以安裝,收工!
- # /usr/local/nginx/sbin/nginx -V
- nginx version: nginx/1.2.6
- built by gcc 4.1.2 20080704 (Red Hat 4.1.2-52)
- TLS SNI support enabled //用于支持SSL多域名證書的喲
- configure arguments: --prefix=/usr/local/nginx --user=nobody --group=nobody --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_geoip_module --with-http_ssl_module --with-http_ssl_module --with-openssl=/root/upgrade/openssl-1.0.1c --with-pcre=/root/upgrade/pcre-8.12 --add-module=/root/upgrade/nginx-upstream-jvm-route/nginx_upstream_jvm_route/ --add-module=/root/upgrade/nginx_upstream_check_module-master/ --add-module=/root/upgrade/nginx-sticky-module-1.1/ --with-google_perftools_module
-
- 環(huán)境驗(yàn)證:
- 前端一臺(tái)反向代理,后端兩臺(tái)異地機(jī)房(A\B機(jī)房),A機(jī)房具有較高的帶寬、B機(jī)房作為備用機(jī)房。
- 要求: 正常情況下,訪問全部走A機(jī)房,在A機(jī)房不可用時(shí),全部訪問走B機(jī)房。理論上我們不需要進(jìn)行session粘滯便可以輕松實(shí)現(xiàn)。但基于未來多機(jī)房分布,session粘滯還是必須需要的。那么當(dāng)前情況下,我有兩個(gè)模塊nginx_upstream_jvm_route(需要配置tomcat\resin等,應(yīng)用環(huán)境有限),nginx-sticky-module-1.1. 根據(jù)不同情況使用。
-
- 目前可以分為兩種情況:
- 1、只有一條主線路和一條備線路
- 要求: 在主線路可以使用時(shí),盡量使用主線路,備線路在主線路故障時(shí)使用。
- 實(shí)際情況: A機(jī)房一線主線路,B機(jī)房一條備用線路(因?yàn)榫€路質(zhì)量差,所以備用)
- 具體配置如下:
- upstream.conf
- //
- upstream online {
- server 172.28.10.161:8080 max_fails=0 fail_timeout=3s ;
- server 172.28.10.163:8080 backup;
-
- check interval=3000 rise=2 fall=1 timeout=1000 type=http;
- check_http_send "GET / HTTP/1.0\r\n\r\n";
- check_http_expect_alive http_2xx http_3xx;
- }
-
- 2、有多條主線路和一條備線路
- 要求: 多條主線路進(jìn)行負(fù)載均衡,在所有主線路都故障時(shí),使用備用線路.
- 實(shí)際情況:A、C兩條線路進(jìn)行負(fù)載均衡、B線路最終備用線路.
- 具體配置如下:
- server.conf
- //
- server {
- ......
- location / {
- proxy_pass http://online;
- }
- error_page 404 502 = @backup; //加502的原因是因?yàn)榫€上系統(tǒng)在online里的upstream全部掛掉時(shí),頁面會(huì)報(bào)502,并不是404
-
- location @failover {
- proxy_pass http://backup;
- }
-
- location /status {
- check_status;
- access_log off;
- allow all; //生產(chǎn)環(huán)境請(qǐng)?jiān)试S特定IP訪問
- }
- ......
- }
-
- upstream.conf
- //
- proxy_next_upstream http_404 http_502; //讓404報(bào)錯(cuò)進(jìn)入max_fails計(jì)數(shù)
- upstream online {
- sticky;
- server 172.28.70.161:8080 max_fails=0 fail_timeout=3s ;
- server 172.28.70.163:8080 max_fails=0 fail_timeout=3s ;
-
- check interval=3000 rise=2 fall=1 timeout=1000 type=http;
- check_http_send "GET / HTTP/1.0\r\n\r\n";
- check_http_expect_alive http_2xx http_3xx;
- }
-
- upstream backup {
- server 172.28.22.29:7777 max_fails=0 fail_timeout=3s;
- }
-
- 如果upstream里的主機(jī)全部掛掉了,日志會(huì)報(bào)
- 2013/01/12 22:57:37 [error] 7627#0: *23641 no live upstreams while connecting to upstream, client: 100.120.111.94, server: *., request: "GET http://www./.....(省略) HTTP/1.1", upstream: "http://online/.....(省略), host: "www.", referrer: "http://www./.....(省略)"
最后需要指出的就是后端的日志記錄問題!這個(gè)在nginx升級(jí)安裝時(shí)已經(jīng)考慮到了,增加http_realip_module模塊。 本文出自 “潛入技術(shù)的海洋” 博客,轉(zhuǎn)載請(qǐng)與作者聯(lián)系!
|