您好,登錄后才能下訂單哦!
使用keepalived+nginx+tomcat搭建高性能web服務器集群,前端用nginx做反向代理實現負載均衡,同時結合keepalived對nginx實現高可用,后端使用tomcat做動態jsp解析,實現了動靜分離。
(本文永久地址:http://woymk.blog.51cto.com/10000269/1928716)
搭建環境:centos 6.9
準備四臺服務器
vip: 192.168.75.130
master: 192.168.75.131 (安裝nginx做反向代理實現負載勻衡,結合keepalived實現高可用)
backup: 192.168.75.132 (同上)
web1: 192.168.75.151 (安裝nginx和tomcat實現動靜分離)
web2: 192.168.75.152 (同上)
這里為節省服務器,nginx和tomcat裝在了一臺服務上,也可以分開配置。
配置步驟如下:
1. 安裝nginx、tomcat軟件
四臺服務器分別安裝好nginx
安裝方法參見:LNMP環境搭建
web1,web2上分別安裝好tomcat
安裝方法參見:Tomcat8.5的安裝配置
web1, web2上分別啟動nginx和tomcat
service nginx start
service tomcat start
2. 在master和backup上配置nginx
vi /usr/local/nginx/conf/nginx.conf
加入或修改以下內容:
upstream web_nginx {
server 192.168.75.151;
server 192.168.75.152;
}
upstream web_tomcat {
#ip_hash;
server 192.168.75.151:8080;
server 192.168.75.152:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://web_nginx/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.jsp$ {
proxy_pass http://web_tomcat/;
}
}
分別在master和backup上啟動nginx服務
service nginx start
3. 配置keepalived
在master和backup上分別安裝keepalived
yum install -y keepalived
1) 配置master
vi /etc/keepalived/keepalived.conf
清空原文,加入如下內容:
! Configuration File for keepalived
global_defs {
notification_email {
#acassen@firewall.loc
#failover@firewall.loc
#sysadmin@firewall.loc
}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80"
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.75.130
}
track_script {
chk_http_port
}
}
2) 配置backup
scp 192.168.75.131:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf
backup的配置文件只需要修改
state MASTER -> state BACKUP
priority 100 -> priority 99
最后,分別在master和backup上啟動keepalived服務,先主后備
/etc/init.d/keepalived start
4. 測試:
在master上停止nginx服務
[root@master ~]# /etc/init.d/nginx stop
停止 nginx: [確定]
在backup上查看日志
[root@backup ~]# tail -f /var/log/messages
May 23 17:21:33 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) forcing a new MASTER election
May 23 17:21:33 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) forcing a new MASTER election
May 23 17:21:34 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Transition to MASTER STATE
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Entering MASTER STATE
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) setting protocol VIPs.
May 23 17:21:35 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.75.130
May 23 17:21:35 backup Keepalived_healthcheckers[1817]: Netlink reflector reports IP 192.168.75.130 added
May 23 17:21:40 backup Keepalived_vrrp[1818]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.75.130
日志顯示已經成功切換到備用節點上。
[root@backup ~]# curl 192.168.75.130 -I
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Tue, 23 May 2017 09:34:21 GMT
Content-Type: text/html
Content-Length: 616
Connection: keep-alive
Last-Modified: Mon, 22 May 2017 10:28:07 GMT
ETag: "5922bd37-268"
Accept-Ranges: bytes
master停掉后,用curl訪問web服務,一切正常。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。