K8S使用hostpath、NFS作为volumes案例

hostpath宿主机文件系统

  • 卷通过将宿主机文件系统上的路径挂载到 Pod 中,使得 Pod 内的容器可以访问该路径

  • 可以实现在宿主节点和 Pod 之间共享数据

  • 可以实现pod访问宿主机上特定的日志文件或数据目录

1.创建一个pod,然后在 pod调度到的节点创建目录:/hostpath-test

bash 复制代码
[root@master23107-volumes]# cat 02-hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: hostpath-pod
spec:
  containers:
    - name: nginx
      image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
      volumeMounts:
        - mountPath: /data
          name: hostpath-volume
  volumes:
    - name: hostpath-volume
      hostPath:
        path: /hostpath-test
        type: Directory

2.进入pod写入数据,然后删除pod。

/ # touch /data/hostpath-test.txt

3.查看worker233节点数据被保留了

bash 复制代码
[root@worker233~]# ls /hostpath-test/
hostpath-test.txt

使用NFS作为volumes

nfs跨节点不同pod共享
bash 复制代码
	1.K8S各节点部署nfs工具
[root@master231 ~]# apt -y install nfs-kernel-server
[root@worker232 ~]# apt -y install nfs-kernel-server
[root@worker233 ~]# apt -y install nfs-kernel-server

	2.配置nfs服务端
[root@master231 ~]# mkdir -pv /oldboyedu/data/nfs-server
mkdir: created directory '/oldboyedu/data'
mkdir: created directory '/oldboyedu/data/nfs-server'
[root@master231 ~]# 
[root@master231 ~]# tail -1 /etc/exports
/oldboyedu/data/nfs-server         *(rw,no_root_squash)
[root@master231 ~]# 
[root@master231 ~]# systemctl restart nfs-server
[root@master231 ~]# 
[root@master231 ~]# exportfs
/oldboyedu/data/nfs-server
		<world>
[root@master231 ~]# 


	3.编写资源清单
[root@master231 pods]# cat 09-pods-volumes-nfs.yaml 
apiVersion: v1
kind: pod
metadata:
  name: xiuxian-nfs-001
spec:
  nodeName: worker232
  volumes:
  - name: data
    # 配置后端存储是nfs
    nfs:
      # 指定nfs server的地址,可以是主机名,前提是hosts文件有解析。
      server: master231
      # 指定nfs的共享目录
      path: /oldboyedu/data/nfs-server
  containers:
  - image: harbor.oldboyedu.com/oldboyedu-web/xiuxian:v2
    name: xiuxian
    volumeMounts:
    - name: data
      mountPath: /xixi

---

apiVersion: v1
kind: pod
metadata:
  name: xiuxian-nfs-002
spec:
  nodeName: worker233
  volumes:
  - name: data
    nfs:
      server: master231
      path: /oldboyedu/data/nfs-server
  containers:
  - image: harbor.oldboyedu.com/oldboyedu-linux/alpine:3.20.2
    name: alpine
    stdin: true
    volumeMounts:
    - name: data
      mountPath: /haha
[root@master231 pods]# 


	4.测试验证 
[root@master231rc]# kubectl exec -it xiuxian-nfs-001 -- sh
/ # ls /xixi/
/ # touch /xixi/nfs-0010-xixi.txt
/ # 

[root@master231k8s-config]# kubectl exec -it xiuxian-nfs-002 -- sh
/ # ls /haha/
nfs-0010-xixi.txt
/ # 

[root@master231k8s-config]# ls /oldboyedu/data/nfs-server/
nfs-0010-xixi.txt
nfs持久存储mysql
bash 复制代码
	0.创建nfs的存储目录
[root@master231 ~]# mkdir -pv /zhiyong18/data/nfs-server/mysql80/
mkdir: created directory '/zhiyong18/data/nfs-server/mysql80/'
[root@master231 ~]# 
[root@master231 ~]# chmod +777 /zhiyong18/data/nfs-server/mysql80/
[root@master231 ~]# 


	1.编写资源清单 
[root@master231 pods]# cat 10-pods-volume-nfs-env-mysql.yaml 
apiVersion: v1
kind: pod
metadata:
  name: mysql-002
  labels:
    apps: mysql80
spec:
  nodeName: worker232
  volumes:
  - name: db
    nfs:
      server: 10.0.0.231
      path: /zhiyong18/data/nfs-server/mysql80
  containers:
  - image: harbor.zhiyong18.com/zhiyong18-db/mysql:8.3.0-oracle
    name: mysql
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: zhiyong18
    - name: MYSQL_DATABASE
      value: wordpress
    - name: MYSQL_USER
      value: zhiyong
    - name: MYSQL_PASSWORD
      value: "123"
    volumeMounts:
    - name: db
      mountPath: /var/lib/mysql
[root@master231 pods]# 
[root@master231 pods]# kubectl get pods -o wide
NAME        READY   STATUS        RESTARTS      AGE    IP            NODE        NOMINATED NODE   READINESS GATES
mysql-002   1/1     Running       1 (26s ago)   42s    10.100.1.39   worker232   <none>           <none>
[root@master231 pods]# 
[root@master231 pods]# kubectl exec mysql-002  -- env 
...
MYSQL_ROOT_PASSWORD=zhiyong18
MYSQL_DATABASE=wordpress
MYSQL_USER=zhiyong
MYSQL_PASSWORD=123
...
[root@master231 pods]# 


	2.测试数据验证
[root@master231 pods]# kubectl exec -it mysql-002  -- mysql -pzhiyong18
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> SELECT user,host FROM mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| zhiyong          | %         |
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

mysql> 
mysql> 
mysql> use wordpress;
Database changed
mysql> 
mysql> SHOW TABLES;
Empty set (0.00 sec)

mysql> 
mysql> 
mysql> CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255) NOT NULL,hobby VARCHAR(255) NOT NULL);
Query OK, 0 rows affected (0.03 sec)

mysql> 
mysql> 
mysql> DESC student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int          | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | NO   |     | NULL    |                |
| hobby | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> INSERT INTO student(name,hobby) VALUE ('HanWenTong','Sleep 10');
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> SELECT * FROM student;
+----+------------+----------+
| id | name       | hobby    |
+----+------------+----------+
|  1 | HanWenTong | Sleep 10 |
+----+------------+----------+
1 row in set (0.00 sec)

mysql> 

3.删除pod调度到另外一个worker节点
[root@master231 pods]# vim 10-pods-volume-nfs-env-mysql.yaml 
[root@master231 pods]# 
[root@master231 pods]# kubectl apply -f 10-pods-volume-nfs-env-mysql.yaml
pod/mysql-002 created
[root@master231 pods]# 
[root@master231 pods]# 
[root@master231 pods]# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
mysql-002   1/1     Running   0          7s    10.100.2.9   worker233   <none>           <none>
[root@master231 pods]# 
[root@master231 pods]# kubectl exec -it mysql-002 -- mysql -pzhiyong18
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> SHOW TABLES FROM wordpress;
+---------------------+
| Tables_in_wordpress |
+---------------------+
| student             |
+---------------------+
1 row in set (0.00 sec)

mysql> 
mysql> SELECT * FROM wordpress.student;
+----+------------+----------+
| id | name       | hobby    |
+----+------------+----------+
|  1 | HanWenTong | Sleep 10 |
+----+------------+----------+
1 row in set (0.00 sec)

mysql>
相关推荐
诡异森林。2 小时前
Docker--Docker Container(容器)
docker·容器·eureka
代码浪人2 小时前
docker 基于Debian镜像安装FreeSwitch1.10.7
docker·容器·debian·freeswitch
qichengzong_right3 小时前
CNCF云原生生态版图-分类指南(一)- 观测和分析
linux·云原生
MavenTalk3 小时前
说说聊聊CNCF(云原生计算基金会)
微服务·云原生·架构·kubernetes·cloud native·cncf
成都 - 阿木木3 小时前
全新的命令行自动化测试框架/运用于云原生/中间件/云计算/混沌测试等场景
云原生·中间件·云计算
LeonNo119 小时前
k8s, deployment
java·容器·kubernetes
真上帝的左手14 小时前
十一、容器化 vs 虚拟化-Docker 命令
docker·容器
我的运维人生14 小时前
Docker实践与应用举例:构建高效开发与部署环境
docker·容器·eureka·运维开发·技术共享
小杨童鞋14 小时前
docker中onlyoffice升级指导
运维·docker·云原生·容器·eureka
一掬净土14 小时前
K8S服务突然中断无法访问:报The node had condition: [DiskPressure]异常
容器·kubernetes·k8s·diskpressure·磁盘压力