I have Kubernetes set up in a home lab and I am able to get a vanilla implementation of nginx running from a deployment.
The next step is to have a custom nginx.conf file for the configuration of nginx. For this, I am using a ConfigMap.
When I do this, I no longer receive the nginx index page when I navigate to http://192.168.1.10:30008 (my local ip address for the node the nginx server is running on). If I try to use the ConfigMap, I receive the nginx 404 page/message.
I am not able to see what I am doing incorrectly here. Any direction would be much appreciated.
nginx-deploy.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes 1;
events {
worker_connections 10240;
}
http {
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-conf
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
readOnly: true
volumes:
- name: nginx-conf
configMap:
name: nginx-conf
items:
- key: nginx.conf
path: nginx.conf
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30008
selector:
app: nginx
Nothing complex, it's the root directory in the nginx.conf
is not defined correctly.
Checking the logs with kubectl logs <<podname>> -n <<namespace>>
gives why the 404 error
is happening for a particular request.
xxx.xxx.xxx.xxx - - [02/Oct/2020:22:26:57 +0000] "GET / HTTP/1.1" 404 153 "-" "curl/7.58.0"
2020/10/02 22:26:57 [error] 28#28: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.1", host: "xxx.xxx.xxx.xxx"
It's because of location
inside your configmap
is referring to wrong directory as root root html
.
Change the location to a directory which has index.html
will fix the issue. Here is the working configmap with root /usr/share/nginx/html
. However this could be manipulated as you want, but we need to make sure files exist in the directory.
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes 1;
events {
worker_connections 10240;
}
http {
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html; #Change this line
index index.html index.htm;
}
}
}