• K8s--06 K8s数据持久化


    K8s数据持久化

    数据持久化 Volume介绍

    Volume介绍:
    Volume是Pad中能够被多个容器访问的共享目录
    Kubernetes中的Volume不Pad生命周期相同,但不容器的生命周期丌相关
    Kubernetes支持多种类型的Volume,并且一个Pod可以同时使用任意多个Volume
    Volume类型包括:
    - EmptyDir:Pod分配时创建, K8S自动分配,当Pod被移除数据被清空。用于临时空间等。
    - hostPath:为Pod上挂载宿主机目录。用于持久化数据。
    - nfs:挂载相应磁盘资源。
    

    1.EmptyDir实验

    EmptyDir实验
    cat >emptyDir.yaml <<EOF
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-empty
    spec:
      containers:
      - name: busybox-pod
        image: busybox
        volumeMounts:
        - mountPath: /data/busybox/
          name: cache-volume
        command: ["/bin/sh","-c","while true;do echo $(date) >> /data/busybox/index.html;sleep 3;done"]
      volumes:
      - name: cache-volume
        emptyDir: {}
    EOF
    
    
    
    
    
    

    2.hostPath实验

    hostPath实验
    1.发现的问题:
    - 目录必须存在才能创建
    - POD不固定会创建在哪个Node上,数据不统一
    
    2.type类型说明
    https://kubernetes.io/docs/concepts/storage/volumes/#hostpath
    
    DirectoryOrCreate  目录不存在就自动创建
    Directory	   目录必须存在
    FileOrCreate	   文件不存在则创建
    File		   文件必须存在
    
    3.根据Node标签选择POD创建在指定的Node上
    方法1: 直接选择Node节点名称
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-nodename
    spec:
      nodeName: node2
      containers:
      - name: busybox-pod
        image: busybox
        volumeMounts:
        - mountPath: /data/pod/
          name: hostpath-volume
        command: ["/bin/sh","-c","while true;do echo $(date) >> /data/pod/index.html;sleep 3;done"]
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /data/node/
          type: DirectoryOrCreate 
    
    
    方法2: 根据Node标签选择Node节点
    kubectl label nodes node3 disktype=SSD
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-nodename
    spec:
      nodeSelector:
        disktype: SSD
      containers:
      - name: busybox-pod
        image: busybox
        volumeMounts:
        - mountPath: /data/pod/
          name: hostpath-volume
        command: ["/bin/sh","-c","while true;do echo $(date) >> /data/pod/index.html;sleep 3;done"]
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /data/node/
          type: DirectoryOrCreate 
    
    
    4.实验-编写mysql的持久化deployment
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mysql-dp
      namespace: default
    spec:
      selector:
        matchLabels:
          app: mysql 
      replicas: 1
      template: 
        metadata:
          name: mysql-pod
          namespace: default
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql-pod
            image: mysql:5.7 
            ports:
            - name: mysql-port
              containerPort: 3306
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456" 
            volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-volume
          volumes:
          - name: mysql-volume
            hostPath:
              path: /data/mysql
              type: DirectoryOrCreate 
          nodeSelector:
            disktype: SSD
    
    

    3.PV和PVC

    PV和PVC
    1.master节点安装nfs
    yum install nfs-utils -y
    mkdir /data/nfs-volume -p
    vim /etc/exports
    /data/nfs-volume 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
    systemctl start rpcbind
    systemctl start nfs
    showmount -e 127.0.0.1
    
    2.所有node节点安装nfs
    yum install nfs-utils.x86_64 -y
    showmount -e 10.0.0.11
    
    3.编写并创建nfs-pv资源
    cat >nfs-pv.yaml <<EOF
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv01
    spec:
      capacity:
        storage: 5Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      storageClassName: nfs
      nfs:
        path: /data/nfs-volume/mysql
        server: 10.0.0.11
    EOF
    
    kubectl create -f nfs-pv.yaml
    kubectl get persistentvolume
    
    3.创建mysql-pvc
    cat >mysql-pvc.yaml <<EOF
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pvc 
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs
    EOF
    kubectl create -f mysql-pvc.yaml
    kubectl get pvc
    
    4.创建mysql-deployment
    cat >mysql-dp.yaml <<EOF
    apiVersion: apps/v1
    kind: Deployment 
    metadata:
      name: mysql
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - name: mysql
            image: mysql:5.7
            ports:
            - containerPort: 3306
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
            volumeMounts:
            - name: mysql-pvc
              mountPath: /var/lib/mysql
            - name: mysql-log
              mountPath: /var/log/mysql
          volumes:
          - name: mysql-pvc
            persistentVolumeClaim:
              claimName: mysql-pvc
          - name: mysql-log
            hostPath:
              path: /var/log/mysql
          nodeSelector:
            disktype: SSD
    EOF
    
    kubectl create -f mysql-dp.yaml
    kubectl get pod -o wide 
    
    5.测试方法
    1.创建nfs-pv
    2.创建mysql-pvc
    3.创建mysql-deployment并挂载mysq-pvc
    4.登陆到mysql的pod里创建一个数据库
    5.将这个pod删掉,因为deployment设置了副本数,所以会自动再创建一个新的pod
    6.登录这个新的pod,查看刚才创建的数据库是否依然能看到
    7.如果仍然能看到,则说明数据是持久化保存的
    
    6.accessModes字段说明
    ReadWriteOnce 单路读写
    ReadOnlyMany  多路只读
    ReadWriteMany 多路读写
    resources 资源的限制,比如至少5G
    
    7.volumeName精确匹配
    #capacity 限制存储空间大小
    #reclaim policy pv的回收策略
    #retain  pv被解绑后上面的数据仍保留
    #recycle pv上的数据被释放
    #delete  pvc和pv解绑后pv就被删除
    备注:用户在创建pod所需要的存储空间时,前提是必须要有pv存在
    才可以,这样就不符合自动满足用户的需求,而且之前在k8s 9.0
    版本还可删除pv,这样造成数据不安全性
    
  • 相关阅读:
    TVM量化代码解析
    如何在 GPU 上优化卷积
    全文翻译(全文合集):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    全文翻译(四) TVM An Automated End-to-End Optimizing Compiler
    全文翻译(三) TVM An Automated End-to-End Optimizing Compiler
    全文翻译(二): TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    全文翻译(一):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning
    pytorch使用gpu加速的方法
    springboot的实践1
    带你玩转stackOverflow-3
  • 原文地址:https://www.cnblogs.com/gongjingyun123--/p/12543236.html
Copyright © 2020-2023  润新知