kmg28801 / kubernetes-study

2 stars 0 forks source link

Chapter 10. 스테이트풀셋: 복제된 스테이트풀 애플리케이션 배포하기 #11

Open kmg28801 opened 1 year ago

kmg28801 commented 1 year ago

GCP 퍼시스턴트 볼륨 생성 명령어

세 개의 퍼시스턴트 볼륨 생성 yaml

kind: List      # 세 개 퍼시스턴트볼륨 생성하기위해 kind: List로 설정
apiVersion: v1
items:
- apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: pv-a
  spec:
    capacity:
      storage: 1Mi # 각 퍼시스턴트 볼륨 용량은 1MB
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle # 클레임에서 볼륨이 해제되면 다시 사용해 재사용
    gcePersistentDisk:
      pdName: pv-a
      fsType: nfs4
- apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: pv-b
  spec:
    capacity:
      storage: 1Mi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    gcePersistentDisk:
      pdName: pv-b
      fsType: nfs4
- apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: pv-c
  spec:
    capacity:
      storage: 1Mi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    gcePersistentDisk:
      pdName: pv-c
      fsType: nfs4

거버닝 서비스 생성 yaml

apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  clusterIP: None
  ports:
  - name: http
    port: 80
  selector:
    app: kubia

스테이트풀셋 생성 yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kubia
spec:
  serviceName: kubia
  replicas: 2
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      labels:
        app: kubia
    spec:
      containers:
      - name: kubia
        image: luksa/kubia-pet
        ports:
        - name: http
          containerPort: 8080
        volumeMounts:
        - name: data
          mountPath: /var/data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Mi
volumes:
  - name: data
    persistentVolumeClaim:
      claimName: data-kubia-1
kubectl get pvc
NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-kubia-0   Bound    pvc-75ab5343-2026-4c6b-9671-671ccd107b92   1Gi        RWO            standard-rwo   6m58s
data-kubia-1   Bound    pvc-b9079d28-9a6b-4585-9bbe-f341a90e4ef5   1Gi        RWO            standard-rwo   6m34s

파드 가지고 놀기

kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
You've hit kubia-0
Data stored on this pod: No data posted yet
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
You've hit kubia-0
Data stored on this pod: No data posted yet
kmg48801@cloudshell:~ (kubers-368504)$
kmg48801@cloudshell:~ (kubers-368504)$
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "Hey There! This greeting was submitted to kubia-0." localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
Data stored on pod kubia-0
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "Hey There! This greeting was submitted to kubia-0." localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
Data stored on pod kubia-0
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
You've hit kubia-0
Data stored on this pod: Hey There! This greeting was submitted to kubia-0.
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-1/proxy/
You've hit kubia-1
Data stored on this pod: No data posted yet
kmg48801@cloudshell:~ (kubers-368504)$ kubectl delete po kubia-0
pod "kubia-0" deleted
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-1/proxy/
You've hit kubia-1
Data stored on this pod: No data posted yet
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/pods/kubia-0/proxy/
You've hit kubia-0
Data stored on this pod: Hey There! This greeting was submitted to kubia-0.
kmg48801@cloudshell:~ (kubers-368504)$

헤드리스 서비스의 DNS SRC 레코드 조회

kmg48801@cloudshell:~ (kubers-368504)$ kubectl run -it srvlookup --image=tutum/dnsutils --rm --restart=Never -- dig SRC kubia.default.svc.cluster.local
Warning: Autopilot set default resource requests for Pod default/srvlookup, as resource requests were not specified. See http://g.co/gke/autopilot-defaults.

; <<>> DiG 9.9.5-3ubuntu0.2-Ubuntu <<>> SRC kubia.default.svc.cluster.local
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 27895
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;SRC.                           IN      A

;; AUTHORITY SECTION:
.                       30      IN      SOA     a.root-servers.net. nstld.verisign-grs.com. 2023011400 1800 900 604800 86400

;; Query time: 95 msec
;; SERVER: 10.1.0.10#53(10.1.0.10)
;; WHEN: Sat Jan 14 09:02:55 UTC 2023
;; MSG SIZE  rcvd: 107

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35932
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;kubia.default.svc.cluster.local. IN    A

;; ANSWER SECTION:
kubia.default.svc.cluster.local. 30 IN  A       10.0.128.90
kubia.default.svc.cluster.local. 30 IN  A       10.0.128.91
kubia.default.svc.cluster.local. 30 IN  A       10.0.128.92

;; Query time: 3 msec
;; SERVER: 10.1.0.10#53(10.1.0.10)
;; WHEN: Sat Jan 14 09:02:55 UTC 2023
;; MSG SIZE  rcvd: 201

pod "srvlookup" deleted

스테이트풀 파드에 접근하기 위한 일반적인 서비스 생성 yaml

apiVersion: v1
kind: Service
metadata:
  name: kubia-public
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: kubia
kmg48801@cloudshell:~ (kubers-368504)$curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy//
Data stored on pod kubia-0
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-1
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-1
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-2
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-2
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The sun is shining" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-1
kmg48801@cloudshell:~ (kubers-368504)$ curl -X POST -d "The weather is sweet" localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
Data stored on pod kubia-1
kmg48801@cloudshell:~ (kubers-368504)$ curl localhost:8001/api/v1/namespaces/default/services/kubia-public/proxy/
You've hit kubia-1
Data stored in the cluster:
- kubia-1.kubia.default.svc.cluster.local: The weather is sweet
- kubia-0.kubia.default.svc.cluster.local: The sun is shining
- kubia-2.kubia.default.svc.cluster.local: The sun is shining
kmg28801 commented 1 year ago

https://suyounglee.notion.site/10-2b71bc8b0c87449981bd7d61d4fa18d9