系统环境

环境: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 /

更多推荐