Kubernetes 中来自 ConfigMap 的自定义 nginx.conf

Eri*_*ric 11 config nginx kubernetes configmap

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

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

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

我无法看到我在这里做错了什么。任何方向将不胜感激。

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 
Run Code Online (Sandbox Code Playgroud)

Bin*_*ter 10

没什么复杂的,它是根目录 nginx.conf没有正确定义。

检查日志kubectl logs <<podname>> -n <<namespace>>给出404 error了特定请求发生的原因。

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"

这是因为location你的内心configmap将错误的目录称为 root root html

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


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

Run Code Online (Sandbox Code Playgroud)