본문 바로가기

리눅스

[kubernetes] 쿠버네티스 볼륨(Volume) - hostPath 볼륨

728x90

쿠버네티스 hostPath 볼륨

hostPath 볼륨

hostPath 볼륨 생성

hostPath1.yaml 파일 작성

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-rs-hostpath
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp-rs-hostpath
  template:
    metadata:
      labels:
        app: myapp-rs-hostpath
    spec:
      containers:
        - name: web-server
          image: nginx:alpine
          volumeMounts:
            - name: web-content
              mountPath: /usr/share/nginx/html
              readOnly: true
          ports:
            - containerPort: 80
              protocol: TCP

      volumes:
        - name: web-content
          hostPath:
            path: /web-contents
            type: Directory

hostPath1-svc.yaml 파일 작성

apiVersion: v1
kind: Service
metadata:
  name: myapp-rs-hostpath
spec:
  selector:
    app: myapp-rs-hostpath
  ports:
  - port: 80
    targetPort: 80

 

$ kubectl get all
NAME                          READY   STATUS              RESTARTS   AGE
pod/myapp-rs-hostpath-5v267   0/1     ContainerCreating   0          31s
pod/myapp-rs-hostpath-lfxsx   0/1     ContainerCreating   0          31s

NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes          ClusterIP   10.233.0.1     <none>        443/TCP   4d18h
service/myapp-rs-hostpath   ClusterIP   10.233.1.237   <none>        80/TCP    7s

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-rs-hostpath   2         2         0       31s

 

$ kubectl describe pods myapp-rs-hostpath
Name:           myapp-rs-hostpath-5v267
Namespace:      default
Priority:       0
Node:           kube-node3/192.168.56.23
Start Time:     Wed, 12 Oct 2022 16:48:16 +0900
Labels:         app=myapp-rs-hostpath
Annotations:    <none>
Status:         Pending
IP:
IPs:            <none>
Controlled By:  ReplicaSet/myapp-rs-hostpath
Containers:
  web-server:
    Container ID:
    Image:          nginx:alpine
    Image ID:
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from web-content (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qw99c (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  web-content:
    Type:          HostPath (bare host directory volume)
    Path:          /web-contents
    HostPathType:  Directory
  kube-api-access-qw99c:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason       Age                From               Message
  ----     ------       ----               ----               -------
  Normal   Scheduled    51s                default-scheduler  Successfully assigned default/myapp-rs-hostpath-5v267 to kube-node3
  Warning  FailedMount  19s (x7 over 51s)  kubelet            MountVolume.SetUp failed for volume "web-content" : hostPath type check failed: /web-contents is not a directory
  ...

 

노트에 디렉터리 생성(kube-node1, kube-node2, kube-node3 각각 생성)

sudo mkdir /web-contents

 

$ kubectl get all
NAME                          READY   STATUS    RESTARTS   AGE
pod/myapp-rs-hostpath-5v267   1/1     Running   0          27m
pod/myapp-rs-hostpath-lfxsx   1/1     Running   0          27m

NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes          ClusterIP   10.233.0.1     <none>        443/TCP   4d18h
service/myapp-rs-hostpath   ClusterIP   10.233.1.237   <none>        80/TCP    7s

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.apps/myapp-rs-hostpath   2         2         2       27m

 

$ kubectl describe pods myapp-rs-hostpath-5v267
Name:         myapp-rs-hostpath-5v267
Namespace:    default
Priority:     0
Node:         kube-node3/192.168.56.23
Start Time:   Wed, 12 Oct 2022 16:48:16 +0900
Labels:       app=myapp-rs-hostpath
Annotations:  cni.projectcalico.org/containerID: a95f7b70f595982a9938f1f9a12c9800973cb2259f01b0394864e84fc778c872
              cni.projectcalico.org/podIP: 10.233.118.73/32
              cni.projectcalico.org/podIPs: 10.233.118.73/32
Status:       Running
IP:           10.233.118.73
IPs:
  IP:           10.233.118.73
Controlled By:  ReplicaSet/myapp-rs-hostpath
Containers:
  web-server:
    Container ID:   docker://09a67f262b8f5052cbc97a33f40d5a956d8dc1fc44ec555c34284a64ee88391b
    Image:          nginx:alpine
    Image ID:       docker-pullable://nginx@sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9e9b5f1c74
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 12 Oct 2022 16:54:38 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /usr/share/nginx/html from web-content (ro)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qw99c (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  web-content:
    Type:          HostPath (bare host directory volume)
    Path:          /web-contents
    HostPathType:  Directory
  kube-api-access-qw99c:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason       Age                     From               Message
  ----     ------       ----                    ----               -------
  Normal   Scheduled    7m37s                   default-scheduler  Successfully assigned default/myapp-rs-hostpath-5v267 to kube-node3
  Warning  FailedMount  3m27s (x10 over 7m37s)  kubelet            MountVolume.SetUp failed for volume "web-content" : hostPath type check failed: /web-contents is not a directory
  Warning  FailedMount  3m16s (x2 over 5m34s)   kubelet            Unable to attach or mount volumes: unmounted volumes=[web-content], unattached volumes=[web-content kube-api-access-qw99c]: timed out waiting for the condition
  Normal   Pulling      83s                     kubelet            Pulling image "nginx:alpine"
  Normal   Pulled       75s                     kubelet            Successfully pulled image "nginx:alpine" in 7.781412538s
  Normal   Created      75s                     kubelet            Created container web-server
  Normal   Started      75s                     kubelet            Started container web-server

 

kube-node3에서만 index.html 파일 생성

sudo touch /web-contents/index.html
echo $HOSTNAME | sudo tee /web-contents/index.html

 

$ kubectl exec myapp-rs-hostpath-5v267 -- cat /usr/share/nginx/html/index.html
kube-node3

 

$ curl 10.233.1.237
kube-node2
$ curl 10.233.1.237
kube-node3
728x90