Confd實現Nginx配置文件自動管理

Confd是一個輕量級的配置管理工具。經過查詢Etcd,結合配置模板引擎,保持本地配置最新,同時具有按期探測機制,配置變動自動reload。其後端支持的數據類型有:etcd、consul、vault、environment variables、redis、zookeeper、dynamodb、stackengine、rancher。不過通常使用Confd和etcd的配合使用比較多。
Confd實現Nginx配置文件自動管理
前端服務器:
服務器IP 主機名 安裝組件 備註
192.168.27.211 Client1 etcd+confd+nginx+keepalived 192.168.27.110(Vip)
(http://nginx.jerry.com)
192.168.27.212 Client2 etcd+confd+nginx+keepalived
192.168.27.213 Client3 etcd+confd+nginx+keepalived
192.168.27.210 master ansible 堡壘機html

後端服務器(web站):
服務器IP 功能
192.168.26.210 web1
192.168.26.211 web2
192.168.26.212 web3前端

安裝etcd集羣確保正常略(ansible k8s -m shell -a'etcdctl endpoint health')。
Confd實現Nginx配置文件自動管理node

後端web服務器安裝配置略(注意VIP域名映射關係):
簡略介紹安裝keepalived安裝配置:
[root@client1 ~]# yum install keepalived –y
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
192.168.27.211:
[root@client1 keepalived]# cat keepalived.conf
! Configuration File for keepalived nginx

global_defs {
router_id nginx1
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2git

}
vrrp_instance VI_1 {
state MASTER
interface ens160
virtual_router_id 20
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}github

192.168.27.212:
! Configuration File for keepalived web

global_defs {
router_id nginx2
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2redis

}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 20
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}
192.168.27.213:
! Configuration File for keepalived shell

global_defs {
router_id nginx3
}
vrrp_script chk_http_port {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2vim

}
vrrp_instance VI_1 {
state BACKUP
interface ens160
virtual_router_id 20
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass jerry520
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.27.110/22
}
}
Nginx檢測腳本:三臺服務器上都須要配置(同樣的)vim /etc/keepalived/chk_nginx.sh
[root@client1 keepalived]# cat chk_nginx.sh
#!/bin/bash
A=ps -C nginx --no-header |wc -l
if [ $A -eq 0 ];then
echo 'nginx server is died'
/etc/init.d/keepalived stop
fi

nginx安裝:
yum install nginx -y
nginx.conf配置文件:三臺服務器保持同樣 vim /etc/nginx/nginx.conf
user nginx ;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream nginx.jerry.com {
server 192.168.26.210:80;
server 192.168.26.211:80;
server 192.168.26.212:80;

}
server {
listen 80;
server_name nginx.jerry.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://nginx.jerry.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
分別安裝配置好KEEPALIved和nginx(轉發器)並啓動運行觀察效果:

Confd安裝配置:

[root@master etc]# ansible k8s -m copy -a"src=/etc/confd dest=/etc/"

[root@master bin]# ansible k8s -m copy -a"src=/usr/bin/confd dest=/usr/bin/confd"
[root@master bin]# ansible k8s -m shell -a"cd /usr/bin;chmod +x confd "
[root@master conf.d]# ansible k8s -m shell -a'ls /usr/bin/confd -l'
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
建立配置目錄
mkdir -p /etc/confd/{conf.d,templates}
conf.d # 資源模板,下面文件必須以toml後綴
templates # 配置文件模板,下面文件必須以tmpl後綴
Confd實現Nginx配置文件自動管理
建立confd配置文件:
[root@client1 confd]# cat conf.d/sync_nginx.toml
[template]
prefix = "/nginx/www"
src = "nginx.conf.tmpl"
dest = "/etc/nginx/conf.d/mynginx.conf"
owner = "nginx"
mode = "0644"
keys = [
"/server_name",
"/upstream",
]
reload_cmd = "/usr/sbin/nginx -s reload"
建立模板文件:
upstream {{getv "/server_name"}}.jerry.com {
{{ range getvs "/upstream/*"}}
server {{.}};
{{end}}
}
server {
listen 80;
server_name {{getv "/server_name"}}.jerry.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://{{getv "/server_name"}}.jerry.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

Confd實現Nginx配置文件自動管理
[root@client1 templates]# confd -watch -backend="etcdv3" -node http://192.168.27.211:2379
Confd實現Nginx配置文件自動管理
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb1 "192.168.26.210"
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb2 "192.168.26.211"
[root@client1 conf.d]# etcdctl put /nginx/www/upstream/serverweb3 "192.168.26.212"
[root@client1 conf.d]# etcdctl put /nginx/www/server_name "nginx"
觀察集羣中每個結點NGINX反向代理配置文件變化:
27.212:
Confd實現Nginx配置文件自動管理
27.213:
Confd實現Nginx配置文件自動管理
27.211:

Confd實現Nginx配置文件自動管理
咱們再來一次觀察下變化,此次經過27.212更換鍵值/nginx/www/server_name 咱們把值改成httpd(原來爲nginx)觀察27.211上NGINX配置文件是否更改變化。
[root@client2 conf.d]# etcdctl put /nginx/www/server_name "httpd"
Confd實現Nginx配置文件自動管理
配置文件瞬間更改:
Confd實現Nginx配置文件自動管理
測試:假如要增刪改後端服務器(把後端WEB服務192.168.26.210進行刪除操做).
[root@client2 conf.d]# etcdctl del /nginx/www/upstream/serverweb1
Confd實現Nginx配置文件自動管理
配置文件中已經將後端服務器192.168.26.210無感知地移出並從新加載配置
Confd實現Nginx配置文件自動管理
訪問也變了
Confd實現Nginx配置文件自動管理
用公網IP:192.168.27.100(vip)訪問(負載均衡採用輪詢):
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
經過域名(http://nginx.jerry.com)訪問後端站點(負載均衡採用輪詢):
記得作公網域名解析或更換本地HOST文件:
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理
Confd實現Nginx配置文件自動管理

參考文獻:https://github.com/kelseyhightower/confd/blob/master/docs/quick-start-guide.md
https://github.com/kelseyhightower/confd/blob/master/docs/template-resources.md
https://github.com/kelseyhightower/confd/blob/master/docs/templates.md

相關文章
相關標籤/搜索