I am trying to use Kubernetes volumes for Nginx, but am facing an issue with it. After volumes are set Nginx is unable to serve the HTML page. Also am tried with PV and PVS this time also got the same error.
nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
volumes:
- name: nginxhtml
# persistentVolumeClaim:
# claimName: pvc
hostPath:
path: /home/amjed/Documents/SPS/k8s/mongo/mnt
containers:
- name: nginx
image: nginx
volumeMounts:
- name: nginxhtml
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80
2
Answers
First, create folder that you want to mount inside minikube:
This folder is what is mapped to
/usr/share/nginx/html
inside Pods, so files you paste here will be displayed when you connect to your service.Make sure that you have some
.html
file inside that folder, otherwise you will get 403 error. For me, exampleindex.html
is this:You also need to add
securityContext
fsGroup
inside your Deployment manifest, so that/usr/share/nginx/html
is owned by nginx user (101 uid).Then, apply Deployment and LoadBalancer resources using this:
After that you can check if the content is served correctly
Let me know if you have more questions.