前面我们已经通过jenkins+docker搭建了基本的持续集成环境,实现了服务的自动构建和部署,但是,我们遇到一个问题,jenkins构建出来的镜像部署后,需要通过ip:port去访问,有什么更好的方法吗?肯定是通过域名啊!前提是你注册一个域名,或者修改机器hosts文件。
本文介绍通过引入etcd+confd实现部署服务的自动注册,自动生成nginx配置文件,实现每个服务独立域名访问。
假设你的域名是: example.com,那么我们可以规划
dev.$servicename.example.com作为开发环境,
test.$servicename.example.comz作为服务的测试环境。
配置步骤:
首先将*.example.com 指向一台nginx服务器
增加vhost配置文件,假设86.6,86.8,86.11 是docker swarm集群中的机器,服务的名称为allinoneservice,那么我们可以增加一个配置文件nginx_vhosts/service.conf:
upstream test_service_allinoneservice { server 192.168.86.11:10091; server 192.168.86.6:10091; server 192.168.86.8:10091; } server { listen 80; server_name test.allinoneservice.example.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_read_timeout 300; proxy_set_header X-Real-IP $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass ; } }
修改nginx配置文件nginx.conf,在最后一个大括号前,将刚新建的配置文件包含进去:
include nginx_vhosts/*.conf;重启nginx,就可以通过test.allinoneservice.example.com访问服务了
通过服务注册自动生成配置文件第一步里,我们需要手动编写配置文件,有更好的方式吗?答案是通过服务注册+confd,自动生成配置文件。
docker 安装etcd集群首先,docker安装etcd作为注册中心,我们安装一个包含3个实例的集群,编写docker-compose.yml:
version: '3' services: etcd0: image: 192.168.86.8:5000/etcd ports: - "2379:2379" volumes: - etcd0:/etcd_data command: - /usr/local/bin/etcd - -name - etcd0 - --data-dir - /etcd_data - -advertise-client-urls - :2379 - -listen-client-urls - :2379 - -initial-advertise-peer-urls - :2380 - -listen-peer-urls - :2380 - -initial-cluster - etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380 etcd1: image: 192.168.86.8:5000/etcd ports: - "2380:2379" volumes: - etcd1:/etcd_data command: - /usr/local/bin/etcd - -name - etcd1 - --data-dir - /etcd_data - -advertise-client-urls - :2379 - -listen-client-urls - :2379 - -initial-advertise-peer-urls - :2380 - -listen-peer-urls - :2380 - -initial-cluster - etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380 etcd2: image: 192.168.86.8:5000/etcd ports: - "2381:2379" volumes: - etcd2:/etcd_data command: - /usr/local/bin/etcd - -name - etcd2 - --data-dir - /etcd_data - -advertise-client-urls - :2379 - -listen-client-urls - :2379 - -initial-advertise-peer-urls - :2380 - -listen-peer-urls - :2380 - -initial-cluster - etcd0=http://etcd0:2380,etcd1=http://etcd1:2380,etcd2=http://etcd2:2380 volumes: etcd0: etcd1: etcd2:注意,上面的image: 192.168.86.8:5000/etcd 是用的私有仓库,大家可以使用官方版本quay.io/coreos/etcd
然后启动etcd:
docker stack deploy -c docker-compose.yml etcd 服务注册etcd注册就简单了,可以通过etcd的rest api,例如:
curl :2379/v2/keys/services/test/allinoneservice/service1 -XPUT -d value="192.168.86.8:10091"所以,我们修改一下jenkins里的docker部署脚本,服务部署后自动向etcd注册,由于是swarm集群,因此我们可以注册多个ip。
echo "start remove old service" docker service rm ${service_name}-${env} echo "start create new service with latest builded image" docker service create --name ${service_name}-${env} --replicas ${replicas} --publish ${service_port}:${docker_expose_port} 192.168.86.8:5000/${service_name}-${env} echo "publish service to nginx" curl :2379/v2/keys/services/${env}/${service_name}/service1 -XPUT -d value="192.168.86.8:${service_port}" curl :2379/v2/keys/services/${env}/${service_name}/service2 -XPUT -d value="192.168.86.11:${service_port}" curl :2379/v2/keys/services/${env}/${service_name}/service3 -XPUT -d value="192.168.86.6:${service_port}"