侧边栏壁纸
博主头像
进一步,海阔天空 博主等级

进一步,海阔天空

  • 累计撰写 140 篇文章
  • 累计创建 19 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

持久化存储Longhorn的使用

海阔天空
2022-05-28 / 0 评论 / 0 点赞 / 461 阅读 / 0 字

前言

开始这篇文章之前,你必须有一个k3os集群,并安装配置好Longhorn,参考我之前的系列博文。

本文我们将使用Longhorn建立一个高可用存贮,并把MySQL数据库的数据存贮到该Volume中。

建立Volume

在建立Volume之前,我们要在三个Node结点中把我们的硬盘加载进来,如下图所示:

添加磁盘

建立Volume,注意Disk Tag选Disk1,这样Longhorn才会吧存贮调度到我们指定的磁盘中去。

建立Volume

部署Mysql

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql-test
spec:
  selector:
    matchLabels:
      app: mysql-test
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-test
        tier: mysql
    spec:
      containers:
      - image: mysql:8.0.18
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-data
kevinmeng@MacBook-Pro longhorn % kubectl apply -f mysql.yaml -n production
deployment.apps/mysql created

#查看创建的Pod
kevinmeng@MacBook-Pro longhorn % kubectl get pod -A | grep mysql
production        mysql-78f4f57d95-2rnqw                       1/1     Running     0          8m48s

#进入Pod并创建一个数据库。
kevinmeng@MacBook-Pro longhorn % kubectl exec -it mysql-78f4f57d95-2rnqw -n production /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@mysql-78f4f57d95-2rnqw:/# mysql -u root -p mysql
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database db_test;
Query OK, 1 row affected (0.10 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| db_test            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_db            |
+--------------------+
6 rows in set (0.03 sec)

mysql>

测试一下,删除建立的pod,发现系统又自动生成了一个pod,进入后发现数据库还是在的,说明我们的存贮是没有问题的。

0

评论区