開放網站對外存取

在 K8s 環境中要將網站對外開放存取服務,不外乎就是開放一個入口,允許網站使用者透過入口進入 K8s 內的網站伺服器。

入口方式

  • Service Type:LoadBalance

  • Ingress:

    • nginx ingress

    • Istio ingressgateway

Service Type

透過Type:Loadbalancer方式建立網站入口,僅適用於雲端K8s平台。 設定此方是否,該 Service 會藉由雲端服務取得公網 IP,管理者即可將網站 Domain 與此 IP綁定好域名對應服務(DNS),即可完成網站入口設定

基本設定概念

apiVersion: v1
kind: Service
metadata:
  name: my-web
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Ingress

此方式的實作,是藉由第三方軟體方案,例如NginxIstio或者其他來實作 Controller 機制,完成下列幾項事項:

  • 配置入口路徑,依第三方特質進行實作。(網域 對應內部網站的 Service)

  • 套用後取得公網 IP

  • 如有必要可結合SSL憑證,實現 HTTPS 網站入口。

基本設定概念

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: service1
          servicePort: 4200
      - path: /bar
        backend:
          serviceName: service2
          servicePort: 8080

Last updated