ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

k8s 数据卷NFS共享卷

2021-12-08 15:01:29  阅读:163  来源: 互联网

标签:nginx nfs NFS 共享 k8s root node3 pod


k8s-数据卷NFS共享卷

1. k8s数据卷NFS共享卷

  • NFS数据卷:提供对NFS挂载支持,可以自动将NFS共享路径挂载到Pod中

  • NFS:是一个主流的文件共享服务器。

  • 安装示例:

    # yum install nfs-utils -y
    # vi /etc/exports
    /ifs/kubernetes *(rw,no_root_squash)
    # mkdir -p /ifs/kubernetes
    # systemctl start nfs
    # systemctl enable nfs
    

    注:每个Node上都要安装nfs-utils包

  • NFS共享卷架构图
    image

  • 示例:将网站程序通过NFS数据卷共享,让所有Pod使用

  • 示例代码:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-nfs
    spec:
      selector:
        matchLabels:
          app: nginx-nfs
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx-nfs
        spec:
          containers:
          - name: nginx-nfs
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            nfs:
              server: 10.100.24.85
              path: /ifs/kubernetes
    

2. 案例

2.1 安装和配置nfs

  • 安装nfs

    [root@k8s-node1 ~]# yum install nfs-utils -y
    [root@k8s-node2 ~]# yum install nfs-utils -y
    [root@k8s-node3 ~]# yum install nfs-utils -y
    
  • 用node3做本次的nfs共享磁盘

    • 修改nfs配置文件

      [root@k8s-node3 ~]# vi /etc/exports
      [root@k8s-node3 ~]# cat /etc/exports
      /ifs/kubernetes *(rw,no_root_squash)
      
    • 创建nfs共享目录

      [root@k8s-node3 ~]# mkdir -p /ifs/kubernetes
      
    • 创建index.html文件

      [root@k8s-node3 ~]# cd /ifs/kubernetes/
      [root@k8s-node3 kubernetes]# vim index.html
      [root@k8s-node3 kubernetes]# cat index.html
      <h1>hello world!</h1>
      
  • 每个node都启动并设置开机启动

    [root@k8s-node3 ~]# systemctl start nfs      #启动nfs
    [root@k8s-node3 ~]# systemctl enable nfs     # 设置开机启动
    Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
    
    

2.2 挂载nfs盘到pod容器

  • 创建代码目录

    [root@k8s-master yaml]# mkdir -p nfs/
    [root@k8s-master yaml]# cd nfs/
    
  • 编写pod代码

    [root@k8s-master nfs]# vim nfs.yaml
    [root@k8s-master nfs]# cat nfs.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-nfs
    spec:
      selector:
        matchLabels:
          app: nginx-nfs
      replicas: 3
      template:
        metadata:
          labels:
            app: nginx-nfs
        spec:
          containers:
          - name: nginx-nfs
            image: nginx
            volumeMounts:
            - name: wwwroot
              mountPath: /usr/share/nginx/html
            ports:
            - containerPort: 80
          volumes:
          - name: wwwroot
            nfs:
              server: 10.100.24.85
              path: /ifs/kubernetes
    
  • 启动服务

    [root@k8s-master nfs]# kubectl apply -f  nfs.yaml
    deployment.apps/web-nfs created
    
  • 检查服务是否启动

    [root@k8s-master nfs]# kubectl get pods,service -o wide
    NAME                           READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READINESS GATES
    pod/configmap-demo-pod         1/1     Running   0          2d4h    10.244.107.209   k8s-node3   <none>           <none>
    pod/my-hostpath                1/1     Running   0          4h45m   10.244.107.211   k8s-node3   <none>           <none>
    pod/secret-demo-pod            1/1     Running   0          46h     10.244.107.210   k8s-node3   <none>           <none>
    pod/web-nfs-84f8d7bf8d-6mj75   1/1     Running   0          2m42s   10.244.107.212   k8s-node3   <none>           <none>
    pod/web-nfs-84f8d7bf8d-n4tpk   1/1     Running   0          2m42s   10.244.169.144   k8s-node2   <none>           <none>
    pod/web-nfs-84f8d7bf8d-qvd2z   1/1     Running   0          2m42s   10.244.36.84     k8s-node1   <none>           <none>
    
    NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
    service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   13d   <none>
    
  • 检查内容是否存在

    [root@k8s-master nfs]# curl -I 10.244.107.212
    HTTP/1.1 200 OK
    Server: nginx/1.19.6
    Date: Mon, 28 Dec 2020 12:43:19 GMT
    Content-Type: text/html
    Content-Length: 22
    Last-Modified: Mon, 28 Dec 2020 12:17:10 GMT
    Connection: keep-alive
    ETag: "5fe9ccc6-16"
    Accept-Ranges: bytes
    
    [root@k8s-master nfs]# curl  10.244.107.212
    <h1>hello world!</h1>
    

标签:nginx,nfs,NFS,共享,k8s,root,node3,pod
来源: https://www.cnblogs.com/scajy/p/15661562.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有