在K8S中部署NFS服务器并在K8S集群外使用NFS

背景说明

有这样一个场景,我们需要使用多个NFS Server来做负载均衡,Server中保存的文件只读,所以不用考虑数据同步。

一种可能的实现方式是在物理服务器上安装NFS Server,同时安装负载均衡软件来实现(如LVS等),配置起来比较繁琐。

这里考虑在kubernetes集群部署NFS server三副本,同时安装metallb实现负载均衡。

安装负载均衡器

由于这里使用的是rke2搭建的kubernetes集群,需要编辑/etc/rancher/rke2/config.yaml文件,增加如下配置后,重启rke2

bash 复制代码
kube-proxy-arg: # 不指定的话,默认是 iptables 模式
  - proxy-mode=ipvs
  - ipvs-strict-arp=true

helm一键安装metallb

bash 复制代码
helm repo add metallb https://metallb.github.io/metallb
helm install metallb metallb/metallb

创建L2Advertisement,使集群外的服务器也可以访问NFS Server

复制代码
cat<<EOF | kubectl apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
spec:
  addresses:
  - 192.168.122.20-192.168.122.24
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: example
EOF

在kubernetes集群中部署NFS Server

bash 复制代码
cat<<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-server
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nfs-server
  template:
    metadata:
      labels:
        app: nfs-server
    spec:
      containers:
      - name: nfs-server
        image: k8s.gcr.io/volume-nfs:0.8
        ports:
        - name: nfs
          containerPort: 2049
        - name: mountd
          containerPort: 20048
        - name: rpcbind
          containerPort: 111
        securityContext:
          privileged: true
        volumeMounts:
        - name: storage
          mountPath: /exports
      volumes:
      - name: storage
        hostPath:
          path: /data/nfs # store all data in "/data/nfs" directory of the node where it is running
          type: DirectoryOrCreate # if the directory does not exist then create it
---
apiVersion: v1
kind: Service
metadata:
  name: nfs-service
spec:
  ports:
  - name: nfs
    port: 2049
  - name: mountd
    port: 20048
  - name: rpcbind
    port: 111
  selector:
    app: nfs-server # must match with the label of NFS pod
  type: LoadBalancer
EOF

使用负载均衡测试nfs记录

bash 复制代码
# kubectl get svc | grep nfs
nfs-service               LoadBalancer   10.43.223.162   192.168.122.21   2049:31390/TCP,20048:31442/TCP,111:31111/TCP   23h

集群外的客户端挂载nfs

bash 复制代码
mount.nfs4  -onolock 192.168.122.21:/ /mnt
ll /mnt

参考链接:

https://github.com/appscode/third-party-tools/blob/master/storage/nfs/README.md

https://metallb.universe.tf/installation/

https://www.qiniu.com/qfans/qnso-53419624

相关推荐
fo安方18 分钟前
运维的利器–监控–zabbix–第三步:配置zabbix–中间件–Tomcat–步骤+验证
运维·中间件·zabbix
爱奥尼欧28 分钟前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
戒不掉的伤怀1 小时前
【Navicat 连接MySQL时出现错误1251:客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端】
服务器·数据库·mysql
超喜欢下雨天1 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
搬码临时工1 小时前
小企业如何搭建本地私有云服务器,并设置内部网络地址提供互联网访问
运维·服务器
old-six-programmer1 小时前
NAT 类型及 P2P 穿透
服务器·网络协议·webrtc·p2p·nat
tan77º2 小时前
【Linux网络编程】网络基础
linux·服务器·网络
风口上的吱吱鼠2 小时前
Armbian 25.5.1 Noble Gnome 开启远程桌面功能
服务器·ubuntu·armbian
18你磊哥2 小时前
Windows 本地安装部署 Apache Druid
运维·debian
笑衬人心。2 小时前
Ubuntu 22.04 + MySQL 8 无密码登录问题与 root 密码重置指南
linux·mysql·ubuntu