nginx+keepalived實現高可用集羣

實驗環境:html

兩臺調度器,一主一備nginx

192.168.200.111web

192.168.200.112shell

兩臺web服務器apache

192.168.200.113vim

192.168.200.114瀏覽器

利用keepalived實現主備的故障切換,從而交替提供服務安全

利用nginx實現負載均衡的調度bash

配置主調度器服務器

1.安裝依賴包

yum -y instsall ipvsadm keepalived

2.將配置文件進行備份,避免出錯

cd /etc/keepalived

cp keepalived.conf keepalived.con.bak

3.編輯配置文件

vim 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_MASTER
}
  #基於nginx腳本實現調度
vrrp_script check_nginx {
script "/opt/chk_nginx.sh"
interval 2
weight -20
}
 
#調度配置
vrrp_instance VI_1 {
state MASTER
interface eno16777728
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254
}
track_script {
check_nginx #引用nginx腳本
}
}
 

4.安裝nginx服務

(1.)源碼安裝,參考nginx安裝的博客文章

(2.)軟件包安裝,參考fpm製做rpm包的博客文章

5.修改nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

紅色字體添加內容:

keepalive_timeout 65;
upstream httpd_server {
server 192.168.200.113 weight=1;
server 192.168.200.114 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;

location / {
root html;
index index.html index.htm;
proxy_pass http://httpd_server;
proxy_set_header Host $http_host;
}

6.配置完成後啓動服務

systemctl start keepalived 

7.查看VIP端口

ip a

8.查看調度服務

ipvsadm  -Ln

9.將主配置文件發送給備調度器,修改相應內容

scp /etc/keepalived/keepalived.conf 192.168.200.112:/etc/keepalived/

10.編輯nginx腳本文件,配置爲週期性任務

mkdir /shell

vim /shell/nginx_check.sh

編輯內容:

#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)"

if [ $count -eq 0 ];then
nginx
sleep 2
if [ `ps -c nginx --no-header` |wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi

11.給腳本添加執行權限

 

chmod +x /shell/nginx_check.sh

 

配置節點服務器:

1.安裝apache服務

yum -y install httpd

2.添加路由

ifconfig lo:0 192.168.200.254 netmask 255.255.255.255

route  add -host 192.168.200.254 dev lo:0

3.建立測試網頁

echo "11111" > /var/www/html/index.html

4.啓動apache服務

systemctl start httpd

5.修改配置文件

vim /etc/sysctl.conf

添加內容;

net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

6.加載配置信息

sysctl -p

##每臺節點服務器都要進行相同的配置

實驗測試:

1.關閉全部服務器的防火牆和Linux安全機制

iptables -F

systemctl stop firewalld

setenforce 0

2.在瀏覽器中輸入虛擬服務器的IP地址

這裏咱們用的是192.168.200.254

 

 

 

實現輪詢的結果

3.關掉nginx和keepalived進行高可用測試

killall -9 nginx
systemctl stop keepalived

while :
> do
> curl 192.168.200.254
> sleep 1
> done

進行測試。

相關文章
相關標籤/搜索