Keepalived Nginx高可用集群

2021-07-05 21:29:20 字數 2851 閱讀 3270

安裝環境:centos6.6 x64

nginx版本:1.9.3

keepalived版本:1.2.18

ipvip

主節點192.168.11.21

192.168.11.20

備節點192.168.11.22

192.168.11.20

1.1、安裝openssl

# tar -zxvf openssl-1.0.0s.tar.gz

# ./configure

# make && make install

# tar -zxvf pcre-7.8.tar.gz

# tar -zxvf nginx-1.9.3.tar.gz

# cd nginx-1.9.3

# ./configure --prefix=/usr/local/nginx--with-pcre=/usr/local/src/pcre-7.8

# make && make install

/usr/loca/nginx/sbin/nginx

# tar -zxvf keepalived-1.2.18.tar.gz

# cd keepalived-1.2.18

# ./configure --prefix=/usr/local/keepalived

# make && make install

將keepalived安裝成linux系統服務:

# mkdir/etc/keepalived

# cp/usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

# cp/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

# cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

# ln -s /usr/local/sbin/keepalived /usr/sbin/

#ln -s /usr/local/keepalived/sbin/keepalived /sbin/

設定keepalived服務開機啟動

# chkconfig keepalived on

修改keepalived配置檔案

# vi/etc/keepalived/keepalived.conf

! configuration file for keepalived

global_defs

vrrp_script chk_nginx

vrrp_instance vi_1

virtual_ipaddress

track_script

}

# vi/etc/keepalived/keepalived.conf

! configuration file for keepalived

global_defs

vrrp_script chk_nginx

vrrp_instance vi_1

virtual_ipaddress

track_script  }

編寫nginx狀態檢測指令碼/etc/keepalived/nginx_check.sh

內容如下:

#!/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 nginx --no-header |wc -l` -eq 0 ];then

killall keepalived

service keepalived stop

fifi

儲存後,給指令碼賦執行許可權:

# chmod +x /etc/keepalived/nginx_check.sh

關閉防火牆:

#service iptable stop

分別在兩台主機上啟動nginx和keepalived

# /usr/loca/nginx/sbin/nginx

# service keepalived start

檢視虛擬ip新增是否成功(

紅色部分為vip)

# ip a

eth1:

mtu 1500 qdiscpfifo_fast state up qlen 1000

link/ether 00:0c:29:2e:4c:00brd ff:ff:ff:ff:ff:ff

inet 192.168.11.21/24 brd192.168.152.255 scope global eth1

inet 192.168.11.20/32scope global eth1

inet6fe80::20c:29ff:fe2e:4c00/64 scope link

valid_lft foreverpreferred_lft forever

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集群部署

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編譯...

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...