PV与PVC使用

1. NFS服务器

1.1 安装(每个节点)
$ yum install -y nfs-utils
1.2 存储
# 创建
$ mkdir /data/k8s/v{1..3} -p

# 添加
$ cat /etc/exports
/data/k8s/v1 192.168.119.0/24(rw,sync,no_root_squash)
/data/k8s/v2 192.168.119.0/24(rw,sync,no_root_squash)
/data/k8s/v3 192.168.119.0/24(rw,sync,no_root_squash)

# 查看
$ showmount -e 192.168.119.190
Export list for 192.168.119.190:
/data/k8s/v3 192.168.119.0/24
/data/k8s/v2 192.168.119.0/24
/data/k8s/v1 192.168.119.0/24
1.3 防火墙、selinux
# firewalld
$ systemctl stop firewalld
$ systemctl disable firewalld

# selinux
$ sed -i '/^SELINUX=/s/enforcing/disabled/' /etc/selinux/config
$ setenforce 0
1.4 服务
# nfs-server
$ systemctl start nfs-server
$ systemctl enable nfs-server

# k8s
$ systemctl start nfs
$ systemctl enable nfs
$ showmount -e 192.168.119.190
Export list for 192.168.119.190:
/data/k8s/v3 192.168.119.0/24
/data/k8s/v2 192.168.119.0/24
/data/k8s/v1 192.168.119.0/24

2. PV

2.1 编写yaml
# yaml文件
# cat pv01.yaml 
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: bzx-pv
spec: 
  capacity: 
    storage: 1Gi
  accessModes: 
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs: 
    path: /data/k8s/v1
    server: 192.168.119.190
2.2 创建
$ kubectl apply -f pv01.yaml
2.3 查看pv
$ kubectl describe pv bzx-pv
Name:            bzx-pv
Labels:          <none>
Annotations:     <none>
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    
Status:          Available
Claim:           
Reclaim Policy:  Recycle
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        1Gi
Node Affinity:   <none>
Message:         
Source:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    192.168.119.190
    Path:      /data/k8s/v1
    ReadOnly:  false
Events:        <none>

3. PVC

3.1 编写yaml文件
# yaml文件
# cat pvc01.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  name: bzx-pvc
spec: 
  accessModes: 
  - ReadWriteOnce
  resources: 
    requests: 
      storage: 1Gi
3.2 创建
$ kubectl apply -f pvc01.yaml 
3.3 查看PVC
$ kubectl describe pvc bzx-pvc
Name:          bzx-pvc
Namespace:     default
StorageClass:  
Status:        Bound
Volume:        bzx-pv
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Used By:       <none>
Events:        <none>

4. deployment、service

4.1 yaml文件
# yaml文件
# cat deployment01.yaml 
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: pvc-dep
spec: 
  replicas: 2
  selector: 
    matchLabels: 
      app: nfs-pvc
  template: 
    metadata: 
      labels: 
        app: nfs-pvc
    spec: 
      containers: 
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports: 
        - name: web
          containerPort: 80
        volumeMounts: 
        - name: www
          mountPath: /usr/share/nginx/html
      volumes: 
      - name: www
        persistentVolumeClaim: 
          claimName: bzx-pvc

---

apiVersion: v1
kind: Service
metadata:
  name: pvc-svc
  labels:
    app: nfs-pvc
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: web
  selector:
    app: nfs-pvc
4.2 创建
$ kubectl apply -f deployment01.yaml
4.3 查看
# deployment
$ kubectl get deployment -o wide
NAME      READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
pvc-dep   2/2     2            2           3m32s   nginx        nginx:latest   app=nfs-pvc

# service 
$ kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        14d
pvc-svc      NodePort    10.1.133.233   <none>        80:31504/TCP   3m54s

5. 访问测试

# 添加首页信息(nfs-server)
$ echo "This is a pvc test page!" > /data/k8s/v1/index.html

# 内部访问
$ curl 10.1.133.233
This is a pvc test page!

# 外部访问
http://192.168.119.191:31504/
http://192.168.119.192:31504/
http://192.168.119.193:31504/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值