Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

쟝이의 세상

Kubernetes(label, namespace) 본문

수업내용

Kubernetes(label, namespace)

zyangee 2024. 9. 27. 16:47

📌 Label

💡 label
: 각 아이템마다 붙여진 속성, 한 객체당 원하는 만큼의 label을 붙일 수 있다.

✔️pod 설정 yaml 파일
apiVersion: v1
kind: Pod
metadata:
    name: simple-webapp
    labels:
        key1: value1
        key2: value2     
# label은 원하는 만큼 작성 가능

spec:
    containers:
    - name: [컨테이너이름지정]
      image: [이미지]
      ports:
        - containerPort: 8080

 

✔️ yaml 파일을 통한 label 속성 설정

[root@k8smaster ~]# cd lab
[root@k8smaster lab]# cd 03
[root@k8smaster 03]# ls
custom-namespace.yaml                        guestbook-liveness-probe.yaml           guestbook-manual.yaml
guestbook-gpu.yaml                           guestbook-manual-custom-namespace.yaml
guestbook-liveness-probe-initial-delay.yaml  guestbook-manual-with-labels.yaml
[root@k8smaster 03]# kubectl create -f guestbook-manual-with-labels.yaml
[root@k8smaster 03]# kubectl get pods --show-labels
NAME                        READY   STATUS        RESTARTS   AGE     LABELS
guestbook-manual            1/1     Terminating   0          3h55m   <none>
guestbook-manual-v2         0/1     Pending       0          27s     creation_method=manual,env=prod

guestbook-manual-v2 이름의 파드가 생성된 것을 볼 수 있고, 뒤에 label 속성이 같이 생성된 것을 알 수 있다.

(yaml 파일의 내용에 의해 생성된 것임)

👇🏻 guestbook-manual-with-labels.yaml 파일 확인

더보기
더보기

guestbook-manual-with-labels.yaml

apiVersion: v1
kind: Pod
metadata:
  name: guestbook-manual-v2
  labels:
    creation_method: manual
    env: prod
spec:
  containers:
  - image: yu3papa/k8s_guestbook:1.0
    name: guestbook
    ports:
    - containerPort: 8080
      protocol: TCP

✔️ 수동으로 label 속성 설정

[root@k8smaster 03]# kubectl label pod guestbook-manual creation_method=manual env=prod
pod/guestbook-manual labeled
[root@k8smaster 03]# kubectl get pods --show-labels
NAME                        READY   STATUS        RESTARTS   AGE     LABELS
guestbook-manual            1/1     Terminating   0          3h59m   creation_method=manual,env=prod
guestbook-manual-v2         0/1     Pending       0          4m31s   creation_method=manual,env=prod

명령어로 guestbook-manual 파드에 label을 수동으로 설정

 

✔️ replicaSet과 label의 관계

[root@k8smaster 04]# kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2qd45   1/1     Running   0          56m
guestbook-79qj5   1/1     Running   0          56m
guestbook-8rkp8   1/1     Running   0          41m
guestbook-cc2sm   1/1     Running   0          56m
guestbook-dv6gc   1/1     Running   0          41m
[root@k8smaster 04]# kubectl label po guestbook-2qd45 app=guest --overwrite
pod/guestbook-2qd45 labeled

👇🏻 결과 출력

더보기
더보기
[root@k8smaster 04]# k get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE   LABELS
guestbook-2qd45   1/1     Running   0          57m   app=guest
guestbook-79qj5   1/1     Running   0          57m   app=guestbook
guestbook-8rkp8   1/1     Running   0          42m   app=guestbook
guestbook-cc2sm   1/1     Running   0          57m   app=guestbook
guestbook-dv6gc   1/1     Running   0          42m   app=guestbook
guestbook-k6q7b   1/1     Running   0          8s    app=guestbook

replicaSet은 label을 보고 만든다는 것을 확인할 수 있다.

 

✔️ label을 통해 pod 삭제

[root@k8smaster 04]# k delete pods -l app=tree
pod "guestbook-2z6lc" deleted

👇🏻 결과 출력

더보기
더보기
[root@k8smaster 04]# k get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE     LABELS
guestbook-7r2zp   1/1     Running   0          26s     app=guestbook
guestbook-cfd2h   1/1     Running   0          5m52s   app=guestbook
guestbook-g9mxr   1/1     Running   0          7m23s   app=guestbook
guestbook-jc4cw   1/1     Running   0          7m23s   app=guestbook
guestbook-m4257   1/1     Running   0          7m23s   app=guestbook

 

📌 Namespace

💡 Namespace
: kubernetes에서 생성해 온 object들
(pod, deployment, service들은 클러스터 내 namespace 안에 구성됨)

✔️ namespace 생성

kubectl create namespace [네임스페이스 이름]​

 

✔️ namespace 설정된 pod 확인해보기

[root@k8smaster 03]# kubectl get namespaces
NAME                   STATUS   AGE
default                Active   276d
kube-node-lease        Active   276d
kube-public            Active   276d
kube-system            Active   276d
kubernetes-dashboard   Active   45h

[root@k8smaster 03]# kubectl get pod --namespace=kube-system -o wide
NAME                                       READY   STATUS        RESTARTS       AGE    IP               NODE        NOMINATED NODE   READINESS GATES
calico-kube-controllers-7c968b5878-8w2m5   1/1     Running       3 (6h3m ago)   276d   10.98.16.140     k8smaster   <none>           <none>
calico-node-2q72n                          0/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
calico-node-dhs52                          1/1     Running       7 (6h3m ago)   276d   192.168.56.101   k8snode1    <none>           <none>
coredns-5dd5756b68-lxcrj                   1/1     Running       3 (6h3m ago)   276d   10.98.16.139     k8smaster   <none>           <none>
coredns-5dd5756b68-wbs6p                   1/1     Running       3 (6h3m ago)   276d   10.98.16.138     k8smaster   <none>           <none>
etcd-k8smaster                             1/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
kube-apiserver-k8smaster                   1/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
kube-controller-manager-k8smaster          1/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
kube-proxy-j4dsf                           1/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
kube-proxy-s59th                           1/1     Running       7 (6h3m ago)   276d   192.168.56.101   k8snode1    <none>           <none>
kube-scheduler-k8smaster                   1/1     Running       4 (6h3m ago)   276d   192.168.56.100   k8smaster   <none>           <none>
metrics-server-6d857ffdc9-4jpk7            1/1     Terminating   1 (6h3m ago)   45h    10.102.249.11    k8snode1    <none>           <none>
metrics-server-6d857ffdc9-wlzdl            0/1     Pending       0              46m    <none>           <none>      <none>           <none>
metrics-server-75bf97fcc9-tlhvm            0/1     Terminating   1 (6h3m ago)   45h    10.102.249.14    k8snode1    <none>           <none>
metrics-server-75bf97fcc9-x74lh            0/1     Pending       0              46m    <none>           <none>      <none>           <none>

 

✔️ yaml 파일을 통한 namespace 생성

[root@k8smaster 03]# kubectl create -f custom-namespace.yaml
namespace/custom-namespace created
[root@k8smaster 03]# kubectl get ns
NAME                   STATUS   AGE
custom-namespace       Active   5s
default                Active   276d
kube-node-lease        Active   276d
kube-public            Active   276d
kube-system            Active   276d
kubernetes-dashboard   Active   45h

 

👇🏻 custom-namespace.yaml 파일 확인

더보기
더보기

custom-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: custom-namespace

✔️ yaml 파일을 통한 pod 생성(namespace 설정)

[root@k8smaster 03]# kubectl create -f guestbook-manual.yaml -n custom-namespace
pod/guestbook-manual created
[root@k8smaster 03]# kubectl get pods -n custom-namespace
NAME               READY   STATUS    RESTARTS   AGE
guestbook-manual   0/1     Pending   0          17s

👇🏻 guestbook-manual.yaml 파일 확인

더보기
더보기

guestbook-manual.yaml

apiVersion: v1
kind: Pod
metadata:
  name: guestbook-manual
spec:
  containers:
  - image: yu3papa/k8s_guestbook:1.0
    name: guestbook
    ports:
    - containerPort: 8080
      protocol: TCP

 

📌 Namespace 실습

미션1.
네임스페이스 생성: shopping-mall
deployment로 파드 생성
 : guestbook  --> 조회 --> 복제본 3개 --> 조회 --> 복제본 개수를 5개로 조정 --> 조회 --> service 생성(guestbook-http)
[root@k8smaster 03]# kubectl create namespace shopping-mall
namespace/shopping-mall created
[root@k8smaster 03]# kubectl get namespace
NAME                   STATUS   AGE
custom-namespace       Active   21m
default                Active   276d
kube-node-lease        Active   276d
kube-public            Active   276d
kube-system            Active   276d
kubernetes-dashboard   Active   45h
shopping-mall          Active   9s
[root@k8smaster 03]# docker images
REPOSITORY              TAG      IMAGE ID        CREATED         PLATFORM       SIZE         BLOB SIZE
ubuntu                  24.04    dfc10878be8d    2 days ago      linux/amd64    81.0 MiB     28.4 MiB
yu3papa/mysql_hangul    2.0      887eb3c63fd4    9 months ago    linux/amd64    359.1 MiB    116.9 MiB
zyange/k8s_guestbook    1.0      2851c231a65e    2 days ago      linux/amd64    554.4 MiB    246.5 MiB
[root@k8smaster 03]# kubectl create deployment guestbook --image=zyange/k8s_guestbook:1.0 --port=8080 -n shopping-mall
deployment.apps/guestbook created
[root@k8smaster 03]# kubectl get deployment -n shopping-mall
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
guestbook   0/1     1            0           19s
[root@k8smaster 03]# kubectl scale deployment guestbook --replicas=3 -n shopping-mall
deployment.apps/guestbook scaled
[root@k8smaster 03]# kubectl get deployment -n shopping-mall                                                    NAME        READY   UP-TO-DATE   AVAILABLE   AGE
guestbook   0/3     3            0           9m1s
[root@k8smaster 03]# kubectl scale deployment guestbook --replicas=5 -n shopping-mall
deployment.apps/guestbook scaled
[root@k8smaster 03]# kubectl get deployment -n shopping-mall
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
guestbook   0/5     5            0           9m9s
[root@k8smaster 03]# kubectl expose deployment guestbook --type=LoadBalancer --name guestbook-http -n shopping-mall
service/guestbook-http exposed
[root@k8smaster 03]# kubectl get service -n shopping-mall
NAME             TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
guestbook-http   LoadBalancer   10.104.235.83   <pending>     8080:31167/TCP   4s

'수업내용' 카테고리의 다른 글

Kubernetes(yaml 파일 수정, service)  (0) 2024.09.30
Kubernetes 실습  (0) 2024.09.27
Kubernetes(POD, Deployment, ReplicaSet, Service 생성)  (1) 2024.09.27
쿠버네티스 개념 정리  (1) 2024.09.25
OWASP ZAP  (2) 2024.09.11