goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running...

55
goo.gl/PaQsVK

Transcript of goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running...

Page 1: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

goo.gl/PaQsVK

Page 2: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Pre-workshop● Install VirtualBox

https://www.virtualbox.org/wiki/Downloads

● Download Ubuntu VMhttp://103.1.160.76:8080/ubuntu-docker.ova

● Import the VM into VirtualBox● Username/password: ubuntu/ubuntu

Page 3: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● (Optional) install PuTTY or other ssh client

http://www.putty.org/

Page 4: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Modern Web Infrastructurewith DockerKitt Tientanopajai, D.Eng.Suchart JoolratBureau of Information TechnologyKhon Kaen University

Page 5: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Part 0Intro & Lab

Page 6: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

VM0

OS

Web DB

Template

Not so long ago ...

PHY (Processor, Memory, Storage, Network)

Hypervisor

OS

Web DB

App

VM1

OS

Web DB

App

Page 7: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

It’s very good solution, but ...● Processors● Memory● Storages● Networks● Dev vs. Ops

Page 8: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Docker Containers

Page 9: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

“Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.”

https://docs.docker.com/engine/understanding-docker/

Page 10: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

https://docs.docker.com/engine/understanding-docker/

Page 11: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Docker on Windows/macOS/Linux● Microsoft Windows

○ Requirement: 64-bit, Windows 10 Pro, Enterprise, Education, Hyper-V○ Docker for Windows○ Docker Toolbox

● Apple macOS○ Requirement: SLAT (Nehalem+), macOS 10.10.3, xhyve○ Docker for Mac○ Docker Toolbox

● Linux○ Requirement: 64-bits, Linux kernel ≥ 3.10○ docker package (depends on the distro.)

Page 12: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

VirtualBox

Docker VM● 2 vCPU / 2 GB RAM / 4 GB Storage● Ubuntu 16.04 LTS

enp0s3

enp0s8

Physical Machine (Windows / macOS / Linux)

NAT PHY

vboxnet0

192.168.99.100 192.168.99.1

10.x.x.x

Internet

Page 13: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Kernel Tuning● Many kernel parameters can be set via /proc or sysctl.

# sysctl -a | less

# ls -R /proc/sys | less

● We can set parameter individually by sysctl, e.g.

# sysctl -w fs.file-max=1048576

# sysctl -a | grep fs.file-max

Page 14: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Or, we can set in /etc/sysctl.d/xx-name.conf. This will be

load automatically when booting the system. ● We can reload all sysctl.conf by

# service procps restart

Page 15: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Install Docker CE on Ubuntu 16.04 LTS● Prerequisite

# apt update

# apt install apt-transport-https ca-certificates curl \

software-properties-common

● Add repository key

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg

| apt-key add -

Page 16: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Check fingerprint

# apt-key fingerprint 0EBFCD88

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

● Add repository

# add-apt-repository \

"deb [arch=amd64] https://.../linux/ubuntu \

$(lsb_release -cs) \

stable"

Page 17: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Install docker

# apt update

# apt install docker-ce

● Install docker-compose

# curl -L https://github.com/docker/compose/releases/

download/1.14.0/docker-compose-`uname -s`-`uname -m` >

/usr/local/bin/docker-compose

# chmod 755 /usr/local/bin/docker-compose

Page 18: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Optional: add user ubuntu to docker group

# usermod -aG docker ubuntu

○ relogin

Page 19: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Part IDocker Basic

Page 20: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure
Page 21: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: Hello World$ docker help

$ docker run hello-world

$ docker ps

$ docker ps -a

$ docker rm <CONTAINER>

$ docker images

$ docker rmi <IMAGE>

Page 22: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Part IISmall-Scale Web Infrastructure

Page 23: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: LEMP stack● Linux● nginx● MariaDB● PHP 7

Page 24: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Docker Host

Images

Volumes

Web #1

web_1

mariadb php-7.1 percona

db_1

Web #2 Web #3 DB #1

master_db

Registry

Imagesweb_2 web_3

nginx

Page 25: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Linux● Linux is a base OS image to run applications or provide

services.● The Docker Hub (https://hub.docker.com/explore) offers

many official linux images○ Ubuntu - ubuntu○ Debian - debian○ CentOS - centos○ Fedora - fedora○ openSUSE - opensuse○ Oracle - oraclelinux○ Alpine - alpine

Page 26: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Let’s try “Alpine Linux”

$ docker run -it alpine /bin/sh

● See if you are running Alpine

# cat /etc/*-release

● You can try basic linux commands (ls, ps, ifconfig, vi,...) ● Alpine is very small.

○ Scalable

Page 27: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Build our own images● We can just run a container from the trusted docker

registry, or we can build our own nginx image from a Dockerfile.

● Build

$ docker build -t [tag] [directory containing Dockerfile]

$ docker build -t wunca/nginx build/nginx

$ docker build -t wunca/nginx-php-7 build/nginx-php-7

$ docker build -t wunca/mariadb build/mariadb

$ docker build -t wunca/wordpress build/wordpress

$ docker build -t wunca/wordpress build/wordpress-aio

Page 28: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: nginx● Run

$ docker run --name nginx -d \

-v $PWD/html:/var/www/html:ro \

-p 80:80 wunca/nginx

● Let’s see more details about the wunca/nginx image

Page 29: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

File: DockerfileFROM alpine:latestMAINTAINER [email protected]

COPY ./repositories /etc/apk/repositoriesCOPY ./default.conf /etc/nginx/conf.d/default.conf

RUN apk update \ && apk add --no-cache nginx \ && adduser -u 82 -D -S -G www-data www-data \ ...

Page 30: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)EXPOSE 80 443

VOLUME ["/var/www/html"]

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Page 31: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

File: repositorieshttp://mirror.kku.ac.th/alpine/latest-stable/main

http://mirror.kku.ac.th/alpine/latest-stable/community

Page 32: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

File: default.confserver {

listen 80 default_server;

root /var/www/html;

location / {

index index.html;

}

location ~ /\.ht {

deny all;

}

}

Page 33: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: nginx + php ● Run

$ docker run --name nginx-php -d \

-v $PWD/html:/var/www/html:ro \

-p 80:80 wunca/nginx-php-7

Page 34: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

MariaDB● Run

$ docker run --name mariadb -d \

-v $PWD/db:/var/lib/mysql \

-p 3306:3306 wunca/mariadb

● Use

$ docker exec -it mariadb /bin/sh

# mysqladmin create <...>

# mysql -uroot [...]

Page 35: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Or, you can create a database when you create a MariaDB

container

$ docker run --name mariadb -d \

-v $PWD/db:/var/lib/mysql \

-p 3306:3306 \

-e MARIA_DB_NAME=db \

-e MARIA_DB_USER=user \

-e MARIA_DB_PASSWORD=password \

wunca/mariadb

Page 36: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: WordPress● From nginx-php-7 image, we can simply create a WordPress

image ● Then, we can run a WordPress container, and use MariaDB

container as a database server.

$ docker run --name wordpress -d \

-v $PWD/html:/var/www/html \

-p 80:80 wunca/wordpress

Page 37: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: Composing WordPress● Instead of running each container manually, we can use

docker-compose, to combine web (wunca/wordpress) and database (wunca/mariadb) together, with networks, and volumes

frontend

backend

wordpress(nginx + php)

MariaDB

web

db

Page 38: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● Run

$ docker-compose up -d

● Stop

$ docker-compose down

● Remove

$ docker-compose rm

Page 39: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

File: docker-compose.ymlversion: '2'services: web: image: wunca/wordpress networks: - frontend - backend ports: - "80:80" volumes: - web:/var/www/html

Page 40: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.) db: image: wunca/mariadb networks: - backend ports: - "3306:3306" volumes: - db:/var/lib/mysql env_file: - .env

Page 41: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)networks: frontend: backend:

volumes: web: db:

Page 42: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Project: WordPress All-in-One● Docker recommends to run only one service per container.

○ Scalable● But, if we don’t care much about scalable, we can put nginx,

PHP, and MariaDB in one image.

$ docker volume create wp-html

$ docker volume create wp-db

$ docker run --name wordpress-aio -d \

-v wp-html:/var/www/html \

-v wp-db:/var/lib/mysql \

-p 80:80 wunca/wordpress-aio

Page 43: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Part IIILarge-Scale Web Infrastructure

Page 44: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Reverse Proxy Server● A reverse proxy is very useful

○ Hiding origin servers○ SSL termination○ Load distribution○ Caching

Server

Server

Server

ReverseProxy

www.example.com

Server

Page 45: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● One of the best reverse proxy is nginx itself.

$ docker run --name proxy -d \

-v $PWD/rev-proxy.conf:/etc/nginx/conf.d/proxy.conf \

-p 80:80 wunca/nginx

Page 46: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont’d)● rev-proxy.conf

upstream web-app {

server alpha;

server beta;

server gamma;

server delta;

}

Page 47: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont’d)server {

server_name rev-proxy.example.com;

location / {

proxy_pass http://web-app;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Host $server_name;

}

}

Page 48: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Caching● nginx is also very capable to do caching.

proxy_cache_path /var/www/cache levels=1:2 keys_zone=cache_zone:8m

use_temp_path=off;

server {

location / {

...

proxy_cache cache_zone;

proxy_cache_key $scheme$host$uri$is_args$args;

proxy_cache_valid 30m;

add_header X-Cache-Status $upstream_cache_status;

}

}

Page 49: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont’d)● Run

$ docker-compose up -d

● Copy wordpress code to nginx:/var/www/html$ cd wordpress

$ docker cp ./ nginx:/var/www/html/

● Create WordPress database

Page 50: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Part IVDatabases

Page 51: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Master-Slave Replication● At Master

> create user 'replicate'@'%' identified by 'Uti6aima';

> grant replication slave on *.* to 'replicate'@'%';

> flush table with read lock;

> show master status;

● We need to put “File” and “Position” at Slave.

Page 52: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● At Slave

> change master to MASTER_HOST='db-master',

MASTER_USER='replicate', MASTER_PASSWORD='Uti6aima',

MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=638;

> start slave;

● Go back to Master to unlock tables

> unlock tables;

● Changes must be done at Master ONLY.

Page 53: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Master-Master (Multimaster) Replication● Do the Master-Slave first, then at Slave

> create user 'replicate'@'%' identified by 'iNgie5ga';

> grant replication slave on *.* to 'replicate'@'%';

> show master status;

● Again, we need to put “File” and “Position”, but at Master.

Page 54: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

(cont.)● At Master

> stop slave;

> change master to MASTER_HOST='db-slave',

MASTER_USER='replicate', MASTER_PASSWORD='iNgie5ga',

MASTER_LOG_FILE='mysql-bin.000007', MASTER_LOG_POS=751;

> start slave;

Page 55: goo.gl/PaQsVK Web... · “Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure

Q & [email protected]@kku.ac.th

Bureau of Information TechnologyKhon Kaen University