天天看點

k8s 中的 ingress 使用細節

k8s中的ingress

什麼是ingress

k8s 中使用 Service 為相同業務的 Pod 對象提供一個固定、統一的通路接口及負載均衡的能力,那麼這些 Service 如何被外部的應用通路,其中常用的就是借助于 ​

​Ingress​

​對象。

Ingress 是 Kubernetes 中的一個資源對象,用來管理叢集外部通路叢集内部服務的方式。

Ingress 對象由 ​

​Ingress Controller​

​ 和 Ingress 政策設定來共同完成。

  • Ingress 政策:用來配置不同的轉發規則;
  • ​Ingress Controller​

    ​ :Ingress 對象的域名解析都由 

    Ingress Controller

     來完成,Ingress Controller 就是一個反向代理程式,它負責解析 Ingress 的反向代理規則,如果 Ingress 有增删改的變動,所有的 

    Ingress Controller

     都會及時更新自己相應的轉發規則,當 

    Ingress Controller

     收到請求後就會根據這些規則将請求轉發到對應的 Service。
k8s 中的 ingress 使用細節

Ingress 如何使用

這裡來個簡單的 demo 來看下 Ingress 如何使用

1、部署ingress-controller

首先來部署下 ​

​Ingress Controller​

​ 這是使用的是 ​

​ingress-nginx​

使用的 k8s 版本是 ​

​v1.19.9​

​,是以這裡選擇的 ​​ingress-nginx​​ 是 ​

​v1.1.3​

裡面的鏡像是需要FQ的,這裡打包了鏡像到 docker-hub ​​安裝腳本​​

$ kubectl apply -f deploy.yaml      

2、部署應用

cat <<EOF >./go-web.yaml
# deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: go-web
  name: go-web
  namespace: study-k8s
spec:
  replicas: 5
  selector:
    matchLabels:
      app: go-web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: go-web
    spec:
      containers:
        - image: liz2019/test-docker-go-hub
          name: go-app-container
          resources: {}
status: {}

---
# service
apiVersion: v1
kind: Service
metadata:
  name: go-web-svc
  labels:
    run: go-web-svc
spec:
  selector:
    app: go-web
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
      name: go-web-http

---
# ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-web-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: www.go-web.com
      http:
        paths:
          - path: /index
            pathType: Prefix
            backend:
              service:
                name: go-web-svc
                port:
                  number: 8000
EOF      

在最下面放了 ingress 的配置,通過 ​

​path: /index​

​ 将 ingress 請求轉發到 go-web-svc 的 service。

➜  ~ kubectl get ingress -n study-k8s
NAME             CLASS    HOSTS            ADDRESS                       PORTS   AGE
go-web-ingress   <none>   www.go-web.com   192.168.56.112,192.168.56.111   80      28m      

通路

$ curl '192.168.56.111:80/index' \
--header 'Host: www.go-web.com'

<h1>hello world</h1><div>你好</div>%      

ingress 使用細節

1、一個叢集中可以有多個 ​

​Ingress Controller​

​​, 在 Ingress 中可以指定使用哪一個​

​Ingress Controller​

​;

2、多個 Ingress 規則可能出現競争;

3、Ingress 可以為多個命名空間服務;

4、關于如何暴露 ingress 服務,讓外面的服務通路到?

  • 1、

    Ingress Controller

     用 DaemonSet 方式部署,使用叢集内部的某個或某些節點作為邊緣節點,給 node 添加 label 來辨別,使用 nodeSelector 綁定到邊緣節點,保證每個邊緣節點啟動一個 

    Ingress Controller

     執行個體,用 hostPort 直接在這些邊緣節點主控端暴露端口,然後我們可以通路邊緣節點中 

    Ingress Controller

     暴露的端口,這樣外部就可以通路到 

    Ingress Controller

     了;
  • 2、使用親和性排程政策,使需要部署 

    Ingress Controller

     的節點,每個節點都有一個 

    Ingress Controller

     部署,然後用 hostPort 直接在這些邊緣節點主控端暴露端口,我們就能通過這些節點的 IP 和 hostPort來通路 

    Ingress Controller

     了。

繼續閱讀