为什么我从 Nginx 收到只读文件系统错误?

use*_*184 5 deployment nginx security-context kubernetes kubernetes-helm

亲爱的 K8S 社区团队,

部署应用程序 pod 时,我从 nginx 收到此错误消息。我的应用程序 angular6 应用程序托管在 nginx 服务器内,该服务器部署为 EKS 内的 docker 容器。

我将我的应用程序配置为“只读容器文件系统”,但我将“emptyDir”类型的“临时挂载”卷与只读文件系统结合使用。

所以我不确定出现以下错误的原因:

2019/04/02 14:11:29 [emerg] 1#1: mkdir() "/var/cache/nginx/client_temp" failed (30: 只读文件系统) nginx: [emerg] mkdir() "/ var/cache/nginx/client_temp”失败(30:只读文件系统)

我的deployment.yaml是:

...
 spec:
      volumes:
        - name: tmp-volume
          emptyDir: {}
        # Pod Security Context
      securityContext:
        fsGroup: 2000
      containers:
      - name: {{ .Chart.Name }}
        volumeMounts:
        - mountPath: /tmp
          name: tmp-volume
        image: "{{ .Values.image.name }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        securityContext:
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - ALL
        securityContext:
          readOnlyRootFilesystem: true
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
...
Run Code Online (Sandbox Code Playgroud)

nginx.conf 是:

...
http {

include           /etc/nginx/mime.types;
  default_type      application/octet-stream;

  # Turn off the bloody buffering to temp files
  proxy_buffering off;

  sendfile          off;
  keepalive_timeout 120;

  server_names_hash_bucket_size 128;

  # These two should be the same or nginx will start writing 
  #  large request bodies to temp files
  client_body_buffer_size 10m;
  client_max_body_size    10m;
...
Run Code Online (Sandbox Code Playgroud)

Cro*_*rou 0

看来您nginx没有以 root 用户身份运行。

自发布以来 1.12.1-r2,nginx 守护进程以用户身份运行 1001

1.12.1-r2

nginx 容器已迁移到非根容器方法。以前,容器以 root 用户身份运行,nginx 守护进程以 nginx 用户身份启动。从现在开始,容器和 nginx 守护进程都以用户 1001 的身份运行。因此,运行 nginx 进程的用户可以写入配置文件。

这就是为什么你无法绑定端口80,有必要使用端口> 1000。

你应该使用:

  ports:
   - '80:8080'
   - '443:8443'
Run Code Online (Sandbox Code Playgroud)

并编辑 nginx.conf,使其侦听端口 8080:

server {
        listen 0.0.0.0:8080;
        ...
Run Code Online (Sandbox Code Playgroud)

或者以 root 身份运行 nginx: command: [ "/bin/bash", "-c", "sudo nginx -g 'daemon off;'" ]