2021. 4. 3. 21:51ใ๐ฏ OpenSource/K8S
โป ๋ณธ ๊ฒ์๊ธ์ ๋์ <์ฟ ๋ฒ๋คํฐ์ค ์ธ ์ก์ >์ ํ ๋๋ก ์ ๋ฆฌ/ํ์ตํ ๋ด์ฉ์ ๋๋ค.
Pod, Deployment, Service ๋ฑ๊ณผ ๊ฐ์ ์ฟ ๋ฒ๋คํฐ์ค ๋ฆฌ์์ค๋ ์ผ๋ฐ์ ์ผ๋ก ์ฟ ๋ฒ๋คํฐ์ค REST API ์๋ํฌ์ธํธ์ ์์ฑํ๋ ค๋ ๋ฆฌ์์ค์ ๋ช ์ธ์๋ฅผ ์ ์กํ์ฌ ์์ฑํ๋ค.
์ด๋ ๋ช ์ธ์๋ yaml, json ๊ณผ ๊ฐ์ ํ์ผ ํ์์ ๊ฐ์ง๋ค.
3.2.1 ๊ธฐ์กด pod์ yaml ๋์คํฌ๋ฆฝํฐ
-o yaml ์ต์ ์ผ๋ก pod์ ๋ํ yaml ์ ์๋ฅผ ํ์ธ ํ ์ ์๋ค.
root@master001:~# kubectl get po kubia-manual -o yaml
yaml ์ ์๋ฅผ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ด์ฉ์ ํ์ธ ๊ฐ๋ฅํ๋ค.
root@master001:~# kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1
root@master001:~# kubectl get po kubia -o yaml
apiVersion: v1 # ํด๋น object๋ฅผ ์์ฑํ๊ธฐ ์ํด ์ฌ์ฉํ๋ kubernetes api ๋ฒ์
kind: Pod # ์์ฑํ object์ ์ ํ
metadata: # object์ ์ด๋ฆ, uid, namespace, label ๋ฑ์ ๋ํ metadata
annotations:
cni.projectcalico.org/podIP: 172.30.254.29/32
cni.projectcalico.org/podIPs: 172.30.254.29/32
creationTimestamp: "2021-04-03T13:14:06Z"
labels:
run: kubia
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:labels:
.: {}
f:run: {}
f:spec:
f:containers:
k:{"name":"kubia"}:
.: {}
f:image: {}
f:imagePullPolicy: {}
f:name: {}
f:ports:
.: {}
k:{"containerPort":8080,"protocol":"TCP"}:
.: {}
f:containerPort: {}
f:protocol: {}
f:resources: {}
f:terminationMessagePath: {}
f:terminationMessagePolicy: {}
f:dnsPolicy: {}
f:enableServiceLinks: {}
f:restartPolicy: {}
f:schedulerName: {}
f:securityContext: {}
f:terminationGracePeriodSeconds: {}
manager: kubectl-run
operation: Update
time: "2021-04-03T13:14:06Z"
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:cni.projectcalico.org/podIP: {}
f:cni.projectcalico.org/podIPs: {}
manager: calico
operation: Update
time: "2021-04-03T13:14:07Z"
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:status:
f:conditions:
k:{"type":"ContainersReady"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:status: {}
f:type: {}
k:{"type":"Initialized"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:status: {}
f:type: {}
k:{"type":"Ready"}:
.: {}
f:lastProbeTime: {}
f:lastTransitionTime: {}
f:status: {}
f:type: {}
f:containerStatuses: {}
f:hostIP: {}
f:phase: {}
f:podIP: {}
f:podIPs:
.: {}
k:{"ip":"172.30.254.29"}:
.: {}
f:ip: {}
f:startTime: {}
manager: kubelet
operation: Update
time: "2021-04-03T13:14:42Z"
name: kubia
namespace: default
resourceVersion: "6920138"
uid: 73cae115-aeb9-4618-804d-38c08ed4e8cf
spec: # ํด๋น ๋ฆฌ์์ค์ ๋ํ ์ ์/๋ด์ฉ (pod์ ์ํ container ์ ๋ณด, volume ๋ฑ)
containers:
- image: luksa/kubia
imagePullPolicy: Always
name: kubia
ports:
- containerPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-xgl85
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: worker002
preemptionPolicy: PreemptLowerPriority
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-xgl85
secret:
defaultMode: 420
secretName: default-token-xgl85
status: # pod์ ๊ทธ ์์ container์ ์์ธํ ์ํ
conditions:
- lastProbeTime: null
lastTransitionTime: "2021-04-03T13:14:06Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2021-04-03T13:14:42Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2021-04-03T13:14:42Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2021-04-03T13:14:06Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://7fd367dc382a19a2628ee52c5a8975f9d04071aec4f88fec2a1f6b4c76333d20
image: luksa/kubia:latest
imageID: docker-pullable://luksa/kubia@sha256:3f28e304dc0f63dc30f273a4202096f0fa0d08510bd2ee7e1032ce600616de24
lastState: {}
name: kubia
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2021-04-03T13:14:41Z"
hostIP: 10.10.11.21
phase: Running
podIP: 172.30.254.29
podIPs:
- ip: 172.30.254.29
qosClass: BestEffort
startTime: "2021-04-03T13:14:06Z"
- Metadata : ์ด๋ฆ, namespace, label, pod ์ ๊ดํ ๊ธฐํ ์ ๋ณด
- Spec : pod ์์ฒด์ ๋ํ ์ค์ ๋ช ์ธ์ด๋ฉฐ, k8s cluster์์ ์ ์งํ ์ํ์ ๋ํ ๋ด์ฉ์ด๋ค. (pod container, volume, ๊ธฐํ ๋ฐ์ดํฐ ๋ฑ)
- Status : ํ์ฌ ์คํ ์ค์ธ pod์ ๋ํ ํ์ฌ ์ ๋ณด (Pod์ ์ํ, ๊ฐ container ์ค๋ช ๊ณผ ์ํ, pod ๋ด๋ถ ip, ๊ธฐํ ์ ๋ณด ๋ฑ)
Status์ ๊ฒฝ์ฐ object์ ์์ฑ ๋ช ๋ น์ด ๋ด๋ ค์ง ์ดํ์ ๋ฆฌ์์ค ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
๋ฐ๋ผ์ ์๋ก์ด ๋ฆฌ์์ค๋ฅผ ๋ง๋ค๋ status ๋ถ๋ถ์ ์์ฑํ ํ์๊ฐ ์๋ค.
3.2.2 Pod๋ฅผ ์ ์ํ๋ ๊ฐ๋จํ YAML ์์ฑํ๊ธฐ
yaml ํ์ผ์ ์์ฑํ์ฌ ์๋ก์ด pod๋ฅผ ๋ง๋ค์ด๋ณด๋๋ก ํ๋ค.
root@master001:~/k8s_in_action/03_pod# cat kubia-manual.yaml
apiVersion: v1
kind: Pod
metadata:
name: kubia-manual
spec:
containers:
- image: luksa/kubia
name: kubia
ports:
- containerPort: 8080
protocol: TCP
- apiVersion : ๋ฆฌ์์ค๋ฅผ ๋ง๋๋ฉด์ ์ฌ์ฉํ API ๋ฒ์ .
- kind : Pod ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ๊ฒ ๋ค๊ณ ์ ์
- metadata : pod์ ์ด๋ฆ ์ ์
- spec/containers/image : ์ปจํ ์ด๋ ์ด๋ฏธ์ง
- spec/containers/ports : ์ ํ๋ฆฌ์ผ์ด์ ์ด ์์ ํ๋ ํฌํธ
๐น ์ปจํ ์ด๋ ํฌํธ ์ง์
yaml ํ์ผ์์ port๋ฅผ ์ง์ ํ์ง ์์๋ ๋ค๋ฅธ pod๋ค๊ณผ์ ํต์ ์ ๊ฐ๋ฅํ๋ค.
ํ์ง๋ง ๋ช ์ํจ์ผ๋ก์จ ํด๋น pod๊ฐ ์ด๋ค port๋ก ๋ ธ์ถ๋์ด ์๋์ง ๋น ๋ฅด๊ฒ ํ์ธ ๊ฐ๋ฅํ๋ค.
๐น kubernetes API ์ค๋ธ์ ํธ ํ๋ ํ์ธํ๊ธฐ
object ์ ์์ฑ ํ์ธ
root@master001:~/k8s_in_action/03_pod# kubectl explain pods
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
๊ฐ ์์ฑ์ ๋ํ ์ธ๋ถ ์ ๋ณด ํ์ธ
root@master001:~/k8s_in_action/03_pod# kubectl explain pod.metadata
KIND: Pod
VERSION: v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard objects metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
clusterName <string>
The name of the cluster which the object belongs to. This is used to
distinguish resources with same name and namespace in different clusters.
This field is not set anywhere right now and apiserver is going to ignore
it if set in create or update request.
... ์๋ต
3.2.3 [ kubectl create ] Pod ์์ฑํ๊ธฐ
๋ค์๊ณผ ๊ฐ์ด yaml , json ๋ช ์ธ์๋ก ๋ฆฌ์์ค๋ฅผ ์์ฑํ ์ ์๋ค.
root@master001:~/k8s_in_action/03_pod# kubectl create -f kubia-manual.yaml
pod/kubia-manual created
root@master001:~/k8s_in_action/03_pod# kubectl get po kubia-manual
NAME READY STATUS RESTARTS AGE
kubia-manual 1/1 Running 0 22s
์คํ ์ค์ธ pod์ ์ ์ฒด ์ ์ ํ์ธํ๊ธฐ
์ด๋ -o ์ต์ ์ yaml/json์ผ๋ก ์ง์ ํ ์ ์๋ค.
root@master001:~/k8s_in_action/03_pod# kubectl get po kubia-manual -o yaml
3.2.4 Application log ๋ณด๊ธฐ
container๋ก ๋์ํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ก๊ทธ๋ฅผ ํ์ค ์ถ๋ ฅ, ํ์ค ์๋ฌ๋ก ๋จ๊ธด๋ค.
์ปจํ ์ด๋์ ๋ก๊ทธ๋ ๋ค์๊ณผ ๊ฐ์ด ํ์ธํ๋ค.
root@master001:~/k8s_in_action/03_pod# kubectl logs kubia-manual
Kubia server starting...
pod ์์ -c ์ต์ ์ผ๋ก ํน์ container์ ๋ก๊ทธ๋ง ๋ณผ ์๋ ์๋ค.
root@master001:~# kubectl logs kubia-manual -c kubia
Kubia server starting...
์ด๋ pod๊ฐ ์ญ์ ๋๋ฉด ๋ก๊ทธ๋ ์ญ์ ๋๋ค. pod๊ฐ ์ญ์ ๋ ํ์๋ ๋ก๊ทธ๋ฅผ ๋ณด๊ธฐ ์ํด์๋ ์ค์์ง์ค์ ๋ก๊น ์ ์ค์ ํ์ฌ ๋ชจ๋ ๋ก๊ทธ๋ฅผ ์ค์ ์ ์ฅ์์ ์ ์ฅํ๋๋ก ํด์ผํ๋ค.
3.2.5 Pod์ ์์ฒญ ๋ณด๋ด๊ธฐ
port forwarding์ ํ์ฌ pod์ ์์ฒญ์ ๋ณด๋ด ๋ณด๋๋ก ํ๋ค.
๐น ๋ก์ปฌ ๋คํธ์ํฌ ํฌํธ๋ฅผ pod์ ํฌํธ๋ก ํฌ์๋ฉ ํ๊ธฐ
service๋ฅผ ์ด์ฉํ์ง ์๊ณ ํน์ pod์ ํต์ ํ๊ธฐ ์ํด์ ๋ก์ปฌ node์ ํฌํธ์ pod์ ํฌํธ๋ฅผ ๋งคํํ์ฌ ํฌํธ ํฌ์๋ฉํ ์ ์๋ค. ๋ค์๊ณผ ๊ฐ์ด ์ค์ ํ๋ฉด ๋ก์ปฌ ๋จธ์ ์ 8888 port๋ก ๋ค์ด์ค๋ ์์ฒญ์ ํด๋น pod์ 8080 port๋ก ํฅํ๊ฒ ํ๋ค.
root@master001:~# kubectl port-forward kubia-manual 8888:8080 &
[1] 2949791
root@master001:~# curl localhost:8888
Handling connection for 8888
You ve hit kubia-manual
root@master001:~# kubectl logs kubia-manual
Kubia server starting...
Received request from ::ffff:127.0.0.1
'๐ฏ OpenSource > K8S' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
06. liveness probe (0) | 2021.04.05 |
---|---|
05. Pod ์ค์ง, ์ ๊ฑฐ (0) | 2021.04.04 |
04. Namespace๋ฅผ ์ด์ฉํ ๋ฆฌ์์ค ๊ทธ๋ฃนํ (0) | 2021.04.04 |
03. Label์ ์ด์ฉํ Pod ๊ตฌ์ฑ (0) | 2021.04.04 |
01. Pod_์ฟ ๋ฒ๋คํฐ์ค์์ ์ปจํ ์ด๋ ์คํํ๊ธฐ (0) | 2021.03.27 |