Contenu connexe Similaire à 2015-01-27 Introduction to Docker (20) Plus de Shuji Yamada (16) 2015-01-27 Introduction to Docker9. From 1995 to 2015
9
Thick Thin
Defined-Stack
best services
and
best app
Monothilic many Resources
1995 2015
16. hello world
$ time docker run debian echo "hello world"
Unable to find image 'debian:latest' locally
debian:latest: The image you are pulling has been verified
1aeada447715: Pull complete
479215127fa7: Pull complete
511136ea3c5a: Already exists
Status: Downloaded newer image for debian:latest
hello world
real 0m15.250s
user 0m0.050s
sys 0m0.027s
16
# Not have an local image
17. hello world
$ time docker run debian echo "hello world"
hello world
real 0m0.169s
user 0m0.009s
sys 0m0.010s
17
# Have a local image :)
18. Image size is light weight
18
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
postgres latest 2389997c2ef2 3 days ago 213.3 MB
nginx latest 90081fa15a0c 3 days ago 91.62 MB
mysql latest 335228ceb173 3 days ago 282.6 MB
debian latest 4d6ce913b130 8 days ago 84.98 MB
centos latest 8efe422e6104 2 weeks ago 210 MB
fedora latest 834629358fe2 3 weeks ago 241.3 MB
busybox latest 4986bf8c1536 3 weeks ago 2.433 MB
ubuntu latest ed5a78b7b42b 4 weeks ago 188.3 MB
19. 19
未回答
不明
10,000 ~
5,000 - 9,999
2,000 - 4,999
500 - 1,999 100 - 499
~ 100 < 100
100-499
500-1999
2000-4999
5000-9999
10000 >
不明
未回答 2.0%
8.0%
8.5%
4.9%
8.4%
16.9%
23.0%
28.3%
企業におけるサーバ台数
http://www.slideshare.net/realgenekim/2014-state-of-devops-findings-velocity-conference
40. Future stage
• DockerUI, Panamax, Rancher.io
• CoreOS (etcd+fleet+flannel), Atomic Host
• DockerSwarm
• Mesos+docker, Consul+docker, kubernetes, Helios, ...
• Deis, Flynn, ...
• Rocket, ...
40
43. Dockerコンテナの特徴
• コンテナ = ホストから分離されているプロセス(ネームスペース)
• 各コンテナはホストOSのkernelを共有する。
• コンテナから見えるデバイスはエミュレーション動作ではない。
• 実際にほとんどの環境上でコンテナを動かせる。
Linux, Windows, OSX...
Cloud, Server, Windows PC, Macbook, RaspberryPi...
43
49. Dockerのインストール
49
$ sudo apt-get update
$ sudo apt-get install docker.io
$ sudo yum install docker
# Firstly, you need to ensure you have the EPEL repository enabled.
$ sudo yum install docker-io
• Ubuntu 14.04
• RHEL7/CentOS7
• RHEL6/CentOS6
53. Dockerfile
• Dockerfileはコンテナイメージを構築す
るためのレシピ。
• FROMでベースイメージを宣言する。
• RUNでアプリケーションセットアッ
プ用のコマンドを宣言する。
• CMDで実行コマンドを宣言する。
• EXPOSEで開放ポートを宣言する。
53
# vim Dockerfile
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo ‘Hi, hello my container’ /
> /usr/share/nginx/html/index.html
CMD nginx -g “daemon off”
EXPOSE 80
典型的なDockerfileのサンプル
54. Dockerfile
• Dockerfileはコンテナイメージを構築す
るためのレシピ。
• FROMでベースイメージを宣言する。
• RUNでアプリケーションセットアッ
プ用のコマンドを宣言する。
• CMDで実行コマンドを宣言する。
• EXPOSEで開放ポートを宣言する。
54
# vim Dockerfile
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo ‘Hi, hello my container’ /
> /usr/share/nginx/html/index.html
CMD nginx -g “daemon off”
EXPOSE 80
典型的なDockerfileのサンプル
55. Dockerfile
• Dockerfileはコンテナイメージを構築す
るためのレシピ。
• FROMでベースイメージを宣言する。
• RUNでアプリケーションセットアッ
プ用のコマンドを宣言する。
• CMDで実行コマンドを宣言する。
• EXPOSEで開放ポートを宣言する。
55
# vim Dockerfile
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo ‘Hi, hello my container’ /
> /usr/share/nginx/html/index.html
CMD nginx -g “daemon off”
EXPOSE 80
典型的なDockerfileのサンプル
56. Dockerfile
• Dockerfileはコンテナイメージを構築す
るためのレシピ。
• FROMでベースイメージを宣言する。
• RUNでアプリケーションセットアッ
プ用のコマンドを宣言する。
• CMDで実行コマンドを宣言する。
• EXPOSEで開放ポートを宣言する。
56
# vim Dockerfile
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo ‘Hi, hello my container’ /
> /usr/share/nginx/html/index.html
CMD nginx -g “daemon off”
EXPOSE 80
典型的なDockerfileのサンプル
57. Dockerfile
• Dockerfileはコンテナイメージを構築す
るためのレシピ。
• FROMでベースイメージを宣言する。
• RUNでアプリケーションセットアッ
プ用のコマンドを宣言する。
• CMDで実行コマンドを宣言する。
• EXPOSEで開放ポートを宣言する。
57
# vim Dockerfile
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo ‘Hi, hello my container’ /
> /usr/share/nginx/html/index.html
CMD nginx -g “daemon off”
EXPOSE 80
典型的なDockerfileのサンプル
59. Build caching
• build実行時にはDockerfileの行単位でビルドキャッシュが生成される。
• 次回build実行時はキャッシュが利用され、変更差分のみビルドされる。
• キャッシュが存在しない行以降はキャッシュが利用されない。
• ADD行は前回ビルド以降に対象ファイルの変更があった場合において
はキャッシュが利用されない。その場合、以降の行も引き続きキャッ
シュが利用されないのでADD行の埋め込み位置には注意が必要。
• apt-get updateなどのキャッシュも残る。--no-cacheを指定しないと
次回のビルド実行時に最新の状態にアップデートされない。
59
64. Docker Registry s
• DockerHub以外にもDockerイメージを保管するためのレジストリ
サービスが存在する。
• DockerHub (official)
• Quay.io
• Google Container Registry
• $(docker pull registry)
64
65. 65
Dockerfile Docker Image
build Push Provisioning
Case1
• Very simple
• At first, it docker build very slowly,
but after some caching, it build much faster
Docker Hub
66. 66
Docker Image
Dockerfile Docker Hub
Push
Automated
Build
Push
Push
GitHub
or
Bitbucket
Provisioning
Case2
• Outsource your docker build
• GitHub or Bitbucket-like
• Automated build is very slow...
67. 67
Docker Base Image
Dockerfile Docker Hub
Push
Automated
Build
Push
Hook
GitHub
Dockerfile
Docker Image
Build
FROM
<Base Image>
Case3
Containerized
Commit Push
Docker Hub
Provisioning
Docker Image
Patch
• Incremental update pattern
• Minimize the build process
• It fast
Container
in Machine
68. Rebuild-pattern VS Upgrade-pattern
• Rebuild (Immutable-like)
• 更新の都度、Dockerfileをbuildして最新版のイメージを作成。
• 冪等性を担保しやすい。
• 変更内容の影響を受けるスコープが大きい。
• 工夫しないとbuild完了までに時間がかかる。
• 設定が複雑なコンテナや頻繁な更新が必要なコンテナには不向き。
68
69. Rebuild-pattern VS Upgrade-pattern
• Upgrade (Patch-like)
• 開発用コンテナにログイン作業するなどして最新の状態に更新。
• 最新の状態に更新されたコンテナを都度commitしてイメージ化。
• 最新イメージはイメージレポジトリにpushしてバージョン管理。
• build待ちの時間を最小限に抑制できる。
• 充分なテストを準備していたとしても、冪等性は担保しにくい。
69
70. Dockerコミュニティの規模
• 10,000,000+ download
• 75,000+ repogitories on DockerHub
• 150+ Meetup Groups in 50 countries
• 740+ contributors
• 50,000+ third-party projects on GitHub
• 100+ user-generated case studies available from companies
70
https://www.docker.com/company/aboutus/
71. ChangeLog
• v0.1.0 (2013-03-23), initial public release
• v0.2.0 (2013-04-23), automatic bridge setup
• v0.3.0 (2013-05-06), volume
• v0.4.0 (2013-06-03), API, docker build
• v0.5.0 (2013-07-17), host volume, UDP ports
• v0.6.0 (2013-08-22), privileged mode
71
72. ChangeLog
72
• v0.7.0 (2013-11-25), links, storage drivers(aufs, DM, vfs)
• v0.8.0 (2014-02-04), BTRFS, OSX CLI, ONBUILD, ADD cache
• v0.9.0 (2014-03-10), libcontainer, Execution Drivers
• v0.10.0 (2014-04-08), TLS API Supports
• v0.11.0 (2014-05-07), SELinux, DNS links, --net=host
• v0.12.0 (2014-06-05), pause/unpause
73. ChangeLog
73
• v1.0.0 (2014-06-09), Production support
• v1.1.0 (2014-07-03), .dockerignore, commit, logs --tail
• v1.2.0 (2014-08-20), auto-restart policies, capability
• v1.3.0 (2014-10-14), docker exec, docker create
• v1.4.0 (2014-12-11), overlayfs
• v1.5.0-rc1 (2015-1-22), IPv6, docker rename
74. v1.0.0 v1.5.0-rc1
• .dockerignore, 特定のファイルやディレクトリを無視
• docker logs, コンテナのログを表示
• --restart=alway/no/on-failure, コンテナの自動再起動ポリシー
• --cap-add/cap-drop, Linux Kernel Capability
• --device=, 利用するデバイス名指定
74
75. v1.0.0 v1.5.0-rc1
• docker exec, 起動中コンテナへのログイン機能
• docker create, コンテナ作成コマンド(起動はしない。)
• Signature (official image only)
• --security-opts (SELinux/AppArmor)
• overlayfs support
• IPv6 support
75
81. docker server (docker daemon)
docker API server
network driver
81
Container
libcontainer
docker.sock
exec driver
(native or lxc)
(docker build...)
(docker run...)
(docker pull...)
API CLI
Docker Daemon
Docker Container
Engine
rootfs (aufs, btrfs, devicemapper...)
graph driver
Driver
other drivers...
Dockerfile
Registry
(DockerHub, etc...)
DockerEngine Client
86. --netオプション
• --net=bridge, Linux Bridge(docker 0)経由の通信。ホスト側で自動
的にNAPTされ、グローバルと通信できる。(default)
• --net=container, 他のコンテナとNICを共有する。IPアドレスと
MACアドレスも同一のアドレスを共有する。
• --net=none, NICを利用しない。
• --net=host, ホストのすべてのネットワークをnativeに利用する。
86
87. Host Serverveth veth veth
ネットワーク
87
--net=bridge
Container Container
--net=container
Container
--net=none
Container
eth0
(veth)
--net=host
Container
Bridge (docker0)
eth0
Container
eth0
(veth)
eth1
(veth)
88. Network Port Mapping
• コンテナにはランダムなポートがマッピングされる。(default)
(下図において、コンテナのPort 5000はホストのPort 49154に
マッピングされている。)
88
49154
Bridge
5000
eth0
Host Container
$ docker run -d --name=myapp -P training/webapp python app.py
$ docker port myapp
5000/tcp -> 0.0.0.0:49154
89. Network Port Mapping
• ただし、DockerfileでEXPOSEを宣言していないコンテナの場合は、
明示的に-Pオプションを指定しても無視される。
89
Bridge
eth0
Host Container
$ docker run -d --name=myapp -P busybox yes
$ docker port myapp
90. Network Port Mapping
• -pオプションでポートマップを指定することも可能。
(下図において、コンテナのPort 5000はホストのPort 5000に
マッピングされるよう明示的に指定している。)
90
5000
Bridge
5000
eth0
Host Container
$ docker run -d --name=myapp -p 5000:5000 training/webapp python app.py
$ docker port myapp
5000/tcp -> 0.0.0.0:5000
91. Network Port Mapping
• -pオプションでポートマップを指定することも可能。
(下図において、コンテナのPort 5000はホストのPort 15000に
マッピングされるよう明示的に指定している。)
91
15000
Bridge
5000
eth0
Host Container
$ docker run -d --name=myapp -p 15000:5000 training/webapp python app.py
$ docker port myapp
5000/tcp -> 0.0.0.0:15000
92. Network Port Mapping
• DockerfileでEXPOSEを宣言していないコンテナの場合でも、明示
的に-pオプションを指定するとポートマッピングされる。
92
5000
Bridge
5000
eth0
Host Container
$ docker run -d --name=myapp -p 5000:5000 busybox yes
$ docker port myapp
5000/tcp -> 0.0.0.0:5000
93. Network Port Mapping
• --net=hostオプションを指定するとコンテナはホストのネットワー
クを利用する。その場合、DockerfileでEXPOSEを宣言しているポー
トがホスト上でListenを開始する。
93
5000
eth0
Host
Container
$ docker run -d --name=myapp --net=host training/webapp python app.py
$ docker port myapp
Bridge
94. Host Server
use pipework
94
Bridge (docker0)
Container
eth0
(veth)
veth
--net=bridge
pipework br1 ...
eth1
(veth)
Bridge (br1)
Container
eth0
(veth)
--net=bridge
pipework br1 ...
eth1
(veth)
Container
eth0
(veth)
--net=bridge
pipework eth1 ...
eth1
macvlan
eth0 eth1
veth veth veth veth
95. ネットワークパフォーマンス
• Linux Bridge経由の通信:ごく かなオーバヘッドが発生する。
• iptables経由の通信:ごく かなオーバーヘッドが発生する。
• コンテナ間通信:大きいオーバヘッドが発生する。
• --net=host:nativeな速度で動作する。
• SR-IOV:nativeな速度で動作する。
• macvlan:nativeな速度で動作する。
95
99. Networkネームスペースの分離
99
# ip addr show | egrep "UP|inet"
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP qlen 1000
inet 153.120.104.254/24 brd 153.120.104.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP qlen 1000
inet 192.168.0.1/24 brd 192.168.0.255 scope global eth1
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
inet 172.17.42.1/16 scope global docker0
$ docker run -it ubuntu /bin/bash
root@8fbffca705a2:/# ip addr show | egrep "UP|inet"
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
inet 127.0.0.1/8 scope host lo
17: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
inet 172.17.0.4/16 scope global eth0
101. IPCネームスペースの分離
101
$ ipcs
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0052e2c1 0 postgres 600 48 5
0x00000000 229377 s-yamada 700 1694000 2 dest
$ docker run -it ubuntu /bin/bash
root@c739668e223a:/# ipcs
------ Message Queues --------
key msqid owner perms used-bytes messages
102. Userネームスペースの分離
102
$ docker run -it ubuntu /bin/bash
root@c739668e223a:/# id -a
uid=0(root) gid=0(root) groups=0(root)
• コンテナのUIDは実際にはホストのUID1000番台以降にマッピング
される。
105. メディアアクセス制御機能
• Linux Kernel Capabilities
• Drop mout capabilities
• Enable what a task needs
• Grsecurity and PaX
• SELinux
• AppArmor
105
106. 106
Kernel
Server
Containers
NameSpaces
• UTS
• IPC
• PID
• User
• Network
• ...
cgroups
• memory
• cpu
• blkio
• devices
• network
• .....
Namespace
Ubuntu base
nginx
Namespace
Debian base
Rails
Namespace
CentOS base
Apache2
MySQLpostgresql
Host
Network
• veth
• bridge
• iptables
• ...
Storage
• aufs
• btrfs
• devicemapper
• overlayfs
• ...
Security
• SElinux
• apparrmor
• capability
• Grsecurity
• PaX
...
Docker EngineDocker
OS
110. 複数台のプロビジョニング
• 自身でコンテナを定義する場合
• Docker Compose(Fig), Maestro-NG, Ansible, Chef, etc...
• APIライク
• Mesos(+ Marathon), Kubernetes, Helios
• PaaSライク
• Flynn, Deis, CloudFoundry, Dokku, Tsuru, OpenShift
• OpenStack (because OpenStack can do everything!)
110
112. コンテナの監視に関する課題
• リソース(CPU, Mem, trafic)の可視化は必要。
• 最低限の外系監視(Ping, HTTP, HTTPS, TCP/UDP)も必要。
• コンテナ自体がプロセスみたいなものなのでプロセス監視は概ね不要。
• その他、コンテナの種類に応じて各種リソースの監視が必要。
• 各種レスポンス・遅延、各種サイズ、同時接続数。
• 比較的手軽なエージェントはsensuとdatadogくらい?
• とは言うものの、コンテナの場合は監視の必要がないアプリケーションも多
い・・・
112