欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > K8S使用hostpath、NFS作为volumes案例

K8S使用hostpath、NFS作为volumes案例

2025/2/23 7:13:36 来源:https://blog.csdn.net/qq_73797346/article/details/144386327  浏览:    关键词:K8S使用hostpath、NFS作为volumes案例

hostpath宿主机文件系统

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

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

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

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

[root@master23107-volumes]# cat 02-hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:name: hostpath-pod
spec:containers:- name: nginximage: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1volumeMounts:- mountPath: /dataname: hostpath-volumevolumes:- name: hostpath-volumehostPath:path: /hostpath-testtype: Directory

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

/ # touch /data/hostpath-test.txt

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

[root@worker233~]# ls /hostpath-test/
hostpath-test.txt

使用NFS作为volumes

nfs跨节点不同pod共享
	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-server2.配置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: worker232volumes:- name: data# 配置后端存储是nfsnfs:# 指定nfs server的地址,可以是主机名,前提是hosts文件有解析。server: master231# 指定nfs的共享目录path: /oldboyedu/data/nfs-servercontainers:- image: harbor.oldboyedu.com/oldboyedu-web/xiuxian:v2name: xiuxianvolumeMounts:- name: datamountPath: /xixi---apiVersion: v1
kind: pod
metadata:name: xiuxian-nfs-002
spec:nodeName: worker233volumes:- name: datanfs:server: master231path: /oldboyedu/data/nfs-servercontainers:- image: harbor.oldboyedu.com/oldboyedu-linux/alpine:3.20.2name: alpinestdin: truevolumeMounts:- name: datamountPath: /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
	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-002labels:apps: mysql80
spec:nodeName: worker232volumes:- name: dbnfs:server: 10.0.0.231path: /zhiyong18/data/nfs-server/mysql80containers:- image: harbor.zhiyong18.com/zhiyong18-db/mysql:8.3.0-oraclename: mysqlenv:- name: MYSQL_ROOT_PASSWORDvalue: zhiyong18- name: MYSQL_DATABASEvalue: wordpress- name: MYSQL_USERvalue: zhiyong- name: MYSQL_PASSWORDvalue: "123"volumeMounts:- name: dbmountPath: /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 - GPLCopyright (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 - GPLCopyright (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>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词