2020年9月7日星期一

Docker swarm init, Add nodes, Deploy services, Scale number of service instances and Delete Services.

Docker Swarm Init

$  docker swarm init --advertise-addr  192.168.99.100

Swarm initialized: current node (3ukhpra7zfoz6a1phx3mayth1) is now a manager.

To add a worker to this swarm, run the following command:

 docker swarm join --token SWMTKN-1-0hn48r8yx3lxr0vwz7t8b39akqm1gukjeie67c4ipomfq54qd9-29sv3h2u5ip07rnor5bjmh39y 192.168.99.100:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

The --advertise-addr flag configures the manager node to publish its address as 192.168.99.100. The other nodes in the swarm must be able to access the manager at the IP address.
The output includes the commands to join new nodes to the swarm. Nodes will join as managers or workers depending on the value for the --token flag.

Note: If you are using Docker Desktop for Mac or Docker Desktop for Windows to test single-node swarm, simply run docker swarm init with no arguments. There is no need to specify--advertise-addr in this case. To learn more, see the topic on how to Use Docker Desktop or Mac or Docker Desktop for Windows with Swarm.

Run docker info to view the current state of the swarm:

$ docker info
Client:
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)

Server:
 Containers: 4
  Running: 2
  Paused: 0
  Stopped: 2
 Images: 13
 Server Version: 19.03.11
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: active
  NodeID: 3ukhpra7zfoz6a1phx3mayth1
  Is Manager: true
  ClusterID: lknzb8g9tgmgp6cumnw2qqo7i
  Managers: 1
  Nodes: 1
  Default Address Pool: 10.0.0.0/8  
  SubnetSize: 24
  Data Path Port: 4789
  Orchestration:
   Task History Retention Limit: 5
  Raft:
   Snapshot Interval: 10000
   Number of Old Snapshots to Retain: 0
   Heartbeat Tick: 1
   Election Tick: 10
  Dispatcher:
   Heartbeat Period: 5 seconds
  CA Configuration:
   Expiry Duration: 3 months
   Force Rotate: 0
  Autolock Managers: false
  Root Rotation In Progress: false
  Node Address: 127.0.0.1
  Manager Addresses:
   127.0.0.1:2377
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 4.4.0-96-generic
 Operating System: Alpine Linux v3.12 (containerized)
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 31.4GiB
 Name: node1
 ID: N37T:M2Y6:XFOS:FNZI:HVOV:KXHH:ZC5W:OK3U:5A4H:VKDZ:M63T:SWYR
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 54
  Goroutines: 167
  System Time: 2020-09-07T08:11:13.359727764Z
  EventsListeners: 0
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: true
 Insecure Registries:
  127.0.0.1
  127.0.0.0/8
 Live Restore Enabled: false
 Product License: Community Engine

WARNING: API is accessible on http://0.0.0.0:2375 without encryption.
         Access to the remote API is equivalent to root access on the host. Refer
         to the 'Docker daemon attack surface' section in the documentation for
         more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
WARNING: No swap limit support
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

Run the docker node ls command to view information about nodes:

$ docker node ls

ID                           HOSTNAME  STATUS  AVAILABILITY  MANAGER STATUS
dxn1zf6l61qsb1josjja83ngz *  manager1  Ready   Active        Leader

The * next to the node ID indicates that you’re currently connected on this node.

Docker Engine swarm mode automatically names the node for the machine host name. The tutorial covers other columns in later steps.

Add nodes to the swarm

Once you’ve created a swarm with a manager node, you’re ready to add worker nodes.

  1. Open a terminal and ssh into the machine where you want to run a worker node. This tutorial uses the name worker1.

  2. Run the command produced by the docker swarm init output from the Create a swarm tutorial step to create a worker node joined to the existing swarm:

    $ docker swarm join \
      --token  SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
      192.168.0.28:2377
    
    This node joined a swarm as a worker.   # This node's ip is 192.168.0.27

  1. Open a terminal and ssh into the machine where the manager node runs and
  2. run the docker node ls command to see the worker nodes:
$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3ukhpra7zfoz6a1phx3mayth1 *   node1               Ready               Active              Leader              19.03.11
ujaw7yd9n932w72i48d01grsk     node2               Ready               Active                                  19.03.11
15dss2zuext1jw28418nugg9x     node3               Ready               Active                                  19.03.11

The MANAGER column identifies the manager nodes in the swarm. The empty status in this column for worker1 and worker2 identifies them as worker nodes.

Swarm management commands like docker node ls only work on manager nodes.

Deploy a service to the swarm

After you create a swarm, you can deploy a service to the swarm. For this tutorial, you also added worker nodes, but that is not a requirement to deploy a service.

  1. Open a terminal and ssh into the machine where you run your manager node. For example, the tutorial uses a machine named manager1.

  2. Run the following command:

    $ docker service create --replicas 1 --name helloworld alpine ping docker.com
    
    9uk4639qpg7npwf3fn2aasksr
    
    • The docker service create command creates the service.
    • The --name flag names the service helloworld.
    • The --replicas flag specifies the desired state of 1 running instance.
    • The arguments alpine ping docker.com define the service as an Alpine Linux container that executes the command ping docker.com.
  3. Run docker service ls to see the list of running services:

    $ docker service ls
    
    ID            NAME        SCALE  IMAGE   COMMAND
    9uk4639qpg7n  helloworld  1/1    alpine  ping docker.com

Inspect a service on the swarm

When you have deployed a service to your swarm, you can use the Docker CLI to see details about the service running in the swarm.

  1. If you haven’t already, open a terminal and ssh into the machine where you run your manager node. For example, the tutorial uses a machine named manager1.

  2. Run docker service inspect --pretty <SERVICE-ID> to display the details about a service in an easily readable format.

    To see the details on the helloworld service:

    [manager1]$ docker service inspect --pretty helloworld
    
    ID:             gg8j8u8qhwtvfbz4kphkx4e1c
    Name:           helloworld
    Service Mode:   Replicated
     Replicas:      1
    Placement:
    UpdateConfig:
     Parallelism:   1
     On failure:    pause      #can change to restart
     Monitoring Period: 5s
     Max failure ratio: 0
     Update order:      stop-first
    RollbackConfig:
     Parallelism:   1
     On failure:    pause
     Monitoring Period: 5s
     Max failure ratio: 0
     Rollback order:    stop-first
    ContainerSpec:
     Image:         alpine:latest@sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
     Args:          ping docker.com 
     Init:          false
    Resources:
    Endpoint Mode:  vip

Tip: To return the service details in json format, run the same command without the --pretty flag.

Run docker service ps <SERVICE-ID> to see which nodes are running the service:

[manager1]$ docker service ps helloworld
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
roelhski88xz        helloworld.1        alpine:latest       node1               Running             Running 5 minutes ago 

Swarm also shows you the DESIRED STATE and CURRENT STATE of the service task so you can see if tasks are running according to the service definition.

Run docker ps on the node where the task is running to see details about the container for the task.

Tip: If helloworld is running on a node other than your manager node, you must ssh to that node.

[worker2]$ docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
70038e250789        alpine:latest       "ping docker.com"        7 minutes ago       Up 7 minutes  

Scale the service in the swarm

Once you have deployed a service to a swarm, you are ready to use the Docker CLI to scale the number of containers in the service. Containers running in a service are called “tasks.”
  1. Run the following command to change the desired state of the service running in the swarm:

    $ docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
    

    For example, start 5 instences of the helloworld sevices:

    $ docker service scale helloworld=5  # start 5 instances. you can change it later.
    
    helloworld scaled to 5
    overall progress: 5 out of 5 tasks 
    1/5: running   
    2/5: running   
    3/5: running   
    4/5: running   
    5/5: running   
    verify: Service converged 
    
  2. Run docker service ps <SERVICE-ID> to see the updated task list:

    $ docker service ps helloworld
    ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
    roelhski88xz        helloworld.1        alpine:latest       node1               Running             Running 10 minutes ago                           
    6o4ts34bmttp        helloworld.2        alpine:latest       node1               Running             Running about a minute ago                       
    7amy0pz964ww        helloworld.3        alpine:latest       node1               Running             Running about a minute ago                       
    zo9ju37c68ef        helloworld.4        alpine:latest       node1               Running             Running about a minute ago                       
    7wnp0v2l0vk1        helloworld.5        alpine:latest       node1               Running             Running about a minute ago                    
    
  3. Run docker ps to see the containers running on the node where you’re connected. The following example shows the tasks running on manager1:

    $ docker ps
    
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    fa05e955ff53        alpine:latest       "ping docker.com"        2 minutes ago       Up 2 minutes                                 helloworld.3.7amy0pz964ww9hfiu2qsyq0uu
    40a365af4a57        alpine:latest       "ping docker.com"        2 minutes ago       Up 2 minutes                                 helloworld.2.6o4ts34bmttpaa44qwhkqdxpr
    86ec03252986        alpine:latest       "ping docker.com"        2 minutes ago       Up 2 minutes                                 helloworld.4.zo9ju37c68efkpn42ltfy7omv
    f896f9b7820d        alpine:latest       "ping docker.com"        2 minutes ago       Up 2 minutes                                 helloworld.5.7wnp0v2l0vk140c16vztmqegl
    70038e250789        alpine:latest       "ping docker.com"        11 minutes ago      Up 11 minutes                                helloworld.1.roelhski88xzkfwfr6v5r0egv
    
    

    If you want to see the containers running on other nodes, ssh into those nodes and run the docker ps command.


Delete the service running on the swarm

The remaining steps in the tutorial don’t use the helloworld service, so now you can delete the service from the swarm.
  1. Run docker service rm helloworld to remove the helloworld service.

    $ docker service rm helloworld
    
    helloworld
    
  2. Run docker service inspect <SERVICE-ID> to verify that the swarm manager removed the service. The CLI returns a message that the service is not found:

    $ docker service inspect helloworld
    []
    Status: Error: no such service: helloworld, Code: 1
    
  3. Even though the service no longer exists, the task containers take a few seconds to clean up. You can use docker ps on the nodes to verify when the tasks have been removed.

    $ docker ps
       CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               




没有评论: