Academy PRO: Docker. Lecture 3

38
Orchestration Basics AcademyPRO

Transcript of Academy PRO: Docker. Lecture 3

Page 1: Academy PRO: Docker. Lecture 3

Orchestration BasicsAcademyPRO

Page 2: Academy PRO: Docker. Lecture 3

Today’sagenda

Docker Networking Basics

Compose

Scaling out with Swarm Mode

Page 3: Academy PRO: Docker. Lecture 3

Default Networks

When you install Docker, it creates three networks automatically. You can list

these networks using the docker network ls command:

docker network ls

NETWORK ID NAME DRIVER

7fca4eb8c647 bridge bridge

9f904ee27bf5 none null

cf03ee007fb4 host host

These three networks are built into Docker. When you run a container, you can

use the --network flag to specify which networks your container should

connect to.

Page 4: Academy PRO: Docker. Lecture 3

Create network

A bridge - default networking mode which will enable the connectivity to the

other interfaces of the host machine as well as among containers.

A none will not configure any IP for the container and doesn’t have any access

to the external network as well as for other containers

A host container will share the host’s network stack and all interfaces from the

host will be available to the container

docker network create isolated_nw

docker network inspect isolated_nw

Page 5: Academy PRO: Docker. Lecture 3

Create network

After you create the network, you can launch containers on it using the docker

run --network=<NETWORK> option.

$ docker run --network=isolated_nw -itd --name=container1

busybox

$ docker network inspect isolated_nw

Page 6: Academy PRO: Docker. Lecture 3

Network

The containers you launch into this network must reside on the same Docker

host. Each container in the network can immediately communicate with other

containers in the network. Though, the network itself isolates the containers

from external networks.

Page 7: Academy PRO: Docker. Lecture 3

Published ports

Within a user-defined network, linking is not supported. You can expose and

publish container ports on containers in this network. This is useful if you want

to make a portion of the network available to an outside network.

Page 8: Academy PRO: Docker. Lecture 3

Let’s try!

docker run -d --net isolated_nw -p 27017:27017 --name

mongo_container mongo

docker run -d --net isolated_nw -p 3000:3000 --name

node_container oleksandrkovalov/node_example_2

Page 9: Academy PRO: Docker. Lecture 3

Docker Compose

Compose is a tool for defining and running multi-container Docker applications.

With Compose, you use a Compose file to configure your application’s services.

Then, using a single command, you create and start all the services from your

configuration.

Page 10: Academy PRO: Docker. Lecture 3

Compose usage

Using Compose is basically a three-step process.

Define your app’s environment with a Dockerfile so it can be reproduced

anywhere.

Define the services that make up your app in docker-compose.yml so

they can be run together in an isolated environment.

Lastly, run docker-compose up and Compose will start and run your entire

app.

Page 11: Academy PRO: Docker. Lecture 3

Compose status

docker-compose ps

Name Command State Ports

------------------------------------------------------------------------------------------

nodeapp_mongo_container_1 docker-entrypoint.sh mongod Up 0.0.0.0:27017->27017/tcp

nodeapp_node_container_1 npm start Up 0.0.0.0:3000->3000/tcp

docker-compose down

Page 12: Academy PRO: Docker. Lecture 3

docker-compose.yml

version: '3'

services:

node_container:

networks:

- isolated_nw

image: "oleksandrkovalov/node_example_2"

links:

- mongo_container

ports:

- "3000:3000"

mongo_container:

networks:

- isolated_nw

image: "mongo"

ports:

- "27017:27017"

networks:

isolated_nw:

Page 13: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild

Configuration options that are applied at build time.

build can be specified either as a string containing a path to the build context,

or an object with the path specified under context and optionally dockerfile and

args.

build: ./dir

Page 14: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild

build:

dockerfile: Dockerfile-alternate

If you specify image as well as build, then Compose names the built image with

the webapp and optional tag specified in image:

build: ./dir

image: webapp:tag

Page 15: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild: CONTEXT

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the

location of the Compose file. This directory is also the build context that is sent

to the Docker daemon.

Compose will build and tag it with a generated name, and use that image

thereafter.

build:

context: ./dir

Page 16: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild: DOCKERFILE

Compose will use an alternate file to build with. A build path must also be

specified.

build:

context: .

dockerfile: Dockerfile-alternate

Page 17: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild: ARGS

Add build arguments, which are environment variables accessible only during

the build process.

First, specify the arguments in your Dockerfile:

ARG buildno

ARG password

RUN echo "Build number: $buildno"

RUN script-requiring-password.sh "$password"

Page 18: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild: ARGS

Then specify the arguments under the build key. You can pass either a mapping

or a list:

build:

context: .

args:

- buildno=1

- password=secret

Page 19: Academy PRO: Docker. Lecture 3

docker-compose.ymlbuild: command

Override the default command.

command: bundle exec thin -p 3000

The command can also be a list, in a manner similar to dockerfile:

command: [bundle, exec, thin, -p, 3000]

Page 20: Academy PRO: Docker. Lecture 3

Disconnecting containers

You can disconnect a container from a network at any time using the docker

network disconnect command.

Disconnect container1 from the isolated_nw network, then inspect container1

and the isolated_nw network.

docker network disconnect isolated_nw container1

docker network inspect isolated_nw

Page 21: Academy PRO: Docker. Lecture 3

Remove a network

When all the containers in a network are stopped or disconnected, you can

remove a network. If a network has connected endpoints, an error occurs.

docker network inspect isolated_nw

docker network rm isolated_nw

docker network ls

Page 22: Academy PRO: Docker. Lecture 3

Docker Swarm

Docker Swarm is the Docker-native solution for deploying a cluster of Docker

hosts. You can use it to quickly deploy a cluster of Docker hosts running either

on your local machine or on supported cloud platforms.

Page 23: Academy PRO: Docker. Lecture 3

How Swarm nodes works

Docker Engine 1.12 introduces swarm

mode that enables you to create a

cluster of one or more Docker Engines

called a swarm. A swarm consists of

one or more nodes: physical or virtual

machines running Docker Engine 1.12

or later in swarm mode.

There are two types of nodes:

managers and workers.

Page 24: Academy PRO: Docker. Lecture 3

How services work

To deploy an application image when

Docker Engine is in swarm mode, you

create a service. Frequently a service will

be the image for a microservice within the

context of some larger application.

Examples of services might include an

HTTP server, a database, or any other type

of executable program that you wish to

run in a distributed environment.

Page 25: Academy PRO: Docker. Lecture 3

Create a swarm

docker swarm init

docker info

......

Swarm: active

NodeID: dxn1zf6l61qsb1josjja83ngz

Is Manager: true

Managers: 1

Nodes: 1

......

Page 26: Academy PRO: Docker. Lecture 3

Inspect nodes

docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS

phfku7dign7xvzppcb0i9fzqj * moby Ready Active Leader

Page 27: Academy PRO: Docker. Lecture 3

Add nodes to the swarm

docker swarm join-token -q manager

docker swarm join \

--token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \

192.168.99.100:2377

Page 28: Academy PRO: Docker. Lecture 3

Deploy a service to the swarm

docker service create --name helloworld --replicas 3 -p 80:80 --network isolated_nw tutum/hello-world

docker service create --name bash --network isolated_nw ubuntu

docker service ls

h3uvf93xt72b bash replicated 1/1 ubuntu:latest

tee36wrfs1z8 helloworld replicated 3/3 tutum/hello-world:latest

docker exec -it 133e43b35f30 bash

apt-get update

apt-get install curl

curl http://helloworld

Page 29: Academy PRO: Docker. Lecture 3

Inspect a service on the swarm

docker service ls

docker service inspect --pretty tee36wrfs1z8

docker service ps tee36wrfs1z8

Page 30: Academy PRO: Docker. Lecture 3

Scale the service in the swarm

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

docker service scale tee36wrfs1z8=4

tee36wrfs1z8rh63c7lkxrfzw scaled to 4

Page 31: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm

deploy

version: '3'

services:

redis:

image: redis:alpine

deploy:

replicas: 6

update_config:

parallelism: 2

delay: 10s

restart_policy:

condition: on-failure

Page 32: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm: UPDATE_CONFIG

Configures how the service should be updated. Useful for configuring rolling

updates.

parallelism: The number of containers to update at a time.

delay: The time to wait between updating a group of containers.

failure_action: What to do if an update fails. One of continue or pause (default:

pause).

monitor: Duration after each task update to monitor for failure

(ns|us|ms|s|m|h) (default 0s).

Page 33: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm: UPDATE_CONFIG

version: '3'

services:

vote:

image: dockersamples/examplevotingapp_vote:before

depends_on:

- redis

deploy:

replicas: 2

update_config:

parallelism: 2

delay: 10s

Page 34: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm: RESOURCES

Configures resource constraints.

version: '3'

services:

redis:

image: redis:alpine

deploy:

resources:

limits:

cpus: '0.001'

memory: 50M

reservations:

cpus: '0.0001'

memory: 20M

Page 35: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm: RESTART_POLICY

Configures if and how to restart containers when they exit. Replaces restart.

condition: One of none, on-failure or any (default: any).

delay: How long to wait between restart attempts, specified as a duration

(default: 0).

max_attempts: How many times to attempt to restart a container before

giving up (default: never give up).

window: How long to wait before deciding if a restart has succeeded, specified

as a duration (default: decide immediately).

Page 36: Academy PRO: Docker. Lecture 3

Docker-compose.yml features for swarm: RESTART_POLICY

version: "3"

services:

redis:

image: redis:alpine

deploy:

restart_policy:

condition: on-failure

delay: 5s

max_attempts: 3

window: 120s

Page 37: Academy PRO: Docker. Lecture 3

Any questions?

Page 38: Academy PRO: Docker. Lecture 3

The endfor today :)