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

How to properly configure ingress cache to get it working?

我正在尝试为特定主机配置缓存,但收到 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

如果我删除这个示例

  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;

一切正常,但没有缓存

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

  nginx.ingress.kubernetes.io/server-snippet: |
      proxy_cache static-cache;

要启用缓存,您需要为 nginx-ingress-controller 配置 proxy_cache_path
您可以通过将 ConfigMap 修改为 nginx-ingress-controller.

来实现

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

首先,创建一个名为 ingress-nginx-controllerConfigMap,如文档 custom_configuration 中所述:
注意:您可能需要修改proxy_cache_path设置,但是共享内存区(keys_zone=static-cache) 应该与 proxy_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

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

$ 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

最后我们可以检查:

$ 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

有关 proxy_cache_pathproxy_cache 的更多信息,请参见 here