Volume

Volume Types

PersistentVolumes

PersistentVolumeClaims

Container Storage Interface (CSI)


Demo

$ minikube ssh _ _ _ _ ( ) ( ) ___ ___ (_) ___ (_)| |/') _ _ | |_ __ /' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\ | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/ (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____) $ ls $ mkdir pod-volume $ ls pod-volume $ cd pod-volume/ $ pwd /home/docker/pod-volume $ exit logout
$ kubectl run --help ... ... --restart='Always': The restart policy for this Pod. Legal values [Always, OnFailure, Never]. If set to 'Always' a deployment is created, if set to 'OnFailure' a job is created, if set to 'Never', a regular pod is created. For the latter two --replicas must be 1. Default 'Always', for CronJobs `Never`.
$ kubectl run share-pod --image=nginx --restart=Never --port=80 --dry-run -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: share-pod name: share-pod spec: containers: - image: nginx name: share-pod ports: - containerPort: 80 resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {}
$ kubectl run share-pod --image=nginx --restart=Never --port=80 --dry-run -o yaml > share-pod.yaml
$ cat share-pod.yaml apiVersion: v1 kind: Pod metadata: labels: run: share-pod name: share-pod spec: volumes: - name: host-volume hostPath: path: /home/docker/pod-volume containers: - image: nginx name: nginx ports: - containerPort: 80 volumeMounts: - mountPath: /usr/share/nginx/html name: host-volume - image: debian name: debian volumeMounts: - mountPath: /host-vol name: host-volume command: ["/bin/sh","-c", "echo Introduction to Kubernetes > /host-vol/index.html; sleep 3600"]
$ kubectl create -f share-pod.yaml pod/share-pod created
$ kubectl get pods NAME READY STATUS RESTARTS AGE share-pod 0/2 ContainerCreating 0 4s $ kubectl get pods NAME READY STATUS RESTARTS AGE share-pod 2/2 Running 0 46s
$ kubectl expose pod share-pod --port=80 --type=NodePort service/share-pod exposed
$ kubectl get services,endpoints NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 41h service/share-pod NodePort 10.110.127.14 <none> 80:31642/TCP 28s NAME ENDPOINTS AGE endpoints/kubernetes 192.168.99.106:8443 41h endpoints/share-pod 172.17.0.6:80 28s
$ minikube service share-pod |-----------|-----------|-------------|-----------------------------| | NAMESPACE | NAME | TARGET PORT | URL | |-----------|-----------|-------------|-----------------------------| | default | share-pod | | http://192.168.99.106:31642 | |-----------|-----------|-------------|-----------------------------| * Opening service default/share-pod in default browser...
$ curl http://192.168.99.106:31642/ Introduction to Kubernetes
$ kubectl delete pod share-pod pod "share-pod" deleted
$ kubectl get pods No resources found in default namespace.
$ kubectl get services,endpoints NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 41h service/share-pod NodePort 10.110.127.14 <none> 80:31642/TCP 6m12s NAME ENDPOINTS AGE endpoints/kubernetes 192.168.99.106:8443 41h endpoints/share-pod <none> 6m12s
$ kubectl run check-pod --image=nginx -l app=share-pod --restart=Never --dry-run -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: app: share-pod name: check-pod spec: containers: - image: nginx name: check-pod resources: {} dnsPolicy: ClusterFirst restartPolicy: Never status: {}
$ kubectl run check-pod --image=nginx -l app=share-pod --restart=Never --dry-run -o yaml > check-pod.yaml
apiVersion: v1 kind: Pod metadata: labels: app: share-pod name: check-pod spec: volumes: - name: check-volume hostPath: path: /home/docker/pod-volume containers: - image: nginx name: nginx ports: - containerPort: 80 volumeMounts: - mountPath: /usr/share/nginx/html name: check-volume
$ kubectl create -f check-pod.yaml pod/check-pod created
$ kubectl get pods NAME READY STATUS RESTARTS AGE check-pod 1/1 Running 0 25s
$ kubectl get services,endpoints NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h service/share-pod NodePort 10.110.127.14 <none> 80:31642/TCP 66m NAME ENDPOINTS AGE endpoints/kubernetes 192.168.99.106:8443 42h endpoints/share-pod <none> 66m
$ kubectl describe services share-pod Name: share-pod Namespace: default Labels: run=share-pod Annotations: <none> Selector: run=share-pod Type: NodePort IP: 10.110.127.14 Port: <unset> 80/TCP TargetPort: 80/TCP NodePort: <unset> 31642/TCP Endpoints: <none> Session Affinity: None External Traffic Policy: Cluster Events: <none>
$ kubectl describe endpoints share-pod Name: share-pod Namespace: default Labels: run=share-pod Annotations: <none> Subsets: Events: <none>
$ kubectl describe pod check-pod Name: check-pod Namespace: default Priority: 0 Node: minikube/192.168.99.106 Start Time: Sat, 29 Feb 2020 13:42:48 +0700 Labels: app=share-pod Annotations: <none> Status: Running IP: 172.17.0.6 ... ...
$ kubectl label pods check-pod run=share-pod pod/check-pod labeled
$ kubectl describe pod check-pod Name: check-pod Namespace: default Priority: 0 Node: minikube/192.168.99.106 Start Time: Sat, 29 Feb 2020 14:30:38 +0700 Labels: app=share-pod run=share-pod Annotations: <none> Status: Running IP: 172.17.0.6 .... ....
$ kubectl get services,endpoints NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42h service/share-pod NodePort 10.110.127.14 <none> 80:31642/TCP 79m NAME ENDPOINTS AGE endpoints/kubernetes 192.168.99.106:8443 42h endpoints/share-pod 172.17.0.6:80 79m
$ curl 192.168.99.106:31642 Introduction to Kubernetes