kdeng / my-blogs

Kefeng's blogs
0 stars 0 forks source link

Learning Kubernetes -3 : volumes #17

Open kdeng opened 5 years ago

kdeng commented 5 years ago

There are a lot of volume types in Kubernetes. And I most working with following volume types.

emptyDir

apiVersion: v1
kind: Pod
metadata:
  name: busypod
spec:
  containers:
  - name: write
    image: busybox
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      # mount data volume to /data folder
      - name: data
        mountPath: /data

  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      # mount data volume to /data folder
      - name: data
      mountPath: /data

 # define a volume
  volumes:
  # volume name 'data'
  - name: data
    emptyDir: {}

hostPath

apiVersion: v1
kind: Pod
metadata:
  name: busy-pod
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    # mount data volume to /data in the container
    - name: data
      mountPath: /data
  volumes:
  - name: data
    # mount /tmp directory from host machine to pod
    hostPath:
      path: /tmp
      type: Directory

NFS

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        #启用数据卷的名字为wwwroot,并挂载到nginx的html目录下
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
    #定义数据卷名字为wwwroot,类型为nfs
      volumes:
      - name: wwwroot
        nfs:
          server: 192.168.1.39
          path: /opt/container_data

PV

###
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: busy-pvc
  labels:
    app: busy-app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
###
apiVersion: v1
kind: Pod
metadata:
  name: busypod-pvc
spec:
  containers:
  - name: busypod
    image: busybox
    volumeMounts:
      - name: local-pvc
        mountPath: "/data"
  volumes:
  - name: busy-pvc
    persistentVolumeClaim:
      claimName: local-pvc