본문 바로가기

* 리눅스

[kubernetes] 쿠버네티스 워크로드 - 파드(pods)

반응형

워크로드 - 파드(pods)

myapp-pod-nginx.yaml 파일 작성

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod-nginx
  labels:
    name: myapp-pod-nginx
spec:
  containers:
  - name: myapp-pod-nginx
    image: nginx:latest
    resources:
      limits:
        memory: "128Mi"
        cpu: "500m"
    ports:
      - containerPort: 80
        protocol: TCP

파드 생성

kubectl create -f myapp-pod-nginx.yaml
$ kubectl create -f myapp-pod-nginx.yaml
pod/myapp-pod-nginx created

파드 목록 확인

kubectl get pods
$ kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
myapp-pod-nginx   1/1     Running   0          30s

실행 중인 파트 정의 확인

kubectl get pods myapp-pod-nginx -o json
$ kubectl get pods myapp-pod-nginx -o json
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "annotations": {
            "cni.projectcalico.org/containerID": "7931c7ca7162af1d342abcf8fd0af8c5a51bf7aab8c464859b064bcf8b33285e",
            "cni.projectcalico.org/podIP": "10.233.74.6/32",
            "cni.projectcalico.org/podIPs": "10.233.74.6/32"
        },
        "creationTimestamp": "2022-10-03T06:22:13Z",
        "labels": {
            "name": "myapp-pod-nginx"
        },
        "name": "myapp-pod-nginx",
        "namespace": "default",
        "resourceVersion": "346436",
        "uid": "a59d83c8-141d-4dcb-9cb0-8f2ebd04b9aa"
    },
    "spec": {
        "containers": [
            {
                "image": "nginx:latest",
                "imagePullPolicy": "Always",
                "name": "myapp-pod-nginx",
                "ports": [
                    {
                        "containerPort": 80,
                        "protocol": "TCP"
                    }
                ],
                "resources": {
                    "limits": {
                        "cpu": "500m",
                        "memory": "128Mi"
                    },
                    "requests": {
                        "cpu": "500m",
                        "memory": "128Mi"
                    }
                },
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "volumeMounts": [
                    {
                        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                        "name": "kube-api-access-td8ss",
                        "readOnly": true
                    }
                ]
            }
        ],
...
        "hostIP": "192.168.56.22",
        "phase": "Running",
        "podIP": "10.233.74.6",
        "podIPs": [
            {
                "ip": "10.233.74.6"
            }
        ],
        "qosClass": "Guaranteed",
        "startTime": "2022-10-03T06:22:13Z"
    }
}

파드의 자세한 정보 확인

kubectl describe pods myapp-pod-nginx
$ kubectl describe pods myapp-pod-nginx
Name:         myapp-pod-nginx
Namespace:    default
Priority:     0
Node:         kube-node2/192.168.56.22
Start Time:   Mon, 03 Oct 2022 15:22:13 +0900
Labels:       name=myapp-pod-nginx
Annotations:  cni.projectcalico.org/containerID: 7931c7ca7162af1d342abcf8fd0af8c5a51bf7aab8c464859b064bcf8b33285e
              cni.projectcalico.org/podIP: 10.233.74.6/32
              cni.projectcalico.org/podIPs: 10.233.74.6/32
Status:       Running
IP:           10.233.74.6
IPs:
  IP:  10.233.74.6
Containers:
  myapp-pod-nginx:
    Container ID:   docker://0c8accc8978ffabe31fe2c7be23a0b5c042c4bccaadc5eb0d28b599c5b35a274
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 03 Oct 2022 15:22:18 +0900
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     500m
      memory:  128Mi
    Requests:
      cpu:        500m
      memory:     128Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-td8ss (ro)
...
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  3m32s  default-scheduler  Successfully assigned default/myapp-pod-nginx to kube-node2
  Normal  Pulling    3m30s  kubelet            Pulling image "nginx:latest"
  Normal  Pulled     3m28s  kubelet            Successfully pulled image "nginx:latest" in 2.528384769s
  Normal  Created    3m28s  kubelet            Created container myapp-pod-nginx
  Normal  Started    3m27s  kubelet            Started container myapp-pod-nginx

파드 로그 확인

kubectl logs myapp-pod-nginx
$ kubectl logs myapp-pod-nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/10/03 06:22:18 [notice] 1#1: using the "epoll" event method
2022/10/03 06:22:18 [notice] 1#1: nginx/1.23.1
2022/10/03 06:22:18 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/10/03 06:22:18 [notice] 1#1: OS: Linux 5.4.0-126-generic
2022/10/03 06:22:18 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/10/03 06:22:18 [notice] 1#1: start worker processes
2022/10/03 06:22:18 [notice] 1#1: start worker process 30

파드 포트 포워딩

kubectl port-forward myapp-pod-nginx 8080:80
$ kubectl port-forward myapp-pod-nginx 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80

curl 테스트

$ curl -I localhost:8080
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Mon, 03 Oct 2022 06:43:35 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

kubectl edit pods myapp-pod-nginx
kubectl delete pods myapp-pod-nginx

레이블 및 셀렉터

myapp-pod-nginx.yaml 파일 수정

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod-nginx
  labels:
    name: myapp-pod-nginx
    env: dev
    tier: frontend
spec:
  containers:
  - name: myapp-pod-nginx
    image: nginx:latest
    resources:
      limits:
        memory: "128Mi"
        cpu: "500m"
    ports:
      - containerPort: 80
        protocol: TCP
kubectl create -f myapp-pod-nginx.yaml

파드 레이블 확인

-L, --label-columns=[]: Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag options like -L label1 -L label2...
kubectl get pods --show-labels
$ kubectl get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE   LABELS
myapp-pod-nginx   1/1     Running   0          16s   env=dev,name=myapp-pod-nginx,tier=frontend

-L, --label-columns=[] 옵션을 사용하여 특정 레이블을 지정하여 필드로 표시할 수 있다

kubectl get pods -L env,tier
$ kubectl get pods -L env,tier
NAME              READY   STATUS    RESTARTS   AGE     ENV   TIER
myapp-pod-nginx   1/1     Running   0          2m33s   dev   frontend

파드 레이블 수정

 - 레이블 추가

kubectl label pod myapp-pod-nginx owner=kadmin
$ kubectl label pod myapp-pod-nginx owner=kadmin
pod/myapp-pod-nginx labeled

 - 레이블 변경

$ kubectl label pod myapp-pod-nginx env=prod
error: 'env' already has a value (dev), and --overwrite is false
kubectl label pod myapp-pod-nginx env=prod --overwrite
$ kubectl label pod myapp-pod-nginx env=prod --overwrite
pod/myapp-pod-nginx labeled

레이블 셀렉터

-l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching objects must satisfy all of the specified label constraints
  • 일치성 기준(Equality-based) 요건
    • =
    • ==
    • !=
  • 집합성 기준(Set-based) 요건
    • in
    • notin
    • exists

-l, --selector=''

일치성 기준 레이블 셀렉터

env 키가 포함되어 있는 레이블

kubectl get pods --show-labels -l 'env'
$ kubectl get pods --show-labels -l 'env'
NAME              READY   STATUS    RESTARTS   AGE   LABELS
myapp-pod-nginx   1/1     Running   0          37m   env=prod,name=myapp-pod-nginx,owner=kadmin,tier=frontend

집합성 기준 레이블 셀렉터

env 키에 debug 또는 prod 값이 포함되어 있는 레이블

kubectl get pods --show-labels -l 'env in (debug,prod)'
$ kubectl get pods --show-labels -l 'env in (debug,prod)'
NAME              READY   STATUS    RESTARTS   AGE   LABELS
myapp-pod-nginx   1/1     Running   0          46m   env=prod,name=myapp-pod-nginx,owner=kadmin,tier=frontend

어노테이션(annotate)

kubectl describe pods myapp-pod-nginx
$ kubectl describe pods myapp-pod-nginx
Name:         myapp-pod-nginx
Namespace:    default
Priority:     0
Node:         kube-node2/192.168.56.22
Start Time:   Mon, 03 Oct 2022 15:50:09 +0900
Labels:       env=prod
              name=myapp-pod-nginx
              owner=kadmin
              tier=frontend
Annotations:  cni.projectcalico.org/containerID: c41b9e40a681f3ef4fe2716d6e3615079b877bfed65e2ea38ab12cf65c01a0cb
              cni.projectcalico.org/podIP: 10.233.74.7/32
              cni.projectcalico.org/podIPs: 10.233.74.7/32
Status:       Running
IP:           10.233.74.7
IPs:
  IP:  10.233.74.7
Containers:
  myapp-pod-nginx:
...
kubectl annotate pods myapp-pod-nginx domain="sangchul.kr"
$ kubectl describe pods myapp-pod-nginx
Name:         myapp-pod-nginx
Namespace:    default
Priority:     0
Node:         kube-node2/192.168.56.22
Start Time:   Mon, 03 Oct 2022 15:50:09 +0900
Labels:       env=prod
              name=myapp-pod-nginx
              owner=kadmin
              tier=frontend
Annotations:  cni.projectcalico.org/containerID: c41b9e40a681f3ef4fe2716d6e3615079b877bfed65e2ea38ab12cf65c01a0cb
              cni.projectcalico.org/podIP: 10.233.74.7/32
              cni.projectcalico.org/podIPs: 10.233.74.7/32
              domain: sangchul.kr
Status:       Running
IP:           10.233.74.7
IPs:
  IP:  10.233.74.7
Containers:
  myapp-pod-nginx:
...