*Kubernetes는 다음을 자동으로 처리하는 common API와 self-heading 프레임워크를 제공한다 : 

- 머신 장애

- 어플리케이션 배포, 로깅 모니터링 간소화


최소한의 통제로 자율성을 얻기 위한 툴이다!


점차 Operation 팀의 역할은 줄어들고 있다...

비즈니스는 점점 서비스와 어플리케이션에 포커스를 두고...인프라는 비중이 작아지고 있음


*Kubernetes의 구성도 : 


1) API Node

Kubernetes의 모든 API서버는 Node들의 모든 정보를 가지고 있어야 한다.

스케줄러가 제일 먼저 하는 일은....어디에 배포할 것인가 결정

API Node 내에 controller manager(replication controller)는 node의 status를 알려준다.

->Kubernetes는 ETCD라는 분산형태 DB를 사용한다,



2) Worker Node 

Core OS가 Rocket(docker가 너무 무거워)이라는 컨테이너를 발표!

Kubelet은 컨테이너에 대한 구성요소 제어를 맡고 있음

각 pod간 통신은 VXLAN(overlay)를 사용

proxy는 app 레벨의 인스턴스가 있고,

실제 호스트에 있는 iptables (kernel 레이어에 있음)에 데이터 처리에 관한 내용이 있음



※ kubelet은 유일하게 시스템 서비스로 들어간다! 나머지는 컨테이너로 설치 가능



- 왜 Swarm을 사용하는가? 사용하기 쉬움

- 왜 Kubernetes를 사용하는가? 많은 Resource를 제어할 수 있음





1. Docker 설치 (모든 노드)


1
2
3
4
5
6
7
8
9
yum install -y docker
systemctl enable docker && systemctl start docker
 
touch /etc/docker/daemon.json
cat <<EOF > /etc/docker/daemon.json
{
"insecure-registries":["10.10.12.0/24"]
}
EOF
cs


2. Kubernetes 설치 (모든 노드)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
sed ---follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
systemctl stop firewalld.service
systemctl disable firewalld.service
 
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
 
swapoff -&& sed -'/swap/s/^/#/' /etc/fstab
 
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet


*MASTER Node 설치(이미지를 전부 다 Load한다) :

> docker load -i <이미지명>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@host01-2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@host01-2 ~]# ls
anaconda-ks.cfg  dns-kube-dns  dns-sidecar  etcd-amd64  hk
[root@host01-2 ~]# cd hk
[root@host01-2 hk]# ls
dns-kube-dns  etcd-amd64  kubeadm.host08-1.root.log.INFO.20180525-060140.2620  kube-apiserver   kube-proxy      weave-kube
dns-sidecar   k8s-dns     kubeadm.INFO                                         kube-controller  kube-scheduler  weave-npc
[root@host01-2 hk]# ls -al
total 974632
drwxr-xr-x. 2 root root       277 May 25 10:27 .
dr-xr-x---5 root root       226 May 25 10:27 ..
-rw-------1 root root  50727424 May 25 10:27 dns-kube-dns
-rw-------1 root root  42481152 May 25 10:27 dns-sidecar
-rw-------1 root root 193461760 May 25 10:27 etcd-amd64
-rw-------1 root root  41239040 May 25 10:27 k8s-dns
-rw-r--r--1 root root       343 May 25 10:27 kubeadm.host08-1.root.log.INFO.20180525-060140.2620
-rw-r--r--1 root root       343 May 25 10:27 kubeadm.INFO
-rw-------1 root root 225319936 May 25 10:27 kube-apiserver
-rw-------1 root root 148110336 May 25 10:27 kube-controller
-rw-------1 root root  98924032 May 25 10:27 kube-proxy
-rw-------1 root root  50635776 May 25 10:27 kube-scheduler
-rw-------1 root root  99517952 May 25 10:27 weave-kube
-rw-------1 root root  47575552 May 25 10:27 weave-npc
[root@host01-2 hk]# docker load -i kube-proxy
582b548209e1: Loading layer [==================================================>]  44.2 MB/44.2 MB
e20569a478ed: Loading layer [==================================================>3.358 MB/3.358 MB
6b4e4941a965: Loading layer [==================================================>51.35 MB/51.35 MB
Loaded image: k8s.gcr.io/kube-proxy-amd64:v1.10.3
[root@host01-2 hk]# docker load -i weave-kube
5bef08742407: Loading layer [==================================================>4.221 MB/4.221 MB
c3355c8b5c3e: Loading layer [==================================================>19.03 MB/19.03 MB
a83fa3df4138: Loading layer [==================================================>29.55 MB/29.55 MB
020fdc01af85: Loading layer [==================================================>]  11.6 MB/11.6 MB
2ea881a632b7: Loading layer [==================================================>2.048 kB/2.048 kB
396aa46bcbea: Loading layer [==================================================>35.09 MB/35.09 MB
Loaded image: docker.io/weaveworks/weave-kube:2.3.0
[root@host01-2 hk]# docker load -i weave-npc
8dccfe2dec8c: Loading layer [==================================================>2.811 MB/2.811 MB
3249ff6df12f: Loading layer [==================================================>40.52 MB/40.52 MB
3dc458d34b22: Loading layer [==================================================>]  2.56 kB/2.56 kB
Loaded image: docker.io/weaveworks/weave-npc:2.3.0
[root@host01-2 hk]#
 
cs

*기타 Node 설치(kube-proxy, weave-kube, weave-npc를 설치한다) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
[root@host01-2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@host01-2 ~]# ls
anaconda-ks.cfg  dns-kube-dns  dns-sidecar  etcd-amd64  hk
[root@host01-2 ~]# cd hk
[root@host01-2 hk]# ls
dns-kube-dns  etcd-amd64  kubeadm.host08-1.root.log.INFO.20180525-060140.2620  kube-apiserver   kube-proxy      weave-kube
dns-sidecar   k8s-dns     kubeadm.INFO                                         kube-controller  kube-scheduler  weave-npc
[root@host01-2 hk]# ls -al
total 974632
drwxr-xr-x. 2 root root       277 May 25 10:27 .
dr-xr-x---5 root root       226 May 25 10:27 ..
-rw-------1 root root  50727424 May 25 10:27 dns-kube-dns
-rw-------1 root root  42481152 May 25 10:27 dns-sidecar
-rw-------1 root root 193461760 May 25 10:27 etcd-amd64
-rw-------1 root root  41239040 May 25 10:27 k8s-dns
-rw-r--r--1 root root       343 May 25 10:27 kubeadm.host08-1.root.log.INFO.20180525-060140.2620
-rw-r--r--1 root root       343 May 25 10:27 kubeadm.INFO
-rw-------1 root root 225319936 May 25 10:27 kube-apiserver
-rw-------1 root root 148110336 May 25 10:27 kube-controller
-rw-------1 root root  98924032 May 25 10:27 kube-proxy
-rw-------1 root root  50635776 May 25 10:27 kube-scheduler
-rw-------1 root root  99517952 May 25 10:27 weave-kube
-rw-------1 root root  47575552 May 25 10:27 weave-npc
[root@host01-2 hk]# docker load -i kube-proxy
582b548209e1: Loading layer [==================================================>]  44.2 MB/44.2 MB
e20569a478ed: Loading layer [==================================================>3.358 MB/3.358 MB
6b4e4941a965: Loading layer [==================================================>51.35 MB/51.35 MB
Loaded image: k8s.gcr.io/kube-proxy-amd64:v1.10.3
[root@host01-2 hk]# docker load -i weave-kube
5bef08742407: Loading layer [==================================================>4.221 MB/4.221 MB
c3355c8b5c3e: Loading layer [==================================================>19.03 MB/19.03 MB
a83fa3df4138: Loading layer [==================================================>29.55 MB/29.55 MB
020fdc01af85: Loading layer [==================================================>]  11.6 MB/11.6 MB
2ea881a632b7: Loading layer [==================================================>2.048 kB/2.048 kB
396aa46bcbea: Loading layer [==================================================>35.09 MB/35.09 MB
Loaded image: docker.io/weaveworks/weave-kube:2.3.0
[root@host01-2 hk]# docker load -i weave-npc
8dccfe2dec8c: Loading layer [==================================================>2.811 MB/2.811 MB
3249ff6df12f: Loading layer [==================================================>40.52 MB/40.52 MB
3dc458d34b22: Loading layer [==================================================>]  2.56 kB/2.56 kB
Loaded image: docker.io/weaveworks/weave-npc:2.3.0
[root@host01-2 hk]#
 
cs


*Master Node에서 실행 :

1
2
3
4
5
kubeadm init
 
mkdir -p $HOME/.kube
cp -/etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
cs


실행방법 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@host01-4 ~]# docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy-amd64                v1.10.3             4261d315109d        3 days ago          97.1 MB
k8s.gcr.io/kube-apiserver-amd64            v1.10.3             e03746fe22c3        3 days ago          225 MB
k8s.gcr.io/kube-controller-manager-amd64   v1.10.3             40c8d10b2d11        3 days ago          148 MB
k8s.gcr.io/kube-scheduler-amd64            v1.10.3             353b8f1d102e        3 days ago          50.4 MB
docker.io/weaveworks/weave-npc             2.3.0               21545eb3d6f9        6 weeks ago         47.2 MB
docker.io/weaveworks/weave-kube            2.3.0               f15514acce73        6 weeks ago         96.8 MB
k8s.gcr.io/etcd-amd64                      3.1.12              52920ad46f5b        2 months ago        193 MB
k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64     1.14.8              c2ce1ffb51ed        4 months ago        41 MB
k8s.gcr.io/k8s-dns-sidecar-amd64           1.14.8              6f7f2dc7fab5        4 months ago        42.2 MB
k8s.gcr.io/k8s-dns-kube-dns-amd64          1.14.8              80cc5ea4b547        4 months ago        50.5 MB
[root@host01-4 ~]# clear
[root@host01-4 ~]# kubeadm init
[init] Using Kubernetes version: v1.10.3
[init] Using Authorization modes: [Node RBAC]
[preflight] Running pre-flight checks.
        [WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [host01-4.cloud.com kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.10.12.14]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [host01-4.cloud.com] and IPs [10.10.12.14]
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated apiserver-etcd-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
[controlplane] Wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"
[controlplane] Wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
[controlplane] Wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"
[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
[init] Waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests".
[init] This might take a minute or longer if the control plane images have to be pulled.
[apiclient] All control plane components are healthy after 25.003852 seconds
[uploadconfig] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[markmaster] Will mark node host01-4.cloud.com as master by adding a label and a taint
[markmaster] Master host01-4.cloud.com tainted and labelled with key/value: node-role.kubernetes.io/master=""
[bootstraptoken] Using token: hlz7wp.qjrgmsq2yn9f94wa
[bootstraptoken] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: kube-dns
[addons] Applied essential addon: kube-proxy
 
Your Kubernetes master has initialized successfully!
 
To start using your cluster, you need to run the following as a regular user:
 
  mkdir -p $HOME/.kube
  sudo cp -/etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
 
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/
 
You can now join any number of machines by running the following on each node
as root:
 
  kubeadm join 10.10.12.14:6443 --token hlz7wp.qjrgmsq2yn9f94wa --discovery-token-ca-cert-hash sha256:43e61417b20ede5ca530fe0638990bc1a805b5f2a9e25b5aa2f40023b392fb50
 
[root@host01-4 ~]#
[root@host01-4 ~]# mkdir -p $HOME/.kube
[root@host01-4 ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@host01-4 ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@host01-4 ~]#
 
cs


위 실행결과에서 토큰을 복사해놓는다 :

1
2
3
  kubeadm join 10.10.12.14:6443 --token hlz7wp.qjrgmsq2yn9f94wa --discovery-token-ca-cert-hash sha256:43e61417b20ede5ca530fe0638990bc1a805b5f2a9e25b5aa2f40023b392fb50
 
 
cs


*Pod Network 설치 (마스터)

1
2
3
4
5
6
7
sysctl net.bridge.bridge-nf-call-iptables=1
 
export kubever=$(kubectl version | base64 | tr -'\n')
 
kubectl apply -"https://cloud.weave.works/k8s/net?k8s-version=$kubever"
 
kubectl get pods --all-namespaces 
cs


실행결과 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@host01-4 ~]# sysctl net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-iptables = 1
[root@host01-4 ~]#
[root@host01-4 ~]# export kubever=$(kubectl version | base64 | tr -d '\n')
[root@host01-4 ~]#
[root@host01-4 ~]# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$kubever"
serviceaccount "weave-net" created
clusterrole.rbac.authorization.k8s.io "weave-net" created
clusterrolebinding.rbac.authorization.k8s.io "weave-net" created
role.rbac.authorization.k8s.io "weave-net" created
rolebinding.rbac.authorization.k8s.io "weave-net" created
daemonset.extensions "weave-net" created
[root@host01-4 ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                         READY     STATUS    RESTARTS   AGE
kube-system   etcd-host01-4.cloud.com                      1/1       Running   0          4m
kube-system   kube-apiserver-host01-4.cloud.com            1/1       Running   0          4m
kube-system   kube-controller-manager-host01-4.cloud.com   1/1       Running   0          5m
kube-system   kube-dns-86f4d74b45-t9df2                    0/3       Pending   0          5m
kube-system   kube-proxy-fs9d8                             1/1       Running   0          5m
kube-system   kube-scheduler-host01-4.cloud.com            1/1       Running   0          4m
kube-system   weave-net-zr5qr                              2/2       Running   0          11s
[root@host01-4 ~]#
 
 
cs


*이전에 Master Node에서 복사한 토큰을 2개 Worker Node에 각각

입력한다 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@host01-3 ~]# kubeadm join 10.10.12.14:6443 --token hlz7wp.qjrgmsq2yn9f94wa --discovery-token-ca-cert-hash sha256:43e61417b20ede5ca530fe0638990bc1a805b5f2a9e25b5aa2f40023b392fb50
[preflight] Running pre-flight checks.
        [WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
[discovery] Trying to connect to API Server "10.10.12.14:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://10.10.12.14:6443"
[discovery] Requesting info from "https://10.10.12.14:6443" again to validate TLS against the pinned public key
[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "10.10.12.14:6443"
[discovery] Successfully established connection with API Server "10.10.12.14:6443"
 
This node has joined the cluster:
* Certificate signing request was sent to master and a response
  was received.
* The Kubelet was informed of the new secure connection details.
 
Run 'kubectl get nodes' on the master to see this node join the cluster.
[root@host01-3 ~]#
 
cs


*Master Node에서 멤버 Join이 이루어졌는지 확인한다(Status에 Ready가 떠야 한다.)

1
2
3
4
5
6
7
8
 
[root@host01-4 ~]# kubectl get no
NAME                 STATUS     ROLES     AGE       VERSION
host01-2.cloud.com   NotReady   <none>    6s        v1.10.3
host01-3.cloud.com   NotReady   <none>    11s       v1.10.3
host01-4.cloud.com   Ready      master    9m        v1.10.3
[root@host01-4 ~]# ^C
 
cs
1
2
3
4
5
6
7
[root@host01-4 ~]# kubectl get no
NAME                 STATUS    ROLES     AGE       VERSION
host01-2.cloud.com   Ready     <none>    2m        v1.10.3
host01-3.cloud.com   Ready     <none>    2m        v1.10.3
host01-4.cloud.com   Ready     master    11m       v1.10.3
[root@host01-4 ~]#
 
cs






*Tab 자동 입력


AWS, Azure에서는 kubectl 명령어가 그냥 'k'임(alias로 등록되어 있음)


*Alias 등록 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@host01-4 ~]# alias k=kubectl
[root@host01-4 ~]# source <(kubectl completion bash | sed  s/kubectl/k/g)
[root@host01-4 ~]# k get
You must specify the type of resource to get. Valid resource types include:
 
  * all
  * certificatesigningrequests (aka 'csr')
  * clusterrolebindings
  * clusterroles
  * componentstatuses (aka 'cs')
  * configmaps (aka 'cm')
  * controllerrevisions
  * cronjobs
  * customresourcedefinition (aka 'crd')
  * daemonsets (aka 'ds')
  * deployments (aka 'deploy')
  * endpoints (aka 'ep')
  * events (aka 'ev')
  * horizontalpodautoscalers (aka 'hpa')
  * ingresses (aka 'ing')
  * jobs
  * limitranges (aka 'limits')
  * namespaces (aka 'ns')
  * networkpolicies (aka 'netpol')
  * nodes (aka 'no')
  * persistentvolumeclaims (aka 'pvc')
  * persistentvolumes (aka 'pv')
  * poddisruptionbudgets (aka 'pdb')
  * podpreset
  * pods (aka 'po')
  * podsecuritypolicies (aka 'psp')
  * podtemplates
  * replicasets (aka 'rs')
  * replicationcontrollers (aka 'rc')
  * resourcequotas (aka 'quota')
  * rolebindings
  * roles
  * secrets
  * serviceaccounts (aka 'sa')
  * services (aka 'svc')
  * statefulsets (aka 'sts')
  * storageclasses (aka 'sc')error: Required resource not specified.
Use "kubectl explain <resource>" for a detailed description of that resource (e.g. kubectl explain pods).
See 'kubectl get -h' for help and examples.
[root@host01-4 ~]# echo "alias l=kubectl" >> ~/.bashrc
[root@host01-4 ~]# echo "source <(kubectl completion bash | sed s/kubectl/k/g)" >> ~/.bashrc
 
cs





*Pod 개념

MS 아키텍처의 기초는 하나의 컨테이너에는 하나의 어플리케이션만 집어넣자!

- 네트워크 공유 : Pod로 구성하면 Container끼리 네트워크를 공유할 수 있게된다(네트워크 Name Space 공유 - localhost로)

- 스토리지 공유 : 볼륨을 pod에 마운트 하여 Storage까지 공유한다!


- 1개 pod 내에서 콘테이너들은 동일 호스트로만 구성이 된다. 똑같은 포트의 서비스를 공유할 수 없다.

(pod를 잘못구성하는 예 : DB Container와 Web Container를 동일 pod 내에 두었을 때 Scale Out 시 이슈가 있음,

따라서 web pod, WAS pod, DB pod 식으로 나눠야 함)

pod가 kubenetes의 최소 단위로 구성해야 함!


각 Work Node의 Pod 들은 Master Node의 replication controller(RC)에서 제어된다.


- 참조/관리 형태

참조를 하는 주체는 selector (key/value 형태)

참조를 받은 대상은 Label (key/value 형태)  = Pod(Node Selector)

Pod/Label의 참조를 받는 대상은 Label (key/value 형태) 

라고 부른다.

- Kubenetes의 Namespace란 : 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@host01-4 hk]# k get po -n kube-system -o  wide
NAME                                         READY     STATUS    RESTARTS   AGE       IP            NODE
etcd-host01-4.cloud.com                      1/1       Running   0          58m       10.10.12.14   host01-4.cloud.com
kube-apiserver-host01-4.cloud.com            1/1       Running   0          58m       10.10.12.14   host01-4.cloud.com
kube-controller-manager-host01-4.cloud.com   1/1       Running   0          59m       10.10.12.14   host01-4.cloud.com
kube-dns-86f4d74b45-t9df2                    3/3       Running   0          1h        10.32.0.2     host01-4.cloud.com
kube-proxy-fs9d8                             1/1       Running   0          1h        10.10.12.14   host01-4.cloud.com
kube-proxy-r5bzj                             1/1       Running   0          51m       10.10.12.13   host01-3.cloud.com
kube-proxy-tvwnv                             1/1       Running   0          51m       10.10.12.12   host01-2.cloud.com
kube-scheduler-host01-4.cloud.com            1/1       Running   0          59m       10.10.12.14   host01-4.cloud.com
weave-net-hf9d5                              2/2       Running   1          51m       10.10.12.12   host01-2.cloud.com
weave-net-p5drv                              2/2       Running   1          51m       10.10.12.13   host01-3.cloud.com
weave-net-zr5qr                              2/2       Running   0          54m       10.10.12.14   host01-4.cloud.com
 
cs

*Cluster PAI 확인 : 

1
2
3
4
5
[root@host01-4 hk]# k cluster-info
Kubernetes master is running at https://10.10.12.14:6443
KubeDNS is running at https://10.10.12.14:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
 
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
cs


*Pod 가 어디있는지 상관없이 연결이 된다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@host01-4 hk]# ls
dns-kube-dns  etcd-amd64  kubeadm.host08-1.root.log.INFO.20180525-060140.2620  kube-apiserver   kube-proxy      weave-kube
dns-sidecar   k8s-dns     kubeadm.INFO                                         kube-controller  kube-scheduler  weave-npc
[root@host01-4 hk]# k run --image=reg.cloud.com/nginx --port=80 --generator=run/v1
error: NAME is required for run
See 'kubectl run -h' for help and examples.
[root@host01-4 hk]# k run --image=reg.cloud.com/nginx --port=80 --generator=run/v1
error: NAME is required for run
See 'kubectl run -h' for help and examples.
[root@host01-4 hk]# k run --image=reg.cloud.com/nginx nginx-app --port=80 --generator=run/v1
replicationcontroller "nginx-app" created
[root@host01-4 hk]# k get rc
NAME        DESIRED   CURRENT   READY     AGE
nginx-app   1         1         0         8s
[root@host01-4 hk]# k get po
NAME              READY     STATUS    RESTARTS   AGE
nginx-app-gb6ch   1/1       Running   0          15s
[root@host01-4 hk]# get po -o wide
-bash: get: command not found
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-gb6ch   1/1       Running   0          32s       10.36.0.1   host01-3.cloud.com
[root@host01-4 hk]# k logs
error: expected 'logs (POD | TYPE/NAME) [CONTAINER_NAME]'.
POD or TYPE/NAME is a required argument for the logs command
See 'kubectl logs -h' for help and examples.
[root@host01-4 hk]# k logs nginx-app-df618
Error from server (NotFound): pods "nginx-app-df618" not found
[root@host01-4 hk]# k logs nginx-app-gb6ch
[root@host01-4 hk]# k exec -it
error: expected 'exec POD_NAME COMMAND [ARG1] [ARG2] ... [ARGN]'.
POD_NAME and COMMAND are required arguments for the exec command
See 'kubectl exec -h' for help and examples.
[root@host01-4 hk]# k exec -it nginx-app-gb6ch bash
root@nginx-app-gb6ch:/#
 
cs

-podname이 container의 hostname이 된다.



*pod name으로 삭제 해도 다시 살아난다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-gb6ch   1/1       Running   0          5m        10.36.0.1   host01-3.cloud.com
[root@host01-4 hk]# delete po nginx-app-gb6ch
-bash: delete: command not found
[root@host01-4 hk]# k delete po nginx-app-gb6ch
pod "nginx-app-gb6ch" deleted
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS              RESTARTS   AGE       IP        NODE
nginx-app-gnpsd   0/1       ContainerCreating   0          6s        <none>    host01-2.cloud.com
[root@host01-4 hk]# k get rc
NAME        DESIRED   CURRENT   READY     AGE
nginx-app   1         1         1         5m
[root@host01-4 hk]#
 
cs


*Scale in Scale out


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
[root@host01-4 hk]# k describe rc nginx-app
dns-kube-dns                                         kubeadm.host08-1.root.log.INFO.20180525-060140.2620  kube-proxy
dns-sidecar                                          kubeadm.INFO                                         kube-scheduler
etcd-amd64                                           kube-apiserver                                       weave-kube
k8s-dns                                              kube-controller                                      weave-npc
[root@host01-4 hk]# k describe rc nginx-app
Name:         nginx-app
Namespace:    default
Selector:     run=nginx-app
Labels:       run=nginx-app
Annotations:  <none>
Replicas:     1 current / 1 desired
Pods Status:  1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  run=nginx-app
  Containers:
   nginx-app:
    Image:        reg.cloud.com/nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  6m    replication-controller  Created pod: nginx-app-gb6ch
  Normal  SuccessfulCreate  1m    replication-controller  Created pod: nginx-app-gnpsd
[root@host01-4 hk]# k get po
NAME              READY     STATUS    RESTARTS   AGE
nginx-app-gnpsd   1/1       Running   0          1m
[root@host01-4 hk]# k describe po nginx-app-gnpsd
Name:           nginx-app-gnpsd
Namespace:      default
Node:           host01-2.cloud.com/10.10.12.12
Start Time:     Fri, 25 May 2018 11:58:20 +0900
Labels:         run=nginx-app
Annotations:    <none>
Status:         Running
IP:             10.44.0.1
Controlled By:  ReplicationController/nginx-app
Containers:
  nginx-app:
    Container ID:   docker://6d0e9cb190b31334dee5dba4877ace52d8afd5a9956d7c50eae35d3107722a58
    Image:          reg.cloud.com/nginx
    Image ID:       docker-pullable://reg.cloud.com/nginx@sha256:a4fb15454c43237dbc6592c4f8e0b50160ceb03e852a10c9895cf2a6d16c7fe2
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 25 May 2018 11:58:29 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-85hdm (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  default-token-85hdm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-85hdm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason                 Age   From                         Message
  ----    ------                 ----  ----                         -------
  Normal  SuccessfulMountVolume  1m    kubelet, host01-2.cloud.com  MountVolume.SetUp succeeded for volume "default-token-85hdm"
  Normal  Scheduled              1m    default-scheduler            Successfully assigned nginx-app-gnpsd to host01-2.cloud.com
  Normal  Pulling                1m    kubelet, host01-2.cloud.com  pulling image "reg.cloud.com/nginx"
  Normal  Pulled                 1m    kubelet, host01-2.cloud.com  Successfully pulled image "reg.cloud.com/nginx"
  Normal  Created                1m    kubelet, host01-2.cloud.com  Created container
  Normal  Started                1m    kubelet, host01-2.cloud.com  Started container
[root@host01-4 hk]# k get rc
NAME        DESIRED   CURRENT   READY     AGE
nginx-app   1         1         1         8m
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-gnpsd   1/1       Running   0          3m        10.44.0.1   host01-2.cloud.com
 
[root@host01-4 hk]# k scale rc nginx-app --replicas=3
replicationcontroller "nginx-app" scaled
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-gnpsd   1/1       Running   0          4m        10.44.0.1   host01-2.cloud.com
nginx-app-jfmkd   1/1       Running   0          10s       10.44.0.2   host01-2.cloud.com
nginx-app-ww6sn   1/1       Running   0          10s       10.36.0.1   host01-3.cloud.com
 
[root@host01-4 hk]# k scale rc nginx-app --replicas=0
replicationcontroller "nginx-app" scaled
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS        RESTARTS   AGE       IP          NODE
nginx-app-gnpsd   0/1       Terminating   0          5m        10.44.0.1   host01-2.cloud.com
nginx-app-jfmkd   0/1       Terminating   0          34s       10.44.0.2   host01-2.cloud.com
[root@host01-4 hk]# k get po -o wide
No resources found.
[root@host01-4 hk]# rc 0
-bash: rc: command not found
[root@host01-4 hk]# k scale rc nginx-app --replicas=1
replicationcontroller "nginx-app" scaled
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-7qpbv   1/1       Running   0          4s        10.36.0.1   host01-3.cloud.com
[root@host01-4 hk]#
 
cs



*Yaml으로 pod 조회

1
2
[root@host01-4 hk]# k get po nginx-app-7qpbv -o yaml
 
cs


*Yaml 파일로 추출 후 pod 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: v1
kind: Pod
metadata:
  labels:
    type: web
  name: nginx-hk-app
spec:
  containers:
  - image: reg.cloud.com/nginx
    name: nginx-app
    ports:                            #expose와 동일
    - containerPort: 80
      protocol: TCP
 
 
 
[root@host01-4 hk]# k get po nginx-app-7qpbv -o yaml > temp.yaml
[root@host01-4 hk]# vi temp.yaml
[root@host01-4 hk]# k create -f temp.yaml
pod "nginx-hk-app" created
[root@host01-4 hk]# k get po -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-7qpbv   1/1       Running   0          6m        10.36.0.1   host01-3.cloud.com
nginx-hk-app      1/1       Running   0          13s       10.44.0.1   host01-2.cloud.com
[root@host01-4 hk]#
 
cs



*Yaml 파일로 rc 생성 후 배포 :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@host01-4 hk]# k get rc nginx-app -o yaml > hk.yaml
[root@host01-4 hk]# vi hk.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  labels:
    run: nginx-app
  name: nginx-app
spec:
  replicas: 1
  selector:
    type: test
  template:
    metadata:
      labels:
        type: test
    spec:
      containers:
      - image: reg.cloud.com/nginx
        name: nginx-app
        ports:
        - containerPort: 80
          protocol: TCP
 
 
[root@host01-4 hk]# k create -f hk.yaml
replicationcontroller "nginx-app2" created
[root@host01-4 hk]# k get rc
NAME         DESIRED   CURRENT   READY     AGE
nginx-app    1         1         1         24m
nginx-app2   1         1         1         9s
[root@host01-4 hk]# k get po -o wide
NAME               READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-app-7qpbv    1/1       Running   0          14m       10.36.0.1   host01-3.cloud.com
nginx-app2-tgqqf   1/1       Running   0          16s       10.36.0.2   host01-3.cloud.com
nginx-hk-app       1/1       Running   0          7m        10.44.0.1   host01-2.cloud.com
[root@host01-4 hk]#
 
cs


*이미지 Tag 명 변경 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
testrun                 latest              f18e71700865        2 hours ago         303MB
<none>                  <none>              8af1360f5b43        2 hours ago         303MB
hongtest                latest              9a8341b96270        3 hours ago         98.4MB
<none>                  <none>              ab0f85b7f2e6        3 hours ago         1.13MB
<none>                  <none>              9579d5ccef21        3 hours ago         1.13MB
hongimages              latest              f6d8cd4a7d71        3 hours ago         1.13MB
ubuntu                  14.04               8cef1fa16c77        3 weeks ago         223MB
ubuntu                  latest              452a96d81c30        3 weeks ago         79.6MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu    latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/busybox   latest              6ad733544a63        6 months ago        1.13MB
reg.cloud.com/centos    latest              d123f4e55e12        6 months ago        197MB
[root@host01-2 overlay2]# vi ~/.bashrc
[root@host01-2 overlay2]# docker tag hongtest registry:3.0
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
testrun                 latest              f18e71700865        2 hours ago         303MB
<none>                  <none>              8af1360f5b43        2 hours ago         303MB
hongtest                latest              9a8341b96270        3 hours ago         98.4MB
registry                3.0                 9a8341b96270        3 hours ago         98.4MB
<none>                  <none>              ab0f85b7f2e6        3 hours ago         1.13MB
<none>                  <none>              9579d5ccef21        3 hours ago         1.13MB
hongimages              latest              f6d8cd4a7d71        3 hours ago         1.13MB
ubuntu                  14.04               8cef1fa16c77        3 weeks ago         223MB
ubuntu                  latest              452a96d81c30        3 weeks ago         79.6MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu    latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/busybox   latest              6ad733544a63        6 months ago        1.13MB
reg.cloud.com/centos    latest              d123f4e55e12        6 months ago        197MB
[root@host01-2 overlay2]#
 
cs



*이미지 Tag 삭제(명령어는 없고 이미지를 지우면 된다) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
testrun                 latest              f18e71700865        2 hours ago         303MB
<none>                  <none>              8af1360f5b43        2 hours ago         303MB
registry                3.0                 9a8341b96270        3 hours ago         98.4MB
hongtest                latest              9a8341b96270        3 hours ago         98.4MB
<none>                  <none>              ab0f85b7f2e6        3 hours ago         1.13MB
<none>                  <none>              9579d5ccef21        3 hours ago         1.13MB
hongimages              latest              f6d8cd4a7d71        3 hours ago         1.13MB
ubuntu                  14.04               8cef1fa16c77        3 weeks ago         223MB
ubuntu                  latest              452a96d81c30        3 weeks ago         79.6MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu    latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/busybox   latest              6ad733544a63        6 months ago        1.13MB
reg.cloud.com/centos    latest              d123f4e55e12        6 months ago        197MB
[root@host01-2 overlay2]# docker rmi hongtest
Untagged: hongtest:latest
[root@host01-2 overlay2]#
 
cs



*docker image tag의 latest 포인터 이동(3.0을 latest로 만들기) :

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
registry                3.0                 9a8341b96270        3 hours ago         98.4MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
[root@host01-2 overlay2]# docker tag registry:3.0 registry:latest
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
registry                3.0                 9a8341b96270        3 hours ago         98.4MB
registry                latest              9a8341b96270        3 hours ago         98.4MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
 
 
 
cs


*Dockerfile 만들기


Dockerfile은 쉽고 간단하며 명확한 구문을 갖는 텍스트 파일로 이미지 생성을 위한 핵심 요소!


-인스트럭션: 대소문자를 구분하지 않지만 가독성을 위해 대문자 사용! 구성 순서가 중요

-주석 : #

-MAINTAINER: 구성 위치 상관없으며 레이어를 생성하지 않음


*인스트럭션 종류

FROM

MAINTAINER

LABEL

RUN

COPY files 

ADD files

EXPOSE


ENTRYPOINT

CMD



- ADD와 COPY의 구분

1
2
3
4
5
6
7
8
COPY files/nginx.conf /etc/nginx/nginx.conf
#COPY는 url을 지정할 수 없다
 
ADD files/nginx.conf /etc/nginx/nginx.conf
#ADD는 url을 지정할 수 있다
 
#주의사항 files는 현재 커서가 있는 위치를 의미한다
#nginx.conf 뒤에 /가 붙으면 해당 폴더 밑에 파일들을 copy한다는 의미(따라서 /는 빼야 )
cs


RUN, COPY, ADD를 할 때마다

Docker에서는 임시 레이어를 만들고 Commit을 수행한다.

그리고 결국 Alpine까지 4개의 레이어로 구성된 이미지가 만들어진다!


*RUN 명령어를 시작할 때 아래와 같이 실행하면 좋은점:
=>RUN은 이미지 빌드 명령어임! 실행 인스트럭션이 아님!

임시 레이어를 매번 생성하지 않아 시간이 단축됨.


좋은 예)

RUN apk add --update nginx && \

       rm -rf /var/cache/apk/* && \

       mkdir -p /tmp/nginx/


나쁜 예)

RUN apk add --update nginx

RUN rm -rf /var/cache/apk/*

RUN mkdir -p /tmp/nginx/



*ENDPOINT 와 CMD는 실행 인스트럭션임(기본적으로 1회만 실행)

=>이미지 생성 시 영향 없음


1) Shell :  ping $HOSTNAME

2) DockerFile : ["ping", "$HOSTNAME"]  

// ping은 shell명령어, $HOSTNAME은 파라미터

// 그래서 2)번은 실행이 안된다...아래와 같이 입력 필요

// ["/bin/bash", "-c", "ping"]

ENTRYPOINT["sh start.sh"]

CMD echo $HOSTNAME

=>CMD안에 값을 overwrite한다


*LABEL은 메타데이터 인스트럭션임

ex1) LABEL description="This is an example Dockerfile for NGINX."

=> Key = description

=> Value = "This is an example Dockerfile for NGINX."


ex2) LABEL key1="value1" \
               key2="value2" \
               key3="value3" 

*ARGS와 ENV : 

ARGS user=kim
ENV password=passwd

- ARGS는 runtime 순간에만 수행되고 없어진다.
- ENV는 docker run 시에도 변수를 사용할 수 있다(runtime 순간 이후에도 메모리에 변수가 떠있다)

*ARG 인스트럭션 :

FROM ubuntu
ARG user=hong
ARG version
1
2
[root@host01-2 overlay2]# docker build --build-arg user=somebodyElse
 
cs
=>user 파라미터가 runtime 빌드 시 덮어씌워진다 






*EXPOSE 인스트럭션

EXPOSE는 config(설정)과는 전혀 상관없다.

정보 제공을 위한 것!


EXPOSE 80

EXPOSE 443


Config에 들어가는 것이 아니라, 단순 Information을 위함

그럼 왜 쓰느냐?

web 서버가 80이 아닌 다른 포트(5500)으로 떴을 시 다른 프로세스가 통신할 때 조회가 필요할 수 있으니 정보를 입력해놓는다.


EXPOSE 80/UDP

EXPOSE 1000-2000

#port range 설정



*RUN 인스트럭션(Cache 문제)


아래 예시를 보자 : 


1) 

FROM reg.cloud.com/ubuntu

RUN apt-get update

RUN apt-get install -y mongodb-server


2) 10년 후

FROM reg.cloud.com/ubuntu

RUN apt-get update

RUN apt-get install -y mongodb-server

RUN apt-get install -y nodejs


(mongodb-server는 당연히 최신으로 받을것으로 예상되지만 10년 전 repo를 그대로 가져옴...cache 재활용의 문제...최신 것으로 받으려면 FROM절을 다르게 해야 함)

=> Docker Cache를 안 쓰게 할 수도 있지만...OFF를 해버리면 전체에서 Cache를 안 쓰는 것으로 판단한다. 오래걸림.


1
2
[root@host01-2 overlay2]# docker build --no-cacheColored by Color Scripter
cs



*USER 인스트럭션 : 

USER tomcat

이라고 하면 이 인스트럭션 이후부터는 root계정이 아닌 tomcat 계정으로 실행하게 된다


만약 계정이 없다면 RUN BUILD 시 생성해준다: 


1
2
RUN groupadd -r tomcat && useradd -r -g tomcat:tomcat 
cs


*HEALTHCHECK 인스트럭션


웹서버를 구성한다고 가정했을 때 컨테이너가 up 상태로 되어 있다 하더라도, ip:port로 request를 했을 때 웹페이지가 실행이 안되는 경우가 있다. 


이런 상황을 방지하기 위해 HEALTHCHECK를 활용하면 좋다 : 

1
2
3
HEALTHCHECK --interval=<interval> --timeout=<timeout> CMD <commadn>
 
HEALTHCHECK --interval=5m --imeout=2s --retries=3 CMD curl -f http://localhost/ || exit 1
cs





*SHELL 인스트럭션 ; 


기본 등록 Shell이 올라가도록 설정


SHELL ["executable", "parameters"]

 

ex)

SHELL ["/bin/sh", "-c"]





* DOCKERFILE - 권장사항


.dockerignore 이라는 파일이 존재한다. 이미지 생성 작업 중에 불필요한 파일을 로드하지 않도록 설정하는 파일임!


*DOCKERFILE로 이미지 생성하기

docker build -f <dockerfile의 파일이름과 경로, 생략시 dockerfile 사용> -t <생성될 이미지에 적용할 이름>


ex) docker build .

docker build -t hong/myapp

docker build -t hong/myapp:1.0.0 -t hong/myapp:latest


*DOCKERFILE 예시 : 

1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:latest
MAINTAINER HONG <hk@daum.net>
RUN apt-get update && apt-get install -y mysql-client mysql-server
ENV username mysqluser
ENV password pass
ENV database db1
ADD databasesetup.sh /
RUN chmod 644 /databasesetup.sh
RUN "/usr/bin/sh databasesetup.sh"
EXPOSE 3306
CMD ["/usr/bin/mysqld_safe"
cs


ENTRYPOINT는 Shell 파일(ex: temp.sh)

CMD는 해당 Shell 파일에 넘겨줄 Parameter를 넣는다

=> CMD["tmp1", "tmp2"] 인데

docker run 시 뒤에 parameter가 1개만 있을 경우

(ex: docker run --rm image:v1 aux)

실행 shell파일은 temp.sh aux가 된다...


*Linux Container(LXC) 와 Docker의 차이점


Docker = LXC + (Layers + Registry)

=>LXC에 MSA, Devops 방법론을 적용한 것!! (App Container)

=>LXC는 (OS Container)


*Registry 구성요소

Repository, Index


*Docker 환경

dockerd (net + volume + swarm ...)


containerd


runC


*Docker 컨테이너를 실행하면...


1
2
3
[root@host01-2 tmp]# docker run -it reg.cloud.com/centos
[root@a63244e84c0b /]#
 
cs


*Docker Container 위치

1
[root@host01-2 tmp]# ls /var/lib/docker/containers/
cs


*Docker Container 폴더 구조

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@host01-2 tmp]# cd /var/lib/docker/containers/
 
[root@host01-2 a63244e84c0b71b01311d52eda786b395aa77f6510c32e08046d3beb8b1da095]# ls -al
total 28
drwx------4 root root  237 May 23 09:36 .
drwx------5 root root  222 May 23 09:35 ..
-rw-r-----1 root root 1540 May 23 09:33 a63244e84c0b71b01311d52eda786b395aa77f6510c32e08046d3beb8b1da095-json.log
drwx------2 root root    6 May 23 09:32 checkpoints
-rw-------1 root root 2636 May 23 09:33 config.v2.json
-rw-r--r--1 root root 1153 May 23 09:33 hostconfig.json
-rw-r--r--1 root root   13 May 23 09:32 hostname
-rw-r--r--1 root root  174 May 23 09:32 hosts
drwx------3 root root   17 May 23 09:32 mounts
-rw-r--r--1 root root   69 May 23 09:32 resolv.conf
-rw-r--r--1 root root   71 May 23 09:32 resolv.conf.hash
 
cs

a63244e84c0b71b01311d52eda786b395aa77f6510c32e08046d3beb8b1da095 = 로그




hostname=Container가 뜰 때 hostname



*Docker 이미지 조회(이미지 식별 = 이미지이름@...)

1
2
3
4
5
6
[root@host01-2 /]# docker image ls --digests
REPOSITORY             TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
registry               2.5                 sha256:a3a4155bb8a3b32679c10451a55f9754f33b8620c1a8f316dfd913bb91ac746d   36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu   latest              <none>                                                                    20c44cd7596f        6 months ago        123MB
reg.cloud.com/centos   latest              sha256:224d7b12549c04bba833d4626a5c51113290e55bd754d39f72d1a437539b3c68   d123f4e55e12        6 months ago        197MB
 
cs


Docker 이미지 생성 시 - ID가 생성된다.

Repository 이름은 옵션임!


DIGEST 값이 없는것은 registry에서 만들어준다(registry에 등록되는 순간 digest가 만들어진다 = Commit + Push 필요!!).


*Docker History(이미지 생성이 어떻게 되었는지 확인 방법):

아래서 위로 순서대로 실행된다 

1
2
3
4
5
6
7
8
9
 
[root@host01-2 /]# docker history reg.cloud.com/ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
20c44cd7596f        6 months ago        /bin/sh -#(nop)  CMD ["/bin/bash"]            0B
<missing>           6 months ago        /bin/sh -c mkdir -/run/systemd && echo 'do…   7B
<missing>           6 months ago        /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   2.76kB
<missing>           6 months ago        /bin/sh -c rm -rf /var/lib/apt/lists/*          0B
<missing>           6 months ago        /bin/sh -set -xe   && echo '#!/bin/sh' > /…   745B
<missing>           6 months ago        /bin/sh -#(nop) ADD file:280a445783f309c90…   123MB
cs






*Docker 이미지는 최소한의 툴만 가지고 뜬다

1
2
3
4
5
6
7
[root@host01-2 /]# docker attach 07802d6d9bd9
[root@07802d6d9bd9 /]# ip addr
bash: ip: command not found
[root@07802d6d9bd9 /]# ifconfig
bash: ifconfig: command not found
[root@07802d6d9bd9 /]#
 
cs


ifconfig = net-tool

ip addr = ip router 

와 같은 툴이 필요하다


*Docker 이미지의 inspect 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
[root@host01-2 /]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
aa5929e423cc        reg.cloud.com/centos   "/bin/bash"         18 seconds ago      Up 17 seconds                           sad_kowalevski
[root@host01-2 /]# docker inspect aa5929e423cc
[
    {
        "Id""aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e",
        "Created""2018-05-23T00:53:41.018342623Z",
        "Path""/bin/bash",
        "Args": [],
        "State": {
            "Status""running",
            "Running"true,
            "Paused"false,
            "Restarting"false,
            "OOMKilled"false,
            "Dead"false,
            "Pid"8735,
            "ExitCode"0,
            "Error""",
            "StartedAt""2018-05-23T00:53:41.640998376Z",
            "FinishedAt""0001-01-01T00:00:00Z"
        },
        "Image""sha256:d123f4e55e1200156d9cbcf4421ff6d818576e4f1e29320a408c72f022cfd0b1",
        "ResolvConfPath""/var/lib/docker/containers/aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e/resolv.conf",
        "HostnamePath""/var/lib/docker/containers/aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e/hostname",
        "HostsPath""/var/lib/docker/containers/aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e/hosts",
        "LogPath""/var/lib/docker/containers/aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e/aa5929e423ccca7c4fe6f42559071f6a2b6b3ae4c8dfbac75c93eff06183384e-json.log",
        "Name""/sad_kowalevski",
        "RestartCount"0,
        "Driver""overlay2",
        "Platform""linux",
        "MountLabel""",
        "ProcessLabel""",
        "AppArmorProfile""",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile""",
            "LogConfig": {
                "Type""json-file",
                "Config": {}
            },
            "NetworkMode""default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name""no",
                "MaximumRetryCount"0
            },
            "AutoRemove"false,
            "VolumeDriver""",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode""shareable",
            "Cgroup""",
            "Links": null,
            "OomScoreAdj"0,
            "PidMode""",
            "Privileged"false,
            "PublishAllPorts"false,
            "ReadonlyRootfs"false,
            "SecurityOpt": null,
            "UTSMode""",
            "UsernsMode""",
            "ShmSize"67108864,
            "Runtime""runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation""",
            "CpuShares"0,
            "Memory"0,
            "NanoCpus"0,
            "CgroupParent""",
            "BlkioWeight"0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod"0,
            "CpuQuota"0,
            "CpuRealtimePeriod"0,
            "CpuRealtimeRuntime"0,
            "CpusetCpus""",
            "CpusetMems""",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota"0,
            "KernelMemory"0,
            "MemoryReservation"0,
            "MemorySwap"0,
            "MemorySwappiness": null,
            "OomKillDisable"false,
            "PidsLimit"0,
            "Ulimits": null,
            "CpuCount"0,
            "CpuPercent"0,
            "IOMaximumIOps"0,
            "IOMaximumBandwidth"0
        },
        "GraphDriver": {
            "Data": {
                "LowerDir""/var/lib/docker/overlay2/c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5-init/diff:/var/lib/docker/overlay2/5654bedcae9b25c616faff203d638b355157830d5431e7fee474feea8d461338/diff",
                "MergedDir""/var/lib/docker/overlay2/c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5/merged",
                "UpperDir""/var/lib/docker/overlay2/c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5/diff",
                "WorkDir""/var/lib/docker/overlay2/c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5/work"
            },
            "Name""overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname""aa5929e423cc",
            "Domainname""",
            "User""",
            "AttachStdin"true,
            "AttachStdout"true,
            "AttachStderr"true,
            "Tty"true,
            "OpenStdin"true,
            "StdinOnce"true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped"true,
            "Image""reg.cloud.com/centos",
            "Volumes": null,
            "WorkingDir""",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date""20170911",
                "license""GPLv2",
                "name""CentOS Base Image",
                "vendor""CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge""",
            "SandboxID""678fe422029b37a8872fc582881d83b6d3f0c10fdfd28c071458de1622be668c",
            "HairpinMode"false,
            "LinkLocalIPv6Address""",
            "LinkLocalIPv6PrefixLen"0,
            "Ports": {},
            "SandboxKey""/var/run/docker/netns/678fe422029b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID""71304bd92f81fc9cc2dc54b529533491949f89e0aeaa63a8f524ab7bb73454d4",
            "Gateway""172.17.0.1",
            "GlobalIPv6Address""",
            "GlobalIPv6PrefixLen"0,
            "IPAddress""172.17.0.2",
            "IPPrefixLen"16,
            "IPv6Gateway""",
            "MacAddress""02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID""76d13a20d92c8d68860705f57e8989055660ac78cafea4eaa033cb1fd856e6fa",
                    "EndpointID""71304bd92f81fc9cc2dc54b529533491949f89e0aeaa63a8f524ab7bb73454d4",
                    "Gateway""172.17.0.1",
                    "IPAddress""172.17.0.2",
                    "IPPrefixLen"16,
                    "IPv6Gateway""",
                    "GlobalIPv6Address""",
                    "GlobalIPv6PrefixLen"0,
                    "MacAddress""02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
 
cs
 "NetworkID""76d13a20d92c8d68860705f57e8989055660ac78cafea4eaa033cb1fd856e6fa",
"EndpointID""71304bd92f81fc9cc2dc54b529533491949f89e0aeaa63a8f524ab7bb73454d4",

=> 종료되지 않은 Container는 위처럼 네트워크가 살아있다


*Docker 이미지란 :
- 읽기 전용 템플릿으로 컨테이너 인스턴스를 저장한 파일

- overlay라는 레이어 파일 시스템을 사용

- 호스트 사이에서 이동성을 제공

- Dockerfile로 자신만의 이미지 생성


1) Docker 이미지 조회 :

1
2
3
4
5
6
[root@host01-2 docker]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
registry               2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu   latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/centos   latest              d123f4e55e12        6 months ago        197MB
 
cs


2) Docker 레이어 파일 시스템 확인 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@host01-2 docker]# cd overlay2/
[root@host01-2 overlay2]# ls
14f7fd95b0bc618853bb4aba43a95e9d61e3ea1cbb45d0cf4b7752729995623c       923e521a724c0099b1a35f27383bb4ca484bb90b18a503b2ed468d084fa6dfe8
14f7fd95b0bc618853bb4aba43a95e9d61e3ea1cbb45d0cf4b7752729995623c-init  ac0c9b3f9d8664c6df53bf4e863a78d107a5c054e5a9a277d32e31a0a1b8977b
336d5fadb8f0ac24b795420979393e4e1d54de4ad75ce3255eac00ede7349012       ba37afeb0b3b6b67fb8e9fdd1aaa75f41caff6b23f3b6bc8e76568e307ec6c8b
34dd34b11d511e3309acd2c978aba79f4ea5d218ab08289cff5bee1ef2362977       backingFsBlockDev
34dd34b11d511e3309acd2c978aba79f4ea5d218ab08289cff5bee1ef2362977-init  c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5
40b303daad22bc5c74373f2aab144a0109d680307364a4815c01d724be20eab5       c1f9c94d4aedf4d4f41347837982600e70c211e9956521186a7938397f166ad5-init
40b303daad22bc5c74373f2aab144a0109d680307364a4815c01d724be20eab5-init  c228ec2be618735dac211a9b503c2aaea4e4a82943bfd27e0fad1239e83f610b
4ed4d836bf9086e79708240589b09e2a6a44e4e155c3ef06e617e84df7bbe88a       ca77f99e41ba3db530bc57b1b66655a5a02e95120a27b6f609f4537cff456000
5654bedcae9b25c616faff203d638b355157830d5431e7fee474feea8d461338       fd2f482cf2042074824ab14c60d38a98dcaaf03acd41af9b3466f9d0dd9b87fa
7e51f10615f7bccd4c0a5bdfd9c5344d9cff32d78e72781fc325fef061fefeb6       l
90c35895a6516132fe8fcd791bfaa9fc42f9b131bb5ef1bf94cb45fab730e0e9
[root@host01-2 overlay2]# pwd
/var/lib/docker/overlay2
 
cs

/var/lib/docker/overlay2 Path에 있는 레이어들을 합쳐서 단일 레이어로 만들어낸다.


*Container Lifecycle

Container 실행방법은 2가지


docker run

docker start


이 둘의 공통점은 container를 실행상태로 만드는것

그리고 새로운 Container로 실행되는것

Shutdown된 컨테이너를 다시 실행도 가능하다!


run을 하면 실행이된다

run => start => stop

run => start => kill


컨테이너 목록을 보려면 ps 명령어를 사용한다 : 

1
2
3
4
[root@host01-2 overlay2]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
aa5929e423cc        reg.cloud.com/centos   "/bin/bash"         35 minutes ago      Up 35 minutes                           sad_kowalevski
 
cs


Tag 형태로 이미지 버전 가져오기

1
2
3
4
5
6
7
8
9
10
11
12
[root@host01-2 overlay2]# docker run ubuntu:14.04
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
324d088ce065: Pull complete
2ab951b6c615: Pull complete
9b01635313e2: Pull complete
04510b914a6c: Pull complete
83ab617df7b4: Pull complete
Digest: sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Status: Downloaded newer image for ubuntu:14.04
[root@host01-2 overlay2]# ls
 
cs


*Docker Shell 로 실행하기( -it  : interactive 옵션)

1
2
3
[root@host01-2 overlay2]# docker run -it reg.cloud.com/busybox sh
/ #
 
cs


*모든 컨테이너 삭제 명령어

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@host01-2 overlay2]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND                 CREATED              STATUS                         PORTS               NAMES
11259166b002        reg.cloud.com/busybox   "sh"                    About a minute ago   Exited (1303 seconds ago                         inspiring_sammet
bb3c126f13d0        reg.cloud.com/busybox   "bash"                  About a minute ago   Created                                            hardcore_lumiere
09c4e0da958a        ubuntu:14.04            "/bin/bash"             4 minutes ago        Exited (04 minutes ago                           jolly_wright
18355d103192        ubuntu:latest           "/bin/bash"             11 minutes ago       Exited (1275 minutes ago                         determined_kalam
aa5929e423cc        reg.cloud.com/centos    "/bin/bash"             41 minutes ago       Up 41 minutes                                      sad_kowalevski
07802d6d9bd9        reg.cloud.com/centos    "/bin/bash"             About an hour ago    Exited (13042 minutes ago                        amazing_curran
a63244e84c0b        reg.cloud.com/centos    "/bin/bash"             About an hour ago    Exited (0) About an hour ago                       sharp_mclean
3e5c8d877885        reg.cloud.com/centos    "cat /etc/os-release"   About an hour ago    Exited (0) About an hour ago                       laughing_carson
[root@host01-2 overlay2]# docker rm -f $(docker ps -aq)
11259166b002
bb3c126f13d0
09c4e0da958a
18355d103192
aa5929e423cc
07802d6d9bd9
a63244e84c0b
3e5c8d877885
 
cs


*이름과 함께 띄우기(docker run -it --name hong  reg.cloud.com/busybox sh)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@host01-2 overlay2]# docker run -it --name hong  reg.cloud.com/busybox sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    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
40: eth0@if41: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search cloud.com
nameserver 10.10.12.1
#호스트의 DNS 네임스페이스 값을 가져온다
cs





*Container Commit 하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/ # exit
[root@host01-2 overlay2]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED              STATUS                       PORTS               NAMES
5bba4cbd910c        reg.cloud.com/busybox   "sh"                About a minute ago   Exited (1304 seconds ago                       hong
[root@host01-2 overlay2]# docker commit hong hongImages
invalid reference format: repository name must be lowercase
[root@host01-2 overlay2]# docker commit hong hongimages
sha256:f6d8cd4a7d7123996b50966b76c3aae5ad61e294fe0bae6bad451d52f241e27b
[root@host01-2 overlay2]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
hongimages              latest              f6d8cd4a7d71        6 seconds ago       1.13MB
ubuntu                  14.04               8cef1fa16c77        3 weeks ago         223MB
ubuntu                  latest              452a96d81c30        3 weeks ago         79.6MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu    latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/busybox   latest              6ad733544a63        6 months ago        1.13MB
reg.cloud.com/centos    latest              d123f4e55e12        6 months ago        197MB
[root@host01-2 overlay2]#
 
cs


*Commit 후 싱글 레이어로 뽑아내기(Export -> Import)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@host01-2 ~]# docker run -it --name c3 reg.cloud.com/ubuntu bash
root@9274dde20cf2:/# mkdir /test
root@9274dde20cf2:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test  tmp  usr  var
root@9274dde20cf2:/# cd /test
root@9274dde20cf2:/test# ls
root@9274dde20cf2:/test# exit
exit
[root@host01-2 ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                       PORTS               NAMES
9274dde20cf2        reg.cloud.com/ubuntu    "bash"              32 seconds ago      Exited (03 seconds ago                         c3
e1d6ad627dcc        reg.cloud.com/busybox   "ls /home"          4 minutes ago       Exited (04 minutes ago                         wizardly_engelbart
515222fedcc3        reg.cloud.com/busybox   "ls /home"          4 minutes ago       Exited (04 minutes ago                         affectionate_archimedes
5bba4cbd910c        reg.cloud.com/busybox   "sh"                10 minutes ago      Exited (1308 minutes ago                       hong
[root@host01-2 ~]# docker export c3 > /tmp/ubuntu2.tar
[root@host01-2 ~]# cd /tmp/
[root@host01-2 tmp]# ls
20c44cd7596ff4807aef84273c99588d22749e2a7e15a7545ac96347baa65eda.json  791a7c2dac840f5430500629700bfe3ba0cdb38a824c4623a96b8f95c94178d0  manifest.json  ubuntu.tar
461feffa712154ef19f136c6eba8ede98241426c8cb3633f2c1da0d4e94770b3       b87aa680bd7b8e8cb13b55aa4024f1e0a08f8f81d1315188ac73685b5a295de4  repositories
4df5c9fd64c6dd1f92a0558541b73a813b77da868b19d58cb94cae42ba32c6a8       daee585a59213f889a01c1441466e0f0aeff76d6fc4d80e166145db3e779a3a5  ubuntu2.tar
[root@host01-2 tmp]# docker import /tmp/ubuntu2.tar hongtest
sha256:9a8341b96270a410180c6d176b63f9cf7f561b0f9bf1be5483355cbad66a9871
[root@host01-2 tmp]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
hongtest                latest              9a8341b96270        7 seconds ago       98.4MB
<none>                  <none>              ab0f85b7f2e6        4 minutes ago       1.13MB
<none>                  <none>              9579d5ccef21        5 minutes ago       1.13MB
hongimages              latest              f6d8cd4a7d71        9 minutes ago       1.13MB
ubuntu                  14.04               8cef1fa16c77        3 weeks ago         223MB
ubuntu                  latest              452a96d81c30        3 weeks ago         79.6MB
registry                2.5                 36e3b1f8d3f1        4 months ago        37.8MB
reg.cloud.com/ubuntu    latest              20c44cd7596f        6 months ago        123MB
reg.cloud.com/busybox   latest              6ad733544a63        6 months ago        1.13MB
reg.cloud.com/centos    latest              d123f4e55e12        6 months ago        197MB
[root@host01-2 tmp]# docker history
"docker history" requires exactly 1 argument.
See 'docker history --help'.
 
Usage:  docker history [OPTIONS] IMAGE [flags]
 
Show the history of an image
[root@host01-2 tmp]# docker history hongtest
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
9a8341b96270        49 seconds ago                          98.4MB              Imported from -
[root@host01-2 tmp]# docker run -it hongtest bash
root@4cbd222f2ce3:/# ls /test/
root@4cbd222f2ce3:/#
 
cs




*PID와 UTS 네임스페이스 공유하기



- 네임스페이스 공유 (컨테이너끼리 공유할 수 있다)

Why?=>컨테이너 안에 보안통제 어플리케이션을 띄운다고 가정했을 때 HOST의 프로세스를 모니터링/확인해야 하기 때문에 필요!


1) HOST와 네임스페이스 PID공유 방법 : 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
[root@host01-2 tmp]# docker run -it reg.cloud.com/ubuntu bash
root@5c47432901b6:/# ps -ef
  PID TTY          TIME CMD
 2681 pts/0    00:00:00 bash
10149 pts/0    00:00:00 bash
10182 pts/0    00:00:00 ps
 
#프로세스가 몇 개 안 
 
root@a36e01acc480:/# exit
exit
 
[root@host01-2 tmp]# docker run -it --pid=host reg.cloud.com/ubuntu bash
root@5c47432901b6:/# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 May20 ?        00:00:04 /usr/lib/systemd/systemd --system --deserialize 15
root         2     0  0 May20 ?        00:00:00 [kthreadd]
root         3     2  0 May20 ?        00:00:00 [ksoftirqd/0]
root         5     2  0 May20 ?        00:00:00 [kworker/0:0H]
root         7     2  0 May20 ?        00:00:00 [migration/0]
root         8     2  0 May20 ?        00:00:00 [rcu_bh]
root         9     2  0 May20 ?        00:00:01 [rcu_sched]
root        10     2  0 May20 ?        00:00:00 [watchdog/0]
root        11     2  0 May20 ?        00:00:00 [watchdog/1]
root        12     2  0 May20 ?        00:00:00 [migration/1]
root        13     2  0 May20 ?        00:00:00 [ksoftirqd/1]
root        15     2  0 May20 ?        00:00:00 [kworker/1:0H]
root        17     2  0 May20 ?        00:00:00 [kdevtmpfs]
root        18     2  0 May20 ?        00:00:00 [netns]
root        19     2  0 May20 ?        00:00:00 [khungtaskd]
root        20     2  0 May20 ?        00:00:00 [writeback]
root        21     2  0 May20 ?        00:00:00 [kintegrityd]
root        22     2  0 May20 ?        00:00:00 [bioset]
root        23     2  0 May20 ?        00:00:00 [kblockd]
root        24     2  0 May20 ?        00:00:00 [md]
root        31     2  0 May20 ?        00:00:00 [kswapd0]
root        32     2  0 May20 ?        00:00:00 [ksmd]
root        33     2  0 May20 ?        00:00:00 [khugepaged]
root        34     2  0 May20 ?        00:00:00 [crypto]
root        42     2  0 May20 ?        00:00:00 [kthrotld]
root        44     2  0 May20 ?        00:00:00 [kmpath_rdacd]
root        45     2  0 May20 ?        00:00:00 [kpsmoused]
root        47     2  0 May20 ?        00:00:00 [ipv6_addrconf]
root        66     2  0 May20 ?        00:00:00 [deferwq]
root        98     2  0 May20 ?        00:00:00 [kauditd]
root       281     2  0 May20 ?        00:00:00 [scsi_eh_0]
root       282     2  0 May20 ?        00:00:00 [scsi_tmf_0]
root       283     2  0 May20 ?        00:00:00 [ata_sff]
root       284     2  0 May20 ?        00:00:00 [vmw_pvscsi_wq_0]
root       286     2  0 May20 ?        00:00:00 [scsi_eh_1]
root       288     2  0 May20 ?        00:00:00 [scsi_tmf_1]
root       289     2  0 May20 ?        00:00:00 [scsi_eh_2]
root       290     2  0 May20 ?        00:00:00 [scsi_eh_3]
root       291     2  0 May20 ?        00:00:00 [scsi_tmf_2]
root       292     2  0 May20 ?        00:00:00 [scsi_tmf_3]
root       293     2  0 May20 ?        00:00:00 [scsi_eh_4]
root       294     2  0 May20 ?        00:00:00 [scsi_tmf_4]
root       295     2  0 May20 ?        00:00:00 [scsi_eh_5]
root       296     2  0 May20 ?        00:00:00 [scsi_tmf_5]
root       297     2  0 May20 ?        00:00:00 [scsi_eh_6]
root       298     2  0 May20 ?        00:00:00 [scsi_tmf_6]
root       299     2  0 May20 ?        00:00:00 [scsi_eh_7]
root       300     2  0 May20 ?        00:00:00 [scsi_tmf_7]
root       301     2  0 May20 ?        00:00:00 [scsi_eh_8]
root       302     2  0 May20 ?        00:00:00 [scsi_tmf_8]
root       303     2  0 May20 ?        00:00:00 [scsi_eh_9]
root       304     2  0 May20 ?        00:00:00 [scsi_tmf_9]
root       305     2  0 May20 ?        00:00:00 [scsi_eh_10]
root       306     2  0 May20 ?        00:00:00 [scsi_tmf_10]
root       307     2  0 May20 ?        00:00:00 [scsi_eh_11]
root       308     2  0 May20 ?        00:00:00 [scsi_tmf_11]
root       309     2  0 May20 ?        00:00:00 [scsi_eh_12]
root       310     2  0 May20 ?        00:00:00 [scsi_tmf_12]
root       311     2  0 May20 ?        00:00:00 [scsi_eh_13]
root       312     2  0 May20 ?        00:00:00 [scsi_tmf_13]
root       313     2  0 May20 ?        00:00:00 [scsi_eh_14]
root       314     2  0 May20 ?        00:00:00 [scsi_tmf_14]
root       315     2  0 May20 ?        00:00:00 [scsi_eh_15]
root       316     2  0 May20 ?        00:00:00 [scsi_tmf_15]
root       317     2  0 May20 ?        00:00:00 [scsi_eh_16]
root       318     2  0 May20 ?        00:00:00 [scsi_tmf_16]
root       319     2  0 May20 ?        00:00:00 [scsi_eh_17]
root       320     2  0 May20 ?        00:00:00 [scsi_tmf_17]
root       321     2  0 May20 ?        00:00:00 [scsi_eh_18]
root       322     2  0 May20 ?        00:00:00 [scsi_tmf_18]
root       323     2  0 May20 ?        00:00:00 [scsi_eh_19]
root       324     2  0 May20 ?        00:00:00 [scsi_tmf_19]
root       325     2  0 May20 ?        00:00:00 [scsi_eh_20]
root       326     2  0 May20 ?        00:00:00 [scsi_tmf_20]
root       327     2  0 May20 ?        00:00:00 [scsi_eh_21]
root       328     2  0 May20 ?        00:00:00 [scsi_tmf_21]
root       329     2  0 May20 ?        00:00:00 [scsi_eh_22]
root       330     2  0 May20 ?        00:00:00 [scsi_tmf_22]
root       331     2  0 May20 ?        00:00:00 [scsi_eh_23]
root       332     2  0 May20 ?        00:00:00 [scsi_tmf_23]
root       333     2  0 May20 ?        00:00:00 [scsi_eh_24]
root       335     2  0 May20 ?        00:00:00 [scsi_eh_25]
root       336     2  0 May20 ?        00:00:00 [scsi_tmf_24]
root       337     2  0 May20 ?        00:00:00 [scsi_eh_26]
root       338     2  0 May20 ?        00:00:00 [scsi_tmf_26]
root       339     2  0 May20 ?        00:00:00 [scsi_eh_27]
root       340     2  0 May20 ?        00:00:00 [scsi_tmf_27]
root       341     2  0 May20 ?        00:00:00 [scsi_eh_28]
root       342     2  0 May20 ?        00:00:00 [scsi_tmf_28]
root       343     2  0 May20 ?        00:00:00 [scsi_eh_29]
root       344     2  0 May20 ?        00:00:00 [scsi_tmf_29]
root       345     2  0 May20 ?        00:00:00 [scsi_eh_30]
root       346     2  0 May20 ?        00:00:00 [scsi_tmf_30]
root       347     2  0 May20 ?        00:00:00 [scsi_eh_31]
root       348     2  0 May20 ?        00:00:00 [scsi_tmf_31]
root       349     2  0 May20 ?        00:00:00 [scsi_eh_32]
root       350     2  0 May20 ?        00:00:00 [scsi_tmf_32]
root       378     2  0 May20 ?        00:00:00 [kworker/u4:30]
root       379     2  0 May20 ?        00:00:39 [kworker/u4:31]
root       380     2  0 May20 ?        00:00:00 [scsi_tmf_25]
root       396     2  0 May20 ?        00:00:00 [ttm_swap]
root       466     2  0 May20 ?        00:00:00 [kdmflush]
root       467     2  0 May20 ?        00:00:00 [bioset]
root       478     2  0 May20 ?        00:00:00 [kdmflush]
root       479     2  0 May20 ?        00:00:00 [bioset]
root       492     2  0 May20 ?        00:00:00 [bioset]
root       493     2  0 May20 ?        00:00:00 [xfsalloc]
root       494     2  0 May20 ?        00:00:00 [xfs_mru_cache]
root       495     2  0 May20 ?        00:00:00 [xfs-buf/dm-0]
root       496     2  0 May20 ?        00:00:00 [xfs-data/dm-0]
root       497     2  0 May20 ?        00:00:00 [xfs-conv/dm-0]
root       498     2  0 May20 ?        00:00:00 [xfs-cil/dm-0]
root       499     2  0 May20 ?        00:00:00 [xfs-reclaim/dm-]
root       500     2  0 May20 ?        00:00:00 [xfs-log/dm-0]
root       501     2  0 May20 ?        00:00:00 [xfs-eofblocks/d]
root       502     2  0 May20 ?        00:00:12 [xfsaild/dm-0]
root       503     2  0 May20 ?        00:00:00 [kworker/0:1H]
root       572     1  0 May20 ?        00:00:01 /usr/lib/systemd/systemd-journald
root       592     1  0 May20 ?        00:00:00 /usr/sbin/lvmetad -f
root       601     1  0 May20 ?        00:00:00 /usr/lib/systemd/systemd-udevd
root       680     2  0 May20 ?        00:00:00 [xfs-buf/sda1]
root       681     2  0 May20 ?        00:00:00 [xfs-data/sda1]
root       682     2  0 May20 ?        00:00:00 [xfs-conv/sda1]
root       683     2  0 May20 ?        00:00:00 [xfs-cil/sda1]
root       684     2  0 May20 ?        00:00:00 [xfs-reclaim/sda]
root       685     2  0 May20 ?        00:00:00 [xfs-log/sda1]
root       686     2  0 May20 ?        00:00:00 [xfs-eofblocks/s]
root       687     2  0 May20 ?        00:00:00 [xfsaild/sda1]
root       716     2  0 May20 ?        00:00:01 [kworker/1:1H]
81         736     1  0 May20 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
999        738     1  0 May20 ?        00:00:00 /usr/lib/polkit-1/polkitd --no-debug
root       739     1  0 May20 ?        00:00:08 /usr/sbin/irqbalance --foreground
root       742     1  0 May20 ?        00:00:09 /usr/sbin/rsyslogd -n
root       743     1  0 May20 ?        00:00:00 /usr/lib/systemd/systemd-logind
root       761     1  0 May20 ?        00:00:00 /sbin/agetty --noclear tty1 linux
root       772     1  0 May20 ?        00:00:01 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root       773     1  0 May20 ?        00:00:04 /usr/sbin/NetworkManager --no-daemon
root      1065     1  0 May20 ?        00:00:00 /usr/sbin/sshd -D
root      1066     1  0 May20 ?        00:00:25 /usr/bin/python -Es /usr/sbin/tuned --P
root      1153     1  0 May20 ?        00:00:01 /usr/libexec/postfix/master -w
89        1167  1153  0 May20 ?        00:00:00 qmgr --t unix -u
root      2675  1065  0 May21 ?        00:00:01 sshd: root@pts/0
root      2681  2675  0 May21 pts/0    00:00:00 -bash
root      2894     1  0 May21 ?        00:00:00 /usr/sbin/crond -n
root      3603     1  0 May21 ?        00:00:00 /sbin/auditd
root      3912     1  0 May21 ?        00:03:55 /usr/bin/dockerd
root      3917  3912  0 May21 ?        00:02:39 docker-containerd --config /var/run/docker/containerd/containerd.toml
89        8177  1153  0 00:27 ?        00:00:00 pickup --t unix -u
root      8946     2  0 01:28 ?        00:00:00 [kworker/0:2]
root      9186     2  0 01:33 ?        00:00:00 [kworker/0:3]
root      9421     2  0 01:38 ?        00:00:00 [kworker/1:0]
root      9668  1065  0 01:43 ?        00:00:00 sshd: root@pts/1
root      9672  9668  0 01:43 ?        00:00:00 -bash
root      9878     2  0 01:47 ?        00:00:00 [kworker/1:1]
root      9998     2  0 01:52 ?        00:00:00 [kworker/1:2]
root     10029     2  0 01:54 ?        00:00:00 [kworker/0:0]
root     10121  9672  2 01:56 ?        00:00:00 docker run -it --pid=host reg.cloud.com/ubuntu bash
root     10133  3917  0 01:56 ?        00:00:00 docker-containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/5
root     10149 10133  0 01:56 pts/0    00:00:00 bash
root     10183 10149  0 01:56 pts/0    00:00:00 ps -ef
 
cs



2) Container와 HOST 간  hostname을(UTS) 공유할 수 있다: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@host01-2 tmp]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                        PORTS               NAMES
ab0611fb1cc8        reg.cloud.com/ubuntu    "bash"              5 minutes ago       Exited (023 seconds ago                         sharp_benz
5c47432901b6        reg.cloud.com/ubuntu    "bash"              10 minutes ago      Exited (1306 minutes ago                        zealous_beaver
a36e01acc480        reg.cloud.com/ubuntu    "bash"              13 minutes ago      Exited (011 minutes ago                         wonderful_gates
4cbd222f2ce3        hongtest                "bash"              15 minutes ago      Exited (12713 minutes ago                       romantic_chandrasekhar
9274dde20cf2        reg.cloud.com/ubuntu    "bash"              21 minutes ago      Exited (020 minutes ago                         c3
e1d6ad627dcc        reg.cloud.com/busybox   "ls /home"          25 minutes ago      Exited (025 minutes ago                         wizardly_engelbart
515222fedcc3        reg.cloud.com/busybox   "ls /home"          25 minutes ago      Exited (025 minutes ago                         affectionate_archimedes
5bba4cbd910c        reg.cloud.com/busybox   "sh"                30 minutes ago      Exited (13028 minutes ago                       hong
 
#--uts 옵션을 통해 host와 hostname을 공유하게 
[root@host01-2 tmp]# docker run -it --uts=host reg.cloud.com/ubuntu
root@host01-2:/# hostname
host01-2.cloud.com
root@host01-2:/# ^C
root@host01-2:/# ^C
root@host01-2:/# exit
exit
[root@host01-2 tmp]# hostname
host01-2.cloud.com
[root@host01-2 tmp]#
 
cs


* Container IP 확인하기(inspect를 통해 얻은 json 포맷의 object에서 필요한 값을 2중 대괄호안에 입력 ) :

1
2
3
[root@host01-2 tmp]# docker inspect -f '{{.NetworkSettings.IPAddress}}' ab0611fb1cc8
172.17.0.2
 
cs


* Container PID 확인하기 :

1
2
3
4
5
6
[root@host01-2 tmp]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED              STATUS              PORTS               NAMES
ab0611fb1cc8        reg.cloud.com/ubuntu   "bash"              About a minute ago   Up 58 seconds                           sharp_benz
[root@host01-2 tmp]# docker inspect -f '{{.State.Pid}}' ab0611fb1cc8
10301
 
cs


1. docker 서비스를 시작하고 시스템 부팅 시에 시작하도록 구성
systemctl enable docker
systemctl start docker
2. docker 호스트 인스턴스에 대한 버전 정보
docker version
3. info서브 명령을 사용하여 현재 구성 정보 살펴보기
docker info
4. dockerd가 시작할 때 생성한 디렉터리 계층구조를 살펴보기
ls -R /var/lib/docker
5. docker가 설치되어 있는 리눅스 환경 특징:
ip a
#명령어 실행 시 docker0로 사설 ip가 할당됨
#외부로 나갈 수는 있지만 패킷이 다시 돌아오지 못함
/var/lib/docker라는 폴더가 있는지 여부(있으면 docker가 설치되어 있는거임)

 


IP1       IP2       IP3

con1    con2    con3


docker 172.17.0.1/16



192.168.10.81



사설 IP가 할당되기 때문에 Container에서 나갈 때는 나갈 수 있지만

다시 돌아올 때는 패킷을 받을 수 없다(IP를 변경해준다)

Source NAT를 사용한다

컨테이너가 나갈 때만 역할 IP...Source IP를 변경해준다

- (Linux Bridge)L2, DHCP, 그리고 나갈 때만 SNAT 역할로 내보내준다.

 




1. docker 서비스를 시작하고 시스템 부팅 시에 시작하도록 구성
systemctl enable docker
systemctl start docker
2. docker 호스트 인스턴스에 대한 버전 정보
docker version
3. info서브 명령을 사용하여 현재 구성 정보 살펴보기
docker info
4. dockerd가 시작할 때 생성한 디렉터리 계층구조를 살펴보기
ls -R /var/lib/docker
5. docker가 설치되어 있는 리눅스 환경 특징:
ip a
#명령어 실행 시 docker0로 사설 ip가 할당됨
#외부로 나갈 수는 있지만 패킷이 다시 돌아오지 못함
/var/lib/docker라는 폴더가 있는지 여부(있으면 docker가 설치되어 있는거임)
6. /etc/docker/daemon.json 파일 생성하면 warning을 제거할 수 있다(SSL접속)
(
"insecure-registries" :["10.10.12.0/24"]
)
7. docker 재시작
systemctl restart docker
8. ubuntu 이미지 생성(이미지 저장소 레지스토리 - 일종의 파일서버에서 가져오기)
docker pull reg.cloud.com/ubuntu
docker images
로 이미지 확인
docker run -it reg.cloud.com/ubuntu
로 이미지 생성
9. apt명령어 실행으로 Ubuntu인지 여부 확인
apt
cat /etc/os-release
로 release 정보 확인


*레지스토리(registry) : 이미지를 저장하는 원격 저장소

docker에서 공식적으로 제공하는 저장소가 존재한다 :


docker hub : https://hub.docker.com/explore/


1
2
3
1. docker 저장소에서 이미지 가져오기(여기서는 busybox)
 
docker pull busybox
cs


*Repository : 여러 docker 이미지들의 콜렉션 네임스페이스


public과 private으로 구분된다.


- 버전을 public repository에서 가져오기 

#ubuntu:16.04


- 버전 입력안 할 시에는 자동으로 latest를 가져온다

#ubuntu


- docker images 명령어로 image tag를 확인 할 수 있다.


접근 repository가 private일 경우 로그인이 필요 : 


1
2
3
4
5
6
1. docker hub 로그인
docker login
docker logout
 
2. 로그인 정보 확인
cat ~/.docker/config.json
cs





*Docker Pull 받기 :

1
2
3
4
5
6
7
8
1. pull 받기
docker pull {registryAddress}/{namespace}/{repositoryName}:{tag}
docker pull {이미지}
 
#사내망에 저장 예시
docker pull {계정 or URL}/{이미지}
docker pull reg.cloud.com/busybox
 
cs


*Docker 이미지/프로세스 지우기 : 

1
2
3
4
5
6
7
8
9
1. docker process 및 이미지 지우기
docker rm -f {프로세스 명}
docker ps -
#로 조회
docker rm -f 0e60b5e39d5c
 
#docker 이미지 지우기
docker rmi <저장소 이름>/<이미지 이름, ID>:<태그>
 
cs


*이미지 수정(Commit & Push)

뒤에서 따로 설명하겠지만, Docker에서 Commit 및 Push를 할 경우,

이미지에서는 Layer를 (revision별?) 서로 공유 하며 Layer는 1개만 가지고 있다. 전체 이미지가 아니라 수정된 부분만 업로드 된 상태로 남는다. 

=> 이미지 전체를 또 받을 필요가 없다...빠르다...분산환경

(버전 컨트롤이 가능!!)


*이미지 레이어 구조 확인 :


1
2
3
4
5
1. 이미지 레이어 확인하기
ls /var/lib/docker/overlay2/ -l
#여러개가 출력되는데 이것을 조립해서 하나의 이미지로 보여준다.
ls -/var/lib/docker/overlay2/{이미지레이어 ID}
#diff link lower 
cs





*Docker 포트 매핑 및 실행: 


1
2
3
4
5
6
7
- 포트 매핑
 
docker run -d(백그라운드 실행) -p {HOST PORT}:{Container PORT} --restart=always --name registry registry:2.5
docker run --5000:5000 --restart=always --name registry registry:2.5
 
 
docker run --5000:5000 --restart=always --name hkregistry registry:2.5
cs


*Docker image명 변경 :


1
2
3
4
5
docker tag reg.cloud.com/ubuntu localhost/ubuntu
 
#Push 한 후에 localhost/ubuntu를 다운받을 수 
docker push localhost/ubuntu
docker pull localhost/ubuntu
cs


*Docker image tar파일로 백업/삭제/업로드

1
2
3
4
5
6
7
8
9
10
*Docker image를 아웃풋 tar 파일로 백업 받기
docker save -/tmp/ubuntu.tar reg.cloud.com/ubuntu
 
*기존 Docker 이미지 삭제
docker ps -a
docker rm -r {Container ID}
docker rmi {image name}
 
*백업받은 tar 이미지 파일 다시 업로드
docker load -i ubuntu.tar
cs


Container에 들어가서 ps -ef로 조회해보면

PID 1번은 컨테이너에 이미지를 띄워주는 프로세스임~

컨테이너 run 시 이미지에 내장된 명령어가 자동으로 실행됨!


*Docker Process 관리 


1
2
3
4
5
6
7
8
9
10
11
*Docker 컨테이너 프로세스 실행
docker container run {image} cat /etc/os-release 
 
*Docker 컨테이너 프로세스 
[root@host01-2 tmp]# docker ps
CONTAINER ID        IMAGE                  COMMAND             CREATED              STATUS              PORTS               NAMES
c02b501f1151        reg.cloud.com/ubuntu   "/bin/bash"         About a minute ago   Up About a minute                       compassionate_euler
 
- Ctrl+p+q 를 누르면 bash shell을 프로세스를 죽이지 않은 상태에서 빠져나올 수 있음
- 다시 bash shell 프로세스로 돌아갈려면  docker attach {container ID} 
 
cs

*VM과 컨테이너 차이

- VM: H/W 가상화/추상화

- 컨테이너: OS 가상화/추상화


*컨테이너의 특징:
- 호스트와 어플리케이션을 분리 : OS 레벨 가상화

- 적은 시스템 리소스와 디스크 공간 소비

- 리소스 사용에 유연성과 효율성 제공

- 가상머신과 달리 개별적인 커널을 갖지 않음


- 네트워크 가상화 => NV(VxLAN), SDN... 

- 스토리지 가상화 => Object File System

OS 가상화(Container) = 네트워크 가상화 + 스토리지 가상화


*컨테이너 기반 기술

- Cgroup 리소스에 대한 제한과 우선순위를 제어

- 네임스페이스: 어플리케이션에 대한 격리 환경을 제공


*H/W, VM는 라이프사이클이 긴 편...


*컨테이너 : 가장 짧은 라이프사이클

컨테이너는 쓰고 바로 지우고 반복(배포가 수 초 단위...속도가 빠름)

DB Data는 지워지면 안되니까...분리해서 사용하되, Stateless 환경처럼 사용(DB 삭제/생성 반복 - 볼륨Volume을 끼우고 빼고...)



* 마이크로서비스 : 

어플리케이션의 모듈화가 아닌 네트워크 분리를 통해 완전히 독립적인 어플리케이션으로 동작하게 만든다.


가상화를 통해 동일 서브넷에 구성하더라도 통신이 어려울 수 있다.

물리적으로는 분리되어 있기 때문...(IP는 사설을 사용할 수 밖에 없음)

따라서 네트워크 가상화 (ex: VnLAN)를 통해 이 둘을 묶어준다...


*DEVOPS : 

"빨리 개발하고 빨리 배포하겠다"하는 스타트업, 중소기업 도입...





* 가상머신과 컨테이너 차이점 : 

 가상머신(VMs)

 컨테이너

 하드웨어 수준의 가상화

운영체제 가상화 

 커널 공유 vs 하이퍼바이저 사용

리소스 사용이 높다 

가상머신보다 리소스 사용이 적다 

프로비저닝이 느리다

거의 리얼타임 프로비저닝과 확장성을 제공 

다양한 어플리케이션 실행 가능 

동일 커널 기반의 서비스만 구성 가능 

제한된 성능

 네이티브 성능

가상머신 간 완전한 격리로 인한 높은 보안성 제공 

프로세스 수준의 격리로 가상머신보단 보안 수준이 낮다 


* 컨테이너 한계점:

- Scale Out은 동일 Docker 호스트에서만 가능하다
- Docker에서는 node 확장에 관여하지 않는다.

Docker를 구성하는 호스트가 VM위에 올라가 있다.

Docker Host는 커널만 있으면 된다!


Atomic Host(500MB까지 제공)

CoreOS(약 500MB)

VmWARE photum


Docker는 이동성에 용이.

이미지로 만들어 배포 용이(재사용성)





*Docker가 제공하는 지원기술 :


- swarm : docker의 클러스터 솔루션

- compose : 여러 컨테이너로 구성된 어플리케이션 생성과 실행 도구

- 머신 : Docker 호스트 설치와 구성 도구

- Docker Trusted Registry: Docker 이미지 저장과 관리에 대한 온프리마이스 도구

- 네트워크: 여러 호스트간 컨테이너 통신을 지원

- 서비스 디스커버리 : 다른 서비스들을 찾아주는 도구

- 오케스트레이션과 클러스터 관리 : 시스템 모니터링과 관리

▶도커 설치 링크 : 


https://docs.docker.com/toolbox/toolbox_install_windows/





*컨테이너 개념 :
2가지 유형이 있다

1) Linux Container

2) Docker


VM환경에서 Container 개념으로 넘어가는 것에 대해서도 생각해봐야한다.


인프라의 변화에 대해 이야기를 해보면

1.물리머신

2.VM

3.Container

으로 변하고 있다!!


*비즈니스 관점에서 살펴보면

(ex: 어플을 각 인프라에 배포한다고 했을 때)

물리머신에서 VM으로 넘어가게 된 계기는 Enterprise 관점에서 이슈가 있었기 때문이다...이 변화는 우연히 바뀐것은 아님...

- 비즈니스에서 고민거리에 대해 누군가 방법론을 만듬

- 방법론을 해결할 수 있는 솔루션이 나옴

항상 이 순서로 진행되어왔다.


*서버 가상화

물리시스템에서 VMWARE로 바뀌면 하드웨어가 많이 사라지게 된다.

많은 인원이 필요 없어짐.

IT Provider에 있어서 원하지 않는 흐름

하지만 서비스 제공자, Business에 있어서는 비용(COST)을 줄일 수 있기 때문에 이 흐름을 환영.


*기존 물리머신 Hardware 비즈니스 관점 이슈

DATA Center가 계속 생겨나면 비용~전기세가 가장 많이 많이 듬

이걸 Business에 있어서는 VM을 통해 획기적으로 줄일 수 있음...

어플리케이션 유지...다른 Business와 같이 사용...규모의 경제

==>Hardware Consolidation

구성만 잘 하면 성능상에서도 기존 대비 별다른 이슈 없음


*Data Center의 주류가 VM으로 변경됨

70%이상의 시스템이 VM기반으로 넘어가게됨...CLOUD...





*Docker Container가 2013년에 시장에서 처음 언급됨...

2014년에 Release(Container의 등장)


MICRO SERVICE ARCHITECTURE, DEVOPS 관점

최근 IT에 있어서 원하는 희망사항?


1) 대규모 통합 서비스(시스템이 무거워짐, Dependency에 걸리는 Library 충돌이 일어나고...)

2) 모듈러 베이스 아키텍쳐(네트워크를 통해 모두 분산시킴)

3) MSA 방법론(분산컴퓨팅에서...하나의 시스템에는 한개의 어플리케이션만...Micro Service...가장 작은 단위의 서비스로 만들어냄)

RESTFUL...네트워크 단위로 분산


*OPERATION 관점에서 살펴보면 : 

- IT는 COST다

IT COST의 7:3에서 7이 유지보수 비용...


가상화를 통해 하드웨어에 대한 유지보수 비용이 줄어든다...S/W로 들어가면 유지보수 비용도 최소화된다.

7: 혁신에 더 많은 비용을 들이고

3: 유지보수는 최소화....



프로그램을 너무 크게 만들어서 문제(경쟁사에 밀리지 않기 위해 Release를 빨리 해야 함...대규모 시스템을 모듈화...최소화...)

=> 소스코드를 검증해서 바로 Release

Release는 자주하면 할 수록 소프트웨어는 신뢰도가 높아진다.

=>어플리케이션을 가장 작은 UNIT으로 잘라낸다.

단점 : 서로 커뮤니케이션이 안됨~전문성 부재(ex: 개발자<=>테스트)

개발팀을 다시 소규모로 줄임...전문성은 자동화?

"코드 한줄을 바꿔서 Release를 얼마나 할 수 있는가(Dependency ↓)?"




이제 PAAS, MSA, DEVOPS 간의 교집합...

VM을 가지고 이 3가지를 모두 접목하기가 어려워짐...한계!!

=>리눅스 컨테이너의 탄생(2008) 

=>Docker(2014)



구조 1) 


APP1, APP2


Kernel


H/W


그런데 이런 구조라면 네트워크 및 프로세스를 공유...무거워진다.



그럼 아래 같은 구조라면? (커널이 3개)

커널을 2번 타게 되면서...Native 보다 성능이 좋지 않다.

리소스 많이 사용.


구조 2) VM


APP1                                APP2

Kernel(4Core /8G)                    Kernel(4Core /8G)


Kernel OS(10Core/64G을 1개 어플리케이션이 모두 점유할 수 없다)


H/W




//위 2개의 중간~>Name Space 개념 도입(격리, Isolation!!)

네트워크, PID, IPC, User Group, Mount가 VM처럼 모두 독립적임, 

리소스를 상대적으로 적게 쓰게 됨(커널이 1개)

베어메탈에 준하는 성능


구조 3) Container


App1            App2

Centos(OS)    Ubuntu(OS)    


Kernel 공유(10Core/64G를 온전히 1개 어플리케이션이 모두 점유할 수 있다)


H/W




*Linux 컨테이너의 한계: 

Linux 컨테이너는 분산배포를 처리하는 개념이 없었다...단순히 VM을 경량화 시킨것

Docker는 컨테이너로 분산배포 지원

배송/배포가 핵심!!



- Linux Container : OS Container (1개의 OS에 여러 App을 띄움)

- Docker Container : App Container (각 OS에 Application을 독립적으로 띄운다, OS Container로도 구성이 가능 - MSA)




* 컨테이너를 때거지로 모아서 관리하는것...

Multi Container Management Solution : 

- Swarm, Compose (Docker : Multi Container 관리)

- Kubernetes(Google: Multi Container 관리)


가벼운 VM을 만들자!!

+ Recent posts