Docker:Service

Prerequisites

Introduction

In part 3, we scale our application and enable load-balancing. To do this, we must go one level up in the hierarchy of a distributed application: the service.

  • Stack
  • Services (you are here)
  • Container (covered in part 2)

About services

In a distributed application, different pieces of the app are called “services.”

For example, if you imagine a video sharing site, it probably includes a service for storing application data in a database, a service for video transcoding in the background after a user uploads something, a service for the front-end, and so on.

Services are really just “containers in production.”

A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on.

Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.

Luckily it’s very easy to define, run, and scale services with the Docker platform

-- just write a docker-compose.yml file.

Your first docker-compose.yml file

A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

docker-compose.yml

Save this file as docker-compose.yml wherever you want. Be sure you have pushed the image you created in Part 2 to a registry, and update this .yml by replacing username/repo:tag with your image details.

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: username/repo:tag
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

 

This docker-compose.yml file tells Docker to do the following:

  • Pull the image we uploaded in step 2 from the registry.
  • Run 5 instances of that image as a service called web, limiting each one to use, at most, 10% of the CPU (across all cores), and 50MB of RAM.
  • Immediately restart containers if one fails.
  • Map port 4000 on the host to web’s port 80.
  • Instruct web’s containers to share port 80 via a load-balanced network called webnet. (Internally, the containers themselves publish to web’s port 80 at an ephemeral port.)
  • Define the webnet network with the default settings (which is a load-balanced overlay network).

Run your new load-balanced app

Before we can use the docker stack deploy command we first run:

docker swarm init

 Note: We get into the meaning of that command in part 4. If you don’t run docker swarm init you get an error that “this node is not a swarm manager.” 

Now let’s run it. You need to give your app a name. Here, it is set to getstartedlab:

docker stack deploy -c docker-compose.yml getstartedlab

Our single service stack is running 5 container instances of our deployed image on one host. Let’s investigate.

Get the service ID for the one service in our application:

docker service ls

Look for output for the web service, prepended with your app name. If you named it the same as shown in this example, the name is getstartedlab_web.

The service ID is listed as well, along with the number of replicas, image name, and exposed ports.

A single container running in a service is called a task. Tasks are given unique IDs that numerically increment, up to the number of replicas you defined in docker-compose.yml. List the tasks for your service:

docker service ps getstartedlab_web

  

Tasks also show up if you just list all the containers on your system, though that is not filtered by service:

docker container ls -q

 

You can run curl -4 http://localhost several times in a row, or go to that URL in your browser and hit refresh a few times.

Either way, the container ID changes, demonstrating the load-balancing;

with each request, one of the 5 tasks is chosen, in a round-robin fashion, to respond.

The container IDs match your output from the previous command (docker container ls -q).

Running Windows 10?

Windows 10 PowerShell should already have curl available, but if not you can grab a Linux terminal emulator like Git BASH, or download wget for Windows which is very similar.

Slow response times?

Depending on your environment’s networking configuration, it may take up to 30 seconds for the containers to respond to HTTP requests. This is not indicative of Docker or swarm performance, but rather an unmet Redis dependency that we address later in the tutorial. For now, the visitor counter isn’t working for the same reason; we haven’t yet added a service to persist data.

Scale the app

You can scale the app by changing the replicas value in docker-compose.yml, saving the change, and re-running the docker stack deploy command:

docker stack deploy -c docker-compose.yml getstartedlab

  

Docker performs an in-place update, no need to tear the stack down first or kill any containers.

Now, re-run docker container ls -q to see the deployed instances reconfigured.

If you scaled up the replicas, more tasks, and hence, more containers, are started.

 

Take down the app and the swarm

  • Take the app down with docker stack rm:

    docker stack rm getstartedlab
    
  • Take down the swarm.
    docker swarm leave --force
    

It’s as easy as that to stand up and scale your app with Docker.

You’ve taken a huge step towards learning how to run containers in production.

Up next, you learn how to run this app as a bonafide swarm on a cluster of Docker machines.

Note: Compose files like this are used to define applications with Docker, and can be uploaded to cloud providers using Docker Cloud, or on any hardware or cloud provider you choose with Docker Enterprise Edition.

Recap and cheat sheet (optional)

Here’s a terminal recording of what was covered on this page:

  

To recap, while typing docker run is simple enough, the true implementation of a container in production is running it as a service.

Services codify a container’s behavior in a Compose file, and this file can be used to scale, limit, and redeploy our app.

Changes to the service can be applied in place, as it runs, using the same command that launched the service: docker stack deploy.

Some commands to explore at this stage:

docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
docker swarm leave --force      # Take down a single node swarm from the manager

  

 

原文地址:https://www.cnblogs.com/panpanwelcome/p/9208845.html

时间: 2024-11-10 01:08:06

Docker:Service的相关文章

docker:编排与部署小神器&gt;&gt;Compose概念篇

docker-compose是什么 Compose是定义和运行多容器Docker应用程序的工具. 使用Compose,您可以使用YAML文件来配置应用程序的服务. 然后,使用单个命令,您可以创建并启动配置中的所有服务. Compose适用于所有环境:生产,开发,测试以及CI工作流程.使用Compose基本上是一个三步过程: 使用Dockerfile定义应用程序的环境,以便在任何地方进行复制. 在docker-compose.yml中定义组成应用程序的服务,以便它们可以在隔离的环境中一起运行. 运

【亲测有效】Centos安装完成docker后启动docker报错docker: unrecognized service的两种解决方案

今天在学习Docker的时候 使用yum install docker安装完后启动不了,报错如下: [[email protected] ~]# service docker start docker: unrecognized service 一直停留在以上步骤,如果有遇到和我一样类似问题的小伙伴可以按照如下方法进行安装,即可安装成功~~ 方法一: 先移除docker [[email protected] ~]# yum remove docker 再移除docker-selinux(如果你之

Docker Kubernetes Service 网络服务代理模式详解

Docker Kubernetes  Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注:kubernetes deployment服务分配服务器负载均衡VIP只能NODE节点单独访问,这里需要外网用户可以放问到容器内,这里就需要用到service. 网络代理模式 kube-proxy v1.0中只支持userspace模式,在v1.1中,添加了iptables代理,在v1.2开始ip

docker:安装mysql多个

docker:安装mysql多个 文章来源:https://www.cnblogs.com/hello-tl/p/9238298.html 1.首先安装docker 参照一下网址安装docker docker:安装 https://www.cnblogs.com/hello-tl/p/8901132.html 参照安装一个mysql docker:安装mysql https://www.cnblogs.com/hello-tl/p/9234429.html 2.创建文件 # mkdir /dat

docker:搭建ELK 开源日志分析系统

ELK 是由三部分组成的一套日志分析系统, Elasticsearch: 基于json分析搜索引擎,Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片, 索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logstash:动态数据收集管道,Logstash是一个完全开源的工具,它可以对你的日志进行收集.分析,并将其存储供以后使用 Kibana:可视化视图,将elasticsearh所收集的data通过视图展现.kibana 是一个

我的Android进阶之旅------&gt;如何解决Android 5.0中出现的警告: Service Intent must be explicit:

1.错误描述 今天在Android4.4 的小米4手机上运行我的程序的时候没有报错,而在Android 5.1的华为P7上运行我的程序的时候报了以下的错误,错误提示如下: E/AndroidRuntime(12500): FATAL EXCEPTION: main E/AndroidRuntime(12500): Process: com.xtc.watch, PID: 12500 E/AndroidRuntime(12500): java.lang.IllegalArgumentExcepti

Android开发6:Service的使用(简单音乐播放器的实现)

前言 啦啦啦~各位好久不见啦~博主最近比较忙,而且最近一次实验也是刚刚结束~ 好了不废话了,直接进入我们这次的内容~ 在这篇博文里我们将学习Service(服务)的相关知识,学会使用 Service 进行后台工作, 学会使用 Service 与 Activity 进行通信,并在此知识基础上学会使用 MediaPlayer和简单的多线程编程.使用 Handle 更新 UI,并设计成功一个简单的音乐播放器. 是不是很高大上呢~一起来学习~ 基础知识 Service作为Android四大组件之一,在每

Linux: service network/Network/NetworkManager

Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从新启动,来激活网络新配置的使得配置生效,这部分操作和从新启动系统时时一样的作用.制御(控制)是/etc/init.d/network这个文件,可以用这个文件后面加上下面的参数来操作网络服务.例如:/etc/init.d/networkrestart同样也可以用service这个命令来操作网络服务例如

赵雅智:service与访问者之间进行通信,数据交换

服务类 中间人:service服务中的bind对象 创建中间人并通过onBinder方法的return暴露出去 在服务类创建一个服务 创建中间人继承Binder MainActivity类 声明服务的中间人 private ServiceTese.MyBinder myBinder; 链接成功的时候赋值service 设置按钮点击事件 输出结果: 赵雅智:service与访问者之间进行通信,数据交换