본문 바로가기
728x90
반응형

테스트 할 환경이 바뀌어서

minikube 로 다시 설치했다.

 

기존꺼 사용해도 똑같음

 

minikube 설치 : https://minikube.sigs.k8s.io/docs/start/ 문서참고

 

[ndh@ndhsotech /u01/ndh]curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 73.1M  100 73.1M    0     0  19.2M      0  0:00:03  0:00:03 --:--:-- 19.2M
[ndh@ndhsotech /u01/ndh]sudo install minikube-linux-amd64 /usr/local/bin/minikube
[sudo] password for ndh: 
[ndh@ndhsotech /u01/ndh]minikube start
* minikube v1.28.0 on Oracle 7.9
* Automatically selected the virtualbox driver. Other choices: none, ssh
* Downloading VM boot image ...
...

[ndh@ndhsotech /u01/ndh]kubectl get nodes
NAME       STATUS   ROLES           AGE   VERSION
minikube   Ready    control-plane   52s   v1.25.3

 

우선 yaml 파일을 이용해 pod를 생성해 늘려보자

[ndh@ndhsotech /u01/ndh/k8s]vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-pod
spec:
  containers:
  - name: my-nginx-container
    image: nginx:latest
    ports:
    - containerPort: 80
      protocol: TCP

[ndh@ndhsotech /u01/ndh/k8s]kubectl apply -f nginx-pod.yaml 
pod/my-nginx-pod created

[ndh@ndhsotech /u01/ndh/k8s]kubectl get pods -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
my-nginx-pod   1/1     Running   0          69s   172.17.0.3   minikube   <none>           <none>

 

yaml 파일 안 metadata 의 name 항목에 입력한대로 생성됨

표시되는 양식은 이전 docker랑 많은 차이는 없다.

exec 명령어를 사용하여 해당 pod bash쉘 안으로 들어가는 것 역시 docker와 비슷하다

 

[ndh@ndhsotech /u01/ndh/k8s]kubectl exec -it my-nginx-pod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@my-nginx-pod:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@my-nginx-pod:/# exit 
exit
[ndh@ndhsotech /u01/ndh/k8s]

 

 

삭제하기

[ndh@ndhsotech /u01/ndh/k8s]kubectl delete -f nginx-pod.yaml 
pod "my-nginx-pod" deleted

또는 kubectl delpet pod my-nginx-pod(pod이름) 으로도 삭제가능

 

 

** 레플리카 셋 **


동일한 목적을 가진 여러 pod를 만들려면?
yaml 안의 name: 을 바꿔가며(pod 이름을 바꿔가며) 여러개를 만들어야 함..

-> replicaset 을 사용하면 정해진 수의 동일한 pod가 항상 실행되도록 관리되며, 노드 장애등으로 pod가 사용할수 없을 경우 다른곳에서 pod를 다시 생성함.
template 기준으로 위는 레플리카셋 정의 아래는 포드 정의

 

[ndh@ndhsotech /u01/ndh/k8s]vi replicaset-nginx.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx-pods-label

  template:
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx-pods-label
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
		
[ndh@ndhsotech /u01/ndh/k8s]kubectl apply -f replicaset-nginx.yaml 
replicaset.apps/replicaset-nginx created

[ndh@ndhsotech /u01/ndh/k8s]kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
replicaset-nginx-bd6p7   1/1     Running   0          24s   172.17.0.5   minikube   <none>           <none>
replicaset-nginx-flznh   1/1     Running   0          24s   172.17.0.3   minikube   <none>           <none>
replicaset-nginx-mlpzh   1/1     Running   0          24s   172.17.0.4   minikube   <none>           <none>
[ndh@ndhsotech /u01/ndh/k8s]kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
replicaset-nginx   3         3         3       39s

 

kubectl get replicasets 또는 rs 로 레플리카셋을 확인할 수 있다.

 

yaml파일에 총 3개가 유지되도록 정의했음

한번 pod한개를 지워 보면

 

[ndh@ndhsotech /u01/ndh/k8s]kubectl delete pod replicaset-nginx-bd6p7
pod "replicaset-nginx-bd6p7" deleted
[ndh@ndhsotech /u01/ndh/k8s]kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
replicaset-nginx-c485m   1/1     Running   0          5s    172.17.0.5   minikube   <none>           <none>
replicaset-nginx-flznh   1/1     Running   0          77s   172.17.0.3   minikube   <none>           <none>
replicaset-nginx-mlpzh   1/1     Running   0          77s   172.17.0.4   minikube   <none>           <none>

bd6p7 대신 c485m 이 자동으로 생겨 3개가 계속 유지되는 것을 확인해볼 수 있다.

 

이 상태에서 옵션값을 (3 -> 4) 로 변경해보면

[ndh@ndhsotech /u01/ndh/k8s]sed -i 's/replicas: 3/replicas: 4/g' replicaset-nginx.yaml 
[ndh@ndhsotech /u01/ndh/k8s]cat replicaset-nginx.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: my-nginx-pods-label

  template:
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx-pods-label
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
[ndh@ndhsotech /u01/ndh/k8s]kubectl apply -f replicaset-nginx.yaml 
replicaset.apps/replicaset-nginx configured
[ndh@ndhsotech /u01/ndh/k8s]kubectl get po
NAME                     READY   STATUS    RESTARTS   AGE
replicaset-nginx-9ks8v   1/1     Running   0          6s
replicaset-nginx-c485m   1/1     Running   0          2m37s
replicaset-nginx-flznh   1/1     Running   0          3m49s
replicaset-nginx-mlpzh   1/1     Running   0          3m49s

sed 명령어로 yaml 파일 내의 레플리카셋을 3개에서 4개로 변경하고 다시 적용해보니 (confiugred 구문을 통해 옵션 변경된 것 확인)

 9ks8v 라는 pod가 새로 생겨 총 4개로 유지되는 것을 확인

 

 

** 디플로이먼트 **

 

디플로이먼트는 레플리카셋 + 버전관리라고 생각됨

 

디플로이먼트 생성

[ndh@ndhsotech /u01/ndh/k8s]vi deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80

 

 

[ndh@ndhsotech /u01/ndh/k8s]kubectl apply -f deployment-nginx.yaml 
deployment.apps/my-nginx-deployment created
[ndh@ndhsotech /u01/ndh/k8s]kubectl get deploy
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
my-nginx-deployment   3/3     3            3           19s

[ndh@ndhsotech /u01/ndh/k8s]kubectl get replicaset
NAME                             DESIRED   CURRENT   READY   AGE
my-nginx-deployment-56b67bfc8b   3         3         3       2m42s
replicaset-nginx                 4         4         4       99m
[ndh@ndhsotech /u01/ndh/k8s]kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
my-nginx-deployment-56b67bfc8b-5c9vc   1/1     Running   0          2m52s
my-nginx-deployment-56b67bfc8b-94br2   1/1     Running   0          2m52s
my-nginx-deployment-56b67bfc8b-cg7pg   1/1     Running   0          2m52s
replicaset-nginx-9ks8v                 1/1     Running   0          96m
replicaset-nginx-c485m                 1/1     Running   0          98m
replicaset-nginx-flznh                 1/1     Running   0          99m
replicaset-nginx-mlpzh                 1/1     Running   0          99m

deployment 3개짜리를 생성하고 현재 pods 를 확인해봤더니

아까 만든 레플리카셋 4개와 디플로이먼트 3개를 확인할 수 있다.

레플리카셋과 다른점은 pod 이름 뒤에 해시값이 붙어있다.

 

 

[ndh@ndhsotech /u01/ndh/k8s]kubectl apply -f deployment-nginx.yaml --record
deployment.apps/my-nginx-deployment configured


[ndh@ndhsotech /u01/ndh/k8s]kubectl set image deployment my-nginx-deployment nginx=nginx:1.11 --record
deployment.apps/my-nginx-deployment image updated


[ndh@ndhsotech /u01/ndh/k8s]kubectl get po
NAME                                   READY   STATUS    RESTARTS   AGE
my-nginx-deployment-7df847cf5f-4r4v2   1/1     Running   0          15s
my-nginx-deployment-7df847cf5f-sn5wm   1/1     Running   0          17s
my-nginx-deployment-7df847cf5f-vhtpd   1/1     Running   0          24s
replicaset-nginx-9ks8v                 1/1     Running   0          99m
replicaset-nginx-c485m                 1/1     Running   0          101m
replicaset-nginx-flznh                 1/1     Running   0          103m
replicaset-nginx-mlpzh                 1/1     Running   0          103m
[ndh@ndhsotech /u01/ndh/k8s]kubectl get replicasets
NAME                             DESIRED   CURRENT   READY   AGE
my-nginx-deployment-56b67bfc8b   0         0         0       6m27s
my-nginx-deployment-7df847cf5f   3         3         3       36s
replicaset-nginx                 4         4         4       103m

--record 옵션으로 기록한 뒤 확인해봄

pod 상태를 보니 업데이트 된 것을 볼 수 있음 (age, pod name에서)

레플리카셋에서 보니 해시값이 새로 생성된걸 볼 수 있음 (56b로 시작하는건 처음만들었던 디플로이먼트 레플리카셋)

 

 

 

+ 다음 실습을 위해 다 지웠음

[ndh@ndhsotech /u01/ndh]kubectl delete deployment,pod,rs --all
deployment.apps "my-nginx-deployment" deleted
pod "my-nginx-deployment-7df847cf5f-4r4v2" deleted
pod "my-nginx-deployment-7df847cf5f-sn5wm" deleted
pod "my-nginx-deployment-7df847cf5f-vhtpd" deleted
pod "replicaset-nginx-9ks8v" deleted
pod "replicaset-nginx-c485m" deleted
pod "replicaset-nginx-flznh" deleted
pod "replicaset-nginx-mlpzh" deleted
replicaset.apps "replicaset-nginx" deleted
728x90
반응형

'IT 공부 > PaaS' 카테고리의 다른 글

[K8S] 2. 파드  (1) 2024.01.11
[K8S] 1. 아키텍처  (1) 2024.01.10
[k8s] 쿠버네티스 설치  (0) 2022.11.02
[Docker] Docker-compose 기본  (1) 2022.10.31
[Docker] 도커 모니터링  (0) 2022.10.31

한걸음 한걸음

개인적인 기록