kubernetes操作StorageClass、pv、pvc

kubernetes存储创建以及操作

准备工作

 kubectl create ns study

创建StorageClass

#create StorageClass
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: study-class
parameters:
  archiveOnDelete: 'false'
provisioner: study-class
reclaimPolicy: Retain
volumeBindingMode: WaitForFirstConsumer

创建 pv

---
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    pv.kubernetes.io/bound-by-controller: 'yes'
  finalizers:
    - kubernetes.io/pv-protection
  name: study-pv
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 10Gi
  nfs:
    path: /data/share/study
    server: 192.168.200.200
  persistentVolumeReclaimPolicy: Retain
  storageClassName: study-class
  volumeMode: Filesystem

创建 pvc

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: 'yes'
  finalizers:
    - kubernetes.io/pvc-protection
  name: study-pvc
  namespace: study
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  volumeMode: Filesystem
  volumeName: study-pv

操作

#查看pv
kubectl get pv
# 删除单个pv
kubectl delete pv/study-pv
# 全部删除
kubectl get pv | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done
# 根据状态全部删除
kubectl get pv | grep Available | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done
kubectl get pv | grep Released | awk '$1 {print$1}' | while read vol; do kubectl delete pv/${vol}; done

#查看所有pvc
kubectl get pvc --all-namespaces
#查看单个空间下的pvc
kubectl get pvc -n study
# 查看pvc详情
kubectl describe pvc study-pvc -n study | grep Finalizers
# 删除pvc
kubectl delete pvc/study-pvc -n study

使用

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    name: nginx
  name: nginx
  namespace: study

spec:
  replicas: 1
  selector:
    matchLabels:
      name: nginx
      app: nginx
  template:
    metadata:
      labels:
        name: nginx
        app: nginx
    spec:
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - nginx
                topologyKey: kubernetes.io/hostname
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - nginx
                topologyKey: "kubernetes.io/hostname"
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
          imagePullPolicy: IfNotPresent
          env:
            - name: TZ
              value: Asia/Shanghai
          volumeMounts:
            - name: study-pvc-cloud
              mountPath: /var/log/nginx
              # 在pv(/data/share/study/)下可以看到logs/文件
              subPath: logs/

      volumes:
        - name: study-pvc-cloud
          persistentVolumeClaim:
            claimName: study-pvc


---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: study
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
      nodePort: 30001

其它

nfs-server安装和端挂盘

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值