首页 > 解决方案 > 使用 Kubernetes / Traefik Ingress 在本地域上启用 https

问题描述

当我在没有 docker 的情况下测试我的 Spring boot 应用程序时,我使用以下方法对其进行测试:

https://localhost:8081/points/12345/search

而且效果很好。如果我使用,我会收到错误http

现在,我想在本地使用 Kubernetes 部署它,使用 url:https://sge-api.local

当我使用时http,我得到与不使用 docker 时相同的错误。

但是当我使用 https 时,我得到:

<html><body><h1>404 Not Found</h1></body></html>

这是我的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: sge-api-local
  name: sge-api-local
  namespace: sge
spec:
  selector:
    matchLabels:
      app: sge-api-local
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: sge-api-local
    spec:
      containers:
      - image: sge_api:local
        name: sge-api-local

这是我的入口:

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: sge-ingress
  namespace: sge
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
  - host: sge-api.local
    http:
      paths:
      - backend:
         serviceName: sge-api-local
         servicePort: 8081
  tls:
  - secretName: sge-api-tls-cert

和 :

kubectl -n kube-system create secret tls sge-api-tls-cert --key=../certs/privkey.pem --cert=../certs/cert1.pem

最后,这是我的服务:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: sge-api-local
  name: sge-api-local
  namespace: sge
spec:
  ports:
  - name: "8081"
    port: 8081
  selector:
    app: sge-api-local

我应该怎么办 ?

编辑:

traefik-config.yml:

kind: ConfigMap
apiVersion: v1
metadata:
  name: traefik-config
data:
  traefik.toml: |
    # traefik.toml
    defaultEntryPoints = ["http","https"]
    [entryPoints]
      [entryPoints.http]
      address = ":80"
      [entryPoints.http.redirect]
        entryPoint = "https"
      [entryPoints.https]
      address = ":443"
        [entryPoints.https.tls]

traefik 部署:

kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: traefik-ingress-controller
  labels:
    k8s-app: traefik-ingress-lb
spec:
  selector:
    matchLabels:
      k8s-app: traefik-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      containers:
      - image: traefik:1.7
        name: traefik-ingress-lb
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: https
          containerPort: 443
          hostPort: 443
        - name: admin
          containerPort: 8080
          hostPort: 8080
        securityContext:
          capabilities:
            drop:
            - ALL
            add:
            - NET_BIND_SERVICE
        args:
        - --api
        - --kubernetes
        - --logLevel=INFO

traefik-service.yml

kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 8080
      name: admin

标签: kubernetestraefik

解决方案


请确保您已启用 TLS。Let's Encrypt 是一个免费的 TLS 证书颁发机构 (CA),您可以使用它来自动请求和续​​订公共域名的 Let's Encrypt 证书。确保您已创建 configmap。检查您是否遵循 traefik 设置期间的每个步骤:traefik-ingress-controller

然后你必须分配必须分配给哪些主机creted secret,egg。

tls:
- secretName: sge-api-tls-cert
    hosts:
    - sge-api.local

请记住在执行链接时添加分配给主机的特定端口。在您的情况下应该是:https://sge-api.local:8081

在集群外部使用 SSL 卸载时,即使没有可用的 TLS 证书,强制重定向到 HTTPS 也可能很有用。您还可以在入口配置文件中添加注释:

    traefik.ingress.kubernetes.io/frontend-entry-points: http, https
    traefik.ingress.kubernetes.io/redirect-entry-point: https

启用重定向到该前端的另一个入口点(例如 HTTPS)。

让我知道它是否有帮助。


推荐阅读