Deployment 加载nfs磁盘持久化配置过程

本文详细介绍了如何在Kubernetes集群中配置NFS作为持久化存储解决方案,并通过Deployment、Service和Ingress来部署和访问NFS挂载的Nginx服务。从NFS服务器的配置到Nginx服务的完全暴露,包括创建Deployment、Service和Ingress的YAML文件,再到最终的服务检查,提供了完整的实践指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

: NFS服务器

nfs服务器: 10.10.10.69  path:/data

对于需要容器内数据持久化的,低成本的方案是自己部署nfs server。

对于性能有要求的可以使用企业级 NAS 或者 ceph glusterfs。

 

 

[root@node01 ~]#

[root@node01 ~]#

[root@node01 ~]# ps -ef|grep nfs

root        1650       2  0 Sep09 ?        00:00:00 [nfsd]

root        1651       2  0 Sep09 ?        00:00:00 [nfsd]

root        1652       2  0 Sep09 ?        00:00:00 [nfsd]

root        1654       2  0 Sep09 ?        00:00:00 [nfsd]

root        1656       2  0 Sep09 ?        00:00:00 [nfsd]

root        1657       2  0 Sep09 ?        00:00:00 [nfsd]

root        1658       2  0 Sep09 ?        00:00:30 [nfsd]

root        1659       2  0 Sep09 ?        00:05:34 [nfsd]

root      177995  177905  0 20:48 pts/0    00:00:00 grep --color=auto nfs

[root@node01 ~]# cat /etc/exports

/data *(rw,async,no_root_squash,no_subtree_check)

 

[root@node01 ~]#

[root@node01 ~]# showmount -e

Export list for node01:

/data *

[root@node01 ~]#

 

创建 deployment

 

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# cat nginx-deploy-nfs01.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deploy-nfs

spec:

  selector:

    matchLabels:

           app: nginx-deploy-nfs

  replicas: 2

  template:

    metadata:

      labels:

        app: nginx-deploy-nfs

    spec:

      containers:

      - name: nginx-deploy-nfs

        image: nginx

        ports:

        - containerPort: 80

        volumeMounts:

          - mountPath: "/usr/share/nginx/html"

            name: nfs

      volumes:

        - name: nfs

          nfs:

            server: 10.10.10.69

            path: "/data/nginx01"

 

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl apply -f   nginx-deploy-nfs01.yaml

deployment.apps/nginx-deploy-nfs created

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl get deployment|grep nfs

nginx-deploy-nfs             0/2     2            0           57s

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl get pod|grep nfs

nginx-deploy-nfs-5c89969cd4-82vjf             1/1     Running             0          54s

nginx-deploy-nfs-5c89969cd4-pl7jv             1/1     Running             0          54s

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl get pod -o wide|grep nfs

nginx-deploy-nfs-5c89969cd4-82vjf             1/1     Running             0          11h     10.244.2.29   k8s3   <none>           <none>

nginx-deploy-nfs-5c89969cd4-pl7jv             1/1     Running             0          11h     10.244.1.34   k8s2   <none>           <none>

[root@k8s1 nfs]#

 

############################################################################################

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl exec -it nginx-deploy-nfs-5c89969cd4-82vjf /bin/bash

root@nginx-deploy-nfs-5c89969cd4-82vjf:/# cat /etc/hosts

# Kubernetes-managed hosts file.

127.0.0.1       localhost

::1     localhost ip6-localhost ip6-loopback

fe00::0 ip6-localnet

fe00::0 ip6-mcastprefix

fe00::1 ip6-allnodes

fe00::2 ip6-allrouters

10.244.2.29     nginx-deploy-nfs-5c89969cd4-82vjf

root@nginx-deploy-nfs-5c89969cd4-82vjf:/# df

Filesystem                1K-blocks     Used Available Use% Mounted on

overlay                    51142248  6644672  44497576  13% /

tmpfs                         65536        0     65536   0% /dev

tmpfs                       1940400        0   1940400   0% /sys/fs/cgroup

/dev/mapper/VG1-root       51142248  6644672  44497576  13% /etc/hosts

shm                           65536        0     65536   0% /dev/shm

10.83.35.69:/data/nginx01  47683072 11531776  36151296  25% /usr/share/nginx/html

tmpfs                       1940400       12   1940388   1% /run/secrets/kubernetes.io/serviceaccount

tmpfs                       1940400        0   1940400   0% /proc/acpi

tmpfs                       1940400        0   1940400   0% /proc/scsi

tmpfs                       1940400        0   1940400   0% /sys/firmware

root@nginx-deploy-nfs-5c89969cd4-82vjf:/#

 

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl exec -it nginx-deploy-nfs-5c89969cd4-pl7jv  /bin/bash

root@nginx-deploy-nfs-5c89969cd4-pl7jv:/# cat /etc/hosts

# Kubernetes-managed hosts file.

127.0.0.1       localhost

::1     localhost ip6-localhost ip6-loopback

fe00::0 ip6-localnet

fe00::0 ip6-mcastprefix

fe00::1 ip6-allnodes

fe00::2 ip6-allrouters

10.244.1.34     nginx-deploy-nfs-5c89969cd4-pl7jv

root@nginx-deploy-nfs-5c89969cd4-pl7jv:/usr/share/nginx/html# df

Filesystem                1K-blocks     Used Available Use% Mounted on

overlay                    51142248  6659992  44482256  14% /

tmpfs                         65536        0     65536   0% /dev

tmpfs                       1940400        0   1940400   0% /sys/fs/cgroup

/dev/mapper/VG1-root       51142248  6659992  44482256  14% /etc/hosts

shm                           65536        0     65536   0% /dev/shm

10.83.35.69:/data/nginx01  47683072 11532800  36150272  25% /usr/share/nginx/html

tmpfs                       1940400       12   1940388   1% /run/secrets/kubernetes.io/serviceaccount

tmpfs                       1940400        0   1940400   0% /proc/acpi

tmpfs                       1940400        0   1940400   0% /proc/scsi

tmpfs                       1940400        0   1940400   0% /sys/firmware

root@nginx-deploy-nfs-5c89969cd4-pl7jv:/usr/share/nginx/html#

 

创建 service

 

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# cat  nginx-service-nfs02.yaml

apiVersion: v1

kind: Service

metadata:

  name: nginx-service-nfs02

spec:

  type: NodePort

  ports:

   - protocol: TCP

     port: 80

     targetPort: 80

  selector:

    app: nginx-deploy-nfs

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl apply -f  nginx-service-nfs02.yaml

service/nginx-service-nfs02 created

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl get service|grep nfs

nginx-service-nfs02                  NodePort    10.1.157.40    <none>        80:31850/TCP     45s

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl describe service nginx-service-nfs02

Name:                     nginx-service-nfs02

Namespace:                default

Labels:                   <none>

Annotations:              kubectl.kubernetes.io/last-applied-configuration:

                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"nginx-service-nfs02","namespace":"default"},"spec":{"ports":[{"po...

Selector:                 app=nginx-deploy-nfs

Type:                     NodePort

IP:                       10.1.157.40

Port:                     <unset>  80/TCP

TargetPort:               80/TCP

NodePort:                 <unset>  31850/TCP

Endpoints:                10.244.1.34:80,10.244.2.29:80

Session Affinity:         None

External Traffic Policy:  Cluster

Events:                   <none>

[root@k8s1 nfs]#

 

通过ip端口检查map

[root@k8s1 nfs]#

[root@k8s1 nfs]# netstat -ntlp|grep 31850

tcp6       0      0 :::31850                :::*                    LISTEN      13962/kube-proxy   

[root@k8s1 nfs]#

[root@k8s1 nfs]#

 

 

创建 ingress

 

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# cat  nginx-ingress-nfs03.yaml

apiVersion: v1

kind: List

items:

- apiVersion: extensions/v1beta1

  kind: Ingress

  metadata:

    name: nginx-ingress-nfs03

    namespace: default

    annotations:

      nginx.ingress.kubernetes.io/rewrite-target: /

  spec:

    rules:

    - host: 07.bqjr.cn

      http:

        paths:

        - path: /

          backend:

            serviceName: nginx-service-nfs02

            servicePort: 80

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl apply -f  nginx-ingress-nfs03.yaml

ingress.extensions/nginx-ingress-nfs03 created

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]#

[root@k8s1 nfs]# kubectl get ing

NAME                                         HOSTS        ADDRESS      PORTS   AGE

 

nginx-ingress-nfs03                          07.taobao.cn   10.1.69.51   80      37s

[root@k8s1 nfs]#

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值