Kubernetes

쿠버네티스 (ReplicationController 의 생성)

law and security 2024. 9. 30. 14:34
rs : ReplicaSet --> replicas개수만큼 파드가 생성

rc: ReplicationController --> 구형의 리소스

    특징 : labelSelector의 내용과 레이블,
    문제점 :  templete의 이미지가 변경되어도 제대로 생성 안됨

create :  없는 객체를 생성
apply : 없으면 생성, 있으면 업데이트 

 

 

* 레플리케이션컨트롤러 생성

 

[root@k8smaster 03]# cd ~/lab/04
[root@k8smaster 04]# ls
batch-job          guestbook-replicaset-matchexpressions.yaml  ssd-monitor
batch-job.yaml     guestbook-replicaset.yaml                   ssd-monitor-daemonset.yaml
cronjob.yaml       multi-completion-batch-job.yaml
guestbook-rc.yaml  multi-completion-parallel-batch-job.yaml
[root@k8smaster 04]# kubectl create -f guestbook-rc.yaml
replicationcontroller/guestbook created
[root@k8smaster 04]# k get rc
NAME        DESIRED   CURRENT   READY   AGE
guestbook   3         3         3       8s
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2kxdf   1/1     Running   0          16s
guestbook-ck5rz   1/1     Running   0          16s
guestbook-v5dj9   1/1     Running   0          16s

 

 


[root@k8smaster 04]# vi guestbook-rc.yaml    -> 수정

 

-> replicas를 5로 변경

 

# replicas를 5개로 변경
[root@k8smaster 04]# vi guestbook-rc.yaml 

[root@k8smaster 04]# k apply -f guestbook-rc.yaml
Warning: resource replicationcontrollers/guestbook is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
replicationcontroller/guestbook configured

#5개확인 
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2kxdf   1/1     Running   0          5m20s
guestbook-4qs69   1/1     Running   0          11s
guestbook-ck5rz   1/1     Running   0          5m20s
guestbook-sbvpq   1/1     Running   0          11s
guestbook-v5dj9   1/1     Running   0          5m20s

#replicas를 3개로 변경
[root@k8smaster 04]# vi guestbook-rc.yaml
[root@k8smaster 04]# k apply -f guestbook-rc.yaml
replicationcontroller/guestbook configured
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2kxdf   1/1     Running   0          6m14s
guestbook-ck5rz   1/1     Running   0          6m14s
guestbook-v5dj9   1/1     Running   0          6m14s
[root@k8smaster 04]#

 

*  image변경 했을 때

 

 

 

 

[root@k8smaster 04]# vi guestbook-rc.yaml
[root@k8smaster 04]# k apply -f guestbook-rc.yaml
replicationcontroller/guestbook configured
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2kxdf   1/1     Running   0          6m14s
guestbook-ck5rz   1/1     Running   0          6m14s
guestbook-v5dj9   1/1     Running   0          6m14s

# replicas는 5개로 변경, image는 choichoiyj로 변경

[root@k8smaster 04]# vi guestbook-rc.yaml
[root@k8smaster 04]# k apply -f guestbook-rc.yaml
replicationcontroller/guestbook configured
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2kxdf   1/1     Running   0          15m
guestbook-ck5rz   1/1     Running   0          15m
guestbook-n8wlb   1/1     Running   0          6s
guestbook-qgh8r   1/1     Running   0          6s
guestbook-v5dj9   1/1     Running   0          15m
[root@k8smaster 04]# k describe rc guestbook
Name:         guestbook
Namespace:    default
Selector:     app=guestbook
Labels:       app=guestbook
Annotations:  <none>
Replicas:     5 current / 5 desired
Pods Status:  5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=guestbook
  Containers:
   guestbook:
    Image:        choichoiyj/k8s_guestbook:1.0
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                    Message
  ----    ------            ----   ----                    -------
  Normal  SuccessfulCreate  15m    replication-controller  Created pod: guestbook-2kxdf
  Normal  SuccessfulCreate  15m    replication-controller  Created pod: guestbook-v5dj9
  Normal  SuccessfulCreate  15m    replication-controller  Created pod: guestbook-ck5rz
  Normal  SuccessfulCreate  10m    replication-controller  Created pod: guestbook-4qs69
  Normal  SuccessfulCreate  10m    replication-controller  Created pod: guestbook-sbvpq
  Normal  SuccessfulDelete  9m27s  replication-controller  Deleted pod: guestbook-4qs69
  Normal  SuccessfulDelete  9m27s  replication-controller  Deleted pod: guestbook-sbvpq
  Normal  SuccessfulCreate  43s    replication-controller  Created pod: guestbook-n8wlb
  Normal  SuccessfulCreate  43s    replication-controller  Created pod: guestbook-qgh8r


[root@k8smaster 04]# k get pods guestbook-2kxdf -o yaml

 

[root@k8smaster 04]# k get pods guestbook-2sbf2 -o yaml  -> image가 변경되지 않았을 때 

 

# pod삭제 후  image 확인 

[root@k8smaster 04]# k delete pods --all
pod "guestbook-2kxdf" deleted
pod "guestbook-ck5rz" deleted
pod "guestbook-n8wlb" deleted
pod "guestbook-qgh8r" deleted
pod "guestbook-v5dj9" deleted
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          7s
guestbook-8bb94   1/1     Running   0          7s
guestbook-9s6dh   1/1     Running   0          6s
guestbook-ps95z   1/1     Running   0          7s
guestbook-qxrpm   1/1     Running   0          7s
[root@k8smaster 04]# k describe rc guestbook
Name:         guestbook
Namespace:    default
Selector:     app=guestbook
Labels:       app=guestbook
Annotations:  <none>
Replicas:     5 current / 5 desired
Pods Status:  5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=guestbook
  Containers:
   guestbook:
    Image:        choichoiyj/k8s_guestbook:1.0
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  55m   replication-controller  Created pod: guestbook-2kxdf
  Normal  SuccessfulCreate  55m   replication-controller  Created pod: guestbook-v5dj9
  Normal  SuccessfulCreate  55m   replication-controller  Created pod: guestbook-ck5rz
  Normal  SuccessfulCreate  49m   replication-controller  Created pod: guestbook-4qs69
  Normal  SuccessfulCreate  49m   replication-controller  Created pod: guestbook-sbvpq
  Normal  SuccessfulDelete  48m   replication-controller  Deleted pod: guestbook-4qs69
  Normal  SuccessfulDelete  48m   replication-controller  Deleted pod: guestbook-sbvpq
  Normal  SuccessfulCreate  40m   replication-controller  Created pod: guestbook-n8wlb
  Normal  SuccessfulCreate  40m   replication-controller  Created pod: guestbook-qgh8r
  Normal  SuccessfulCreate  21s   replication-controller  Created pod: guestbook-ps95z
  Normal  SuccessfulCreate  21s   replication-controller  Created pod: guestbook-2sbf2
  Normal  SuccessfulCreate  20s   replication-controller  Created pod: guestbook-qxrpm
  Normal  SuccessfulCreate  20s   replication-controller  Created pod: guestbook-8bb94
  Normal  SuccessfulCreate  20s   replication-controller  Created pod: guestbook-9s6dh

[root@k8smaster 04]# k get pods guestbook-2sbf2 -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: 9eee6e57155f033b0cca9a7b761668e8534e43c9036cfd211070db266cbc5171
    cni.projectcalico.org/podIP: 10.102.249.48/32
    cni.projectcalico.org/podIPs: 10.102.249.48/32
  creationTimestamp: "2024-09-30T06:28:08Z"
  generateName: guestbook-
  labels:
    app: guestbook
  name: guestbook-2sbf2
  namespace: default
  ownerReferences:
  - apiVersion: v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicationController
    name: guestbook
    uid: 43ecca11-3b56-4757-9b8e-d6b42e3d526a
  resourceVersion: "151633"
  uid: 8e683406-a457-4167-9b51-6bcca267242f
spec:
  containers:
  - image: choichoiyj/k8s_guestbook:1.0
    imagePullPolicy: IfNotPresent
    name: guestbook
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-66j4b
      readOnly: true

 

ReplicationController 삭제   : RC를 삭제하면 관리되던 POD도 함께 삭제

[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          2m18s
guestbook-8bb94   1/1     Running   0          2m18s
guestbook-9s6dh   1/1     Running   0          2m17s
guestbook-ps95z   1/1     Running   0          2m18s
guestbook-qxrpm   1/1     Running   0          2m18s

#특정 Pod에 레이블을 추가하거나 기존 레이블을 수정
[root@k8smaster 04]# k label po guestbook-2sbf2 app=guest --overwrite
pod/guestbook-2sbf2 labeled

#모든 Pod의 상태와 함께 각 Pod에 추가된 레이블을 조회
[root@k8smaster 04]# k get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE    LABELS
guestbook-2sbf2   1/1     Running   0          3m8s   app=guest
guestbook-8bb94   1/1     Running   0          3m8s   app=guestbook
guestbook-9s6dh   1/1     Running   0          3m7s   app=guestbook
guestbook-b9bkp   1/1     Running   0          12s    app=guestbook
guestbook-ps95z   1/1     Running   0          3m8s   app=guestbook
guestbook-qxrpm   1/1     Running   0          3m8s   app=guestbook

[root@k8smaster 04]# k scale rc guestbook --replicas=3
replicationcontroller/guestbook scaled
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          5m58s
guestbook-8bb94   1/1     Running   0          5m58s
guestbook-ps95z   1/1     Running   0          5m58s
guestbook-qxrpm   1/1     Running   0          5m58s

# RC를 삭제하면관리되던POD도함께삭제

[root@k8smaster 04]# k delete rc guestbook   
replicationcontroller "guestbook" deleted
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          7m49s
[root@k8smaster 04]# k create -f guestbook-rc.yaml
replicationcontroller/guestbook created
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          8m28s
guestbook-5xrvq   1/1     Running   0          6s
guestbook-7xrr8   1/1     Running   0          6s
guestbook-b7khk   1/1     Running   0          6s
guestbook-dh8lk   1/1     Running   0          6s
guestbook-tgvkx   1/1     Running   0          6s

[root@k8smaster 04]# k delete rc guestbook --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
replicationcontroller "guestbook" deleted
[root@k8smaster 04]# k get rc
No resources found in default namespace.
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-2sbf2   1/1     Running   0          10m
guestbook-5xrvq   1/1     Running   0          2m6s
guestbook-7xrr8   1/1     Running   0          2m6s
guestbook-b7khk   1/1     Running   0          2m6s
guestbook-dh8lk   1/1     Running   0          2m6s
guestbook-tgvkx   1/1     Running   0          2m6s

#지금까지 생성한 쿠버네티스리소스 모두 삭제

[root@k8smaster 04]# k delete all --all
pod "guestbook-2sbf2" deleted
pod "guestbook-5xrvq" deleted
pod "guestbook-7xrr8" deleted
pod "guestbook-b7khk" deleted
pod "guestbook-dh8lk" deleted
pod "guestbook-tgvkx" deleted
service "kubernetes" deleted

[root@k8smaster 04]# k get pods
No resources found in default namespace.
[root@k8smaster 04]# k get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   53s

 

ReplicaSet생성

 

[root@k8smaster 04]# k create -f guestbook-replicaset.yaml
replicaset.apps/guestbook created

[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-47czn   1/1     Running   0          11s
guestbook-fxctd   1/1     Running   0          11s
guestbook-rj6vw   1/1     Running   0          11s

[root@k8smaster 04]# k delete pod --all
pod "guestbook-47czn" deleted
pod "guestbook-fxctd" deleted
pod "guestbook-rj6vw" deleted
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-g6fdr   1/1     Running   0          6s
guestbook-hndgm   1/1     Running   0          6s
guestbook-l24b8   1/1     Running   0          6s

#relicaset image : choichoiyj으로 변경

[root@k8smaster 04]# vi guestbook-replicaset.yaml

[root@k8smaster 04]# k delete po --all
pod "guestbook-g6fdr" deleted
pod "guestbook-hndgm" deleted
pod "guestbook-l24b8" deleted
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-5qwhp   1/1     Running   0          8s
guestbook-79gzn   1/1     Running   0          8s
guestbook-lpsj4   1/1     Running   0          8s
[root@k8smaster 04]# k describe po guestbook-5qwhp
Name:             guestbook-5qwhp
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8snode1/192.168.56.101
Start Time:       Mon, 30 Sep 2024 15:49:05 +0900
Labels:           app=guestbook
Annotations:      cni.projectcalico.org/containerID: 1eec1c6b8d9b381313c74641a1ce608358f134a97c834b4ac98d004fe508dfbb
                  cni.projectcalico.org/podIP: 10.102.249.55/32
                  cni.projectcalico.org/podIPs: 10.102.249.55/32
Status:           Running
IP:               10.102.249.55
IPs:
  IP:           10.102.249.55
Controlled By:  ReplicaSet/guestbook
Containers:
  guestbook:
    Container ID:   containerd://ab91a6ee3ffc7ef621b0b13e6a4413f18065b377c1e33f3168b54069ee022a30
    Image:          choichoiyj/k8s_guestbook:1.0
    Image ID:       docker.io/choichoiyj/k8s_guestbook@sha256:90a2e1344a475dd003e515f7d0dfb3ba20784493f92e4a13d4fcc754f9a3c398
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 30 Sep 2024 15:49:06 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-w449w (ro)
      
  # label생성, 삭제
  
  [root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-5qwhp   1/1     Running   0          2m8s
guestbook-79gzn   1/1     Running   0          2m8s
guestbook-lpsj4   1/1     Running   0          2m8s
[root@k8smaster 04]# k label po guestbook-5qwhp app=tree --overwrite
pod/guestbook-5qwhp labeled
[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-58d4x   1/1     Running   0          7s
guestbook-5qwhp   1/1     Running   0          2m37s
guestbook-79gzn   1/1     Running   0          2m37s
guestbook-lpsj4   1/1     Running   0          2m37s

[root@k8smaster 04]# k get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE    LABELS
guestbook-58d4x   1/1     Running   0          31s    app=guestbook
guestbook-5qwhp   1/1     Running   0          3m1s   app=tree
guestbook-79gzn   1/1     Running   0          3m1s   app=guestbook
guestbook-lpsj4   1/1     Running   0          3m1s   app=guestbook
[root@k8smaster 04]# k delete pods -l app=tree
pod "guestbook-5qwhp" deleted

[root@k8smaster 04]# k get pods
NAME              READY   STATUS    RESTARTS   AGE
guestbook-58d4x   1/1     Running   0          58s
guestbook-79gzn   1/1     Running   0          3m28s
guestbook-lpsj4   1/1     Running   0          3m28s