도커#

컨테이너 명령어 실행#

docker container exec -it (컨테이너명) (명령어)
docker container exec -it mysql mysql -u root -p volume_test
docker container exec -it mysql /bin/bash

도커스웜#

이름 역할 명령어
컴포즈 여러 컨테이너로 구성된 도커 애플리케이션을 관리 (단일 호스트) docker compose
스웜 클러스터 구축및 관리 (멀티 호스트) docker swarm
서비스 스웜에서 클러스터 안의 서비스(컨테이너 하나 이상의 집합)를 관리 docker service
스택 스웜에서 여러개의 서비스를 합친 전체 애플리케이션을 관리 docker stack

샘플#

MySQL#

master

  • MYSQL_ROOT_PASSWORD
  • MYSQL_DATABASE
  • MYSQL_USER
  • MYSQL_PASSWORD

slave

  • MYSQL_MASTER_HOST
  • MYSQL_ROOT_PASSWORD
  • MYSQL_DATABASE
  • MYSQL_USER
  • MYSQL_PASSWORD
  • MYSQL_REPL_USER
  • MYSQL_REPL_PASSWORD

Dockerfile

FROM mysql:5.7

# (1) 패키지 업데이트및 wget 설치
RUN apt-get update
RUN apt-get install -y wget

# (2) entrykit 설치
RUN wget https://github.com/progrium/entrykit/releases/download/v0.4.0/entrykit_0.4.0_linux_x86_64.tgz
RUN tar -xvzf entrykit_0.4.0_linux_x86_64.tgz
RUN rm entrykit_0.4.0_linux_x86_64.tgz
RUN mv entrykit /usr/local/bin/
RUN entrykit --symlink

# (3) 스크립트및 각종설정 파일 복사
COPY add-server-id.sh /usr/local/bin/
COPY etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/
COPY etc/mysql/conf.d/mysql.cnf /etc/mysql/conf.d/
COPY prepare.sh /docker-entrypoint-initdb.d
COPY init-data.sh /usr/local/bin/
COPY sql /sql

# (4) 스크립트, mysqld 실행
ENTRYPOINT [ \
  "prehook", \
    "add-server-id.sh", \
    "--", \
  "docker-entrypoint.sh" \
]

CMD ["mysqld"]

쿠버네티스#

다운로드#

C:\Users\user> kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.8.3/src/deploy/recommended/kubernetes-dashboard.yaml

secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created

C:\Users\user>kubectl get pod --namespace=kube-system -l k8s-app=kubernetes-dashboard
NAME                                    READY   STATUS    RESTARTS   AGE
kubernetes-dashboard-6fd7f9c494-ckthw   1/1     Running   0          118s

C:\Users\user>kubectl proxy
Starting to serve on 127.0.0.1:8001

// http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/ 접속

쿠버네티스 주요개념#

  • 노드 : 컨테이너가 배치되는 서버
  • 네임스페이스 : 쿠버네티스 클러스터 안의 가상 클러스터
  • 파드 : 컨테이너의 집합 중 가장 작은 단위로 컨테이너의 실행 방법을 정의한다.
  • 레플리카세트 : 같은 스펙을 갖는 파드를 여러개 생성하고 관리하는 역할을 한다.
  • 디플로이먼트 : 레플리카 세트의 리비전을 관리한다.
  • 서비스 : 파드의 집합에 접근하기 위한 경로를 정의한다.
  • 인그레스 : 서비스를 쿠버네티스 클러스터 외부에 노출시킨다.
  • 컨피그맵 : 설정 정보를 정의하고 파드에 전달한다.
  • 퍼시스턴트볼륨 : 파드가 사용할 스토리지의 크기 및 종류를 정의
  • 퍼시스턴트볼륨클레임 : 퍼시스턴트 볼륨을 동적으로 확보
  • 스토리지클래스 : 퍼시스턴트 볼륨이 확보하는 스토리지의 종류를 정의
  • 스테이트풀세트 : 같은 스펙으로 모두 동일한 파드를 여러개 생성ㄷ하고 관리한다.
  • 잡 : 상주 실행을 목적으로 하지 않는 파드를 여러개 생성하고 정상적인 종료를 보장한다.
  • 크론잡 : 크론 문법으로 스케줄링되는 잡
  • 시크릿 : 인증정보 같은 기밀데이터를 정의한다.
  • 롤 : 네임스페이스 안에서 조작가능한 쿠버네티스 리소스의 규칙을 정의한다.
  • 롤바인딩 : 쿠버네티스 리소스 사용자와 롤을 연결짓는다.
  • 클러스터롤 : 클러스터 전체적으로 조작가능한 쿠버네티스 리소스의 규칙을 정의한다.
  • 클러스터롤바인딩 : 쿠버네티스 리소스 사용자와 클러스터롤을 연결짓는다.
  • 서비스계정 : 파드가 쿠버네티스 리소스를 조작할때 사용하는 계정
C:\Users\user>kubectl get nodes
NAME             STATUS   ROLES    AGE   VERSION
docker-desktop   Ready    master   17h   v1.14.3

C:\Users\user>kubectl get namespaces
NAME              STATUS   AGE
default           Active   18h
docker            Active   18h
kube-node-lease   Active   18h
kube-public       Active   18h
kube-system       Active   18h

C:\Users\user>kubectl get pods
No resources found.

샘플#

파드(pod)#

simple-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: simple-echo
spec:
  containers:
  - name: nginx 
    image: gihyodocker/nginx:latest 
    env: 
    - name: BACKEND_HOST
      value: localhost:8080
    ports:
    - containerPort: 80 
  - name: echo
    image: gihyodocker/echo:latest
    ports:
    - containerPort: 8080
D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_06_1>kubectl apply -f simple-pod.yaml
pod/simple-echo created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_06_1>kubectl get pods
NAME          READY   STATUS              RESTARTS   AGE
simple-echo   0/2     ContainerCreating   0          34s

// 컨테이너가 여러개일 경우 -c 옵션으로 컨테이너 지정
D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_06_1>kubectl exec -it simple-echo sh -c nginx
#

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_06_1>kubectl logs -f simple-echo -c echo
2020/02/18 02:43:44 start server

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_06_1>kubectl delete pod simple-echo
pod "simple-echo" deleted

레플리카세트(replicaset)#

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: echo 
  labels:
    app: echo 
spec:
  replicas: 3
  selector:
    matchLabels:
      app: echo 
  template: # template 아래는 파드 리소스 정의와 같음
    metadata:
      labels:
        app: echo
    spec:
      containers:
      - name: nginx 
        image: gihyodocker/nginx:latest 
        env: 
        - name: BACKEND_HOST
          value: localhost:8080
        ports:
        - containerPort: 80 
      - name: echo
        image: gihyodocker/echo:latest
        ports:
        - containerPort: 8080

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_07>kubectl apply -f simple-replicaset.yaml
replicaset.apps/echo created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_07>kubectl get pod
NAME         READY   STATUS              RESTARTS   AGE
echo-bntx9   0/2     ContainerCreating   0          10s
echo-hnjzn   0/2     ContainerCreating   0          10s
echo-njfhb   0/2     ContainerCreating   0          10s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_07>kubectl delete -f simple-replicaset.yaml
replicaset.apps "echo" deleted

디플로이먼트(deployment)#

apiVersion: apps/v1
kind: Deployment 
metadata:
  name: echo 
  labels:
    app: echo 
spec:
  replicas: 3
  selector:
    matchLabels:
      app: echo 
  template: # template 아래는 파드 리소스 정의와 같음
    metadata:
      labels:
        app: echo
    spec:
      containers:
      - name: nginx 
        image: gihyodocker/nginx:latest 
        env: 
        - name: BACKEND_HOST
          value: localhost:8080
        ports:
        - containerPort: 80 
      - name: echo
        image: gihyodocker/echo:patched
        env: 
        - name: HOGE 
          value: fuga 
        ports:
        - containerPort: 8080
// --record는 기록을 남기는 옵션
D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl apply -f simple-deployment.yaml --record
deployment.apps/echo created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl get pod,replicaset,deployment --selector app=echo
NAME                        READY   STATUS    RESTARTS   AGE
pod/echo-84f498cd74-f9wk2   2/2     Running   0          52s
pod/echo-84f498cd74-hcnsz   2/2     Running   0          52s
pod/echo-84f498cd74-s98c8   2/2     Running   0          52s

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-84f498cd74   3         3         3       52s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/echo   3/3     3            3           52s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl rollout history deployment echo
deployment.extensions/echo
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=simple-deployment.yaml --record=true

simple-deployment.yaml 파일에 replicas 를 3에서 4로 수정 후..

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl apply -f simple-deployment.yaml --record
deployment.apps/echo configured

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl get pod,replicaset,deployment --selector app=echo
NAME                        READY   STATUS    RESTARTS   AGE
pod/echo-84f498cd74-f9wk2   2/2     Running   0          4m18s
pod/echo-84f498cd74-gk6sf   2/2     Running   0          7s
pod/echo-84f498cd74-hcnsz   2/2     Running   0          4m18s
pod/echo-84f498cd74-s98c8   2/2     Running   0          4m18s

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-84f498cd74   4         4         4       4m18s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/echo   4/4     4            4           4m18s

simple-deployment.yaml 파일에 이미지 버전 수정 후

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl get pod,replicaset,deployment --selector app=echo
NAME                        READY   STATUS              RESTARTS   AGE
pod/echo-5c8fbb8854-9lwvw   0/2     ContainerCreating   0          5s
pod/echo-5c8fbb8854-dtp7q   2/2     Running             0          5s
pod/echo-5c8fbb8854-vszw4   0/2     ContainerCreating   0          0s
pod/echo-84f498cd74-f9wk2   2/2     Running             0          7m45s
pod/echo-84f498cd74-gk6sf   2/2     Terminating         0          3m34s
pod/echo-84f498cd74-hcnsz   2/2     Terminating         0          7m45s
pod/echo-84f498cd74-s98c8   2/2     Running             0          7m45s

NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-5c8fbb8854   3         3         1       5s
replicaset.extensions/echo-84f498cd74   2         2         2       7m45s

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/echo   3/4     3            3           7m45s

롤백하기

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl rollout history deployment echo
deployment.extensions/echo
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=simple-deployment.yaml --record=true
2         kubectl apply --filename=simple-deployment.yaml --record=true


D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl rollout history deployment echo --revision=1
deployment.extensions/echo with revision #1
Pod Template:
  Labels:       app=echo
        pod-template-hash=84f498cd74
  Annotations:  kubernetes.io/change-cause: kubectl apply --filename=simple-deployment.yaml --record=true
  Containers:
   nginx:
    Image:      gihyodocker/nginx:latest
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:
      BACKEND_HOST:     localhost:8080
    Mounts:     <none>
   echo:
    Image:      gihyodocker/echo:patched
    Port:       8080/TCP
    Host Port:  0/TCP
    Environment:
      HOGE:     fuga
    Mounts:     <none>
  Volumes:      <none>

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_08>kubectl rollout undo deployment echo
deployment.extensions/echo rolled back  

서비스#

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: echo-spring
  labels:
    app: echo 
    release: spring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo 
      release: spring
  template:
    metadata:
      labels:
        app: echo
        release: spring
    spec:
      containers:
      - name: nginx 
        image: gihyodocker/nginx:latest 
        env: 
        - name: BACKEND_HOST
          value: localhost:8080
        ports:
        - containerPort: 80 
      - name: echo
        image: gihyodocker/echo:latest
        ports:
        - containerPort: 8080

---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: echo-summer
  labels:
    app: echo 
    release: summer 
spec:
  replicas: 2
  selector:
    matchLabels:
      app: echo 
      release: summer 
  template:
    metadata:
      labels:
        app: echo
        release: summer 
    spec:
      containers:
      - name: nginx 
        image: gihyodocker/nginx:latest 
        env: 
        - name: BACKEND_HOST
          value: localhost:8080
        ports:
        - containerPort: 80 
      - name: echo
        image: gihyodocker/echo:latest
        ports:
        - containerPort: 8080

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl apply -f simple-replicaset-with-label.yaml
replicaset.apps/echo-spring created
replicaset.apps/echo-summer created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl get pod,replicaset,deployment
NAME                    READY   STATUS              RESTARTS   AGE
pod/echo-spring-smb7j   0/2     ContainerCreating   0          8s
pod/echo-summer-b7mtg   0/2     ContainerCreating   0          8s
pod/echo-summer-t9mdt   0/2     ContainerCreating   0          8s

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-spring   1         1         0       8s
replicaset.extensions/echo-summer   2         2         0       8s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl get pod,replicaset,deployment  -l app=echo -l release=spring
NAME                    READY   STATUS    RESTARTS   AGE
pod/echo-spring-smb7j   2/2     Running   0          9m57s

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-spring   1         1         1       9m57s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl get pod,replicaset,deployment  -l app=echo -l release=summer
NAME                    READY   STATUS    RESTARTS   AGE
pod/echo-summer-b7mtg   2/2     Running   0          10m
pod/echo-summer-t9mdt   2/2     Running   0          10m

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.extensions/echo-summer   2         2         2       10m

apiVersion: v1
kind: Service
metadata:
  name: echo 
spec:
  selector:
    app: echo
    release: summer
  ports:
    - name: http
      port: 80

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl apply -f simple-service.yaml
service/echo created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl get svc echo
NAME   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
echo   ClusterIP   10.102.160.35   <none>        80/TCP    10s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_09>kubectl run -i --rm --tty debug --image=gihyodocker/fundamental:0.1.0 --restart=Never -- bash -il
If you don't see a command prompt, try pressing enter.
debug:/# curl http://echo/
Hello Docker!!debug:/#

C:\Users\user>kubectl logs -f echo-summer-b7mtg -c echo
2020/02/18 04:52:00 start server

C:\Users\user>kubectl logs -f echo-summer-t9mdt -c echo
2020/02/18 04:52:02 start server
2020/02/18 05:06:16 received request

C:\Users\user>kubectl logs -f echo-spring-smb7j -c echo
2020/02/18 04:52:05 start server

서비스의 네임 레졸루션

  • 쿠버네티스 클러스터의 DNS는 "서비스명.네임스페이스명.svc.local"
  • 디폴트 네임스페이스는 default 라서 서비스명이 echo라면 http://echo.default.svc.local 로 요청가능
  • 단 디폴트 네임스페이스와 svc.local은 생략가능해서 http://echo.default.svc.local 의 경우 http://echo 로 사용가능

NodePort 서비스

LoadBalancer서비스

ExternalName서비스

인그레스#

  • 서비스를 이용한 쿠버네티스 클러스터 외부에 대한 노출과 가상 호스트 및 경로 기반의 정교한 HTTP라우팅을 양립시킬수 있다. HTTP/HTTPS 서비스를 노출하려는 경우에는 십중팔구 인그레스를 사용한다.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: echo
spec:
  rules:
  - host: ch05.gihyo.local
    http:
      paths:
      - path: /
        backend:
          serviceName: echo
          servicePort: 80

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.16.2/deploy/mandatory.yaml
namespace/ingress-nginx created
deployment.extensions/default-http-backend created
service/default-http-backend created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.extensions/nginx-ingress-controller created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.16.2/deploy/provider/cloud-generic.yaml
service/ingress-nginx created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl -n ingress-nginx get service,pod
NAME                           TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/default-http-backend   ClusterIP      10.106.21.161   <none>        80/TCP                       88s
service/ingress-nginx          LoadBalancer   10.109.149.78   localhost     80:31895/TCP,443:31961/TCP   48s

NAME                                           READY   STATUS    RESTARTS   AGE
pod/default-http-backend-55b84578bf-4nkw2      1/1     Running   0          88s
pod/nginx-ingress-controller-b5d545f8f-46xw8   1/1     Running   0          87s

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl apply -f simple-service.yaml
service/echo configured

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl apply -f simple-ingress.yaml
ingress.extensions/echo created

D:\apps\workspace_docker\docker-kubernetes\ch05\ch05_10_1>kubectl get ingress
NAME   HOSTS              ADDRESS   PORTS   AGE
echo   ch05.gihyo.local             80      11s

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-17) was last changed on 18-Feb-2020 16:46 by DongGukLee