k8s pv pvc 持久化存储

在服务器搭建nfs

yum install -y nfs-utils

复制代码
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports

mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
#配置生效
exportfs -r

测试一下 showmount -e localhost 看看nfs服务启动正常

复制代码
[admin@localhost data]$ showmount -e localhost
Export list for localhost:
/nfs/data *

nfs服务启动成功

编写yml文件

复制代码
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mynginx
  template:
    metadata:
      labels:
        app: mynginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25                
        imagePullPolicy: IfNotPresent 
        volumeMounts:
          - name: html
            mountPath: /usr/share/nginx/html
      volumes:
      - name: html
        persistentVolumeClaim:
          claimName: nginx-pvc      
                
---

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: mynginx 
  ports:
  - port: 8000
    protocol: TCP
    targetPort: 80 
    
    
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv01
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /nfs/data
    server: 192.168.123.67
      
---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 200Mi  
  storageClassName: nfs

PVC在PV的基础上选择符合条件的PV,其实就是加了一层

kubectl get 一下确定nfs已经挂在到pv上,pvc选择了合适的pv,pod将pvc选好的pv进行挂载

复制代码
[admin@localhost k8s]$ kubectl apply -f nginx.yaml
deployment.apps/nginx unchanged
service/nginx unchanged
persistentvolume/pv01 unchanged
persistentvolumeclaim/nginx-pvc created
[admin@localhost k8s]$ kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP    14m
nginx        ClusterIP   10.99.104.71   <none>        8000/TCP   7m30s
[admin@localhost k8s]$ kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
pv01   10Gi       RWX            Retain           Bound    default/nginx-pvc   nfs            <unset>                          2m10s
[admin@localhost k8s]$ kubectl get pv -o wide
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE     VOLUMEMODE
pv01   10Gi       RWX            Retain           Bound    default/nginx-pvc   nfs            <unset>                          2m18s   Filesystem
[admin@localhost k8s]$ kubectl get pvc -o wide
NAME        STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE   VOLUMEMODE
nginx-pvc   Bound    pv01     10Gi       RWX            nfs            <unset>                 43s   Filesystem
[admin@localhost k8s]$ kubectl get pv -o wide
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE     VOLUMEMODE
pv01   10Gi       RWX            Retain           Bound    default/nginx-pvc   nfs            <unset>                          2m36s   Filesystem

[admin@localhost data]$ kubectl describe pv  pv01
Name:            pv01
Labels:          <none>
Annotations:     pv.kubernetes.io/bound-by-controller: yes
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    nfs
Status:          Bound
Claim:           default/nginx-pvc
Reclaim Policy:  Retain
Access Modes:    RWX
VolumeMode:      Filesystem
Capacity:        10Gi
Node Affinity:   <none>
Message:
Source:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    192.168.123.67
    Path:      /nfs/data
    ReadOnly:  false
Events:        <none>

[admin@localhost data]$ kubectl describe pvc  nginx-pvc
Name:          nginx-pvc
Namespace:     default
StorageClass:  nfs
Status:        Bound
Volume:        pv01
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      10Gi
Access Modes:  RWX
VolumeMode:    Filesystem
Used By:       nginx-7d78c7fcdf-55z9w
               nginx-7d78c7fcdf-c8xbq
Events:        <none>

随便进入一个pod,向挂载目录写个文件

复制代码
[admin@localhost root]$ kubectl get service -o wide
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP    17m     <none>
nginx        ClusterIP   10.99.104.71   <none>        8000/TCP   9m44s   app=mynginx
[admin@localhost root]$ kubectl run -it nginx-7d78c7fcdf-c8xbq -- /bin/bash
Error: required flag(s) "image" not set
[admin@localhost root]$ kubectl exec -it nginx-7d78c7fcdf-c8xbq -- /bin/bash
root@nginx-7d78c7fcdf-c8xbq:/# cd /usr/share/nginx/html
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# ls
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# echo 123 > 1.html

在服务器的挂载目录也能看到1.html

复制代码
[admin@localhost data]$ ls
1.html
[admin@localhost data]$ cat 1.html
123

curl一下service ip

复制代码
root@nginx-7d78c7fcdf-c8xbq:/usr/share/nginx/html# curl 10.99.104.71:8000/1.html
123

成功

相关推荐
予枫的编程笔记几秒前
【Linux进阶篇】从基础到实战:grep高亮、sed流编辑、awk分析,全场景覆盖
linux·sed·grep·awk·shell编程·文本处理三剑客·管道命令
Sheep Shaun1 分钟前
揭开Linux的隐藏约定:你的第一个文件描述符为什么是3?
linux·服务器·ubuntu·文件系统·缓冲区
Java后端的Ai之路4 分钟前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway
devmoon6 分钟前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
Tfly__9 分钟前
在PX4 gazebo仿真中加入Mid360(最新)
linux·人工智能·自动驾驶·ros·无人机·px4·mid360
野犬寒鸦9 分钟前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
陈桴浮海11 分钟前
【Linux&Ansible】学习笔记合集二
linux·学习·ansible
云姜.13 分钟前
java抽象类和接口
java·开发语言
带刺的坐椅13 分钟前
Claude Code Skills,Google A2A Skills,Solon AI Skills 有什么区别?
java·ai·solon·a2a·claudecode·skills
生活很暖很治愈19 分钟前
Linux——环境变量PATH
linux·ubuntu