Kubernetes中来自ConfigMap的自定义nginx.conf [英] Custom nginx.conf from ConfigMap in Kubernetes

查看:102
本文介绍了Kubernetes中来自ConfigMap的自定义nginx.conf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在家庭实验室中设置了Kubernetes,并且能够从部署中运行nginx的有效实现.

I have Kubernetes set up in a home lab and I am able to get a vanilla implementation of nginx running from a deployment.

下一步是拥有用于配置nginx的自定义nginx.conf文件.为此,我正在使用ConfigMap.

The next step is to have a custom nginx.conf file for the configuration of nginx. For this, I am using a ConfigMap.

这样做时,导航到 http://192.168.1.10时,我不再收到nginx索引页:30008 (我的Nginx服务器正在其上运行的节点的本地IP地址).如果尝试使用ConfigMap,则会收到nginx 404页面/消息.

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

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 

推荐答案

很复杂,它是 nginx.conf 中的根目录,定义不正确.

Nothing complex, it's the root directory in the nginx.conf is not defined correctly.

使用 kubectl日志<<< podname>>>检查日志.-n<< namespace>> 给出了为什么针对特定请求发生 404错误的原因.

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"404153-""curl/7.58.0"2020/10/02 22:26:57 [错误] 28#28:* 1"/etc/nginx/html/index.html"未找到(2:没有此类文件或目录),客户端:xxx.xxx.xxx.xxx,服务器:localhost,请求:"GET/HTTP/1.1",主机:"xxx.xxx.xxx.xxx";

这是因为 configmap 中的 location 将错误的目录称为根 root html .

It's because of location inside your configmap is referring to wrong directory as root root html.

将位置更改为具有 index.html 的目录将解决此问题.这是带有 root/usr/share/nginx/html 的有效configmap.但是,可以根据需要进行操作,但是我们需要确保目录中存在文件.

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;
        }
      }
    }

这篇关于Kubernetes中来自ConfigMap的自定义nginx.conf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆