使用Kubernetes / Traefik Ingress在本地域上启用https

时间:2019-10-29 15:21:51

标签: kubernetes traefik

当我在没有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

1 个答案:

答案 0 :(得分:0)

请确保已启用TLS。 Let’s Encrypt是免费的TLS证书颁发机构(CA),您可以使用它自动为公共域名请求和续订Let’s Encrypt证书。确保已创建configmap。检查在traefik设置过程中是否遵循所有步骤:traefik-ingress-controller

然后,您必须将必须创建crecreed 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

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

让我知道是否有帮助。