集羣規劃php
主機名 | IP | VIP | Nginx:port | KeepAlived主備 |
KA_NG_01 | 192.168.30.130 | 192.168.30.120 | 8088 | MASTER |
KA_NG_02 | 192.168.30.131 | 192.168.30.120 | 8088 | BACKUP |
實驗環境:html
[root@KA_NG_01 ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@KA_NG_01 ~]# keepalived -v
Keepalived v1.2.13 (03/19,2015)
[root@KA_NG_01 ~]# nginx -v
nginx version: nginx/1.14.0linux
若是是編譯安裝的的話,在本機首先配置好YUM源,保證機器能夠訪問公網,而後安裝如下依賴:nginx
[root@KA_NG_01 ~]# yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-develc++
去官網下載對應的的軟件包進行編譯安裝web
Nginx的官網下載地址:vim
http://nginx.org/en/download.htmlcentos
keepalived的官方下載地址:瀏覽器
http://www.keepalived.org/software/keepalived-1.4.3.tar.gzbash
編譯安裝之後有時間再搞,這裏爲了快速見效,採用YUM一鍵安裝部署。
使用YUM安裝Nginx:http://nginx.org/en/linux_packages.html#stable
參考:http://nginx.org/en/linux_packages.html#stable
在/etc/yum.repo.d/新建nginx.repo文件,文件內容以下所示:
[root@KA_NG_01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
依次執行yum clean all,yum list.
安裝nginx
[root@KA_NG_01 ~]# yum install -y nginx
安裝完成以後,nginx的相關文件位置以下:
[root@KA_NG_01 ~]# find / -name nginx
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/etc/logrotate.d/nginx
/etc/nginx #主配置文件
/usr/lib64/nginx
/usr/sbin/nginx #啓動文件
/usr/share/nginx #默認的網頁存放位置
/var/lib/yum/repos/x86_64/6/nginx
/var/lock/subsys/nginx
/var/log/nginx #日誌位置
/var/cache/nginx
/var/cache/yum/x86_64/6/nginx
安裝完成以後,能夠先啓動下,看看可否訪問:
[root@KA_NG_01 ~]# service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@KA_NG_01 ~]# elinks 192.168.30.130:80 --dump #注意這裏是兩個橫線
Welcome to nginx !
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
出現這個,證實安裝沒問題。
配置Nginx:
[root@KA_NG_01 ~]# cp /etc/nginx/nginx.conf{,.bak} #修改配置文件以前,先備份,養成好習慣
[root@KA_NG_01 ~]# vim /etc/nginx/nginx.conf
31 include /etc/nginx/conf.d/*.conf; #大概在31行左右,這行說明了nginx的默認網頁存放路徑等的相關配置文件的位置
[root@KA_NG_01 ~]# ls /etc/nginx/conf.d/default.conf #這個文件指定了nginx的默認網頁根目錄,及端口等等
/etc/nginx/conf.d/default.conf
[root@KA_NG_01 ~]# cat !$
cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
兩種修改方式,第一種能夠把/etc/nginx/nginx.conf中的31行左右 「include /etc/nginx/conf.d/*.conf;」註釋掉;第二種能夠在default.conf中直接修改,(改以前備份下),這裏我採用的是第一種方式註釋掉31行。修改完後的配置文件以下:
[root@KA_NG_01 ~]# vim /etc/nginx/nginx.conf #注意縮進,由於直接Ctrl+v過來的,縮進不規範
1
2 #user nginx;
3 user root;
4 worker_processes 1;
5
6 #error_log /var/log/nginx/error.log warn;
7 #pid /var/run/nginx.pid;
8
9
10 events {
11 worker_connections 1024;
12 }
13
14
15 http {
16 include /etc/nginx/mime.types;
17 default_type application/octet-stream;
18
19 # log_format main '$remote_addr - $remote_user [$time_local] "$request" '
20 # '$status $body_bytes_sent "$http_referer" '
21 # '"$http_user_agent" "$http_x_forwarded_for"';
22 #
23 # access_log /var/log/nginx/access.log main;
24 #
25 sendfile on;
26 #tcp_nopush on;
27
28 keepalive_timeout 65;
29
30 #gzip on;
31
32 #include /etc/nginx/conf.d/*.conf;
33 server {
34 listen 8088; #指定端口號
35 server_name localhost;
36 location / { #指定網頁根目錄
37 root /usr/share/nginx/html;
38 index index.html index.htm;
39 }
編寫測試頁面:
[root@KA_NG_01 ~]# vim /usr/share/nginx/html/index.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Welcome to nginx_server_01!</title>
5 <style>
6 body {
7 width: 35em;
8 margin: 0 auto;
9 font-family: Tahoma, Verdana, Arial, sans-serif;
10 }
11 </style>
12 </head>
13 <body>
14 <h1>Welcome to nginx KA_NG_01!</h1>
15 <h1><b>KA_NG_01:192.168.30.130:8088</b></h1>
16 <p>If you see this page, the nginx web server is successfully installed and
17 working. Further configuration is required.</p>
18
19 <p>For online documentation and support please refer to
20 <a href="http://nginx.org/">nginx.org</a>.<br/>
21 Commercial support is available at
22 <a href="http://nginx.com/">nginx.com</a>.</p>
23
24 <p><em>Thank you for using nginx.</em></p>
25 </body>
26 </html>
檢查配置nginx配置文件的合法性
[root@KA_NG_01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重啓Nginx,瀏覽器訪問測試:
開始安裝Keepalived:
[root@KA_NG_01 ~]# yum install -y keepalived #一條命令搞定
配置keepalived:
[root@KA_NG_01 ~]# cp /etc/keepalived/keepalived.conf{,.bak} #修改以前先備份
[root@KA_NG_01 ~]# vim /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs { #全局定義
4 notification_email { #郵件通知在實驗中沒有涉及,因此沒有配置,若是須要,須要開啓sendmail服務,或者其餘郵件服務
5 sendmail@example.com
6 receive@example.com
7 }
8 notification_email_from receive@example.com
9 smtp_server mail.example.com
10 smtp_connect_timeout 30
11 router_id KA_NG_01 # 節點標識,設爲主機名便可
12 }
13
14 vrrp_script chk_nginx { #檢測nginx的運行狀態
15 script "/etc/keepalived/nginx_check.sh" #腳本路徑
16 interval 2 #探測間隔時間
17 weight -20 #若是條件知足,權重-20
18 }
19 vrrp_instance VI_1 { #定義虛擬路由 VI_1爲標識符,也可自定義
20 state MASTER #主節點標識,備節點爲BACKUP
21 interface eth0 #綁定VIP接口,與本機IP地址接口相同
22 virtual_router_id 30 #虛擬路由ID標識號,可自定義,可是主備必須一致,也使用IP最後一段,以相同的VRID爲一個組,其決定多播的MAC地址
23 mcast_scr_ip 192.168.30.130 #本機IP地址
24 priority 100 #節點優先級,範圍0~254,MASTER要比BACKUP高
25 nopreempt #優先級的高級設置,nopreempt解決異常恢復後再次搶佔的問題
26 advert_int 1 #組播信息發送間隔,主備必須一致,默認1s
27 authentication { #驗證信息,主備需一致
28 auth_type PASS
29 auth_pass 1111 #生產環境下,按實際狀況來定
30 }
31 track_script { #將track_script塊加入instance配置塊
32 chk_nginx #執行Nginx檢測
33 }
34 virtual_ipaddress { #虛擬IP主備需一致
35 192.168.30.120 #虛擬IP可定義多個
36 }
37 }
38
編寫 nginx_check.sh腳本文件
[root@KA_NG_01 ~]# vim /etc/keepalived/nginx_check.sh
1 #!/bin/bash
2 A=`ps -C nginx –no-header |wc -l`
3 if [ $A -eq 0 ];then
4 /etc/init.d/nginx start
5 sleep 2
6 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
7 killall keepalived
8 fi
9 fi
[root@KA_NG_01 ~]# chmod +x !$
chmod +x /etc/keepalived/nginx_check.sh
關機,而後使用VMware的克隆功能,直接克隆出第二臺機器(KA_NG_02),開啓克隆出來的機器,將主機名修改成KA_NG_02,IP設置爲192.168.30.131,對keepalived.conf作如下變更
[root@KA_NG_02 ~]# vim /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 sendmail@example.com
6 receive@example.com
7 }
8 notification_email_from receive@example.com
9 smtp_server mail.example.com
10 smtp_connect_timeout 30
11 router_id KA_NG_02
12 }
13
14 vrrp_script chk_nginx {
15 script "/etc/keepalived/nginx_check.sh"
16 interval 2
17 weight -20
18 }
19 vrrp_instance VI_1 {
20 state BACKUP
21 interface eth0
22 virtual_router_id 30
23 mcast_src_ip 192.168.30.131
24 priority 90
25 nopreempt
26 advert_int 1
27 authentication {
28 auth_type PASS
29 auth_pass 1111
30 }
31 track_script {
32 chk_nginx
33 }
34 virtual_ipaddress {
35 192.168.30.120
36 }
37 }
nginx的測試頁內容更改成:
[root@KA_NG_02 ~]# cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx_server_02!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx KA_NG_02!</h1>
<h1><b>KA_NG_02:192.168.30.131:8088</b></h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
關機。重啓機器,開啓nginx,keepalived服務進行測試
經過VIP進行訪問
手動關閉KA_NG_01上的nginx和keepalived,再次訪問測試
大功告成。
文中內容有寫是參考這裏: