Room9

CKAD - Application Design and Build (Multi-Container) 본문

Kubernetes

CKAD - Application Design and Build (Multi-Container)

Room9_ 2022. 1. 8. 21:26

Multi-Container Pods

서비스를 하다보면 동일한 수명주기를 갖는 서비스가 존재함 예시로 웹 서버 인스턴스와 로깅 서비스(SideCar)

두개의 서비스를 하나로 묶어서 확장 및 축소를 할 수 있다. 동일한 라이프 사이클을 가지는 멀티 컨테이너 파드를 사용 한다. 동일한 네트워크 공간을 공유하고 동일한 스토리지 볼륨에 엑세스 할 수 있다. 추가적으로 네트워크를 활성화 하기 위하여 파드 간 서비스나 볼륨 공유를 설정할 필요가 없다.


Multi-Container Design Patterns

Sidecar Pattern

사이드카 패턴은 원래의 목적 이외에 기능 강화나 확장을 위한 컨테이너를 추가하는 것을 말한다. 실제 사이드카의 모습처럼 원래 목적의 컨테이너 옆에 붙어서 Multi-Container로 동작한다. 위에서 언급했듯 좋은 예시로 웹서버 컨테이너는 원래의 기능을 하며, 남긴 로그들은 옆에 붙어 있는 로그수집 컨테이너가 파일시스템에 쌓이는 로그를 수집해서 외부의 로그수집 시스템으로 보내는 역할하게 된다.

 

Ambassador Pattern

앰버서더 패턴은 파드내에 프록시 역할을 하는 컨테이너를 추가하는 패턴이다. 파드내부에서 외부 서버에 접근 할때 같이 만들어진 프록시 컨테이너에 접근하게 설정하고 실제 외부연결은 프록시가 처리하는 방식. 실제로 서비스 메시를 구현하는 이스티오의 경우에는 파드마다 프록시를 추가하여 트래픽 처리를 하는 구조로 생성된다.

 

Adapter Pattern

어탭터 패턴은 컨테이너 파드 외부로 노출되는 정보를 표준화하는 역할을 한다. 예시로 파드의 모니터링 지표를 어댑터 컨테이너를 통해 일괄적으로 표준화된 형식으로 변경하는 역할을 담당한다. 사용처는 Prometheus로 클러스터 모니터링 할 때 사용하는 Prometheus Adapter를 확인할 수 있다.


KodeKloud

MULTI-CONTAINER PODS, MULTI-CONTAINER PODS

Q1. Identify the number of containers created in the red pod.

root@controlplane:~# kubectl get pods 
NAME        READY   STATUS    RESTARTS   AGE
app         1/1     Running   0          6m10s
fluent-ui   1/1     Running   0          6m11s
red         3/3     Running   0          6m2s

Q2. Identify the name of the containers running in the blue pod.

root@controlplane:~# kubectl describe pod blue 
Name:         blue
Namespace:    default
Priority:     0
Node:         controlplane/10.20.104.9
Start Time:   Sat, 08 Jan 2022 12:01:38 +0000
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.0.10
IPs:
  IP:  10.244.0.10
Containers:
  teal:
    Container ID:  docker://152395394c0b5907a205a905d4b7dbc8ac3d309fee6deabf67b1c3288f051465
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      4500
    State:          Running
      Started:      Sat, 08 Jan 2022 12:01:41 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zvmps (ro)
  navy:
    Container ID:  docker://7101118a28f88d004fc0e3d10e9b867f8f8cb687e3073c29e46b9ab6f985b293
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      4500
    State:          Running
      Started:      Sat, 08 Jan 2022 12:01:42 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zvmps (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-zvmps:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zvmps
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  31s   default-scheduler  Successfully assigned default/blue to controlplane
  Normal  Pulling    29s   kubelet            Pulling image "busybox"
  Normal  Pulled     29s   kubelet            Successfully pulled image "busybox" in 157.219086ms
  Normal  Created    29s   kubelet            Created container teal
  Normal  Started    28s   kubelet            Started container teal
  Normal  Pulling    28s   kubelet            Pulling image "busybox"
  Normal  Pulled     28s   kubelet            Successfully pulled image "busybox" in 145.798694ms
  Normal  Created    28s   kubelet            Created container navy
  Normal  Started    27s   kubelet            Started container navy

> teal & navy


Q3. Create a multi-container pod with 2 containers.

Use the spec given below.

If the pod goes into the crashloopbackoff then add sleep 1000 in the lemon container.

  • Name: yellow
  • Container 1 Name: lemon
  • Container 1 Image: busybox
  • Container 2 Name: gold
  • Container 2 Image: redis
apiVersion: v1
kind: Pod
metadata:
  name: yellow
spec:
  containers:
  - name: lemon
    image: busybox
    command:
      - sleep
      - "1000"

  - name: gold
    image: redis
root@controlplane:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
app         1/1     Running   0          12m
blue        2/2     Running   0          6m
fluent-ui   1/1     Running   0          12m
red         3/3     Running   0          12m
yellow      2/2     Running   0          46s

Q4. We have deployed an application logging stack in the elastic-stack namespace.Inspect it.

Before proceeding with the next set of questions, please wait for all the pods in the elastic-stack namespace to be ready. This can take a few minutes.


Q6. Inspect the app pod and identify the number of containers in it.

It is deployed in the elastic-stack namespace.

root@controlplane:~# kubectl -n elastic-stack get pod
NAME             READY   STATUS    RESTARTS   AGE
app              1/1     Running   0          16m
elastic-search   1/1     Running   0          16m
kibana           1/1     Running   0          16m

> 1


Q7. The application outputs logs to the file /log/app.log. View the logs and try to identify the user having issues with Login. Inspect the log file inside the pod.

root@controlplane:~# kubectl -n elastic-stack logs app
[2022-01-08 12:12:35,976] INFO in event-simulator: USER4 is viewing page2
[2022-01-08 12:12:36,977] INFO in event-simulator: USER2 is viewing page3
[2022-01-08 12:12:37,978] INFO in event-simulator: USER4 is viewing page1
[2022-01-08 12:12:38,979] INFO in event-simulator: USER2 logged in
[2022-01-08 12:12:39,980] INFO in event-simulator: USER4 is viewing page2
[2022-01-08 12:12:40,981] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-01-08 12:12:40,982] INFO in event-simulator: USER4 logged out

> USER5


Q8. Edit the pod to add a sidecar container to send logs to Elastic Search. Mount the log volume to the sidecar container. Only add a new container. Do not modify anything else. Use the spec provided below.

  • Name: app
  • Container Name: sidecar
  • Container Image: kodekloud/filebeat-configured
  • Volume Mount: log-volume
  • Mount Path: /var/log/event-simulator/
  • Existing Container Name: app
  • Existing Container Image: kodekloud/event-simulator

apiVersion: v1
kind: Pod
metadata:
  name: app
  namespace: elastic-stack
  labels:
    name: app
spec:
  containers:
  - name: app
    image: kodekloud/event-simulator
    volumeMounts:
    - mountPath: /log
      name: log-volume

  - name: sidecar
    image: kodekloud/filebeat-configured
    volumeMounts:
    - mountPath: /var/log/event-simulator/
      name: log-volume

  volumes:
  - name: log-volume
    hostPath:
      path: /var/log/webapp
      type: DirectoryOrCreate
root@controlplane:~/elastic-search# kubectl get pods -n elastic-stack   
NAME             READY   STATUS    RESTARTS   AGE
app              2/2     Running   0          24s
elastic-search   1/1     Running   0          29m
kibana           1/1     Running   0          29m

 

Comments