第一部分 環境準備
一:Nginx+keepalived服務器兩臺(調度器,雙機熱備)
系統:Linux—CentOS7.4
IP地址:192.168.80.100(lvs01)
192.168.80.101(lvs02)
軟件需求:nginx安裝包(nginx-1.13.9.tar.gz)
Keepalived安裝包(keepalived-1.4.2.tar.gz)
二:tomcat服務器兩臺(服務器池)
系統:Linux—CentOS7.4
IP地址:192.168.80.102(TM01)
192.168.80.103(TM02)
軟件需求:java環境jdk包(jdk-8u144-linux-x64.tar.gz)
tomcat安裝包(apache-tomcat-8.5.23.tar.gz)javascript
一、源碼安裝Nginxphp
yum install -y \ //安裝編譯工具及插件 gcc \ gcc-c++ \ make \ openssl-devel \ zlib-devel \
useradd -s /sbin/nologin -M nginx --建立一個不能登陸服務器,而且沒有家目錄的用戶
css
tar vxf nginx-?.tar.gz ---解壓nginx軟件包
html
cd nginx-1.13.9 --切換到解壓目錄裏
java
./configure \ //個性化配置 --user=nginx \ --group=nginx \ --with-file-aio \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_realip_module
make && make install --編譯且安裝
node
vi /usr/local/nginx/conf/nginx.conf --nginx主配置文件
linux
user nginx nginx; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; add_header X-Server $hostname; server_names_hash_bucket_size 128; server_name_in_redirect off; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 60; client_header_buffer_size 32k; large_client_header_buffers 4 128k; client_max_body_size 512m; open_file_cache max=65535 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 1; gzip on; gzip_static on; gzip_http_version 1.1; gzip_comp_level 2; gzip_min_length 1024; gzip_vary on; gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss; server_tokens off; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 512k; fastcgi_buffers 6 512k; fastcgi_busy_buffers_size 512k; fastcgi_temp_file_write_size 512k; fastcgi_intercept_errors on; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 32k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 2m; proxy_ignore_client_abort on; proxy_cache_path /usr/local/nginx/cache_temp levels=2:2 keys_zone=cache_temp:128m inactive=30m max_size=2g; proxy_cache_valid 200 302 10m; include /usr/local/nginx/conf/conf.d/*.conf; server { listen 80; server_name localhost; charset UTF-8; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
cd /usr/local/nginx/conf/ --切換到nginx配置文件目錄中 mkdir conf.d --建立一個 conf.d的目錄 cd conf.d/ vi lvs01.conf //新建子配置文件 server { listen 80; server_name lvs01 192.168.80.100; //服務器名稱與IP地址 index index.html index.jsp; root /usr/local/nginx/html; access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main; location ~ .*\.jsp$ { index index.jsp; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Client-IP $remote_addr; proxy_set_header X-For $proxy_add_x_forwarded_for; proxy_pass http://center_pool; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; proxy_pass http://center_pool; } location ~ .*\.(js|css)?$ { expires 1h; proxy_pass http://center_pool; } } vi pool.conf //建立服務器池 upstream center_pool { //默認輪詢 server 192.168.80.102:8080; server 192.168.80.103:8080; }
//製做啓動腳本 [root@lvs01 conf.d]# vi /etc/init.d/nginx #!/bin/bash #chkconfig: 35 99 20 #description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
chmod +x /etc/init.d/nginx //增長執行權限 chkconfig --add nginx //加入系統管理服務 service nginx start //啓動nginx服務 netstat -anpt | grep 80
二、源碼編譯keepalivednginx
yum -y install \ //安裝插件 > popt-devel \ > kernel-devel \ > openssl-devel
tar xvf keepalived-1.4.2.tar.gz --解壓keepalived壓縮包 cd keepalived-1.4.2 --切換到解壓目錄 ./configure --prefix=/ --配置 make && make install --編譯與安裝
cp keepalived/etc/init.d/keepalived /etc/init.d/
c++
//如下編輯keepalived配置文件cd /etc/keepalived/
vi keepalived.conf
web
! Configuration File for keepalived global_defs { route_id NGINX-01 } vrrp_script nginx { script "/opt/nginx.sh" interval 2 weight -10 } vrrp_instance VI_1 { state MASTER interface ens32 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { nginx } virtual_ipaddress { 192.168.80.100 } } //注意,主備的優先級配置的相差50
建立腳本文件
判斷keepalived進程是否存在,在就啓動nginx不在就關閉vi /opt/nginx.sh
#!/bin/bash #Filename:nginx.sh A=$(ps -ef | grep keepalived | grep -v grep | wc -l) if [ $A -gt 0 ]; then /etc/init.d/nginx start else /etc/init.d/nginx stop fi
chmod +x /opt/nginx.sh --給腳本文件加執行權限
service keepalived start --啓動keepalived,會連nginx一塊啓動
ip addr show dev ens32 --查看漂移地址是否生成
---------------測試驗證------------ [root@lvs01 keepalived]# systemctl stop keepalived //關閉keepalived服務 [root@lvs01 keepalived]# killall -9 nginx //關閉nignx服務 [root@lvs01 keepalived]# netstat -anpt | grep 80 //80端口已中止運行 [root@lvs01 keepalived]# systemctl start keepalived //開啓keepalived服務 [root@lvs01 keepalived]# netstat -anpt | grep 80 //nginx隨keepalived啓動
192.168.80.101
---------------------配置從服務器--------------------------------
一、源碼編譯安裝nginx
從服務器源碼安裝nginx,和主服務器同樣,根據主服務器操做在作一遍
須要修改的地方
二、源碼安裝keepalived
操做和主服務器相同,在寫到nginx.sh腳本文件時不一樣,其它的操做如出一轍。
#!/bin/bash #Filename:nginx.sh A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l) if [ $A -gt 0 ]; then /etc/init.d/nginx start else /etc/init.d/nginx stop fi
chmod +x /opt/nginx.sh --給腳本執行權限
service keepalived start --啓動 keepalived
----------測試驗證----------
一:查看從服務器狀態
[root@lvs02 ~]#ip addr show dev ens33
//查看漂移地址
//因爲主服務器在運行,漂移地址並未同步過來//當主服務器在運行的時候,從服務器虛擬地址並未生成,nginx服務並未隨keepalived啓動
二:模擬主服務故障
- 主服務器
killall 命令須要安裝 : yum install psmisc -y
//80端口已不運行
[root@lvs01 ~]# ip addr show dev ens32
//漂移地址消失,不工做
從服務器
[root@lvs02 ~]# ip addr show dev ens32 //查看漂移地址
//漂移地址自動生成
[root@lvs02 ~]# netstat -anpt | grep 80
[root@lvs01 ~]# service keepalived start [root@lvs01 ~]# ip addr show dev ens33
[root@lvs01 ~]# netstat -anpt | grep 80
//主服務器已恢復工做
//漂移地址自動移除
[root@lvs02 ~]# netstat -anpt | grep 80
```\
//nginx自動中止服務
//雙機熱備驗證成功
192.168.80.103
提示:操做與192.168.80.102基本同樣
須要修改的地方就是把 SERVER AA 改爲 SERVER BB j 就能夠了
--------測試------
地址欄裏輸入:192.168.80.188:8080,點擊刷新按鈕查看是否動態輪詢。
#192.168.80.102
第三部分 部署服務器池—搭建Tomcat
//如下在兩臺tomcat服務器上操做
第一步:部署第一個節點服務器TM01(192.168.80.102)
----------部署java環境,jdk---------
[root@tom01 ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解壓jdk
[root@tom01 ~]# cp -r jdk1.8.0_144/ /usr/local/java //建立java源目錄
[root@tom01 ~]# vi /etc/profile
最後添加如下內容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
保存退出
[root@tom01 ~]# source /etc/profile //當即生效
[root@tom01 ~]# java –version //查看版本
java環境部署完成
----------部署tomcat----------
[root@tom01 ~]# tar xvf apache-tomcat-8.5.23.tar.gz //解壓apache-tomcat
[root@tom01 ~]# cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //建立tomcat源目錄
//作個軟連接,使tomcat開啓與關閉更加方便
[root@tom01 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tom01 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tom01 ~]# tomcatup //開啓tomcat
[root@tom01 ~]# netstat -anpt | grep 8080
Win7下訪問默認主頁:http://192.168.80.102:8080
//tomcat部署成功
服務器池中有兩臺tomcat服務器,爲了便於識別,主頁上添加點標記
[root@tom01 ~]# vi /usr/local/tomcat8/webapps/ROOT/index.jsp
添加一行內容:
保存退出
[root@tom01 ~]# tomcatdown
[root@tom01 ~]# tomcatup //重啓
//再次訪問默認主頁http://192.168.80.102:8080
192.168.80.103 提示:操做與192.168.80.102基本同樣須要修改的地方就是把 SERVER AA 改爲 SERVER BB j 就能夠了--------測試------地址欄裏輸入:192.168.80.188:8080,點擊刷新按鈕查看是否動態輪詢。