Kubernetes(8) Volumes

1 emptyDir

emptyDir provide a temporare directory, it comes and disapeares with pod.

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    crazy-chinese.com/created-by: "cluster-admin"
spec:
  containers:
    - name: mypp
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80
      volumeMounts:
        - mountPath: /usr/share/nginx/html/
          name: html
    - name: busybox
      image: busybox:latest
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - mountPath: /data/
          name: html
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo $(date) >> /data/index.html; sleep 2; done"]
  volumes:
    - name: html
      emptyDir: {}

in this example busybox conainer will produce every two sec. a new record in /data/index.html file, which is the same mount point in container myapp under /usr/share/nginx/html/

with curl + pod-ip you can see the record.

2 hostPath

hostPath places a space on the disk of host machine and provide a persistence storage.

I have to worker node: k82node1 and k8snode2. For both nodes I have created new dir named: /root/learning-kubernetes/pod/volumes

and added index.html file with content node1 on k8snode1 and node2 on k8snode2.

Then perform template below:

apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    crazy-chinese.com/created-by: "cluster-admin"
spec:
  containers:
    - name: mypp
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80
      volumeMounts:
        - mountPath: /usr/share/nginx/html/
          name: html
  volumes:
    - name: html
      hostPath:
        path: /root/learning-kubernetes/pod/volumes
        type: DirectoryOrCreate

with curl and cluster-ip you can see the content if index.html

猜你喜欢

转载自www.cnblogs.com/crazy-chinese/p/10404189.html