본문 바로가기

클라우드/쿠버네티스(Kubernetes)

쿠버네티스 Kubernetes 오브젝트 관리 / 명령형 커맨드 (Imperative Commands)

더보기

# 명령형 커맨드 
YAML 파일을 작성하지 않는다 명령어와 옵션만 사용
가장 단순하게 리소스 오브젝트 만드는법 

# deployment 생성 
# deployment가 replicasets를 만들고 replicasets가 pod를 만든다 
# 사용자가 직접 pod를 만들지 않는다
vagrant@k-control:~$ kubectl create deployment ldh --image=gsfo/c1tfsf7/go-sddf
deployment.apps/ldh created
vagrant@k-control:~$ kubectl get deployments

# 리소스 확인 
vagrant@k-control:~$ kubectl get deployments,replicasets,pods
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/ldh   1/1     1            1           2m1s

NAME                            DESIRED   CURRENT   READY   AGE
replicaset.apps/ldh-5d679d977   1         1         1       2m1s

NAME                      READY   STATUS    RESTARTS   AGE
pod/ldh-5d679d977-d67qc   1/1     Running   0          2m1s
pod/ldh-pod               1/1     Running   1          17h


# 서비스 생성
# 서비스를 생성해 deployment를 노출시킨다
:~$ kubectl expose deployment ldh --port=80 --protocol=TCP --target-port=8080 --name ldh-svc --type=LoadBalancer
                                                     # 서비스 포트             # 백엔드 컨테이너

# 서비스 확인 
# 로드밸런서 ,네트워크 제공 
vagrant@k-control:~$ kubectl get svc
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        41h
ldh-svc      LoadBalancer   10.107.196.188   <pending>     80:32240/TCP   35s
                                              #외부용 ip /pending->안되는것 

# 서비스 접근 
vagrant@k-control:~$ curl http://192.168.200.51:32240
Hello World!
ldh-5d679d977-d67qc

:~/vagrant/k8s$  curl http://192.168.200.51:32240
Hello World!
ldh-5d679d977-d67qc
-> 외부에서도 접근 가능


# 파드 스케일링 
# 복제본 만들기 
vagrant@k-control:~$ kubectl scale deployment ldh --replicas=3
deployment.apps/ldh scaled

# 서비스 요청 시 분산된 접근 확인
vagrant@k-control:~$ curl http://192.168.200.51:32240
Hello World!
ldh-5d679d977-bhhxp
vagrant@k-control:~$ curl http://192.168.200.51:32240
Hello World!
ldh-5d679d977-mh8nz

# 생성된 파드 확인 
vagrant@k-control:~$ kubectl get po
NAME                  READY   STATUS    RESTARTS   AGE
ldh-5d679d977-bhhxp   1/1     Running   0          48s
ldh-5d679d977-d67qc   1/1     Running   0          13m
ldh-5d679d977-mh8nz   1/1     Running   0          48s




# 생성된 파드 삭제 
vagrant@k-control:~$ kubectl delete pod ldh-5d679d977-bhhxp
pod "ldh-5d679d977-bhhxp" deleted

# 삭제 확인 
vagrant@k-control:~$ kubectl get po
NAME                  READY   STATUS    RESTARTS   AGE
ldh-5d679d977-2z2p2   1/1     Running   0          12s
ldh-5d679d977-d67qc   1/1     Running   0          16m
ldh-5d679d977-mh8nz   1/1     Running   0          4m8s

-> 지워졌지만 다른 파드가 생성되었다 
삭제하면 다시 늘어난다 컨트롤이 계속 지켜보고있음
시용자가 복제본을 세개로 구성하라고 선언했기 때문에
하나 지우면 또 하나를 만들어 세개를 맞춰준다 
디플로이먼트를 지우지 않는 한 파드를 디플로이먼트가 관리 -> 정의된대로 관리함
그래서 파드를 지우고싶다? -> 디플로이먼트를 지워줘야한다

 

 

 

더보기

# 디플로이먼트 컨트롤러 삭제
vagrant@k-control:~$ kubectl delete deployments ldh
deployment.apps "ldh" deleted

# 컨트롤러가 삭제되면 pod도 같이 삭제 된다
vagrant@k-control:~$ kubectl get all
NAME                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        42h
service/ldh-svc      LoadBalancer   10.107.196.188   <pending>     80:32240/TCP   38m

# 서비스 삭제 
vagrant@k-control:~$ kubectl delete service ldh-svc
service "ldh-svc" deleted

# 확인 
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   42h
vagrant@k-control:~$