Traefikv2.2在Kubernetes上的入口:HTTP和HTTPS无法共存

问题描述

我在Kubernetes上使用Traefik(v2.2),并使用通配域证书进行HTTPS访问。@H_404_1@

以下是我的Traefik部署和Ingress配置:@H_404_1@

kind: Deployment
apiVersion: apps/v1
Metadata:
  namespace: ingress-traefik
  name: traefik
  labels:
    app: traefik

spec:
  replicas: 1
  selector:
    matchLabels:
      app: traefik
  template:
    Metadata:
      labels:
        app: traefik
    spec:
      serviceAccountName: traefik-ingress-controller
      containers:
        - name: traefik
          image: traefik:v2.2
          ports:
            - name: web
              containerPort: 80
            - name: websecure
              containerPort: 443
            - name: admin
              containerPort: 8080
          args:
            - --providers.kubernetesingress
            - --entrypoints.web.Address=:80
            - --entrypoints.websecure.Address=:443
            - --ping.entryPoint=web

---
apiVersion: extensions/v1beta1
kind: Ingress
Metadata:
  name: traefik-ingress
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/router.entrypoints: web,websecure

    # remark the following line,HTTPS will return "404 page not found"
    # enable the following line,HTTP will return "404 page not found"
    traefik.ingress.kubernetes.io/router.tls: "true"

    cert-manager.io/cluster-issuer: letsencrypt-prod

spec:
  tls:
    - secretName: cert-stage-wildcard

  rules:
    - host: user.example.io
      http:
        paths:
        - path: /
          backend:
            serviceName: user-service
            servicePort: http

我想同时使用HTTP和HTTPS,但似乎不能同时使用:@H_404_1@

如果我对入口的注释'traefik.ingress.kubernetes.io/router.tls:“ true”进行注释,则HTTP将起作用,而HTTPS将不会:@H_404_1@

$ curl -v https://user.example.io
*   Trying 159.203.52.215:443...
* TCP_NODELAY set
* Connected to user.example.io (159.203.52.215) port 443 (#0)
* ALPN,offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /home/ken.tsoi/anaconda3/ssl/cacert.pem
  CApath: none
* TLSv1.3 (OUT),TLS handshake,Client hello (1):
* TLSv1.3 (IN),Server hello (2):
* TLSv1.3 (IN),Encrypted Extensions (8):
* TLSv1.3 (IN),Certificate (11):
* TLSv1.3 (IN),CERT verify (15):
* TLSv1.3 (IN),Finished (20):
* TLSv1.3 (OUT),TLS change cipher,Change cipher spec (1):
* TLSv1.3 (OUT),Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN,server accepted to use http/1.1
* Server certificate:
*  subject: CN=*.example.io
*  start date: Oct 28 16:10:10 2020 GMT
*  expire date: Jan 26 16:10:10 2021 GMT
*  subjectAltName: host "user.example.io" matched cert's "*.example.io"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
> GET / HTTP/1.1
> Host: user.example.io
> User-Agent: curl/7.68.0
> Accept: */*
> 
* TLSv1.3 (IN),Newsession Ticket (4):
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Wed,28 Oct 2020 21:45:38 GMT
< Content-Length: 19
< 
404 page not found
* Connection #0 to host user.example.io left intact

如果启用注释行,HTTP将返回“ 404页面未找到”:@H_404_1@

$ curl -v http://user.example.io
*   Trying 159.203.52.215:80...
* TCP_NODELAY set
* Connected to user.example.io (159.203.52.215) port 80 (#0)
> GET / HTTP/1.1
> Host: user.example.io
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Wed,28 Oct 2020 21:49:07 GMT
< Content-Length: 19
< 
404 page not found
* Connection #0 to host user.example.io left intact

我猜设置应该有一些小错误,任何人都可以找出问题出在哪里吗?@H_404_1@

解决方法

traefik/v2.2/routing/routers/tls的文档中说,“当指定TLS节时,它指示Traefik当前路由器仅专用于HTTPS请求(并且该路由器应忽略HTTP(非TLS)请求)。”。

这说明了我所遇到的一切。

我的解决方案是创建两个入口路由器,一个用于HTTP,另一个用于HTTPS。