我是k8s的新手。我遵循official tutorial使用minikube在k8s中设置Nginx容器,安装卷并提供index.html。

当我挂载并转到hompe页面时,出现此错误directory index of "/usr/share/nginx/html/" is forbidden

如果我没有安装任何东西,我会收到“欢迎使用Nginx”页面。

这是挂载之前该文件夹的内容。挂载为空后

root@00c1:/usr/share/nginx/html# ls -l
total 8
-rw-r--r-- 1 root root 494 Jul 23 11:45 50x.html
-rw-r--r-- 1 root root 612 Jul 23 11:45 index.html


为什么安装后 pods 内的已安装文件夹为空?

这是我的设置
apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/my_username/test/html"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Mi
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-hello-rc
spec:
  replicas: 2
  selector:
    app: hello-nginx-tester
  template:
    metadata:
      labels:
        app: hello-nginx-tester
    spec:
      securityContext:
        fsGroup: 1000
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-storage
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-tester
  labels:
    app: hello-nginx-tester
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30500
    protocol: TCP
  selector:
    app: hello-nginx-tester

任何信息,将不胜感激。谢谢

最佳答案

我已经在运行的k8s环境中检查了您的配置。经过一些调整后,以下 list 对我来说很顺利:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/my_username/test/html"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Mi
  volumeName: task-pv-volume
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-hello-rc
spec:
  replicas: 2
  selector:
    app: hello-nginx-tester
  template:
    metadata:
      labels:
        app: hello-nginx-tester
    spec:
      securityContext:
        fsGroup: 1000
      volumes:
        - name: task-pv-volume
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: task-pv-container
          image: nginx
          ports:
            - containerPort: 80
              name: "http-server"
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: task-pv-volume
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-tester
  labels:
    app: hello-nginx-tester
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30500
    protocol: TCP
  selector:
    app: hello-nginx-tester

关于kubernetes - Kubernetes/Minikube:挂载卷后,pod挂载目录为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57507618/

10-15 20:28