Upload
ji-woong-choi
View
968
Download
10
Embed Size (px)
Citation preview
2016. 08
Docker on kubernetes
2 - Internal Use Only -
클라우드 속의 Docker
IBM cloud and open technologies
출처: Animesh Singh - http://www.slideshare.net/AnimeshSingh/cloud-foundry-docker-openstack-leading-open-source-triumvirate
3 - Internal Use Only -
클라우드 속의 Docker
IaaS와 PaaS와 SaaS가 유기적으로 협업하여 운영될 때, 우리가 원하는 목표에 도달 가능
출처: Animesh Singh - http://www.slideshare.net/AnimeshSingh/cloud-foundry-docker-openstack-leading-open-source-triumvirate
4 - Internal Use Only -
클라우드 속의 Docker
전통적인 SW 개발 환경 --> dev/test/prod이 구분되어 있는 환경
이런 환경은 deploy시 애플리케이션의 환경이 정확히 일치하지 않아 많음 문제 초래 (애플리케이션과 OS와의 의존성)
OS와 deploy할 환경을 제공해 주는 library와 APP로 구분되어짐.
만일 애플리케이션과 OS/Library와의 의존성이 모두 없어진다면?
모두 같은 이미지를 사용
출처: https://www.linkedin.com/pulse/how-linux-containers-shatter-traditional-sdlc-ken-thompson
5 - Internal Use Only -
클라우드 속의 Docker
그리고 이를 orachestration 할 수 있는 Kubernetes 같은 자동 deploy, 스케일링, 운영할수 있는 플랫폼만 있다면…
출처: https://www.linkedin.com/pulse/how-linux-containers-shatter-traditional-sdlc-ken-thompson
To Amazon Web Service
소개
Docker Machine을 사용하여 Amazon EC2에
Docker가 움직이는 호스트를 여러 대 구축
동시에 이러한 호스트를 클러스터링하고
Docker Swarm 환경을 구축
Docker Swarm 환경에 Docker Compose에서
컨테이너를 생성하고 연계
Amazon Web Service
To DigitalOcean - https://cloud.digitalocean.com
소개
Docker Machine을 사용하여 DigitalOcean에
Docker가 움직이는 호스트를 여러 대 구축
동시에 이러한 호스트를 클러스터링하고
Docker Swarm 환경을 구축
Docker Swarm 환경에 Docker Compose에서
컨테이너를 생성하고 연계
Digitalocean
Cloud 상에 docker machine 만들기
https://cloud.digitalocean.com/에 docker-machine을 통해 컨테이너를 만들고 시작
$ [root@centos7 ~]# docker-machine create \ > --driver digitalocean \ > --digitalocean-access-token XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \ > --digitalocean-size 2gb \ > dobox Creating SSH key... Creating Digital Ocean droplet...
To see how to connect Docker to this machine, run: docker-machine env dobox
digitalocean-access-token을 API에서 만든다.
Cloud 상에 docker machine 만들기
이를 확인하고 접근
[root@centos7 ~]# docker-machine env dobox export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://159.203.111.85:2376" export DOCKER_CERT_PATH="/root/.docker/machine/machines/dobox" export DOCKER_MACHINE_NAME="dobox" # Run this command to configure your shell: # eval "$(docker-machine env dobox)" [root@centos7 ~]# eval "$(docker-machine env dobox)" [root@centos7 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM containerhost virtualbox Stopped dobox * digitalocean Running tcp://159.203.111.85:2376
10 - Internal Use Only -
Docker’s architecture
client-serer architecture 채용
Socket/RESTful API를 통해 docker_host와 연결
어디서나 docker host를 관리 가능
개발은 docker 내에서 이루어지고, 이것을 가지고 운영으로 넘겨서 delopy하는 방식
11 - Internal Use Only -
Docker internal
출처 : Steven J. Vaughan-Nichols - Docker libcontainer unifies Linux container powers
"Docker"는 미국 Docker 사 (구 dotCloud)가 개발 한 오픈 소스 컨테이너 관리 소프트웨어의 하나. Go 언어로
구현
경량 가상화 환경인 컨테이너를 제공하고 응용 프로그램 실행 환경을 그대로 Docker 이미지로 저장 가능
가상 서버가 아닌 애플리케이션에 최적화되어 있기 때문에 원칙적으로 하나의 컨테이너에 하나의 응용
프로그램이 실행 구성
원래 Docker는 LXC를 통해 Linux 컨테이너 기능을 이용. Docker Ver0.9보다 libcontainer라는 독자적인
라이브러리를 통하도록 개선. 옵션으로 LXC를 선택 가능
OS위에 설치하지 않고, 프로세스로 동작 (성능상 이익)
이미지 관리가 편리
오픈소스 개발자 생태계 중심으로 개발
모든 환경이 언제, 어디서든, 어느 시점에서나 동일하게
Host와 동일한 커널(리눅스)만 실행 가능
12 - Internal Use Only -
Docker ( Build once… (Finally) run anywhere )
독립적이고, 안정적인 portable한 실행환경 구축
OS/HW dependency 제거
쉬운 패키지 방법
HW 직접 구축
single userspace
실행환경 공유
h/w 리소스만 통제
가상화 hypervisor
공유환경 제한
리소스 통제
vm별 권한
Docker 환경
hypervisor의 복잡성 제거
애플리케이션별 패키지
컨테이너 기술로 애플리케이션별 관리
13 - Internal Use Only -
Docker ( Build once… (Finally) run anywhere )
LXC는 cgroups 기능을 사용하여 다양한 자원을 분리하여 독립적 인 환경을 제공
- Namespace
- 마운트 호스트 도메인 이름, 프로세스 공간 소켓이나 메시지 큐 공간, 사용자 그룹, 네트워크 등의 이름 공
간마다 식별 은폐 할 수 있는 기능
- Cgroup
- "Control Group"의 약자. 프로세스를 그룹화하고 그룹 내에 존재하는 프로세스에 대해 그룹 단위의 자원
관리를 할 수있는 기능.
- Chroot
- 루트 디렉토리를 특정 디렉토리로 변경할 수있는 기능.
- Netns
- (Linux Network Namespace)은 호스트 서버에 가상으로 브리지 "br0"며 "veth"라는 직결 된 가상 NIC의 쌍
으로 이루어진 가상 네트워크를 구축한다.
- 컨테이너마다 별도의 veth가 생성되어 그 한쪽을 컨테이너 내부의 이더넷 "eth0"로 할당한다.
14 - Internal Use Only -
Docker internal - AUFS
출처 : Colin Surprenant - Docker
AUFS (Another Union FS)은 계층 구조의 파일 시스템 (layerd storage)
부모의 파일 시스템을 read-only로 그 위에 쓰기 가능한 계층을 추가한다. 이렇게 여러 레이어를 쌓아 레이어를
통해 하나의 파일 시스템과 같이 취급한다.
부모 컨테이너에서 차등 관리 할 수 있기 때문에 컨테이너 이미지의 용량을 경량화 할 수 있다.
기본 kernel을 필두로, 계속적으로 바뀐
이미지를 기록하고 바뀐 부분만 계속해서
다운로드 받는 방식으로 운영
Docker commit시 지속적으로 update된
이미지 생성
소스바뀐 일부분만 배포 빠르고, 쉬운 배포
15 - Internal Use Only -
Docker internal - Docker image & workflow
Docker 이미지는 Docker 컨테이너 생성의 기반- ubuntu이나 centos 등의 배포가 이미지화되어 있음.
Docker 이미지를 기반으로 개별 응용 프로그램의 환경을 설치하여 만들 컨테이너 실행 환경.
출처 : Andrea Laspada - http://www.slideshare.net/Pokerone/docker-45628208
생태계를 위해 docker hub제공
개인적으로 docker hub를 구성할 수
있도록 소스 공개
Docker - Getting Started with Docker Hub.
Docker Hub
먼저 docker site에 가입 (history 관리)
Hub.docker.com
Docker 사는 Docker Hub라는 클라우드 저장소를 제공
다양한 단체와 개인이 만든 Docker 이미지를 이용하거나 직접 만든 이미지를 공유.
[root@Tiger ~]# docker login Username: oscinfra Password: Email: [email protected] Login Succeeded
Docker - Installing Docker on CentOS 7
2016년 8월 현재
Hostname setting
Docker install / Docker start 등록 / 최신버전 update
#docker installation/setting
yum -y install update > /dev/null ; yum -y install docker; systemctl enable docker; systemctl start
docker; systemctl status docker | grep Active
# check the version
[root@worker01 ~]# docker version
Client:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-46.el7.centos.10.x86_64
Go version: go1.6.3
Git commit: d381c64-unsupported
Built: Thu Aug 4 13:21:17 2016
OS/Arch: linux/amd64
Server:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-46.el7.centos.10.x86_64
Go version: go1.6.3
Git commit: d381c64-unsupported
Built: Thu Aug 4 13:21:17 2016
OS/Arch: linux/amd64
Docker - Installing Docker APP
docker nginx install
[root@worker01 ~]# docker run --name mynginx1 -P -d nginx
[root@worker01 ~]# docker images
[root@worker01 ~]# docker ps
Docker - Installing Docker APP
docker nginx install 상세
#docker nginx install
[root@worker01 ~]# docker run --name mynginx1 -P -d nginx
Unable to find image 'nginx:latest' locally
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
357ea8c3d80b: Pull complete
0fc04568277e: Pull complete
0bed9719ddcb: Pull complete
Digest: sha256:d33834dd25d330da75dccd8add3ae2c9d7bb97f502b421b02cecb6cb7b34a1b6
Status: Downloaded newer image for docker.io/nginx:latest
B279f968419ab706590fdd481509dd6c9d0afbfc292128211f204e07cfe44b17
[root@worker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 4efb2fcdb1ab 5 days ago 183.4
MB
[root@worker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
b279f968419a nginx "nginx -g 'daemon off" 47 seconds ago
Up 44 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp mynginx1
Docker - Installing Docker APP
docker nginx install
[root@worker01 ~]# docker run --name mynginx2 -v /var/www:/usr/share/nginx/html:ro -v
/var/nginx/conf:/etc/nginx:ro -P -d nginx
[root@worker01 ~]# docker images
[root@worker01 ~]# docker ps
Docker - Installing Docker APP
docker nginx install 상세
[root@worker01 conf]# docker run --name mynginx2 -v /var/www:/usr/share/nginx/html:ro
-v /var/nginx/conf:/etc/nginx:ro -P -d nginx
e41d056a3a8c5a03db1096523f8de452974cc03f6364801040f7c0059cc0242f
[root@worker01 conf]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 4efb2fcdb1ab 5 days ago 183.4
MB
[root@worker01 conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
e41d056a3a8c nginx "nginx -g 'daemon off" 5 seconds ago
Up 3 seconds 0.0.0.0:32773->80/tcp, 0.0.0.0:32772->443/tcp mynginx2
b279f968419a nginx "nginx -g 'daemon off" 40 minutes ago
Up 40 minutes 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp mynginx1
Docker - Docker 구축 기초
Docker Command
pull/push 이미지 조작
images 이미지 리스트
run 컨테이너 실행
ps 생성된 컨테이너
리스트
logs Stdout 로그
stop 중지
rm 삭제
23 - Internal Use Only -
Dockerfile
실행을 위해 dockerfile에 명시하여 작성 가능
[root@centos7 httpd]# cat Dockerfile
FROM centos:latest
MAINTAINER oscinfra<[email protected]>
RUN yum install -y httpd
EXPOSE 80
CMD ["/usr/sbin/httpd“ "-D"
"FOREGROUND"]
[root@centos7 httpd]# cat Dockerfile
① centos(latest)를 기반으로하는
② 관리자는 hoge oscinfra<[email protected]>
③ httpd를 설치
④ 80 번 포트를 오픈
⑤ httpd 서버를 FOREGROUND로 시작
FROM 기반이 되는 Docker 이미지 지정
MAINTAINER 작성자 정보
RUN 명령의 실행
ADD 파일 / 디렉토리 추가
CMD 컨테이너의 실행 명령 1
ENTRYPOINT 컨테이너의 실행 명령 2
WORKDIR 작업 디렉토리 지정
ENV 환경 변수 지정
USER 실행 사용자 지정
EXPOSE 포트 내보내기
VOLUME 볼륨 마운트
Docker - Dockerfile
[root@centos7 httpd]# docker build -t oscinfra/httpd:1.0 ./
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM centos:latest
---> 0f73ae75014f
Step 1 : MAINTAINER oscinfra<[email protected]>
---> Using cache
---> 92af619625fc
Step 2 : RUN yum install -y httpd
---> Running in 1387cc55e281
---> 3887d832801c
Removing intermediate container 1387cc55e281
Step 3 : EXPOSE 80
---> Running in e4ec8d71efb2
---> 2d19e2d29feb
Removing intermediate container e4ec8d71efb2
Step 4 : CMD ["httpd", "-D" "FOREGROUND"]
---> Running in 20d5eaab4f76
---> 041a74b4ddb1
Removing intermediate container 20d5eaab4f76
Successfully built 041a74b4ddb1
oscinfra/httpd:1.0이라는Docker 이미지를 생성
25 - Internal Use Only -
Docker 서비스 해보기 (wordpress)
DB생성
docker run --name=mysql -d -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
[root@worker01 mysql]# mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]> create database wordpress CHARACTER SET utf8;
MySQL [(none)]> grant all privileges on wordpress.* to wp@'%' identified by 'wp';
MySQL [(none)]> flush provileges;
26 - Internal Use Only -
Docker 서비스 해보기
Wordpress 실행
그전에 DB생성
[root@worker01 mysql]# docker run --name=mysql -d -p 3306:3306 -e
MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
Unable to find image 'mysql:latest' locally
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
256a92f57ae8: Pull complete
d5ee0325fe91: Pull complete
a15deb03758b: Pull complete
7b8a8ccc8d50: Pull complete
1a40eeae36e9: Pull complete
833539b56478: Pull complete
4f350efbb7e5: Pull complete
266f161e49c6: Pull complete
b53a75e45220: Pull complete
75196bb75c29: Pull complete
Digest: sha256:c8f03238ca1783d25af320877f063a36dbfce0daa56a7b4955e6c6e05ab5c70b
Status: Downloaded newer image for docker.io/mysql:latest
627ab1482cf7b077bf15d95a97f38f7641c7ca00f423cc5805f9e13cc392e713
27 - Internal Use Only -
Docker 서비스 해보기
Wordpress 실행
그전에 DB생성
[root@worker01 ~]# mysql -h127.0.0.1 -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> create database wp CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> grant all privileges on wp.* to wp@'%' identified by 'wp';
Query OK, 0 rows affected, 1 warning (0.01 sec)
28 - Internal Use Only -
Docker 서비스 해보기
Wordpress 실행
docker run \
--publish 80:80 \
--name wordpress \
--env WORDPRESS_DB_HOST=192.168.0.204 \
--env WORDPRESS_DB_USER=wp \
--env WORDPRESS_DB_PASSWORD=wp \
wordpress
Docker - Docker 컨테이너 생성 + 시작
Docker은 가상 서버가 아닌 애플리케이션에 최적화되어 있기 때문에 원칙적으로 하나의 컨테이너에 하나의
응용 프로그램이 실행 구성
centos 최신 버전 이미지에서 centos01라는 이름으로 컨테이너를 작성하고 응용 프로그램에 bash를 지정하여
실행
즉, Docker은 docker run에서 만든 컨테이너에서이 명령에서 지정하는 하나의 애플리케이션을 실행
지정한 응용 프로그램이 exit 함과 동시에 그 Docker 컨테이너도 정지
즉, Docker 컨테이너의 bash를 exit하면 컨테이너 자체도 종료
[root@centos7 ~]# docker run -it --name ubuntu01 ubuntu /bin/bash
root@dbe8896b758a:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 LTS"
[root@centos7 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
dbe8896b758a ubuntu "/bin/bash" 2 minutes ago Up 2 minutes
ubuntu01
Docker - Docker 컨테이너 생성 + 시작
centos 최신 버전 이미지에서 centos01라는 이름으로 컨테이너를 작성하고 응용 프로그램에 bash를 지정하여
실행
Docker 컨테이너의 bash를 exit하면 컨테이너 자체도 종료
[root@centos7 ~]# docker run -i -t --name centos01 centos:latest /bin/bash
[root@a51a1f6629aa /]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@a51a1f6629aa /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
15: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.5/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:5/64 scope link
valid_lft forever preferred_lft forever
[root@centos7 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
a51a1f6629aa centos:latest "/bin/bash" About a minute ago Up About a minute
centos01
Docker - Docker 컨테이너 분리 +연결 + 재시작
일시 분리
다시 연결
기존 컨테이너 centos01를 다시 시작
[root@centos7 ~]# docker run -i -t --name centos01 centos:latest /bin/bash
[root@a51a1f6629aa /]# [Ctrl + p] → [Ctrl + q]
[root@centos7 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
a51a1f6629aa centos:latest "/bin/bash" 16 minutes ago Up 16 minutes
centos01
[root@centos7 ~]# docker attach a51a1f6629aa
[root@a51a1f6629aa /]#
[root@centos7 ~]# docker attach a51a1f6629aa
[root@a51a1f6629aa /]# exit
exit
[root@centos7 ~]# docker ps -a | grep centos
a51a1f6629aa centos:latest "/bin/bash" 20 minutes ago
Exited (0) 9 seconds ago centos01
[root@centos7 ~]# docker start -i centos01
[root@a51a1f6629aa /]#
Docker - Docker 컨테이너 관리
실행중인 centos01 컨테이너를 중지
Docker 컨테이너 삭제
[root@a51a1f6629aa /]# [root@centos7 ~]#
[root@centos7 ~]# docker ps | grep centos
a51a1f6629aa centos:latest "/bin/bash" 25 minutes ago
Up 4 minutes centos01
[root@centos7 ~]# docker stop centos01
centos01
[root@centos7 ~]# docker images | grep centos
centos latest 0f73ae75014f 4 weeks ago 172.3
MB
[root@centos7 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a51a1f6629aa centos:latest "/bin/bash" 26 minutes ago
Exited (137) 19 seconds ago centos01
[root@centos7 ~]# docker rm centos01
Centos01
Docker - Docker 컨테이너 관리
Docker 이미지 만들기
centos03 컨테이너를 생성하고 bash에서 시작
centos03 컨테이너에 httpd를 설치
Ctrl + p + q에서 Docker 컨테이너를 분리
Oscinfra/httpd라는 이미지 생성
Oscinfra/httpd라는 이미지를 가지고 Web01이라는 컨테이너를 실행
[root@centos7 ~]# docker run -it --name centos03 centos:latest /bin/bash
[root@04e2e6423b12 /]# yum install -y httpd
….
Installed:
httpd.x86_64 0:2.4.6-31.el7.centos.1
[root@04e2e6423b12 /]# [root@centos7 ~]# docker stop centos03
[root@centos7 ~]# docker commit centos03 oscinfra/httpd
72c1fec3a9bd9b0e7ed8db4e7d69c90cee6339b158847963f501eb180b2cba40
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
VIRTUAL SIZE
oscinfra/httpd latest 72c1fec3a9bd 13 seconds ago 278.8
MB
centos latest 0f73ae75014f 4 weeks ago 172.3
MB
[root@centos7 ~]# docker run -d -p 8080:80 --name web01 oscinfra/httpd /usr/sbin/httpd
-D FOREGROUND
Be20ff0d21b0886cb1303a6b0e2eedc805297314dfd644015d83094a82fdc943
Docker - Docker 컨테이너 관리
Web01 컨테이너 연결
백그라운드에서 실행중인 web01 컨테이너에 연결
백그라운드에서 실행중인 web01 컨테이너를 중지
[root@centos7 ~]# docker exec -it web01 /bin/bash
[root@be20ff0d21b0 /]# ps -ef | grep httpd
root 1 0 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 5 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 6 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 7 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 8 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 9 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 10 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 11 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
apache 12 1 0 19:31 ? 00:00:00 /usr/sbin/httpd -D FOREGROUND
root 29 13 0 19:38 ? 00:00:00 grep --color=auto httpd
[root@be20ff0d21b0 /]# [root@centos7 ~]#
[root@centos7 ~]# docker stop web01
web01
Docker - Docker Hub에서 이미지 공유
백그라운드에서 실행중인 web01 컨테이너에 연결
Docker Hub에 로그인
Docker Hub에 방금 만든 “oscinfra / httpd"이미지를 PUSH
[root@centos7 ~]# docker login
Username: oscinfra
Password:
Email: [email protected]
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@centos7 ~]# docker push oscinfra/httpd
The push refers to a repository [docker.io/oscinfra/httpd] (len: 1)
72c1fec3a9bd: Buffering to Disk
Docker - Docker 이미지 빌드
출처 : http://www.slideshare.net/durdn/be-a-better-developer-with-docker
Docker - Data Volume
여러 컨테이너간에 영구적인 데이터와 공유 데이터를 처리하는 특별한 볼륨(디렉토리).
Data Volume 대한 변경은 직접 반영되어 이미지의 변경에 포함되지 않는다.
Data Volume은 참조 할 컨테이너가 없어져도 살아난다.
출처 : http://www.slideshare.net/durdn/be-a-better-developer-with-docker
호스트 볼륨 공유
docker run -it -v /opt/test-app:/app ubuntu /bin/bash
root@fa6912b4909b:/app# pwd /app root@fa6912b4909b:/app# touch from_container_to_host root@fa6912b4909b:/app# ls from_container_to_host from_host_to_container
[root@centos7 test-app]# pwd /opt/test-app [root@centos7 test-app]# touch from_host_to_container [root@centos7 test-app]# ls from_container_to_host from_host_to_container
Docker - Data Volume Container
사용자 데이터 등 깨지지 않는 정보의 저장 장소로 Data Volume Container를 이용한다.
LOG VOLUME
Web 서버 컨테이너 AP 서버 컨테이너 DB 서버 컨테이너
DB 저장 volume
Docker - 네트워크 포트 매핑
컨테이너를 시작할 때 컨테이너 내부에서 사용하는 포트를 임의의 호스트 측 포트 번호 (49152 이상 권장)에
할당
호스트 측의 포트 번호는 49000 ~ 49900의 범위에서 자동으로 임의의 번호를 할당하거나 사용자가 임의의
번호를 지정 가능
컨테이너에서 열린 포트를 호스트 측 임의의 포트 번호에 매핑
[root@centos7 ~]# docker run -d -P oscinfra/sshd2:latest /usr/sbin/sshd -D a210be9d2fae3b4a9967b504785307e0afc321df8fbc8447a9578ba7d092214c [root@centos7 ~]# docker run -d -p 22 oscinfra/sshd2:latest /usr/sbin/sshd -D 815cfb3316ed767b83926e1bf0060c6e8387216ec7559de25dc32e82a21f11ae
[root@centos7 ~]# docker ps -a 815cfb3316ed oscinfra/sshd2:latest "/usr/sbin/sshd -D" 2 minutes ago Up 2 minutes 0.0.0.0:32768->22/tcp stoic_thompson 54e6440174ad ubuntu:latest "/usr/sbin/sshd -D" 6 minutes ago Created condescending_davinci a210be9d2fae oscinfra/sshd2:latest "/usr/sbin/sshd -D" 7 minutes ago Up 7 minutes determined_wilson
컨테이너 포트 22이 호스트의 어떤 포트에 매핑되었는지 아는 방법
[root@centos7 ~]# docker port 815cfb3316ed 22 0.0.0.0:32768 [root@centos7 ~]# docker port a210be9d2fae 22 Error: No public port '22/tcp' published for a210be9d2fae
Docker - 네트워크 포트 매핑
컨테이너 포트 22을 유저가 지정하는 호스트 측면 포트 번호 2222에 매핑
[root@centos7 ~]# docker run -d -p 2222:22 oscinfra/sshd2:latest /usr/sbin/sshd -D 03ca82b92ac41b991262bc929938c2eb741f7a7e475173c7fd37841f04b6508a [root@centos7 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03ca82b92ac4 oscinfra/sshd2:latest "/usr/sbin/sshd -D" 6 seconds ago Up 5 seconds 0.0.0.0:2222->22/tcp high_jones 815cfb3316ed oscinfra/sshd2:latest "/usr/sbin/sshd -D" 12 minutes ago Up 12 minutes 0.0.0.0:32768->22/tcp stoic_thompson [root@centos7 ~]# docker port 03ca82b92ac4 22 0.0.0.0:2222
Docker - 컨테이너 링크
여러 컨테이너간에 전용 네트워크를 구축
원본 컨테이너 A:
docker run –d --name pg ubuntu:latest postmaster -D /usr/local/pgsql/data
컨테이너 A에 연결하는 컨테이너 B:
docker run -d --link pg:db ubuntu:latest /someService
컨테이너 B
환경 변수 DB_PORT_5432_TCP_ADDR = 172.17.0.2
DB_PORT = tcp : //172.17.0.2 : 5432 DB_5432_TCP = tcp : //172.17.0.2 : 5432
DB_PORT_5432_TCP_PORT = 5432
/etc/hosts 172.17.0.2 db
42 - Internal Use Only -
Docker’s architecture
Dockerfile
Build docker image
docker build -t oscinfra/web01:latest .
Run docker container
docker run -p 32770:80 -d --name web01 oscinfra/web01:latest
Push to remote registry
docker push oscinfra/web01:latest
root@osc-openstack01:~/web01# cat Dockerfile
FROM nginx
MAINTAINER Hojin Kim<[email protected]>
COPY index.html /usr/share/nginx/html/index.html
CMD ["nginx", "-g", "daemon off;" ],
43 - Internal Use Only -
Why k8s
애플리케이션을 더 작고, 더 관리용이하고, 더 확장가능하게 나누어 마이크로서비스를
가능하게 함.
Node failure에 대해 fault-tolerant 하게 구성
간단한 명령어로 시스템의 scale-out가능
리소스의 효율적인 사용
Infra와 개발팀의 각자 인터페이스에 대한 관리 불필요
Container
process isolation
namespace isolation
own network interface
own filesystem
Microservice
one container = one
process
44 - Internal Use Only -
Kubernetes의 목적
여러 개의 도커 서버를 하나의 Pool로 사용 여러 개의 도커 데몬 서버에 알아서 컨테이너가 생성
여러 개의 서버에 분산되어서 컨테이너가 생성
다른 서버에 있는 컨테이너와의 통신
이는 kube-proxy와 flanneld 라는 서브 툴로 가능하게 한다
컨테이너 fail 시 재생성
Load Balance
출처 : http://blog.naver.com/alice_k106/220589774457
45 - Internal Use Only -
Why k8s
애플리케이션을 더 작고, 더 관리용이하고, 더 확장가능하게 나누어 마이크로서비스를
가능하게 함.
Replication Controller
Pod ● smallest deployable unit
● shared context
● N pods running
● pod template
Service
Label ● key/value pair
● organize, select objects
● targets pods
● long-lived
출처 : http://www.slideshare.net/erialc_w/kubernetes-50626679
46 - Internal Use Only -
Pod
Set of containers
Settings in a template
Flat shared networking namespace
Shared storage volumes
출처 : http://www.slideshare.net/erialc_w/kubernetes-50626679
Pod
Webserver
Content
Management
Server
Labels
Metadata with semantic meaning
Label selector
set of objects
47 - Internal Use Only -
Replication controllers
Services
Replication
Controller
#Pods: 2
label selector: v1
Pod Pod
v1 v1
Replication
Controller
#Pods: 1
label selector: v2 Pod
v2
Pod
FE
Service
label selector: FE
Replication
Controller
#Pods: 2
label selector: FE,
v1
v1
Pod
FE
v1
48 - Internal Use Only -
Service
label selector: FE
Scaling
Rolling update
Service
label selector: FE
Pod
FE
Replication
Controller
#Pods: 2
label selector: FE,
v1
v1
Pod
FE
v1
Pod
FE
v1
Replication
Controller
#Pods: 3
label selector: FE,
v1
Pod
FE
Replication
Controller
#Pods: 3
label selector: FE,
v1
v1
Pod
FE
v1
Pod
FE
v1
Replication
Controller
#Pods: 2
label selector: FE,
v2
v2 v2
49 - Internal Use Only -
Kubernetes (k8s)
Container cluster manager
자동 배포/스케일링/멀티노드의 전체적인 운영관리
etcd - a simple, secure, fast and reliable distributed key-value store.
Kubernetes master - exposes the Kubernetes API using which containers are run on nodes to handle tasks.
Service proxy - runs on each node to provide the Kubernetes service interface for clients.
kubelet - an agent that runs on each node to monitor the containers running on the node, restarting them if required to
keep the replication level.
50 - Internal Use Only -
Kubernetes (k8s)
Cotainer cluster manager
자동 배포/스케일링/멀티노드의 전체적인 운영관리
Node
kubelet proxy
docker
Pod
Master
API
scheduler replication
controller
distributed
storage
kubectl
Pod
51 - Internal Use Only -
Kubernetes (k8s) 구성요소 - Compute Model
Node Pod
Physical/virtual machine
Network namespace
volumes
Managed lifecycle
apiVersion: v1 kind: Pod metadata: name: hello-world spec: containers: - image: tutum/hello-world name: hello-world ports: -containerPort: 8080
service
A logical set of pods
apiVersion: v1 kind: Service metadata: labels: app: hello-world name: hello-world spec: ports: - name: http port: 80 targetPort: http selector: app: hello-world type: LoadBalancer
Replication controller(rc)
ReplicaSet(rs)
apiVersion: v1 kind: ReplicationController metadata: name: hello-world spec: replicas: 2 template: metadata: labels: app: hello-world spec: containers: - image: tutum/hello-world name: hello-world ports: - containerPort: 8080 name: http
52 - Internal Use Only -
How to start Kubernetes (k8s) service
Installing Docker
Installing Kubernetes
Starting etcd
Starting Kubernetes Master
Starting Service Proxy
Listing the Kubernetes Docker Containers
Installing kubectl
Listing Services
Listing Nodes
Testing the Kubernetes Installation
53 - Internal Use Only -
Installing Docker
osc@osc-docker01:~$ cat /etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo ubuntu-trusty main
Add repository
osc@osc-docker01:~$ sudo apt-get install docker-engine
Install Docker
#Kernel setting change (MEMCG parameter)
#cat /etc/default/grub
#GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
root@osc-docker01:/kubernetes# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-4.2.0-41-generic root=/dev/mapper/osc--openstack01--vg-root ro cgroup_enable=memory
swapaccount=1
setting Kubernetes env
54 - Internal Use Only -
Installing K8s
docker run --net=host -d gcr.io/google_containers/etcd:2.0.12 /usr/local/bin/etcd --addr=127.0.0.1:4001 --bind-
addr=0.0.0.0:4001 --data-dir=/var/etcd/data
Starting etcd
docker run \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/dev:/dev \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
--volume=/var/run:/var/run:rw \
--net=host \
--pid=host \
--privileged=true \
-d \
gcr.io/google_containers/hyperkube:v1.0.1 \
/hyperkube kubelet --containerized --hostname-override="127.0.0.1" --address="0.0.0.0" –api-
servers=http://localhost:8080 --config=/etc/kubernetes/manifests
Starting Kubernetes Master
55 - Internal Use Only -
Installing K8s
docker run -d --net=host --privileged gcr.io/google_containers/hyperkube:v1.0.1 /hyperkube proxy --
master=http://127.0.0.1:8080 --v=2
Starting Service Proxy
root@osc-docker01:/kubernetes# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
f833c488adc6 gcr.io/google_containers/hyperkube:v1.0.1 "/hyperkube proxy --m" 3 minutes ago Up 3 minutes
high_heisenberg
7cd00b1b1d11 gcr.io/google_containers/hyperkube:v1.0.1 "/hyperkube scheduler" 5 minutes ago Up 5 minutes
k8s_scheduler.2744e742_k8s-master-127.0.0.1_default_f3ccbffbd75e3c5d2fb4ba69c8856c4a_be098670
ecf796c99eea gcr.io/google_containers/hyperkube:v1.0.1 "/hyperkube apiserver" 5 minutes ago Up 5 minutes
k8s_apiserver.cfb70250_k8s-master-127.0.0.1_default_f3ccbffbd75e3c5d2fb4ba69c8856c4a_ea72362d
77f6adafc9a0 gcr.io/google_containers/hyperkube:v1.0.1 "/hyperkube controlle" 5 minutes ago Up 5 minutes
k8s_controller-manager.1598ee5c_k8s-master-127.0.0.1_default_f3ccbffbd75e3c5d2fb4ba69c8856c4a_791aba02
ae1587171f61 gcr.io/google_containers/pause:0.8.0 "/pause" 5 minutes ago Up 5 minutes
k8s_POD.e4cc795_k8s-master-127.0.0.1_default_f3ccbffbd75e3c5d2fb4ba69c8856c4a_694e223f
849241db99ed gcr.io/google_containers/hyperkube:v1.0.1 "/hyperkube kubelet -" 5 minutes ago Up 5 minutes
prickly_joliot
1dd137229294 gcr.io/google_containers/etcd:2.0.12 "/usr/local/bin/etcd " 13 minutes ago Up 13 minutes
naughty_sinoussi
Output
56 - Internal Use Only -
Installing kubectl
sudo wget https://storage.googleapis.com/kubernetes-release/release/v1.0.1/bin/linux/amd64/kubectl
sudo chmod +x kubectl
sudo mv kubectl /usr/local/bin/
kubectl get services
kubectl get nodes
Kubectl install
57 - Internal Use Only -
Testing the kubernetes Installation
kubectl -s http://localhost:8080 run nginx --image=nginx --port=80
Run nginx
root@osc-docker01:/kubernetes# kubectl get svc nginx
NAME LABELS SELECTOR IP(S) PORT(S)
nginx run=nginx run=nginx 10.0.0.147 80/TCP
root@osc-docker01:/kubernetes# kubectl get svc nginx --template={{.spec.clusterIP}}
10.0.0.147
Output
root@osc-docker01:/kubernetes# curl 10.0.0.147
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
8848a7e6300e nginx "nginx -g 'daemon off" 10 minutes ago Up 10 minutes
k8s_nginx.d7d3eb2f_nginx-38nei_default_21c402bf-5594-11e6-8ad7-525400e1d08a_e03c12b7
58 - Internal Use Only -
Testing the kubernetes Installation
Output
root@osc-docker01:/kubernetes# docker inspect 8848a7e6300e | grep -i port
"PortBindings": null,
"PublishAllPorts": false,
"ExposedPorts": {
"KUBERNETES_PORT_443_TCP_PROTO=tcp",
"NGINX_PORT_80_TCP_PROTO=tcp",
"NGINX_PORT_80_TCP_PORT=80",
"NGINX_PORT_80_TCP=tcp://10.0.0.147:80",
"KUBERNETES_SERVICE_PORT=443",
"KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443",
"NGINX_PORT=tcp://10.0.0.147:80",
"NGINX_SERVICE_PORT=80",
"NGINX_PORT_80_TCP_ADDR=10.0.0.147",
"KUBERNETES_PORT=tcp://10.0.0.1:443",
"KUBERNETES_PORT_443_TCP_PORT=443",
"KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1",
root@osc-docker01:/kubernetes# ps -ef | grep 20550
root 20550 20535 0 22:57 ? 00:00:00 nginx: master process nginx -g daemon off;
sshd 20554 20550 0 22:57 ? 00:00:00 nginx: worker process
59 - Internal Use Only -
Testing the kubernetes Installation
Output
root@osc-docker01:/kubernetes# tail -f
/var/lib/docker/containers/8848a7e6300ef53317e3ecb67779d96e7f6f3c65d7e3aa207beca2216c6cb732/8848a7e6300ef53317
e3ecb67779d96e7f6f3c65d7e3aa207beca2216c6cb732-json.log
{"log":"172.17.0.1 - - [29/Jul/2016:14:03:11 +0000] \"GET / HTTP/1.1\" 200 612 \"-\" \"curl/7.35.0\" \"-
\"\n","stream":"stdout","time":"2016-07-29T14:03:11.559324964Z"}
root@osc-docker01:~# kubectl get services
NAME LABELS SELECTOR IP(S) PORT(S)
kubernetes component=apiserver,provider=kubernetes <none> 10.0.0.1 443/TCP
nginx run=nginx run=nginx 10.0.0.147 80/TCP
root@osc-docker01:~# kubectl get nodes
NAME LABELS STATUS
127.0.0.1 kubernetes.io/hostname=127.0.0.1 Ready
60 - Internal Use Only -
K8s server setting
Creating an Application
Creating an Application including a Replication Controller and Pod Replica/s
Usage:
kubectl run NAME --image=image [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [flags]
root@osc-docker01:~# kubectl -s http://localhost:8080 run hello-world --image=tutum/hello-world --replicas=1 --port=80
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
hello-world hello-world tutum/hello-world run=hello-world 1
root@osc-docker01:~# kubectl get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
hello-world hello-world tutum/hello-world run=hello-world 1
nginx nginx nginx run=nginx 1
root@osc-docker01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-nlewg 1/1 Running 0 1m
k8s-master-127.0.0.1 3/3 Running 0 7h
nginx-38nei 1/1 Running 0 6h
61 - Internal Use Only -
K8s server setting
Creating a Service
Create a Kubernetes Service using the kubectl expose command, which creates a Service from a Pod, Replication Controller, or another Service.
Usage:
kubectl expose RESOURCE NAME --port=port [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--
public-ip=ip] [--type=type] [flags]
root@osc-docker01:~# kubectl expose rc hello-world --port=8080 --type=LoadBalancer //(--selector="run=hello-world“)
NAME LABELS SELECTOR IP(S) PORT(S)
hello-world run=hello-world run=hello-world 8080/TCP
root@osc-docker01:~# kubectl get services
NAME LABELS SELECTOR IP(S) PORT(S)
hello-world run=hello-world run=hello-world 10.0.0.86 8080/TCP
kubernetes component=apiserver,provider=kubernetes <none> 10.0.0.1 443/TCP
nginx run=nginx run=nginx 10.0.0.147 80/TCP
62 - Internal Use Only -
K8s server setting
Describing a Pod
Create a Kubernetes Service using the kubectl expose command, which creates a Service from a Pod, Replication Controller, or another Service.
root@osc-docker01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-mpgon 1/1 Running 0 7m
k8s-master-127.0.0.1 3/3 Running 0 9h
nginx-38nei 1/1 Running 0 7h
root@osc-docker01:~# kubectl describe pod hello-world-mpgon
Name: hello-world-mpgon
Namespace: default
Image(s): tutum/hello-world
Node: 127.0.0.1/127.0.0.1
Labels: run=hello-world
Status: Running
Reason:
Message:
IP: 172.17.0.3
Replication Controllers: hello-world (1/1 replicas created)
Containers:
hello-world:
Image: tutum/hello-world
State: Running
Started: Sat, 30 Jul 2016 06:18:03 +0900
Ready: True
Restart Count: 0
Conditions:
Type Status
Ready True
63 - Internal Use Only -
K8s server setting
Hello-world application
root@osc-docker01:~# kubectl scale rc hello-world --replicas=4
scaled
root@osc-docker01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-858l7 0/1 Image: tutum/hello-world is ready, container is creating 0 4s
hello-world-srgxo 1/1 Running 0 2m
hello-world-txt0g 0/1 Image: tutum/hello-world is ready, container is creating 0 4s
hello-world-vcnw2 0/1 Image: tutum/hello-world is ready, container is creating 0 4s
k8s-master-127.0.0.1 3/3 Running 0 8h
nginx-38nei 1/1 Running 0 6h
root@osc-docker01:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-858l7 1/1 Running 0 38s
hello-world-srgxo 1/1 Running 0 2m
hello-world-txt0g 1/1 Running 0 38s
hello-world-vcnw2 1/1 Running 0 38s
k8s-master-127.0.0.1 3/3 Running 0 8h
nginx-38nei 1/1 Running 0 6h
64 - Internal Use Only -
K8s server setting
Hello-world application
root@osc-docker01:~# kubectl describe svc hello-world
Name: hello-world
Namespace: default
Labels: run=hello-world
Selector: run=hello-world
Type: LoadBalancer
IP: 10.0.0.91
Port: <unnamed> 8080/TCP
NodePort: <unnamed> 30723/TCP
Endpoints: 172.17.0.3:8080,172.17.0.4:8080,172.17.0.5:8080 + 1 more...
Session Affinity: None
No events.
root@osc-docker01:~# kubectl get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
hello-world hello-world tutum/hello-world run=hello-world 4
nginx nginx nginx run=nginx 1
65 - Internal Use Only -
K8s server setting
Deleting a Replication Controller
oot@osc-openstack01:~# kubectl delete rc hello-world
replicationcontrollers/hello-world
root@osc-openstack01:~#
root@osc-openstack01:~# kubectl get rc
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
nginx nginx nginx run=nginx 1
oot@osc-openstack01:~# kubectl delete svc hello-world
services/hello-world
root@osc-openstack01:~#
root@osc-openstack01:~# kubectl get services
NAME LABELS SELECTOR IP(S) PORT(S)
kubernetes component=apiserver,provider=kubernetes <none> 10.0.0.1 443/TCP
nginx run=nginx run=nginx 10.0.0.194 80/TCP
66 - Internal Use Only -
OPEN
SHARE
CONTRIBUTE
ADOPT
REUSE