基于deployment控制器实现redis pod数据NFS持久化

一、安装nfs-server服务端

挂载一块新磁盘,并格式化挂载

parted /dev/vdb mklable xfs
parted /dev/vdb primay 0% 100%
mkfs.xfs /dev/vdb1 
echo "/dev/vdb1 /nfs_share xfs defaults 0 0" >> /etc/fstab
mount -a 

安装nfs服务

apt install nfs-kernel-server -y
echo "/nfs_share 172.16.88.0/24(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports
exportfs -r

二、安装nfs客户端

安装客户端工具

apt-get install nfs-common -y

showmount 验证是否可以查看远程主机nfs

三、在k8s部署redis进行测试

创建pv、pvc

root@easzlab-deploy:~/redis-cluster/redis-k8s# cat redis-pv.yaml 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: redis-datadir-pv-1
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /nfs_share/k8sdata/magedu/redis-datadir-1
    server: 172.16.88.169
root@easzlab-deploy:~/redis-cluster/redis-k8s#
root@easzlab-deploy:~/redis-cluster/redis-k8s# cat redis-pvc.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-datadir-pvc-1
  namespace: magedu
spec:
   volumeName: redis-datadir-pv-1
   accessModes:
      - ReadWriteOnce
   resources:
     requests:
       storage: 10Gi
root@easzlab-deploy:~/redis-cluster/redis-k8s#
root@easzlab-deploy:~/redis-cluster/redis-k8s# kubectl create ns magedu #创建命名空间
namespace/magedu created
root@easzlab-deploy:~/redis-cluster/redis-k8s# kubectl apply -f redis-pvc.yaml -f redis-pv.yaml
persistentvolumeclaim/redis-datadir-pvc-1 created
persistentvolume/redis-datadir-pv-1 created
root@easzlab-deploy:~/redis-cluster/redis-k8s# kubectl get pvc -A
NAMESPACE   NAME                  STATUS   VOLUME               CAPACITY   ACCESS MODES   STORAGECLASS   AGE
magedu      redis-datadir-pvc-1   Bound    redis-datadir-pv-1   10Gi       RWO                           69s
root@easzlab-deploy:~/redis-cluster/redis-k8s# kubectl get pv -A
NAME                 CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                        STORAGECLASS   REASON   AGE
redis-datadir-pv-1   10Gi       RWO            Retain           Bound    magedu/redis-datadir-pvc-1                           9m53s
root@easzlab-deploy:~/redis-cluster/redis-k8s#

编写deployment,部署redis

root@easzlab-deploy:~/redis-cluster/redis-k8s# cat redis-k8s-deployment.yaml 
kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: devops-redis
  name: deploy-devops-redis
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: devops-redis
  template:
    metadata:
      labels:
        app: devops-redis
    spec:
      containers:
        - name: redis-container
          image: harbor.magedu.net/magedu/redis:v4.0.14
          imagePullPolicy: Always
          volumeMounts:
          - mountPath: "/data/redis-data/"
            name: redis-datadir
      volumes:
        - name: redis-datadir
          persistentVolumeClaim:
            claimName: redis-datadir-pvc-1

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: devops-redis
  name: srv-devops-redis
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 6379
    targetPort: 6379
    nodePort: 31379
  selector:
    app: devops-redis
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800
root@easzlab-deploy:~/redis-cluster/redis-k8s#
root@easzlab-deploy:~/redis-cluster/redis-k8s# kubectl apply -f redis-k8s-deployment.yaml 
deployment.apps/deploy-devops-redis created
service/srv-devops-redis created
root@easzlab-deploy:~/redis-cluster/redis-k8s# 

验证redis pv、pvc及磁盘挂载

posted @ 2022-08-20 17:39  cyh00001  阅读(211)  评论(0编辑  收藏  举报