首页 > 其他分享 >K8S配置nfs

K8S配置nfs

时间:2023-03-09 14:35:50浏览次数:62  
标签:name 配置 systemctl nfs provisioner K8S root localhost

在 k8s 中当我们需要持久化存储一些数据的使用,会使用到的就是 PV 和 PVC,但 PV 和 PVC 都是需要手动创建的话会很麻烦,特别是当有 StatefulSet 应用存在的时候,如果你需要手动对每个 pod 都创建一个 PVC 和 PV 就非常麻烦,于是 StorageClass 就是来解决这个问题的。

pv、pvc、StorageClass请参考此链接
https://blog.csdn.net/weixin_46902396/article/details/124780531

准备
首先你需要一个 nfs 或其他存储,这里我以 nfs 为例进行部署

我们先来梳理一下思路和几个需要的东西:

  • nfs 是我们最终的存储
  • nfs-client 是用来动态创建 pv 和 pvc 的,我们称为 provisioner
  • StorageClass 关联到对应的 provisioner 就可以使用
  • statefulset(或别的资源)需要配置 storageClassName 进行使用

一、安装NFS并挂载共享目录
1、服务端安装NFS

[root@localhost ~]#yum-y install nfs-utils rpcbind

2、创建共享目录并设置权限

[root@localhost ~]#mkdir-p /data/{kafka,zookeeper}

[root@localhost ~]#chmod 755 -R /data/*

3、修改配置文件

[root@localhost ~]# cat >> /etc/exports<<EOF

/data/kafka  *(rw,sync,no_root_squash,insecure)
/data/zookeeper  *(rw,sync,no_root_squash,insecure)

EOF

4、服务端启动

[root@localhost ~]# systemctl start rpcbind.service

[root@localhost ~]# systemctl enable rpcbind

[root@localhost ~]# systemctl status rpcbind

[root@localhost ~]# systemctl start nfs.service

[root@localhost ~]# systemctl enable nfs

[root@localhost ~]# systemctl status nfs

5、客户端安装NFS

[root@localhost ~]#yum -y install nfs-utils rpcbind

6、客户端启动

[root@localhost ~]# systemctl start rpcbind.service

[root@localhost ~]# systemctl enable rpcbind

[root@localhost ~]# systemctl status rpcbind

[root@localhost ~]# systemctl start nfs.service

[root@localhost ~]# systemctl enable nfs

[root@localhost ~]# systemctl status nfs

7、查看共享目录

[root@localhost ~]# showmount -e 172.171.2.148

8、客户端挂载共享目录

[root@localhost ~]#mkdir -p /data/{kafka,zookeeper}

[root@localhost ~]#mount 172.171.2.148:/data/kafka /data/kafka

[root@localhost ~]#mount 172.171.2.148:/data/zookeeper /data/zookeeper

二、设置ServiceAcount,为操作pv和pvc设置操作权限
1、配置.yaml文件

点击查看代码
[root@k8s-master conf]# vim serviceAccount/conf.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-provisioner
  namespace: middleware

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-provisioner-runner
  namespace: middleware
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-provisioner
    namespace: middleware
roleRef:
  kind: ClusterRole
  name: nfs-provisioner-runner
  apiGroup: rbac.authorization.k8s.io

2、应用.yaml文件

kubectl apply -f conf.yaml

三、部署nfs-client

创建一个 deployment 用于部署 nfs-client 来分配想对应所需资源,注意其中 NFS_SERVERNFS_PATH 需要修改为你自己的地址

1、配置.yaml文件

点击查看代码

kind: Deployment
apiVersion: apps/v1
metadata:
  name: zs-nfs-client-provisioner
  namespace: middleware
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: zs-nfs-client-provisioner
  template:
    metadata:
      labels:
        app: zs-nfs-client-provisioner
    spec:
      serviceAccountName: nfs-provisioner
      containers:
        - name: zs-nfs-client-provisioner
          #image: quay.io/external_storage/nfs-client-provisioner:latest,使用这个会报错 unexpected error getting claim reference: selfLink was empty, can't make reference,故使用下面的镜像
          image: registry.cn-beijing.aliyuncs.com/mydlq/nfs-subdir-external-provisioner:v4.0.0
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              # 这个名称你可以自定义,需要记住,下面还要用
              value: zk/nfs
            - name: NFS_SERVER
              value: 172.171.2.148
            - name: NFS_PATH
              value: /data/zookeeper
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
            limits:
              cpu: 200m
              memory: 512Mi
      volumes:
        - name: nfs-client-root
          nfs:
            server: 172.171.2.148
            path: /data/zookeeper

这里以配置zookeeper为样例,配置Kafka请修改红色部分

2、应用yaml文件

kubectl apply -f zk-nfs.yaml
四、创建StorageClass

1、配置.yaml文件
[root@k8s-master nfs-client]# vim ../storageClass/zk.yaml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: zk-nfs-storage
  namespace: middleware
mountOptions:
- vers=4.0
- nolock,tcp,noresvport
provisioner: zk/nfs
reclaimPolicy: Retain

Reclaim Policy 回收策略:

  • Delete(删除):当 PVC 被删除时,PV 同样会被删除。
  • Retain(保留):当 PVC 被删除时,PV 并不会被删除,需要手动进行删除。
  • Recycle(回收):当 PVC 被删除时,PV 上的数据也会随之删除,以便和新的 PVC 进行绑定(已被遗弃)

2、应用yaml

kubectl apply -f zk.yaml

五、可能遇到的问题

问题: 创建完 PVC 后,一直处于 Pending 状态,通过查看 PVC 信息发现,输出:waiting for a volume to be created, either by external provisioner "zk/nfs" or manually created by system administrator 报错。

原因是因为: 在 Kubernetes 的 1.20 以上版本,默认禁用了 SelfLink 功能。但是由于 nfs-client-privisioner 服务需要依赖此功能。所以,需要在 apiserver 的配置文件中开启此功能。

[root@k8s-master ~]# vim /etc/systemd/system/kube-apiserver.service

  • 增加:--feature-gates=RemoveSelfLink=false 配置。

标签:name,配置,systemctl,nfs,provisioner,K8S,root,localhost
From: https://www.cnblogs.com/llds/p/17198194.html

相关文章

  • IIS配置
    2021-09-13IIS无故自动关闭停止已经不是罕见的事情了,处理这个问题是让我很头痛的事情,遇到这个问题不太可能一次性解决,多数都是用排除法一个个测试排除错误,最终找到那个错......
  • IdentityServer4: 配置项持久化
    目录IdentityServer配置项持久化创建IdentityServer项目添加依赖包添加QuickstartUI数据库迁移ConfigurationDbContextPersistedGrantDbContext生成初始化数据严重BU......
  • 【笔记】nginx配置
    一、创建Nginx运行使用的用户1.1创建system用户组/usr/sbin/groupaddsystem1.2创建用户nginx/usr/sbin/useradd-gsystemnginx1.3查看默认配置文件cat/usr/local......
  • 前端从0-1使用nginx打包部署静态资源,以及hash和history配置汇总
    第一:我们要搭建nginx部署基础环境具体流程可参考这个链接从0-1超详细教你实现前端代码nginx部署全流程第二:我们要知道前端路由hash和history实现以及区别路由功能:1、记......
  • npm 基础使用配置淘宝镜像
    查看npm配置文件npmconfiglist配置npm使用淘宝镜像npmconfigsetregistryhttp://registry.npm.taobao.org/安装依赖npminstall注意大家如果np......
  • 数据源配置
    spring:#数据源配置datasource:type:com.alibaba.druid.pool.DruidDataSourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://127.0.0.......
  • gitlab 配置 ssh keys
    gitlab 配置sshkeys1、生成id_rsa.pubssh-keygen-trsa-C‘email’ 2、.ssh文件夹目录在c:\users\用户名\下,找到id_rsa.pub文件并复制里面的内容  3......
  • Main自动配置
    packagecom.example;importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.generator.AutoGenerator;importcom.baomidou.mybatispl......
  • arch-i3配置rofi
    Rofi,类似于苹果电脑的SpotlightSearch。在i3wm中,Rofi可以让我们快速的打开应用程序和切换窗口。安装Installsudopacman-Srofi初识Getstarted安装好之后,就可以简单......
  • linux在线安装配置jdk
    1.在线安装jdk步骤一:检查是否已安装有jdk   使用rpm-qa|grepjdk 或者 yumlistinstalled|grepjdk查询都可以步骤二:在线搜索可安装的版本   yumse......