192.168.132.130(伺服器1)、192.168.132.132(伺服器2),虛擬ip:192.168.132.131
yum install gcc-c++:gcc是gnu的c語言編譯器,至於c++編譯器,它的名字叫做g++
yum install -y pcre pcre-devel:在nginx編譯需要 pcre(perl compatible regular expression),因為nginx 的rewrite模組和http 核心模組會使用到pcre正規表示式語法
yum install -y zlib zlib-devel:壓縮和解壓縮庫
yum install -y openssl openssl-devel:是乙個ssl協議的開源實現
cd /usr/local:進入目錄
tar -zxvf nginx-1.9.9.tar.gz:解壓
cd nginx-1.9.9:進入解壓後的資料夾下
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module:使用/usr/local/nginx/nginx.conf下的配置
make:編譯
make install:安裝
cd /usr/local/nginx/sbin/:進入安裝後的nginx資料夾
./nginx -c /usr/local/nginx/nginx.conf:指定配置檔案
./nginx -s reload:啟動
其他命令:
./nginx -s stop 等待nginx處理任務完畢,停止nginx程序
./nginx -s quit 先查出程序id,再使用kill命令強制殺掉程序
./nginx -s reload 不用重啟nginx,使配置檔案生效(若修改了nginx.conf)
ps -ef |
grep nginx 查詢 nginx 程序
whereis nginx 查詢nginx的安裝路徑
./nginx -t 檢查nginx.cnf的語法是否正確
systemctl disable firewalld:永久關閉防火牆
systemctl stop firewalld:暫時關閉防火牆
systemctl status firewalld:檢視防火牆狀態
通過192.168.132.130或192.168.132.132訪問時會到nginx的index.html,分別修改index.html使其能分清楚是哪個伺服器。
yum install -y libnl3-devel;提供了一套應用於linux系統基於netlink協議通訊的api介面
yum install -y net-snmp-devel;網路管理協議,它屬於tcp/ip五層協議中的應用層協議
伺服器1(主):
! configuration file for keepalived
global_defs
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lvs_devel # 使用者標識本節點的名稱,通常為hostname
vrrp_skip_check_adv_addr
#vrrp_strict 要注釋掉否則虛擬ip訪問不了
vrrp_garp_interval 0
vrrp_gna_interval 0
}vrrp_instance vi_1
virtual_ipaddress
}
伺服器2(備):
! configuration file for keepalived
global_defs
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lvs_devel # 使用者標識本節點的名稱,通常為hostname
vrrp_skip_check_adv_addr
#vrrp_strict 要注釋掉否則虛擬ip訪問不了
vrrp_garp_interval 0
vrrp_gna_interval 0
}vrrp_instance vi_1
virtual_ipaddress
}
其他命令:
systemctl restart keepalived:重啟
systemctl stop keepalived:停止
systemctl status keepalived:檢視狀態
tail -f /var/log/messages:檢視是否啟動
通過ip addr命令檢視:
伺服器1:
ens33:
mtu 1500 qdisc pfifo_fast state up group default qlen 1000
link/ether 00:0c:29:12:b1:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.132.130/24 brd 192.168.132.255 scope global noprefixroute dynamic ens33
valid_lft 1519sec preferred_lft 1519sec
inet 192.168.132.131/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::9b3e:6d14:d65:c286/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ens33:
mtu 1500 qdisc pfifo_fast state up group default qlen 1000
link/ether 00:0c:29:43:0a:46 brd ff:ff:ff:ff:ff:ff
inet 192.168.132.132/24 brd 192.168.132.255 scope global noprefixroute dynamic ens33
valid_lft 1374sec preferred_lft 1374sec
inet6 fe80::e8b1:993c:4765:abb7/64 scope link noprefixroute
valid_lft forever preferred_lft forever
check_ng.s**件如下:
#!/bin/bash
run=
`ps -c nginx --no-header |
wc -l`if[
$run -eq 0 ]
then
killall keepalived
fi
【注】此檔案一定要在linux下看下檔案格式,在notepad編輯的為:使用vi檢視檔案,:set ff檢視格式,如為fileformat=dos則:set fileformat=unix
將keepalived修改為:
! configuration file for keepalived
global_defs
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id lvs_devel
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}vrrp_script chk_nginx
vrrp_instance vi_1
virtual_ipaddress
track_script
}
keepalived nginx主從切換
五 nginx指令碼,新建nginx check.sh指令碼放到 etc keepalived下 指令碼內容 bin bash a ps c nginx no header wc l if a eq 0 then usr local nginx sbin nginx sleep 2 if ps c ...
keepalived nginx負載安裝
環境 centos7.6 keepalived工具可以生成虛擬浮動ip,繫結到網絡卡上。172.21.210.19 master 172.21.210.20 backup 172.21.210.30 vip 浮動ip 1 兩個節點安裝keepalived yum y install keepaliv...
keepalived nginx離線安裝
keepalived nginx安裝 把keepalived 1.2.23.tar.gz傳送到 opt下 cd opt tar zxvf keepalived 1.2.23.tar.gz cd keepalived 1.2.23 configure prefix usr local keepaliv...