基于Ubuntu-22.04安装K8s-v1.28.2实验(三)数据卷挂载NFS(网络文件系统)

安装 NFS 服务器: 首先,打开终端并安装 NFS 服务器软件包:

sudo apt update
sudo apt install nfs-kernel-server

创建共享目录: 决定哪个目录你想要共享,然后创建它(如果还不存在):

sudo mkdir -p /nfs/data

配置共享目录权限: 配置目录的权限,确保 NFS 客户端可以访问它:

sudo chown root:root /nfs/data
sudo chmod 777 /nfs/data

编辑 /etc/exports 文件: 决定你想要共享的目录和哪些客户端可以访问它,然后编辑 /etc/exports 文件:

sudo nano /etc/exports
在这个文件中,添加以下行

/nfs/data *(rw,sync,no_subtree_check,no_root_squash)

应用 exports 文件并重启服务: 修改完 /etc/exports 文件后,应用更改并重启 NFS 服务:

sudo exportfs -ra
sudo systemctl restart nfs-kernel-server

节点安装数据卷驱动

每个节点都要运行 apt install nfs-common

k8s配置文件

# hustoj-storage-class.yml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: hustoj-storage-class
provisioner: kubernetes.io/nfs
parameters:
  nfsServer: 192.168.215.129
  nfsPath: /nfs/data
  readOnly: "false"
#pv-definition.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: hustoj-pv
spec:
  storageClassName: hustoj-storage-class
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    server: 192.168.215.129
    path: /nfs/data
#pvc-definition.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: hustoj-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: hustoj-storage-class
#nginx-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
 namespace: default
spec:
 replicas: 6
 selector:
   matchLabels:
     app: nginx
 template:
   metadata:
     labels:
       app: nginx
   spec:
     securityContext:
       fsGroup: 0
       runAsUser: 0
     containers:
       - name: nginx
         image: registry.cn-shanghai.aliyuncs.com/shiningrise/hustoj:v1.6
         securityContext:
           runAsUser: 0
           allowPrivilegeEscalation: true
         ports:
         - containerPort: 80
         volumeMounts:
         - mountPath: /volume  # 将卷挂载到容器内的 `/path/to/mount` 路径
           name: hustoj-volume  # 引用之前定义的卷名称
     volumes:
       - name: hustoj-volume
         persistentVolumeClaim:
           claimName: hustoj-pvc
#nginx-service.yml
apiVersion: v1
kind: Service
metadata:
 name: nginx-service
 namespace: default
spec:
 selector:
   app: nginx
 loadBalancerIP: 192.168.215.200
 ports:
   - protocol: TCP
     port: 80
     targetPort: 80
 type: LoadBalancer #NodePort LoadBalancer

相关文件下载

https://files.cnblogs.com/files/shiningrise/k8s_3.zip?t=1715732180&download=true

posted @ 2024-05-14 23:00  shiningrise  阅读(120)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css