목차
Pod Label
# run으로 배포 시 labels은 run임
[root@m-k8s 6.7]# k run nginx --image=nginx -o yaml --dry-run=client
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
# create으로 배포 시 labels은 app임
[root@m-k8s 6.7]# k create deployment nginx --image=nginx -o yaml --dry-run=client
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
YAML
복사
[root@m-k8s 6.7]# k run nginx --image=nginx
pod/nginx created
[root@m-k8s 6.7]# k get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
net 0/1 ImagePullBackOff 0 46h run=net
nginx 1/1 Running 0 7s run=nginx
# Pod Label 커스텀하기
[root@m-k8s 6.7]# k label pod nginx purpose=web
pod/nginx labeled
[root@m-k8s 6.7]# k get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
net 0/1 ImagePullBackOff 0 46h run=net
nginx 1/1 Running 0 39s purpose=web,run=nginx
[root@m-k8s 6.7]# k get po -l purpose=web
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 63s
[root@m-k8s 6.7]# k get po -l run
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 46h
nginx 1/1 Running 0 70s
[root@m-k8s 6.7]# k label pod nginx purpose-
pod/nginx labeled
YAML
복사
Static Pod
•
kubelet이 마스터노드의 /etc/kubernetes/manifests 내 파일을 보고 정적으로 쿠버네티스 클러스터 구성요소 Pod를 배포한다.
[root@m-k8s 6.7]# ls /etc/kubernetes/manifests
etcd.yaml kube-apiserver.yaml kube-controller-manager.yaml kube-scheduler.yaml
YAML
복사
•
즉, 다음과 같이 /etc/kubernetes/manifests에 있는건 그냥 배포한다.
[root@m-k8s 7.3]# ls
static-pod.yaml
[root@m-k8s 7.3]# cp ./* /etc/kubernetes/manifests/
[root@m-k8s 7.3]# k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 46h 172.16.221.158 w1-k8s <none> <none>
static-pod-m-k8s 1/1 Running 0 6s 172.16.171.72 m-k8s <none> <none>
[root@m-k8s 7.3]# scp ./* w1-k8s:/etc/kubernetes/manifests
root@w1-k8s's password:
static-pod.yaml 100% 109 44.4KB/s 00:00
[root@m-k8s 7.3]# k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 47h 172.16.221.158 w1-k8s <none> <none>
static-pod-m-k8s 1/1 Running 0 3m9s 172.16.171.72 m-k8s <none> <none>
static-pod-w1-k8s 1/1 Running 0 8s 172.16.221.165 w1-k8s <none> <none>
YAML
복사
•
/etc/kubernetes/manifests디렉터리 내 파일을 삭제하면 pod도 삭제된다.
[root@m-k8s 7.3]# rm -rf /etc/kubernetes/manifests/static-pod.yaml
[root@m-k8s 7.3]# k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 47h 172.16.221.158 w1-k8s <none> <none>
static-pod-w1-k8s 1/1 Running 0 3m 172.16.221.165 w1-k8s <none> <none>
YAML
복사
restartPolicy
[Pod의 restartPolicy]
구분 | 설명 |
Always | 항상 다시 시작함(기본 값) |
Never | 절대 다시 시작하지 않음 |
OnFailure | 실패한 경우에만 다시 시작함 |
apiVersion: v1
kind: Pod
metadata:
labels:
run: pod-always
name: pod-always
spec:
containers:
- image: sysnet4admin/net-tools
name: net-tools
command: ["/bin/sh", "-c"]
args:
- nslookup kubernetes
restartPolicy: Always // or Never or OnFailure
[Always인 경우]
[root@m-k8s 7.4]# k get po -w
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 47h
pod-always 0/1 ContainerCreating 0 1s
pod-always 0/1 ContainerCreating 0 2s
pod-always 0/1 Completed 0 7s
pod-always 0/1 Completed 1 10s
pod-always 0/1 CrashLoopBackOff 1 (2s ago) 11s
pod-always 0/1 Completed 2 (18s ago) 27s
pod-always 0/1 CrashLoopBackOff 2 (2s ago) 29s
[Never인 경우]
[root@m-k8s 7.4]# k get po -w
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 47h
pod-always 0/1 Completed 3 (37s ago) 64s
pod-never 0/1 ContainerCreating 0 3s
pod-never 0/1 Completed 0 4s
pod-never 0/1 Completed 0 4s
[OnFailure인 경우]
[root@m-k8s 7.4]# k get po -w
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 47h
pod-always 0/1 Completed 4 (64s ago) 2m1s
pod-never 0/1 Completed 0 60s
pod-onfailure 0/1 ContainerCreating 0 2s
pod-onfailure 0/1 Completed 0 4s
pod-onfailure 0/1 Completed 0 5s
[OnFailure인 경우-아규먼트에 오타]
[root@m-k8s 7.4]# k get po -w
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 47h
pod-onfailure-retry 0/1 ContainerCreating 0 1s
pod-onfailure-retry 0/1 ContainerCreating 0 1s
pod-onfailure-retry 0/1 Error 0 4s
pod-onfailure-retry 0/1 Error 1 (3s ago) 7s
pod-onfailure-retry 0/1 CrashLoopBackOff 1 (2s ago) 8s
pod-onfailure-retry 0/1 Error 2 (18s ago) 24s
YAML
복사
[Deployment의 restartPolicy]
구분 | 설명 |
Always | 항상 다시 시작함(기본 값)
Replicaset을 유지해야하기 때문에 Always 옵션만 사용 가능함 |
애플리케이션 상태 탐사
[탐사 방법]
종류 | 설명 | 체크 실패 시 동작 |
StartupProbe | 가장 우선해서 컨테이너의 상태를 체크함 | 컨테이너는 죽고, restartPolicy에 의해서 동작함 |
livenessProbe | 컨테이너가 의도한 대로 동작 중인지를 체크 | 컨테이너는 죽고 restartPolicy에 의해서 동작함 |
readinessProbe | 컨테이너의 애플리케이션이 요청을 처리할 수 있는 상태인지 체크 | 컨테이너는 살아있지만 트래픽은 전달되지 않는 상태가 됨 |
[체크 방식]
체크 방법 | 설명 |
exec | 컨테이너에서 지정한 명령을 실행해서 성공하는지를 체크 |
httpGet | 지정된 주소 및 포트 번호에 HTTP GET 명령을 수행해서 응답을 체크 |
tcpSocket | 컨테이너의 주소 및 포트가 살아 있는 상태인지를 체크 |
StartupProbe
•
시작할 때 탐색한다.
•
문제가 있다면 재시작한다.
apiVersion: v1
kind: Pod
metadata:
labels:
run: startup-w-others
name: startup-w-others
spec:
containers:
- name: tardy-nginx
image: sysnet4admin/tardy-nginx
startupProbe:
exec:
command:
- cat
- /tmp/healthy-on
initialDelaySeconds: 10
periodSeconds: 60
livenessProbe:
exec:
command:
- cat
- /tmp/healthy-on
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
exec:
command:
- cat
- /tmp/healthy-on
initialDelaySeconds: 5
periodSeconds: 5
YAML
복사
livenessProbe
•
애플리케이션 상태를 파악한다.
•
문제가 있다면 애플리케이션을 재시작한다.
apiVersion: v1
kind: Pod
metadata:
labels:
run: liveness-exec
name: liveness-exec
spec:
containers:
- name: tardy-nginx
image: sysnet4admin/tardy-nginx
livenessProbe:
exec:
command:
- cat
- /tmp/healthy-on
initialDelaySeconds: 10
periodSeconds: 10 #it cannot start properly
---------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: liveness-httpget
name: liveness-httpget
spec:
containers:
- name: healthz-nginx
image: sysnet4admin/healthz-nginx
livenessProbe:
httpGet:
path: /healthz
port: 80
httpHeaders:
- name: purpose
value: health-check
initialDelaySeconds: 3
periodSeconds: 3
---------------------------------------------------
apiVersion: v1
kind: Pod
metadata:
labels:
run: liveness-tcpsocket
name: liveness-tcpsocket
spec:
containers:
- name: healthz-nginx
image: sysnet4admin/healthz-nginx
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 3
periodSeconds: 3
YAML
복사
readinessProbe
•
통신상의 문제 또는 애플리케이션과 연동된 곳에 문제가 있는지 파악한다.
•
문제가 있다면 엔드포인트를 빼고, 다시 복구한다.
apiVersion: v1
kind: Pod
metadata:
labels:
run: readiness-exec
name: readiness-exec
spec:
containers:
- name: tardy-nginx
image: sysnet4admin/tardy-nginx
readinessProbe:
exec:
command:
- cat
- /tmp/healthy-on
initialDelaySeconds: 10
periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
name: readiness-exec-lb
spec:
selector:
run: readiness-exec
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
YAML
복사
init 컨테이너
[root@m-k8s 7.6]# cat pod-initContainers.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-initcontainers
labels:
app: nginx
spec:
containers:
- name: web-page
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html
name: empty-directory
initContainers:
- name: html-builder
image: alpine
volumeMounts:
- mountPath: /html-dir
name: empty-directory
command: ["/bin/sh", "-c"]
args:
- echo "This page created on $(date +%Y-%m-%d) by initContainers" > /html-dir/index.html;
volumes:
- name: empty-directory
emptyDir: {}
# InitContainers는 초기화할때만 사용되므로 READY가 1개이다.
[root@m-k8s 7.6]# k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 5d1h 172.16.221.158 w1-k8s <none> <none>
pod-initcontainers 0/1 PodInitializing 0 9s 172.16.132.52 w3-k8s <none> <none>
[root@m-k8s 7.6]# k get po -o wide w
[root@m-k8s 7.6]# k get po -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 5d1h 172.16.221.158 w1-k8s <none> <none>
pod-initcontainers 1/1 Running 0 19s 172.16.132.52 w3-k8s <none> <none>
YAML
복사
멀티컨테이너 패턴
•
멀티 컨테이너 : 파드 내 여러 개 컨테이너
Sidecar
•
웹 페이지 생성 Nginx
Ambassador
•
컨테이너 프록시 서버
Adapter
•
컨테이너 데이터 변환
nginx-conf.yaml pod-adapter.yaml
[root@m-k8s 7.7]# cat pod-adapter.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-adapter
labels:
app: nginx
spec:
containers:
- name: web-page
image: nginx
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: nginx-conf
- name: adapter
image: nginx/nginx-prometheus-exporter:0.9.0
env:
- name: SCRAPE_URI
value: http://localhost/stub_status
ports:
- containerPort: 9113
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
items:
- key: default.conf
path: default.conf
[root@m-k8s ch7]# k apply -f 7.7
configmap/nginx-conf created
pod/pod-adapter created
[root@m-k8s ch7]# k get po -w
NAME READY STATUS RESTARTS AGE
net 0/1 ImagePullBackOff 0 5d21h
pod-adapter 0/2 ContainerCreating 0 3s
pod-initcontainers 1/1 Running 0 19h
pod-adapter 2/2 Running 0 11s
^C[root@m-k8s ch7]# k get po -wide
Error: unknown shorthand flag: 'i' in -ide
See 'kubectl get --help' for usage.
[root@m-k8s ch7]# k get p -o wide
error: the server doesn't have a resource type "p"
[root@m-k8s ch7]# k get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net 0/1 ImagePullBackOff 0 5d21h 172.16.221.158 w1-k8s <none> <none>
pod-adapter 2/2 Running 0 54s 172.16.132.54 w3-k8s <none> <none>
pod-initcontainers 1/1 Running 0 19h 172.16.103.182 w2-k8s <none> <none>
[root@m-k8s ch7]# curl 172.16.132.54/stub_status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
[root@m-k8s ch7]# curl 172.16.132.54:9113/metrics
# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 3
# HELP nginx_connections_active Active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 1
# HELP nginx_connections_handled Handled client connections
# TYPE nginx_connections_handled counter
nginx_connections_handled 3
# HELP nginx_connections_reading Connections where NGINX is reading the request header
# TYPE nginx_connections_reading gauge
nginx_connections_reading 0
# HELP nginx_connections_waiting Idle client connections
# TYPE nginx_connections_waiting gauge
nginx_connections_waiting 0
# HELP nginx_connections_writing Connections where NGINX is writing the response back to the client
# TYPE nginx_connections_writing gauge
nginx_connections_writing 1
# HELP nginx_http_requests_total Total http requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total 3
# HELP nginx_up Status of the last metric scrape
# TYPE nginx_up gauge
nginx_up 1
# HELP nginxexporter_build_info Exporter build information
# TYPE nginxexporter_build_info gauge
nginxexporter_build_info{commit="5f88afbd906baae02edfbab4f5715e06d88538a0",date="2021-03-22T20:16:09Z",version="0.9.0"} 1
YAML
복사