728x90
<Kubernetes :: 이니셜 컨테이너 initial container 실습 1>
- initial container: Pod 내에서 다른 컨테이너가 시작되기 전에 먼저 실행되는 컨테이너
- 본래의 애플리케이션 컨테이너가 시작되기 전에 데이터베이스를 초기화하거나 설정 파일을 다운로드하는 등의 작업을 수행
- 이전 초기 컨테이너가 성공적으로 완료되어야 다음 초기 컨테이너가 시작
1. pod 파일 생성 (외부에서 yaml 파일 가져옴)
<weather.yaml>
- 초기화 컨테이너
- 이름: download-config
- 이미지: curlimages/curl:7.85.0
- HTTPS를 통해 날씨 정보를 다운로드하고 "/usr/share/nginx/html/index.html" 경로에 저장
- 볼륨 마운트: "/usr/share/nginx/html" 경로에 "weather-data" 볼륨을 마운트하여 다운로드한 데이터를 저장
- 메인 컨테이너
- 이미지: nginx:1.25.3-alpine
- 포트: 80
- 볼륨 마운트: "/usr/share/nginx/html" 경로에 "weather-data" 볼륨을 마운트하여 초기화 컨테이너에서 다운로드한 데이터에 액세스
apiVersion: v1
kind: Pod
metadata:
name: weather-pod
namespace: default
spec:
volumes:
- emptyDir: {}
name: weather-data
initContainers:
- name: download-config
image: curlimages/curl:7.85.0
args: ["https://api.open-Meteo.com/v1/forecast?latitude=37.5443878&longitude=127.03744246¤t_weather=true", "-o", "/usr/share/nginx/html/index.html"]
volumeMounts:
- mountPath: /usr/share/nginx/html
name: weather-data
containers:
- image: nginx:1.25.3-alpine
name: nginx-container
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: weather-data
2. 포드 실행
PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f weather.yaml
pod/weather-pod created
<weather 포드 실행 확인>
A. kubectl get po
PS C:\Users\admin\Desktop\k8s-pods> kubectl get po -o wide | grep weather
B. curl 확인
ubuntu@k8s-master:~$ curl 10.111.156.101
<Kubernetes :: 이니셜 컨테이너 initial container 실습 2>
1. 명령어를 통해 initial container 설정된 yaml 생성
PS C:\Users\admin\Desktop\k8s-pods> kubectl run myapp-init-pod --image=busybox --dry-run=client -o yaml > myapp-init-pod.yaml
<myapp-init-pod.yaml>
- 메인 컨테이너: busybox:1.28 이미지를 사용 / "The app is running!"을 출력하고 3600초 동안 실행
- 초기 컨테이너: busybox:1.28 이미지를 사용 / 컨테이너는 Pod가 시작될 때 실행되는 초기화 작업을 수행 (Pod의 네임스페이스 내에서 "myservice" 서비스의 DNS 이름을 확인하기 위해 실행)
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: myapp-pod
spec:
containers:
- image: busybox:1.28
name: myapp-container
command: ["sh", "-c", "echo The app is running! && sleep 3600"]
initContainers:
- image: busybox:1.28
name: init-myservice
command: ["sh", "-c", "until nslookup myservice .$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
2. pod 파일 적용
PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f myapp-init-pod.yaml
pod/myapp-pod created
3. 서비스 yaml 파일 제작 및 서비스 선언
<myapp-init-svc.yaml>
- 이름: myapp-svc
- 포트: 80
- 타겟 포트: 9376
- 프로토콜: TCP
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
- 서비스 선언
PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f myapp-init-svc.yaml
svc/myapp-pod created
<포드 및 서비스 생성 확인>
'클라우드 + DevOps > Kubernetes (k8s)' 카테고리의 다른 글
Kubernetes :: 사이드카 컨테이너 sidecar container 제작 실습 (0) | 2024.04.22 |
---|---|
Kubernetes 쿠버네티스 :: StatefulSet 활용 (0) | 2024.04.22 |
Kubernetes 쿠버네티스 실습 :: 생성한 Deployment 수정 (0) | 2024.04.21 |
Kubernetes :: 런타임 컨테이너 runtime container 실습 (1) | 2024.04.20 |
Kubernetes 쿠버네티스 :: workload resources (1) | 2024.04.20 |