k8s持久化存储

作草分茶About 2 min

k8s持久化存储

本文已 Redis 持久化为例。

本地磁盘存储

Redis 配置 ConfigMap

kubectl create configmap redis-cm --from-file=redis.conf

Redis PV 配置

apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-pv
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  # 指定   storageClass
  storageClassName: redis-storage-class
  hostPath:
    path: /k8s/data

Redis PVC 配置

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pvc
spec:
  resources:
    requests:
      storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  # 指定   storageClass
  storageClassName: redis-storage-class

Redis Deployment 配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7
        resources:
          limits:
            memory: "128Mi"
            cpu: "200m"
        ports:
        - containerPort: 6379
        volumeMounts:
          - mountPath: /data
            name: redis-data
      volumes:
        - name: redis-data
          persistentVolumeClaim:
            claimName: redis-pvc

Redis Service 配置

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - name: redis-port
    port: 6379
    targetPort: 6379
    protocol: TCP 
    nodePort: 30379
  type: NodePort

NFS 存储

NFS 服务器安装

  1. 安装 NFS 服务器。
sudo apt install nfs-kernel-server nfs-common
  1. 创建 NFS 存储路径。
mkdir /nfs-data
  1. 修改 NFS 配置,添加如下配置,IP 填自己的 IP。。
$ vim /etc/exports 
/nfs-data 192.168.0.58/24(rw,sync,no_subtree_check,no_root_squash,insecure)
  1. 启动 NFS 服务器。
sudo systemctl start  nfs-server
sudo systemctl enable nfs-server
sudo systemctl status nfs-server

NFS 客户端安装

  1. 安装 NFS 客户端。
sudo apt install nfs-common
  1. 检查 NFS 是否可以正常挂载。
$ showmount -e 192.168.0.58
Export list for 192.168.0.58:
/nfs-data 192.168.0.58/24
  1. 创建一个目录作为挂载点,并完成挂载。
mkdir -p /tmp/test
sudo mount -t nfs 192.168.0.58:/nfs-data /tmp/test
  1. 在挂载点写入文件,测试在 nfs 目录下是否有文件生成。
$ touch /tmp/test/myfile.yml
$ ls /nfs-data
myfile.yml

k8s 使用 NFS 作为 PV 静态存储

Redis NFS PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-nfs-pv
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  # 指定   storageClass
  storageClassName: redis-nfs-storage-class
  nfs:
    path: /nfs-data/redis-data
    server: 192.168.0.58

Redis NFS PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-nfs-pvc
spec:
  resources:
    requests:
      storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  # 指定   storageClass
  storageClassName: redis-nfs-storage-class

Redis NFS Service

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  selector:
    app: redis
  ports:
  - name: redis-port
    port: 6379
    targetPort: 6379
    protocol: TCP 
    nodePort: 30379
  type: NodePort

Redis NFS Deplpyment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: redis:7
          command:
            - "redis-server"
          args:
            - "/conf/redis/redis.conf"
            - "--protected-mode"
            - "no"
          resources:
            limits:
              memory: "128Mi"
              cpu: "200m"
          ports:
            - containerPort: 6379
          volumeMounts:
            - mountPath: /redis-data
              name: redis-data
            - mountPath: /conf/redis
              name: redis-conf
      volumes:
        - name: redis-data
          persistentVolumeClaim:
            claimName: redis-nfs-pvc
        # redis-server 配置
        - name: redis-conf
          configMap:
            name: redis-cm
            items:
              - key: redis.conf
                path: redis.conf

k8s 使用 NFS 作为 PV 动态存储