美文网首页
k8s-04-持久化存储

k8s-04-持久化存储

作者: UncleZ_strive | 来源:发表于2019-12-12 21:34 被阅读0次

    1.emptyDir类型

    删除容器后数据会保存,但是删除pod后数据丢失

    [root@k8s-master tomcat_demo]# cat mysql-rc.yml 
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: mysql
      namespace: tomcat
    spec:
      replicas: 1
      selector:
        app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          volumes:  *
          - name: mysql  *
            emptyDir: {}  *
          containers:
            - name: mysql
              image: 10.0.0.11:5000/mysql:5.7
              volumeMounts:  *
              - name: mysql  *
                mountPath: /var/lib/mysql  *
              ports:
              - containerPort: 3306
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: '123456'
    

    2. HostPath类型

    删除容器与删除pod,数据都会保存

    [root@k8s-master tomcat_demo]# cat mysql-rc.yml 
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: mysql
      namespace: tomcat
    spec:
      replicas: 1
      selector:
        app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          nodeName: 10.0.0.13  
          volumes:
          - name: mysql
            hostPath:  *
              path: /data/mysql  *
          containers:
            - name: mysql
              image: 10.0.0.11:5000/mysql:5.7
              volumeMounts:  *
              - name: mysql  *
                mountPath: /var/lib/mysql  *
              ports:
              - containerPort: 3306
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: '123456'
    

    3.nfs类型

    [root@k8s-master tomcat_demo]# cat mysql-rc.yml
    apiVersion: v1
    kind: ReplicationController
    metadata:
      name: mysql
      namespace: tomcat
    spec:
      replicas: 1
      selector:
        app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          nodeName: 10.0.0.13
          volumes:
          - name: mysql
            nfs:  *
              path: /data/tomcat-db  *
              server: 10.0.0.11  *
          containers:
            - name: mysql
              image: 10.0.0.11:5000/mysql:5.7
              volumeMounts:  *
              - name: mysql  *
                mountPath: /var/lib/mysql  *
              ports:
              - containerPort: 3306
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: '123456'
    

    4.分布式存储glusterfs

    Glusterfs是一个开源分布式文件系统,具有强大的横向扩展能力,可支持数PB存储容量和数千客户端,通过网络互联成一个并行的网络文件系统。具有可扩展性、高性能、高可用性等特点

    1.安装glusterfs
    • 所有节点:
    yum install  centos-release-gluster -y
    yum install  install glusterfs-server -y
    systemctl start glusterd.service
    systemctl enable glusterd.service
    
    • 为gluster集群增加存储单元brick
    mkdir -p /gfs/test1
    mkdir -p /gfs/test2
    mkdir -p /gfs/test3
    
    2.添加存储资源池
    master节点:
    gluster peer probe k8s-node1
    gluster peer probe k8s-node2
    gluster pool list
    
    3.glusterfs卷管理
    • 创建分布式复制卷
      gluster volume create dddd replica 2 k8s-master:/gfs/test1 k8s-node-1:/gfs/test1 k8s-master:/gfs/test2 k8s-node-1:/gfs/test2 force
    • 启动卷
      gluster volume start dddd
    • 查看卷
      gluster volume info dddd
    • 挂载卷
      mount -t glusterfs 10.0.0.11:/dddd /mnt
    4.添加分布式复制卷

    gluster volume add-brick oldxu replica 2 k8s-master:/gfs/test2 k8s-node-1:/gfs/test2 k8s-node-2:/gfs/test2 force

    5.扩容

    gluster volume add-brick oldxu k8s-master:/gfs/test3 k8s-node-1:/gfs/test3 force

    6.k8s 对接glusterfs存储

    glusterfs对接需要创建endpoints和service

    • 创建endpoints
    vi  glusterfs-ep.yaml
    iapiVersion: v1
    kind: Endpoints
    metadata:
      name: glusterfs
      namespace: tomcat
    subsets:
    - addresses:
      - ip: 10.0.0.11
      - ip: 10.0.0.12
      - ip: 10.0.0.13
      ports:
      - port: 49152
        protocol: TCP
    
    • 创建service
    vi  glusterfs-svc.yaml
    iapiVersion: v1
    kind: Service
    metadata:
      name: glusterfs
      namespace: tomcat
    spec:
      ports:
      - port: 49152
        protocol: TCP
        targetPort: 49152
      sessionAffinity: None
      type: ClusterIP
    

    endpoints和service关联只需要name跟namespace相同

    • 修改mysql的rc文件
      path为创建的卷名


      image.png

    5.k8s映射外部服务

    把外部的服务,通过svc和endpoint映射到k8s内部
    不再创建mysql-rc,创建endpoint链接外端数据库

    • 编写endpoint文件
    [root@k8s-master wordpress]# cat mysql-ep.yaml
    apiVersion: v1
    kind: Endpoints
    metadata:
      name: mysql-wp
      namespace: wordpress
    subsets:
    - addresses:
      - ip: 10.0.0.12
      ports:
      - port: 3306
        protocol: TCP
    
    
    [root@k8s-master wordpress]# cat mysql-svc.yml 
    apiVersion: v1
    kind: Service
    metadata:
      namespace: wordpress
      name: mysql-wp
    spec:
      ports:
        - port: 3306
          targetPort: 3306
    
    
    • 在12节点创建数据库
      yum install -y mariadb-server
      systemctl start mariadb
      create database wordpress;
      grant all on wordpress.* to wordpress@'%' identified by 'wordpress';
    • 启动
      kubectl create namespace wordpress
      kubectl create -f .

    6.pv和pvc

    全局资源:所有namespace都能看见的资源 node、naspace、pv
    局部资源:只属于某一个namespace下的资源 pod、rc、dashdoard、pvc ~ ~ ~
    管理员创建pv,pvc使用资源

    • 创建两个pv
    [root@k8s-master pv_pvc]# cat mysql_pv.yaml 
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv1
      labels:
        type: nfs
    spec:
      capacity:
        storage: 10Gi 
      accessModes:
        - ReadWriteMany 
      persistentVolumeReclaimPolicy: Recycle
      nfs:
        path: "/data/pv1"
        server: 10.0.0.11
    
    
    [root@k8s-master pv_pvc]# cat mysql_pv2.yaml 
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv2
      labels:
        type: nfs
    spec:
      capacity:
        storage: 30Gi 
      accessModes:
        - ReadWriteMany 
      persistentVolumeReclaimPolicy: Recycle
      nfs:
        path: "/data/pv2"
        server: 10.0.0.11
    
    • 创建pvc
    [root@k8s-master pv_pvc]# cat mysql_pvc.yaml 
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pvc1
      namespace: tomcat
    spec:
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 20Gi
    
    创建的pvc为20G,会在pv1上使用
    
    image.png
    • 指定存储类型为pvc
        spec:
          nodeName: 10.0.0.13
          volumes:
          - name: mysql
            persistentVolumeClaim:
              claimName: pvc1
    

    相关文章

      网友评论

          本文标题:k8s-04-持久化存储

          本文链接:https://www.haomeiwen.com/subject/atftnctx.html