系統環境
環境:centos7
etcd3
nginx1.14
confd
更換yum源
yum install vim wget net-tools -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
enabled=0
yum clean all
yum makecache
安裝NGINX
mkdir -p /opt/software
groupadd nginx
useradd -g nginx nginx
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel wget GeoIP GeoIP-devel
wget -P /opt/software http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf /opt/software/nginx-1.14.0.tar.gz -C /opt/software
cd /opt/software/nginx-1.14.0
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf
mkdir -p /usr/local/nginx/conf/conf.d
mkdir -p /usr/local/nginx/tmp/client
make && make install
/usr/local/nginx/sbin/nginx -v
confd安裝
# Download the binary
wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
# 重命名二進制文件,並移動到PATH的目錄下
mv confd-0.16.0-linux-amd64 /usr/local/bin/confd
chmod +x /usr/local/bin/confd
mkdir -p /etc/confd/conf.d
mkdir -p /etc/confd/template
# 驗證是否安裝成功
confd --help
etcd3安裝
yum install etcd3 -y
創建confd的模板文件
vim /etc/confd/conf.d/test.conf.toml
[template]
src = "test.conf.tmpl"
dest = "/usr/local/nginx/conf/conf.d/test.conf"
keys = [
"/nginx",
]
check_cmd = "/usr/local/nginx/sbin/nginx -t -c {{.src}}"
reload_cmd = "/usr/local/nginx/sbin/nginx nginx reload"
vim /etc/confd/template/test.conf.tmpl
upstream www {
{{range getvs "/nginx/https/www/upstream/*"}}server {{.}};{{end}}
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
etcd3創建key,vlaue
用的etcd3 所以要先傳環境變量
export ETCDCTL_API=3
創建key
etcdctl mkdir /nginx/https/www/upstream
給KEY賦值
etcdctl put /nginx/https/www/upstream/server1 192.168.1.2:80
etcdctl put /nginx/https/www/upstream/server1 192.168.1.11:80
etcdctl put /nginx/https/www/upstream/server4 192.168.1.44:80
測試
etcdctl get /nginx/https/www/upstream/server4
執行confd
confd -watch -backend etcdv3 -node http://127.0.0.1:2379
V3 查看全部KEY`
etcdctl --endpoints="http://127.0.0.1:2379" --prefix --keys-only=true get /