【问题标题】:Unable to run nginx container as non-root无法以非 root 身份运行 nginx 容器
【发布时间】:2020-02-19 05:17:14
【问题描述】:

我正在尝试以非 root 用户身份运行 nginx 容器 我正在尝试配置我的 nginx.conf 文件,然后将其放入 k8s configmap 中,但是当容器启动时,它不断抛出诸如

之类的错误

这里不允许使用“pid”指令 /etc/nginx/conf.d/nginx-kibana.conf:4

以及随后的每一个

我需要在配置中修复或调整什么,或者我需要调整 nginx-deployment.yaml 中的volume:

这是我的 nginx.conf

error_log /tmp/error.log;

# The pidfile will be written to /var/run unless this is set.
pid /tmp/nginx.pid;

worker_processes 1;

events {
  worker_connections 1024;
}
http {
  # Set an array of temp and cache file options that will otherwise default to
  # restricted locations accessible only to root.
  client_body_temp_path /tmp/client_body;
  fastcgi_temp_path /tmp/fastcgi_temp;
  proxy_temp_path /tmp/proxy_temp;
  scgi_temp_path /tmp/scgi_temp;
  uwsgi_temp_path /tmp/uwsgi_temp;

  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  index index.html index.htm index.php;

  default_type application/octet-stream;
  server {
        listen 8080 default_server;
        listen [::]:8080 default_server ipv6only=on;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # the UI will send the request with query string pageId to kibana to load a specific page
        # e.g: iframe src="/kibana/page?pageId=dashboard"
        # set proxy_pass to root kibana does not see the query params, so we have to go to /app/kibana
        location ^~ /${KIBANA_PATH}/page {
          proxy_pass http://127.0.0.1:5601/app/kibana/${ESC}is_args${ESC}args;
          proxy_http_version 1.1;
          proxy_set_header Upgrade ${ESC}http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host ${ESC}host;
          proxy_cache_bypass ${ESC}http_upgrade;
        }

        # have to re-write URLs for kibana to strip out the /kibana part
        location /${KIBANA_PATH}/ {
          proxy_pass http://127.0.0.1:5601/;
          proxy_http_version 1.1;
          proxy_set_header Upgrade ${ESC}http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host ${ESC}host;
          proxy_cache_bypass ${ESC}http_upgrade;
        }

    }
}

这就是我将 configmap 挂载到容器上的方式

      securityContext:
        fsGroup: 2000
        runAsUser: 2000
      volumes:
      - name: nginxconfigmap-volume
        configMap:
          name: my-nginx-configmap

      containers:
      - name: nginx
        image: nginx:stable
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        livenessProbe:
          httpGet:
            scheme: HTTP
            path: /
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        readinessProbe:
          httpGet:
            scheme: HTTP
            path: /
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 2
          failureThreshold: 6
        volumeMounts:
        - mountPath: /etc/nginx/conf.d
          name: nginxconfigmap-volume

【问题讨论】:

  • 能否提供您错误提及的 nginx-kibana.conf?

标签: nginx kubernetes containers security-context


【解决方案1】:

解决了两个问题。 首先,我必须确保将我的 conf 文件命名为 nginx.conf 而不是 nginx-kibana.conf

其次必须确保我将安装路径设置为 挂载路径:/etc/nginx

【讨论】:

    【解决方案2】:

    如果我将卷挂载路径修改为

       volumeMounts:
            - mountPath: /etc/nginx
              name: nginxconfigmap-volume
    

    然后我得到这个错误 2019/10/23 02:50:49 [emerg] 1#1: open() "/etc/nginx/nginx.conf" 失败(2: 没有这样的文件或目录) nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

    不确定如何继续

    【讨论】:

    • 请用新信息编辑您的问题。它将在社区中更加明显。
    【解决方案3】:

    文件nginx.conf 进入路径/etc/nginx 而不是/etc/nginx/conf.d。你得到的错误是 nginx 相关的,与 kubernetes 无关。更改路径以正确加载您的 nginx 配置。

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2017-07-08
      • 1970-01-01
      • 2020-12-19
      • 2021-05-03
      • 1970-01-01
      • 2018-06-18
      • 2018-07-02
      • 2016-01-25
      相关资源
      最近更新 更多