微服务实践——Docker与服务发现
Posted 架构师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微服务实践——Docker与服务发现相关的知识,希望对你有一定的参考价值。
Docker Java App Load Balanced By nginx Or Apache HTTP, Mongo Replica Set And Hazelcast Cluster
背景
为了发挥Docker在跨多个服务器的分布式应用程序的部署(甚至是跨区域)的能力,人们不应该限制哪些服务进到哪个服务器。动态可扩展性(或自动缩放)的关键环境的要求(如生产环境)不只是适用于新的微服务架构设计。也适用于典型的单片应用程序的部署。如果这些服务绑定在特定的服务器上,那么这些程序的未来的可扩展性是很困难的。
为了能够实现服务发现,我们需要满足以下条件:
Service registration 在服务器上保存这些正在运行服务的端口。
Service discovery 实现发现我们在服务器保存在注册过程中的信息
我们还要实现强的服务,以满足企业级对服务发现的要求。包括以下这些问题:
我们如何注销停止工作的服务?
我们如何对这些”发现了的”服务实施负载均衡?
如果服务在横向扩展或者缩放的过程中被删除了怎么办?
大多数典型的服务发现工具拥有某种高度可用的分布式(key/value)存储,大家可以参考阅读这篇博客http://technologyconversations.com/2015/09/08/service-discovery-zookeeper-vs-etcd-vs-consul/
这些工具的主要缺点是他们自己在容器中运行对第三方工具的依赖。为了使用Consul,打个比方。一个用户需要在程序中使用Consul 和Registrator 容器-这一定会使容器的数量增加。那么用户就需要管理这些容器。
DCHQ,从另一方面说。如果使用代理来协调服务注册和服务发现。把信息存储在底层DCHQ数据库。这意味着我们不需要附加的容器。另外,服务发现框架,允许用户自定义某些事件需要被执行的同时,利用的不只是IP和应用程序中的其他容器的主机名的信息提供了更大的灵活性。在部署的时候使用的也是环境变量的值。
这并不等于说服务发现框架使用DHCQ就能完全替代其它工具,最好的工具还是能够满足业务的需求。
在这篇blog中,我们将介绍使用Docker部署3个服务发现的程序,包括:Nginx & Apache HTTP Server load balancing, Mongo Replica Set and Hazelcast Cluster.
包括以下内容:
构建能够在任意Linux 主机上面运行的YAML-based 程序模板
在任何云基础架构上的配置和自动扩展(本文我们以Microsoft Azure为例)
在Microsoft Azure集群部署分布式,高可用性应用程序
监控运行容器的 CPU, Memory & I/O
Tomacat 应用服务集群扩展
MongoDB的分片服务扩展
Hazelcast 集群扩展
构建能够在任意Linux 主机上面运行的YAML-based 程序模板
用户一旦登录到DCHQ (either the hosted DCHQ.io or on-premise version),用户可以导航到Manage>App/Machine,然后可以点击+创建新的Docker Compose模板
我们使用 Docker Hub官方镜像创建了5 个程序的模板
这些模板包括了一下程序栈:
Multi-Tier Java Application with Nginx, Tomcat, Solr and Mongo — using Consul and Registrator for service discovery
Multi-Tier Java Application with Nginx, Tomcat, Solr and Mongo — using DCHQ’s service discovery framework
Multi-Tier Java Application with Apache HTTP Server, Tomcat, Solr, and Cassandra) — using DCHQ’s service discovery framework
Mongo Replica Set — using DCHQ’s service discovery framework
Hazelcast Cluster — using DCHQ’s service discovery framework
更多关于Docker application modeling in DCHQ的资料可以访问: http://dchq.co/docker-compose.html
更多关于Docker service discovery in DCHQ的资料可以访问: http://dchq.co/docker-service-discovery.html
Request Time & Post-Provision 的Web Servers 和 Application Servers配置插件
通过这些程序的模板,你会发现一些容器为了配置容器会调用Bash脚本插件。这些脚本对post-provision操作执行的非常好。
这些插件可以在Manage > Plug-ins导航中创建,一旦这些脚本启动是,DHCQ代理服务会执行这些容器里面的脚本。用户可以指定一个能被在请求之后被覆盖的参数。在$符号之前的都可以被成为参数,比如:$file_url 可以是一个参数,它允许开发者指定的网址下载一个WAR文件,还可以在当用户希望更新运行在容器上例如Java WAR文件请求时间和后规定被覆盖:
该插件ID需要在定义YAML应用程序模板时候提供,例如:调用Nginxbash脚本插件。我们可以参照下面的plug-in ID:
你会发现,同样的Nginx插件,在不同的阶段得到了执行。
当 Nginx 容器 创建的时候 — 在这种情况下, 应用程序的容器IP会被注入到默认配置文件下面。以便实现服务的负载均衡。
然而这里的服务发现框架同时做着服务发现和服务注册的事情。
服务发现插件的生命周期
on_create — executes the plug-in when creating the container
on_start — executes the plug-in after a container starts
on_stop — executes the plug-in before a container stops
on_destroy — executes the plug-in before destroying a container
post_create — executes the plug-in after the container is created and running
post_start[:Node] — executes the plug-in after another container starts
post_stop[:Node] — executes the plug-in after another container stops
post_destroy[:Node] — executes the plug-in after another container is destroyed
post_scale_out[:Node] — executes the plug-in after another cluster of containers is scaled out
post_scale_in[:Node] — executes the plug-in after another cluster of containers is scaled in
要访问Nginx, Apache HTTP Server (httpd), Mongo Replica Set, and Hazelcast Cluster需要遵循EULA许可。
Sign Up for FREE on DCHQ.io — http://dchq.io (no credit card required)
Download DCHQ On-Premise Standard Edition for FREE — http://dchq.co/dchq-on-premise-download.html
配置部署在多台主机HA的 cluster_size 与host 参数
host1, host2, host3, etc. – selects a host randomly within a data-center (or cluster) for container deployments
IP Address 1, IP Address 2, etc. — allows a user to specify the actual IP addresses to use for container deployments
Hostname 1, Hostname 2, etc. — allows a user to specify the actual hostnames to use for container deployments
Wildcards (e.g. “db-”, or “app-srv-”) – to specify the wildcards to use within a hostname
通过镜像绑定环境变量
{{alphanumeric | 8}} – creates a random 8-character alphanumeric string. This is most useful for creating random passwords.
{{Image Name | ip}} – allows you to enter the host IP address of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a connection with the database.
{{Image Name | container_ip}} – allows you to enter the name of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a secure connection with the database (without exposing the database port).
{{Image Name | container_private_ip}} – allows you to enter the internal IP of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a secure connection with the database (without exposing the database port).
{{Image Name | port_Port Number}} – allows you to enter the Port number of a container as a value for an environment variable. This is most useful for allowing the middleware tier to establish a connection with the database. In this case, the port number specified needs to be the internal port number – i.e. not the external port that is allocated to the container. For example, {{PostgreSQL | port_5432}} will be translated to the actual external port that will allow the middleware tier to establish a connection with the database.
{{Image Name | Environment Variable Name}} – allows you to enter the value an image’s environment variable into another image’s environment variable. The use cases here are endless – as most multi-tier applications will have cross-image dependencies.
构建 Nginx Docker 镜像的 Consul
Git URL – https://github.com/dchqinc/nginx-consul.git
Git Branch – this field is optional — but a user can specify a branch from a GitHub project. The default branch is master.
Git Credentials – a user can store the credentials to a private GitHub repository securely in DCHQ. This can be done by navigating toManage > Cloud Providers and Repos and clicking on the + to select Credentials
Cluster – the building of Docker images is orchestrated through the DCHQ agent. As a result, a user needs to select a cluster on which an agent will be used to execute the building of Docker images. If a cluster has not been created yet, please refer to this sectionto either register already running hosts or automate the provisioning of new virtual infrastructure.
Push to Registry – a user can push the newly created image on either a public or private repository on Docker Hub or Quay. To register a Docker Hub or Quay account, a user should navigate toManage > Cloud Providers and clicking on the + to select Docker Registries
Repository – this is the name of the repository on which the image will be pushed. For example, our image was pushed todchq/nginx-consul:latest
Tag – this is the tag name that you would like to give for the new image. The supported tag names in DCHQ include:
{{date}} — formatted date
{{timestamp}} — the full time-stamp
Cron Expression – a user can schedule the building of Docker images using out-of-box cron expressions. This facilitates daily and nightly builds for users.
Multi-Tier Java (Consul-Nginx-Tomcat-Solr-Mongo)
Nginx:
image: dchqinx-consul:latest
publish_all: true
mem_min: 50m
host: host1
plugins:
- !plugin
id: GINmu
restart: true
lifecycle: on_create
arguments:
- APPSERVER_IP={{AppServer | container_private_ip}}
- SERVICE_NAME={{AppServer | SERVICE_NAME}}
- SERVICE_TAGS={{AppServer | SERVICE_TAGS}}
- CONSUL_IP={{Consul | container_private_ip}}
AppServer:
image: tomcat:8.0.21-jre8
mem_min: 600m
host: host1
cluster_size: 1
environment:
- mongo_url={{Mongo|container_private_ip}}:27017/dchq
- solr_host={{Solr|container_private_ip}}
- solr_port=8983
- SERVICE_NAME=app
- SERVICE_TAGS=production
plugins:
- !plugin
id: oncXN
restart: true
arguments:
- file_url=https://github.com/dchqinc/dchq-docker-java-solr-mongo-cassandra-example/raw/master/dbconnect.war
- dir=/usr/local/tomcat/webapps/ROOT.war
- delete_dir=/usr/local/tomcat/webapps/ROOT
Consul:
image: progrium/consul:latest
host: host1
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
command: -server -bootstrap -advertise 10.0.2.15
Registrator:
image: gliderlabs/registrator:latest
host: host1
command: consul://<HOST_IP>:8500
volumes:
- "ar/run/docker.sock:/tmp/docker.sock"
Solr:
image: solr:latest
mem_min: 300m
host: host1
publish_all: false
plugins:
- !plugin
id: doX8s
restart: true
arguments:
- file_url=https://github.com/dchqinc/dchq-docker-java-solr-mongo-cassandra-example/raw/master/names.zip
Mongo:
image: mongo:latest
host: host1
mem_min: 400m
Nginx:
image: dchqinx-consul:latest
publish_all: true
mem_min: 50m
host: host1
plugins:
- !plugin
id: GINmu
restart: true
lifecycle: on_create
arguments:
- APPSERVER_IP={{AppServer | container_private_ip}}
- SERVICE_NAME={{AppServer | SERVICE_NAME}}
- SERVICE_TAGS={{AppServer | SERVICE_TAGS}}
- CONSUL_IP={{Consul | container_private_ip}}
AppServer:
image: tomcat:8.0.21-jre8
mem_min: 600m
host: host1
cluster_size: 1
environment:
- mongo_url={{Mongo|container_private_ip}}:27017/dchq
- solr_host={{Solr|container_private_ip}}
- solr_port=8983
- SERVICE_NAME=app
- SERVICE_TAGS=production
plugins:
- !plugin
id: oncXN
restart: true
arguments:
- file_url=https://github.com/dchqinc/dchq-docker-java-solr-mongo-cassandra-example/raw/master/dbconnect.war
- dir=/usr/local/tomcat/webapps/ROOT.war
- delete_dir=/usr/local/tomcat/webapps/ROOT
Consul:
image: progrium/consul:latest
host: host1
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:53/udp"
command: -server -bootstrap -advertise 10.0.2.15
Registrator:
image: gliderlabs/registrator:latest
host: host1
command: consul://<HOST_IP>:8500
volumes:
- "ar/run/docker.sock:/tmp/docker.sock"
Solr:
image: solr:latest
mem_min: 300m
host: host1
publish_all: false
plugins:
- !plugin
id: doX8s
restart: true
arguments:
- file_url=https://github.com/dchqinc/dchq-docker-java-solr-mongo-cassandra-example/raw/master/names.zip
Mongo:
image: mongo:latest
host: host1
mem_min: 400m
来源:谢权'blog
原文:https://xiequan.info/在docker上运行微服务实践/
转载文章,向原作者致敬!如有侵权或不周之处,敬请劳烦联系若飞(微信:1321113940)马上删除,谢谢!
·END·
我们都是架构师!
以上是关于微服务实践——Docker与服务发现的主要内容,如果未能解决你的问题,请参考以下文章