Keepalived介紹:
Keepalived是一個基于VRRP協定來實作的服務高可用方案,可以利用其來避免IP單點故障,類似的工具還有heartbeat、corosync、pacemaker。但是它一般不會單獨出現,而是與其它負載均衡技術(如lvs、haproxy、nginx)一起工作來達到叢集的高可用。
準備工作:
1.環境兩台CentOS7版本的系統
檢視系統版本:
cat /etc/redhat-release,這種方法隻适合Redhat系的Linux
2.安裝docker和docker-compose
注意版本,我這個版本是可以的
centos7離線安裝docker,docker-compose
3.服務ip
兩台伺服器ip為:
10.10.11.79,10.10.11.81
keepalived的虛拟ip為10.10.11.77,10.10.11.78
下面開始部署:
主控端上建立檔案目錄:
目錄結構:
- node_1
- docker-compose.yml
- Dockerfile
- keepalived.conf
- nginx.conf
- node_2 同上
分别用于部署在兩台不同的伺服器上
每一個節點都需要四個檔案,下面詳細配置一下四個檔案。
1.nginx.conf
配置master nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server
{
listen 80;
default_type text/plain;
# client_body_in_single_buffer on;
return 200 'im master.......\n';
}
}
從節點nginx.conf配置:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
server
{
listen 80;
default_type text/plain;
# client_body_in_single_buffer on;
return 200 'im slave.......\n';
}
}
如果已經up 起來的容器後續修改了nginx的conf檔案,主從都要重新docker-compose up一下,不然會不生效。
2.Dockerfile
制作keepalived docker鏡像,兩個節點的配置檔案内容相同。
這裡需要注意一下:
如果keepalived.conf,你修改了,那麼之前生成的鏡像要删掉重新制作。
FROM osixia/keepalived
MAINTAINER nanlist
ADD ./keepalived.conf /container/service/keepalived/assets/keepalived.conf
3.docker-compose.yml
兩個節點的配置檔案内容相同。
這裡有兩個地方需要注意一下:
1.啟動之前要檢視一下nginx 的端口是否被占用了,兩個節點要用相同的端口。
2.提前在主控端上建立好挂載目錄/etc/cluster/nginx.conf,并把步驟1的nginx.conf檔案放進去。
version: '2'
services:
keepalived:
build: ./
depends_on:
- nginx
network_mode: "host"
cap_drop:
- NET_ADMIN
privileged: true
restart: on-failure:3
nginx:
image: nanlist/nginx1.23.1:v1.0
ports:
- "82:80"
volumes:
- "/etc/cluster/nginx.conf:/etc/nginx/nginx.conf"
restart: on-failure:3
4.keepalived.conf
預設使用雙BACKUP,nopreempt非搶占模式,根據自己伺服器的實際情況進行修改:
- interface # 目前進行vrrp通訊的網絡接口卡(目前centos的網卡) 用ifconfig檢視你具體的網卡
- auth_pass # 指定認證方式。PASS簡單密碼認證(推薦),AH:IPSEC認證(不推薦)
- unicast_peer # 另外一台的伺服器ip
- virtual_ipaddress # 定義虛拟ip(VIP),可多設,每行一個
這個檔案 兩個節點的配置是不一樣的,
node1:
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface ens192 # 目前進行vrrp通訊的網絡接口卡(目前centos的網卡) 用ifconfig檢視你具體的網卡
virtual_router_id 51 # 虛拟路由編号,主從要一至
priority 150 # 優先級,數值越大,擷取處理請求的優先級越高 master要大于slave
nopreempt
authentication {
auth_type PASS # 指定認證方式。PASS簡單密碼認證(推薦),AH:IPSEC認證(不推薦)
auth_pass 1111 # 指定認證所使用的密碼。最多8位
}
unicast_peer { # 另外一台的伺服器ip,如果是多台就配多個ip
10.10.11.81
}
virtual_ipaddress { # 指定VIP位址
10.10.11.77
}
notify "/container/service/keepalived/assets/notify.sh"
}
vrrp_instance VI_2 {
state BACKUP
interface ens192 # 目前進行vrrp通訊的網絡接口卡(目前centos的網卡) 用ifconfig檢視你具體的網卡
virtual_router_id 52 # 虛拟路由編号,主從要一至
priority 99 # 優先級,數值越大,擷取處理請求的優先級越高 master要大于slave
nopreempt
authentication {
auth_type PASS # 指定認證方式。PASS簡單密碼認證(推薦),AH:IPSEC認證(不推薦)
auth_pass 1111 # 指定認證所使用的密碼。最多8位
}
unicast_peer { # 另外一台的伺服器ip,如果是多台就配多個ip
10.10.11.81
}
virtual_ipaddress { # 指定VIP位址
10.10.11.78
}
notify "/container/service/keepalived/assets/notify.sh"
}
node2:
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface ens192 # 目前進行vrrp通訊的網絡接口卡(目前centos的網卡) 用ifconfig檢視你具體的網卡
virtual_router_id 51 # 虛拟路由編号,主從要一至
priority 99 # 優先級,數值越大,擷取處理請求的優先級越高 master要大于slave
nopreempt
authentication {
auth_type PASS # 指定認證方式。PASS簡單密碼認證(推薦),AH:IPSEC認證(不推薦)
auth_pass 1111 # 指定認證所使用的密碼。最多8位
}
unicast_peer { # 另外一台的伺服器ip,如果是多台就配多個ip
10.10.11.79
}
virtual_ipaddress { # 指定VIP位址
10.10.11.77
}
notify "/container/service/keepalived/assets/notify.sh"
}
vrrp_instance VI_2 {
state BACKUP
interface ens192 # 目前進行vrrp通訊的網絡接口卡(目前centos的網卡) 用ifconfig檢視你具體的網卡
virtual_router_id 52 # 虛拟路由編号,主從要一至
priority 150 # 優先級,數值越大,擷取處理請求的優先級越高 master要大于slave
nopreempt
authentication {
auth_type PASS # 指定認證方式。PASS簡單密碼認證(推薦),AH:IPSEC認證(不推薦)
auth_pass 1111 # 指定認證所使用的密碼。最多8位
}
unicast_peer { # 另外一台的伺服器ip,如果是多台就配多個ip
10.10.11.79
}
virtual_ipaddress { # 指定VIP位址
10.10.11.78
}
notify "/container/service/keepalived/assets/notify.sh"
}
每個節點這個四個檔案都配置好以後,可以啟動了。
執行
啟動:
docker-compose -f docker-compose.yml up -d
停止:
docker-compose down
檢視日志:
docker-compose logs
驗證
如果有一台Nginx伺服器挂了,Keepalived會自動在備Nginx伺服器上選一台當主伺服器.
1.可以把其中一台主控端中的nginx的容器停掉,然後通路兩個虛拟ip,發現是可以漂移的。而且等待兩秒鐘,nginx容器會自動重新開機。
2.直接把其中一台伺服器的nginx容器和keepalived容器全部停掉模拟伺服器當機的場景,通路兩個虛拟ip發現也是可以切換的。但是這種情況 容器不會自動重新開機,隻能手動重新開機。