如何正确配置入口缓存以使其正常工作?

Сер*_*лов 9 kubernetes nginx-ingress microk8s

我正在尝试为特定主机配置缓存,但收到 404。另外,我的配置似乎未包含在最终的 nginx.conf 中。该文件不包含它

我的 ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: images-ingress
  labels:
    last_updated: "14940999355"
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 8m
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 10m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;
spec:
  tls:
  - hosts:
    - static.qwstrs.com
    secretName: letsencrypt-prod
  rules:
  - host: static.qwstrs.com
    http:
      paths:
      - path: /
        backend:
          serviceName: imaginary
          servicePort: 9000
Run Code Online (Sandbox Code Playgroud)

如果我删除这个样本

  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 10m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;
Run Code Online (Sandbox Code Playgroud)

一切正常,但没有缓存

即使我上面的代码片段中有一行,它也会产生 404 错误并且不起作用

  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;
Run Code Online (Sandbox Code Playgroud)

mat*_*t_j 10

要启用缓存,您需要配置nginx-ingress-controller.
您可以通过修改ConfigMapfor来做到这一点nginx-ingress-controller


我创建了一个示例来说明它是如何工作的(我假设您有kubernetes/ingress-nginx)。

首先,按照文档中所述创建一个ConfigMap命名的custom_configuration注意:您可能需要修改设置,但共享内存区域 (keys_zone= static-cache ) 应与指令中的相同。ingress-nginx-controller
proxy_cache_pathproxy_cache

$ cat configmap.yml
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: ingress-nginx-controller
  namespace: default
data:
  http-snippet: "proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=static-cache:10m max_size=10g  inactive=60m use_temp_path=off;"
  
$ kubectl apply -f configmap.yml 
configmap/ingress-nginx-controller configured
Run Code Online (Sandbox Code Playgroud)

然后创建Ingress资源(我稍微修改了您的入口资源以演示X-Cache-Status标头的工作原理):

$ cat ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: images-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: 8m
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid any 60m;
      add_header X-Cache-Status $upstream_cache_status;
spec:
  tls:
  - hosts:
    - static.qwstrs.com
    secretName: letsencrypt-prod
  rules:
  - host: static.qwstrs.com
    http:
      paths:
      - path: /
        backend:
          serviceName: imaginary
          servicePort: 9000
          
$ kubectl apply -f ingress.yml
ingress.extensions/images-ingress configured
Run Code Online (Sandbox Code Playgroud)

最后我们可以检查:

$ curl -k -I https://static.qwstrs.com
HTTP/2 200
...
x-cache-status: MISS
accept-ranges: bytes

$ curl -k -I https://static.qwstrs.com
HTTP/2 200
...
x-cache-status: HIT
accept-ranges: bytes
Run Code Online (Sandbox Code Playgroud)

proxy_cache_path有关和 的更多信息可以在此处proxy_cache找到。