使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- 持久化存储(NFS网络存储)
  eDx7eii7I8JJ 2023年11月01日 40 0

使用 Kubeadm 部署 Kubernetes(K8S) 安装
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- Ingress-Ngnix
Volume 是 Pod 中能够被多个容器访问的共享目录。 Kubernetes 的 Volume 定义在 Pod 上,它被一个 Pod 中的多个容 器挂载到具体的文件目录下。
Volume 与 Pod 的生命周期相同,但与容器的生命周期不相关, 当容器终止或重启时, Volume 中的数据也不会丢失。 要使用volume, pod 需要指定 volume 的类型和内容( 字段) , 和映射到容器的位置( 字段) 。
Kubernetes 支持多种类型的 Volume,包括: emptyDir、 hostPath、 gcePersistentDisk、awsElasticBlockStore、 nfs、 iscsi、 flocker、 glusterfs、 rbd、 cephfs、 gitRepo、secret、 persistentVolumeClaim、 downwardAPI、 azureFileVolume、 azureDisk、vsphereVolume、 Quobyte、 PortworxVolume、 ScaleIO。 emptyDirEmptyDir 类型的 volume
创建于 pod 被调度到某个宿主机上的时候, 而同一个 pod 内的容器都能读写 EmptyDir 中的同一个文件。 一旦这个 pod 离开了这个宿主机, EmptyDir 中的数据就会被永久删除。 所以目前 EmptyDir 类型的 volume 主要用作临时空间, 比如 Web 服务器写日志或者 tmp 文件需要的临时目录

NFS 网络存府:POD 重启,数据还存在,缺陷,需要知道NFS服务器的地址,配在 yaml中

找一台服务器,做NFS服务端

选择合适的节点部署,这边使用 K8SMaster 主节点做为nfs服务器

安装nfs

[root@k8smaster ~]# yum install -y nfs-utils
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 nfs-utils.x86_64.1.1.3.0-0.66.el7 将被 升级
---> 软件包 nfs-utils.x86_64.1.1.3.0-0.68.el7.2 将被 更新
--> 解决依赖关系完成

依赖关系解决

============================================================================================================
 Package                    架构                     版本                    源                    大小
============================================================================================================
正在更新:
 nfs-utils                 x86_64               1:1.3.0-0.68.el7.2          updates               413 k

事务概要
============================================================================================================
升级  1 软件包

总计:413 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                 1/2 
  清理        : 1:nfs-utils-1.3.0-0.66.el7.x86_64                                                   2/2 
  验证中      : 1:nfs-utils-1.3.0-0.68.el7.2.x86_64                                                 1/2 
  验证中      : 1:nfs-utils-1.3.0-0.66.el7.x86_64                                                   2/2 

更新完毕:
  nfs-utils.x86_64 1:1.3.0-0.68.el7.2   

完毕!
[root@k8smaster ~]# 

设置挂载路径

# 创建nfs挂载目录,
[root@k8smaster ~]# mkdir /nfs
# 配置挂载路径
[root@k8smaster ~]# vi /etc/exports
#/nfs 挂载路径   * 所有内容 (rw 读写权限)
/nfs *(rw,sync,no_root_squash,no_subtree_check) 
[root@k8smaster ~]# 

K8S node 节点安装 nfs

k8snode1、k8snode2 都进行安装

#节点安装 nfs, 会自动进行挂载--好像没生效
[root@k8snode1 ~]# yum install -y nfs-utils 
[root@k8snode1 ~]# 
# 将master 的 nfs 挂载到 节点上。
[root@k8snode3 ~]# mount -t nfs 172.16.3.181:/nfs /nfs
[root@k8snode3 ~]# chmod +x /etc/rc.d/rc.local
[root@k8snode3 ~]# echo '#nfs' >> /etc/rc.d/rc.local
[root@k8snode3 ~]# echo 'mount -t nfs 172.16.3.181:/nfs /nfs' >> /etc/rc.local

启动 nfs 服务

# 启动 nfs 服务
[root@k8smaster ~]# systemctl start nfs && systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@k8smaster ~]# ps -ef|grep nfs
root     31990     2  0 11:23 ?        00:00:00 [nfsd4_callbacks]
root     31996     2  0 11:23 ?        00:00:00 [nfsd]
root     31997     2  0 11:23 ?        00:00:00 [nfsd]
root     31998     2  0 11:23 ?        00:00:00 [nfsd]
root     31999     2  0 11:23 ?        00:00:00 [nfsd]
root     32000     2  0 11:23 ?        00:00:00 [nfsd]
root     32001     2  0 11:23 ?        00:00:00 [nfsd]
root     32002     2  0 11:23 ?        00:00:00 [nfsd]
root     32003     2  0 11:23 ?        00:00:00 [nfsd]
root     34377 29719  0 11:29 pts/0    00:00:00 grep --color=auto nfs
[root@k8smaster ~]# 

部署应用验证

nfs-ngins.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-nfs
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
        - name: wwwroot
          nfs:
            server: 172.16.3.181
            path: /nfs
# 具休内容见上
[root@k8smaster ~]# vi nfs-nginx.yaml
[root@k8smaster ~]# kubectl apply -f nfs-nginx.yaml 
deployment.apps/nginx-nfs created
[root@k8smaster ~]# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
javademo1-d7856c75c-czv2g    1/1     Running   0          20h
javademo1-d7856c75c-n28rs    1/1     Running   0          20h
javademo1-d7856c75c-xzqjc    1/1     Running   0          20h
nginx-f89759699-5hkdw        1/1     Running   0          26d
nginx-nfs-788564fbc8-z9srr   1/1     Running   0          3m7s
# 查看内部信息
[root@k8smaster ~]# kubectl describe pod nginx-nfs-788564fbc8-z9srr
# 进入pod 容器
[root@k8smaster ~]# kubectl exec -it nginx-nfs-788564fbc8-z9srr bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
# 查看html目录下的内容【这时候没内容】
root@nginx-nfs-788564fbc8-z9srr:/# ls /usr/share/nginx/html/
root@nginx-nfs-788564fbc8-z9srr:/# 

# 到 nfs 服务器 创建文件【再开一个连接命令窗口】
[root@k8smaster ~]# cd /nfs/
[root@k8smaster nfs]# vi index.html
hello nfs

# 回到容器内,再看下有没有文件
root@nginx-nfs-788564fbc8-z9srr:/# ls /usr/share/nginx/html/
index.html #这时候文件就有了
root@nginx-nfs-788564fbc8-z9srr:/# 

# 对外暴露 nginx-nfs 服务
[root@k8smaster nfs]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
javademo1    NodePort    10.106.43.46   <none>        8111:31452/TCP   21d
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          26d
nginx        NodePort    10.103.87.81   <none>        80:30339/TCP     26d
[root@k8smaster nfs]# kubectl expose deployment nginx-nfs --port=80 --type=NodePort
service/nginx-nfs exposed
[root@k8smaster nfs]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
javademo1    NodePort    10.106.43.46   <none>        8111:31452/TCP   21d
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP          26d
nginx        NodePort    10.103.87.81   <none>        80:30339/TCP     26d
nginx-nfs    NodePort    10.99.84.9     <none>        80:30205/TCP     5s
[root@k8smaster nfs]# 

image

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2xk0JyO908yA   2024年04月11日   22   0   0 Kubernetes
  az2L92p17wYQ   20天前   23   0   0 Kubernetes
eDx7eii7I8JJ
最新推荐 更多

2024-05-17