[英]enable https on local domain with Kubernetes / Traefik Ingress
當我在沒有 docker 的情況下測試我的 Spring 啟動應用程序時,我測試它:
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
請確保您已啟用 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)。
讓我知道它是否有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.