etcd+confd管理nginx

1.安装

wget https://github.com/etcd-io/etcd/releases/download/v3.3.12/etcd-v3.3.12-linux-amd64.tar.gz

tar zxvf etcd-v3.3.12-linux-amd64.tar.gz

cd etcd-v3.3.12-linux-amd64

cp etcd etcdctl /usr/bin/

 

2.配置

vi /usr/lib/systemd/system/etcd.service

[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

 

vi  /etc/etcd/etcd.conf

#[Member]
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="node1"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.56.14:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.56.14:2379,http://192.168.56.14:4001"
ETCD_INITIAL_CLUSTER="master=http://192.168.56.11:2380,node1=http://192.168.56.14:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

 

3.启动服务并测试

systemctl restart etcd

systemctl status etcd

etcdctl member list   #查看状态

etcdctl  set  hello  aaa   #设置测试值

到另外一台节点 etcdctl get hello  能看到值即可

4.confd配置

vi /etc/confd/conf.d/app01.conf.toml

[template]
# 默认在/etc/confd/templates目录下
src = "app01.conf.tmpl"
# 要更新的配置文件
dest = "/usr/local/nginx/conf/vhost/app01.conf"
keys = [
"/nginx/www/"
]
# 最后执行的命令
reload_cmd ="echo test"

 

vi /etc/confd/templates/app01.conf.tmpl

upstream www.{{getv "/nginx/www/server_name"}} {
{{range getvs "/nginx/www/upstream/*"}}
server {{.}};
{{end}}
}

server {
server_name www.{{getv "/nginx/www/server_name"}};
location / {
proxy_pass http://www.{{getv "/nginx/www/server_name"}};
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

5.在etcd上设置key

etcdctl -C http://192.168.56.11:2379 set /nginx/www/server_name   123.com

etcdctl -C http://192.168.56.11:2379 set /nginx/www/upstream/server01 “192.168.1.10:80”

etcdctl -C http://192.168.56.11:2379 set /nginx/www/upstream/server02 “192.168.1.11:80”

 

6.启动confd

confd -watch -backend etcd -node http://192.168.56.11:2379

 

posted @ 2019-04-18 18:31  liumj  阅读(618)  评论(0编辑  收藏  举报