Keepalived Nginx高可用配置

2021-08-14 03:07:47 字數 4361 閱讀 6258

很久就想試試keepalived+nginx高可用怎麼配置,一直沒有動手。正所謂"紙上得來終覺淺,絕知此事要躬行"。so let's go, step by step.

主要需要keepalived+nginx+apache,所以要安裝這幾個。我用的centos7的虛擬機器。ip和埠規劃如下:

作用ip

埠web

192.168.37.128

8828

web192.168.37.129

8829

nginx

192.168.37.128

80nginx

192.168.37.12980

keepalived虛擬ip :192.168.37.120 (前提是要配置好固定ip,虛擬ip在這個範圍接近就可以了)

建議web的ip在其他機器上,沒有更多的虛擬機器自己試驗,所以web和nginx放在一台上。

1,搭建apache

充當web伺服器,用apache搞web方便快捷。

修改配置  /etc/httpd/conf/httpd.conf

#listen 12.34.56.78:80

listen 8829

另外乙個上面修改 /etc/httpd/conf/httpd.conf

#listen 12.34.56.78:80

listen 8828

另外為了方便,修改/usr/share/httpd/noindex/index.html 可以區分是那個web上面的。

啟動apache,可以分別訪問網頁試試:192.168.37.128:8828 

2,搭建nginx

rpm -ivh

yum install nginx

/etc/nginx/nginx.conf  (下面列出部分配置,其他不用修改)

這裡面包含乙個/etc/nginx/conf.d/default.conf

server

在兩台虛擬機器上做同樣的配置,然後啟動nginx。

service nginx start

這樣可以到瀏覽器上面測試下,輸入   或者 

重新整理幾次,發現頁面內容在128,129之間自動切換。這樣就明白了負載均衡是什麼意思了。

3,配置keepalived

首先當然是安裝keepalived。建議最好是自己編譯安裝,各種版本的依賴的坑,很容易掉進去。

yum install keepalived

修改配置/etc/keepalived/keepalived.conf

global_defs

notification_email_from [email protected]

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id lvs_devel

}vrrp_instance vi_1

virtual_ipaddress

}在另外一台上面安裝,修改配置/etc/keepalived/keepalived.conf

global_defs

notification_email_from [email protected]

smtp_server smtp.163.com

smtp_connect_timeout 30

router_id lvs_devel

}vrrp_instance vi_1

virtual_ipaddress

}主要的不同就是state,priority。一般來說把master的priority配高點。

inte***ce eno16777736  這裡也要注意,看了網上很多把eno16777736  配置成eth0,eth2,dev等等,開始一直不明白,原來是要網絡卡的名稱。

可以自己用命令ifconfig看下,你是用的哪個,寫哪個就可以了。

兩台機器配置好了之後,就可以開始啟動了。

service keepalived restart

不出意外,瀏覽器可以看到。如果沒有看到,也不要急,遇到困難多看看日誌/var/log/messages

仔細看啟動的過程,有很多資訊可以給你提示。

我在129上面成功了,128上面遇到:

dec 26 22:15:07 vm11 keepalived_vrrp[10099]: opening file '/etc/keepalived/keepalived.conf'.

dec 26 22:15:07 vm11 keepalived_vrrp[10099]: unable to load ipset library - libipset.so.3: cannot open shared object file: no such file or directory

dec 26 22:15:07 vm11 keepalived_vrrp[10099]: vrrp_instance(vi_1) removing protocol vips.

yum install libnl3-devel ipset-devel

又發現問題:

dec 26 22:25:28 vm11 keepalived_vrrp[10366]: opening file '/etc/keepalived/keepalived.conf'.

dec 26 22:25:29 vm11 kernel: netfilter messages via netlink v0.30.

dec 26 22:25:29 vm11 kernel: ip_set: protocol 6

dec 26 22:25:29 vm11 keepalived_vrrp[10366]: vrrp_instance(vi_1) removing protocol vips.

是不是和防火牆有關啊。果斷決定  systemctl stop firewalld  

dec 26 22:33:05 vm11 keepalived_vrrp[10987]: using linkwatch kernel netlink reflector...

dec 26 22:33:05 vm11 keepalived_vrrp[10987]: vrrp_instance(vi_1) entering backup state

dec 26 22:33:05 vm11 keepalived_vrrp[10987]: vrrp sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]

貌似這次正常了。瀏覽器訪問下,ok了

關掉129上面的nginx,service nginx stop,還是可以訪問,說明生效了。在看下日誌/var/log/messages

dec 26 22:34:28 vm11 keepalived_vrrp[10987]: vrrp_instance(vi_1) transition to master state

dec 26 22:34:29 vm11 keepalived_vrrp[10987]: vrrp_instance(vi_1) entering master state

dec 26 22:34:29 vm11 keepalived_vrrp[10987]: vrrp_instance(vi_1) setting protocol vips.

dec 26 22:34:29 vm11 keepalived_vrrp[10987]: sending gratuitous arp on eno16777736 for 192.168.37.120

dec 26 22:34:29 vm11 keepalived_vrrp[10987]: vrrp_instance(vi_1) sending/queueing gratuitous arps on eno16777736 for 192.168.37.120

dec 26 22:34:29 vm11 keepalived_vrrp[10987]: sending gratuitous arp on eno16777736 for 192.168.37.120

驚喜的發現128上面的entering master state

再關掉128上的service nginx stop,192.168.37.120訪問不了,兩個都掛了,神仙也沒有辦法了。

是的,重啟試試service nginx start,一切又回來了。

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