치킨맛코드 2022. 12. 16. 17:33

Fastcampus k8s와 docker로 끝내는 컨테이너 기반 MSA 강의 내용을 정리한 것 입니다.

 

https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#define-an-environment-variable-for-a-container

 

Define Environment Variables for a Container

This page shows how to define environment variables for a container in a Kubernetes Pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run

kubernetes.io

https://kubernetes.io/ko/docs/tasks/inject-data-application/define-command-argument-container/

<문제.01 pod 생성 관리>

특정 이름을 가진 namespace를 만들고, 그 네임스페이스 아래에 다음과 같은 pod를 생성하시오.

 

<조건>

작업 클러스터 : k8s

namespace : cka-exam
pod name : pod-01
image : busybox
환경변수(env) : CERT= "CKA-cert"
command: /bin/sh
args: -c "while true; do echo $(CERT); sleep 10; done

<풀이과정>

k8s의 해당 위치로 이동한다. => namespace를 생성한다. => kubectl명령어를 사용해 yaml값을 생성 및 수정한다.=> yaml을 실행한다.

 

1. 문제에서 요구하는 k8s의 위치로 이동한다.

==> kubeclt config use-context k8s

 

2. namespace 생성
==> kubectl create namespace cka-exam
==> kubectl get namespace cka-exam

 

3. yaml(etc..)생성하기
==> kubectl run "pod name" --image="image name" --diy-run -o yaml > yaml_name.yaml

 

4. vi로 yaml 값 수정하기
==> 문제에서 요구하는 내용을 추가하기.
      보통 pod name, namespace, image name, env, command, args를 추가
metadata ==> pod name. namespace 추가
spec ==> env, name, value, command, args추가

 

*

namespace와 pod를 생성하는 방법에 대해 알아본다.pod는 k8s에서 사용할 수 있는 가장 작은 가상 컴퓨터에 대한 단위이다. pod 1개는 pc1대와 같다. 라고 생각할 수 있다. 이러한 pod와 k8s를 운용하는데 필요한 service등을 포함하여 하나의 그룹단위로 묶어주는 것을 namespace라고 표현한다.

*

https://kubernetes.io/docs/concepts/cluster-administration/logging/#sidecar-container-with-logging-agent

 

Logging Architecture

Application logs can help you understand what is happening inside your application. The logs are particularly useful for debugging problems and monitoring cluster activity. Most modern applications have some kind of logging mechanism. Likewise, container e

kubernetes.io

<문제.02 pod 로그 확인 및 결과 추출>

pod의 특정 엡에 대한 log를 모니터링한 후 특정 메세지를 포함하는 log line을 추출 하십시오. 추출된 결과는 특정 위치에 기록하시오.

 

<조건>

앱 이름 : custom-app
희망 메세지 : file not found
로그 저장 위치 : /opt/REPOPRT/2022/custom-app-log.(해당 dir는 사전 생성되어 있음)
작업 클러스터 위치 : hk8s

<풀이과정>

k8s의 위치를 먼저 확인한 후 해당 위치로 이동한다. => kubectl명령어와 grep명령어를 사용해 문제에서 요구하는 메세지만 출력한다. => 해당 메세지를 지정위치에 저장한다.


1. 현재 바라보고있는 cluster를 확인한다.
==> kubectl config current-context

 

2. 문제에서 요구하는 cluster로 이동한다.
==> kubectl config use-context hk8s

 

3. pod가 실행되고 있는지 확인한다.(문제에서 요구하는 특정 앱"custom-app"이 있는지 확인)
==> kubectl get pods

 

4. 문제에서 요구한 custom-app이라는 pod에서 file not found라는 메세지만 출력한다.
==>  kubectl logs custom-app | grep 'file not found'

 

5.문제에서 지정한 해당 위치로 logs를 저장한다.
==>   kubectl logs custom-app | grep 'file not found' > /opt/REPOPRT/2022/custom-app-log

 

https://kubernetes.io/docs/tasks/configure-pod-container/static-pod/#static-pod-creation

 

Create static Pods

Static Pods are managed directly by the kubelet daemon on a specific node, without the API server observing them. Unlike Pods that are managed by the control plane (for example, a Deployment); instead, the kubelet watches each static Pod (and restarts it i

kubernetes.io

<문제.03 Static Pod 생성>

hk8s-w1에 static pod를 만들어라.

 

<조건>

static pod를 생성할 위치 : hk8s-w1

static pod 생성 설정을 확인할 수 있는 위치 : /var/lib/kubelet/config.yaml

pod name : nginx-static-pod

port : 80

pod image : nginx


<풀이과정>

hk8s-w1에 먼저 접근한다 => 관리자 권한을 부여한다. => config.yaml로 접근해 경로를 확인한다. => yaml값을 생성한다. => 해당 yaml을 편집한다.

 

1. hk8s-w1로 접근한다. 
==> ssh hk8s-w1

 

2. 최초 접근시 user로 접근이된다. user는 static pod를 생성할 수 있는 권한이 없기에, 권한이 있는 master 계정으로 변경한다.
==> sudo -i 

 

3. 경로를 확인한다.(cat/vi/vim등 결과값을 확인할 수 있으면 어느 명령어든 상관없다.)
==>  cat /var/lib/kubelet/config.yaml

 

4. pod.yaml을 생성한다. 
==> vi /etc/kubernetes/manifests/pod.yaml

 

5.  kubectl run nginx-static-pod --image=nginx --port=80 --dry-run
==>yaml값을 입력한다. yaml을 하나하나 입력하기 힘들다면 아무 pod를 생성한 후 그 yaml값을 사용해도 된다.