zoukankan      html  css  js  c++  java
  • PV和PVC实现Pod自动迁移、MySQL数据持久化

    MySQL基于PV和PVC实现数据持久化

    演示如何为 MySQL 数据库提供持久化存储,步骤为:

    1. 创建 PV 和 PVC。
    2. 部署 MySQL。
    3. 向 MySQL 添加数据。
    4. 模拟节点宕机故障,Kubernetes 将 MySQL 自动迁移到其他节点。
    5. 验证数据一致性。

    第一步:控制节点部署NFS

    [root@ken1 ~]# cat /etc/exports
    /ken/mysql *(rw,no_root_squash)

    注意:这里必须加入no_root_squash意思是不降低root的权限,不加这个报错如下:

    chown: changing ownership of '/var/lib/mysql/': Operation not permitted

    no_root_squash 访问共享目录时,用户如果是root权限,对共享目录也具有root权限

    root_squash 如果访问共享目录是root的权限用户,对共享目录的权限会被压缩为nfsnobody用户的权

    all_squash 不管你访问共享目录的用户是谁,都必须压缩为nfsnobody用户的权限

    第二步:首先创建 PV 和 PVC,配置如下:

    pv.yml

    [root@ken1 ~]# cat pv.yml 
    apiVersion: v1
    kind: PersistentVolume
    metadata:
    name: mypv
    spec:
    capacity:
    storage: 1Gi
    accessModes:
    - ReadWriteMany
    nfs:
    path: /ken
    server: 192.168.64.11

    pvc.yml

    [root@ken1 ~]# cat pvc.yml 
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: mypvc
    spec:
    volumeName: mypv
    accessModes:
    - ReadWriteMany
    resources:
    requests:
    storage: 1Gi

    第三步:创建pv和pvc

    [root@ken1 ~]# kubectl apply -f pv.yml 
    persistentvolume/mypv unchanged
    [root@ken1 ~]# kubectl get pv
    NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
    mypv 1Gi RWX Retain Available 8d
    [root@ken1 ~]# kubectl apply -f pvc.yml
    persistentvolumeclaim/mypvc created
    [root@ken1 ~]# kubectl get pvc
    NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    mypvc Bound mypv 1Gi RWX 3s

    第四步:接下来部署 MySQL,配置文件如下

    [root@ken1 ~]# cat mysql.yml
    apiVersion: v1
    kind: Service
    metadata:
    name: mysql-svc
    spec:
    type: NodePort
    selector:
    run: mysql
    ports:
    - port: 80
    targetPort: 3306
    NodePort: 3306
    ---
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    name: mysql
    spec:
    template:
    metadata:
    labels:
    run: mysql
    spec:
    containers:
    - name: mysql
    image: mysql
    imagePullPolicy: IfNotPresent
    env:
    - name: MYSQL_ROOT_PASSWORD
    value: ken
    ports:
    - name: mysql
    containerPort: 3306
    volumeMounts:
    - name: mysql
    mountPath: /var/lib/mysql
    volumes:
    - name: mysql
    persistentVolumeClaim:
    claimName: mypvc

    第五步:查看pod

    [root@ken1 ~]# kubectl get po -o wide
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    mysql-587dbd4dd6-79tn5 1/1 Running 0 9m20s 10.244.1.60 ken2 <none> <none>

    第六步:登入数据库创建数据库ken

    [root@ken1 ~]# kubectl exec -it mysql-587dbd4dd6-79tn5 bash
    root@mysql-587dbd4dd6-79tn5:/# mysql -uroot -pken
    mysql> create database ken;
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | ken |
    | mysql |
    | performance_schema |
    | sys |
    +--------------------+
    5 rows in set (0.00 sec)

    第七步:关闭运行该pod的ken2节点,模拟故障

    poweroff

    第八步:一段时间后k8s把pod移动到ken3节点

    [root@ken1 ~]# kubectl get po -o wide -w
    NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
    mysql-587dbd4dd6-79tn5 1/1 Running 0 14m 10.244.1.60 ken2 <none> <none>
    mysql-587dbd4dd6-79tn5 1/1 Terminating 0 18m 10.244.1.60 ken2 <none> <none>
    mysql-587dbd4dd6-gxnpq 0/1 Pending 0 0s <none> <none> <none> <none>
    mysql-587dbd4dd6-gxnpq 0/1 Pending 0 0s <none> ken3 <none> <none>
    mysql-587dbd4dd6-gxnpq 0/1 ContainerCreating 0 0s <none> ken3 <none> <none>
    mysql-587dbd4dd6-gxnpq 1/1 Running 0 3s 10.244.2.139 ken3 <none> <none>

    第九步:在ken3节点登录数据库验证数据一致性

    [root@ken3 ~]# docker exec -it 2aaf1b234ebf bash
    root@mysql-587dbd4dd6-gxnpq:/# mysql -uroot -pken
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    mysql> show databases;
    +--------------------+
    | Database |
    +--------------------+
    | information_schema |
    | ken |
    | mysql |
    | performance_schema |
    | sys |
    +--------------------+
    5 rows in set (0.02 sec)

    MySQL 服务恢复,数据也完好无损。

  • 相关阅读:
    SQL注入过滤
    ASP.NET长文章分页
    简单的权限管理类
    不错的面试题
    【转载】【重要】Ubuntu Linux 下 Ffmpeg 及 Mencoder 安装使用小结
    回到xwindows
    suse11 linux不自动启动xwindows
    flash的几种工具
    mencoder和ffmpeg参数详解
    ffmpeg和Mencoder使用实例小全
  • 原文地址:https://www.cnblogs.com/johnnyblog/p/11442252.html
Copyright © 2011-2022 走看看