k8s~istio的安裝與核心組件

安裝istio

$ curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.20.2 TARGET_ARCH=x86_64 sh -
$ cd istio-1.20.2
$ export PATH=$PWD/bin:$PATH

$ istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete

卸載

istioctl x uninstall --purge

開啓pod自動注入sidecar

$ kubectl label namespace default istio-injection=enabled --overwrite 

[root@elasticsearch02 istio]#  kubectl get namespace -L istio-injection
NAME                          STATUS   AGE     ISTIO-INJECTION
apisix                        Active   7d22h
bigdata                       Active   343d
cattle-impersonation-system   Active   674d
cattle-pipeline               Active   356d
cattle-prometheus             Active   568d
cattle-prometheus-p-whn4g     Active   568d
cattle-system                 Active   2y1d
default                       Active   2y1d    enabled

k8s和istio中的組件對比

查看istio的組件

查看pod

kubectl get pods -n istio-system
NAME                                   READY   STATUS    RESTARTS   AGE
istio-egressgateway-67b78cc94c-rm8t5   1/1     Running   0          7m41s
istio-ingressgateway-7d57fd4b-kn8nh    1/1     Running   0          7m41s
istiod-5ddd75f545-kc4lm                1/1     Running   0          8m36s

查看服務

kubectl get svc -n istio-system
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                                                                      AGE
istio-egressgateway    ClusterIP      10.43.208.79    <none>        80/TCP,443/TCP                                                               4d17h
istio-ingressgateway   LoadBalancer   10.43.88.59     <pending>     15021:27218/TCP,80:25359/TCP,443:23532/TCP,31400:13324/TCP,15443:25647/TCP   4d18h
istiod                 ClusterIP      10.43.130.158   <none>        15010/TCP,15012/TCP,443/TCP,15014/TCP       

istio-ingressgateway 是 Istio 提供的一個組件,它作爲 Kubernetes 集羣的入口,接收從集羣外部來的流量,並根據 Istio 的路由規則將流量轉發到集羣內部的服務。

在 Kubernetes 中,istio-ingressgateway 通常被部署爲一個 LoadBalancer 類型的 Service。如果你的 Kubernetes 集羣運行在支持自動創建負載均衡器的雲平臺上(如 AWS、GCP、Azure 等),那麼當你部署 istio-ingressgateway 時,雲平臺會自動爲它創建一個外部的 L4 層負載均衡器,並將該負載均衡器的 IP 地址或者域名設置到 Service 的 status.loadBalancer.ingress 字段。

然而,如果你的 Kubernetes 集羣運行在不支持自動創建負載均衡器的環境中(如裸金屬服務器、某些私有云環境等),那麼 istio-ingressgateway 的狀態就會一直是 Pending,因爲 Kubernetes 無法爲它創建外部的負載均衡器。這種情況下,你需要手動配置一個方式(如使用 NodePort、HostNetwork 或者使用外部的負載均衡器軟件)來讓外部流量能夠訪問到 istio-ingressgateway

總的來說,istio-ingressgateway 的作用是接收並轉發從集羣外部來的流量。如果它的狀態一直是 Pending,那麼可能是因爲你的環境不支持自動創建負載均衡器,你需要手動配置一個訪問方式。

安裝後組件的說明

k8s pod,istio service
在Kubernetes集羣中安裝 Istio 後,會創建一個名爲 istio-system 的命名空間,並在其中部署一些核心的 Istio 組件。這些組件包括:

  1. istio-egressgateway:Istio出口網關(Egress Gateway)負責管理服務對外部服務的訪問,允許服務在網格之外訪問外部服務。它可以配置和執行流量控制、安全策略、路由等功能,以便管理服務對外部服務的通信。

  2. istio-ingressgateway:Istio入口網關(Ingress Gateway)用於接收來自外部流量並將其引導到網格內部的服務。它充當了整個服務網格的入口點,可以執行流量路由、負載均衡、TLS終止等功能。

  3. istiod:istiod 是 Istio 服務網格的核心組件,負責實現服務發現、流量管理、安全性和遙測等功能。istiod 運行着Pilot、Mixer 和 Citadel 等子組件,它們共同協作來提供服務網格的各種功能。

在Kubernetes中使用 Istio,你可以通過以下方式進行操作:

  • 部署微服務應用:將你的微服務應用程序部署到Kubernetes集羣中,並且通過Istio的流量管理功能實現服務之間的通信、負載均衡和故障恢復等。

  • 配置流量控制:利用Istio的流量管理功能,可以對服務之間的流量進行路由、限速、重試策略等靈活的控制。

  • 實現安全策略:通過Istio的安全功能,可以在服務之間實現強大的身份驗證、安全通信、訪問控制等。

  • 收集和展示遙測數據:Istio提供了豐富的遙測功能,可以幫助你收集並展示服務之間的流量、延遲、錯誤等數據,從而進行監控和故障排查。

總之,在Kubernetes中使用 Istio,你可以利用其豐富的功能來實現微服務架構中的流量管理、安全控制、遙測分析等需求,從而更好地管理和運維你的微服務應用程序。

實例體現

要配置 Istio 的流量管理功能,你需要使用 Istio 的資源對象(如 VirtualService、DestinationRule 等)來定義你的流量控制策略。下面是一些基本的步驟和示例,幫助你開始配置 Istio 的流量管理功能:

demo天氣例子

apiVersion: v1
kind: Service
metadata:
  name: frontend
  labels:
    app: frontend
    service: frontend
spec:
  ports:
  - port: 3000
    name: http
  selector:
    app: frontend
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-v1
  labels:
    app: frontend
    version: v1 # 注意,這塊對應的是istio配置中的DestinationRule裏的label,DestinationRule根據v1找到pod中version爲v1的pod
spec:
  selector:
    matchLabels:
      app: frontend
      version: v1
  replicas: 1
  template:
    metadata:
      labels:
        app: frontend
        version: v1
    spec:
      containers:
      - name: frontend
        image: istioweather/frontend:v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: advertisement
  labels:
    app: advertisement
    service: advertisement
spec:
  ports:
  - port: 3003
    name: http
  selector:
    app: advertisement
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: advertisement-v1
  labels:
    app: advertisement
    version: v1
spec:
  selector:
    matchLabels:
      app: advertisement
      version: v1
  replicas: 1
  template:
    metadata:
      labels:
        app: advertisement
        version: v1
    spec:
      containers:
      - name: advertisement
        image: istioweather/advertisement:v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3003
---
apiVersion: v1
kind: Service
metadata:
  name: forecast
  labels:
    app: forecast
    service: forecast
spec:
  ports:
  - port: 3002
    name: http
  selector:
    app: forecast
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: forecast-v1
  labels:
    app: forecast
    version: v1
spec:
  selector:
    matchLabels:
      app: forecast
      version: v1
  replicas: 1
  template:
    metadata:
      labels:
        app: forecast
        version: v1
    spec:
      containers:
      - name: forecast
        image: istioweather/forecast:v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3002
---

部署istio服務

步驟一:創建Gateway

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: weather-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*" #對所有域名都生效

步驟一:創建 VirtualService

VirtualService 定義了請求應該如何路由到目標服務。例如,你可以根據請求的路徑、主機名等條件將請求路由到不同的服務版本。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-service
spec:
  hosts:
  - "vue.xip.com" #這裏配置哪個域名走istio流量控制
  http:
  - route:
    - destination:
        host: frontend
        port:
          number: 8080
        subset: v1

步驟二:創建 DestinationRule

DestinationRule 定義了對特定服務的流量策略,包括負載均衡、TLS設置等。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: my-service
spec:
  host: frontend
  subsets:
  - name: v1
    labels:
      version: v1 # 轉發到pod中標籤有version的,並且值爲v1的
  - name: v2
    labels:
      version: v2

查看已經創建的gateway,virtualService

kubectl get gateway -n <namespace>
kubectl describe gateway <gateway-name> -n <namespace>

kubectl get virtualservice -n <namespace>
kubectl describe virtualservice <virtualservice-name> -n <namespace>

查看部署後的gateway和virutalservice

gateway

[root@elasticsearch02 istio]# kubectl describe gateway weather-gateway  -n istio-system
Name:         weather-gateway
Namespace:    istio-system
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"networking.istio.io/v1alpha3","kind":"Gateway","metadata":{"annotations":{},"name":"weather-gateway","namespace":"istio-sys...
API Version:  networking.istio.io/v1beta1
Kind:         Gateway
Metadata:
  Creation Timestamp:  2024-01-24T03:08:34Z
  Generation:          4
  Managed Fields:
    API Version:  networking.istio.io/v1alpha3
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .:
          f:kubectl.kubernetes.io/last-applied-configuration:
      f:spec:
        .:
        f:selector:
          .:
          f:istio:
        f:servers:
    Manager:         kubectl
    Operation:       Update
    Time:            2024-01-24T03:08:34Z
  Resource Version:  190297478
  UID:               4e92bf14-dda0-468a-8c9f-00b3297b121e
Spec:
  Selector:
    Istio:  ingressgateway
  Servers:
    Hosts:
      *
    Port:
      Name:      http
      Number:    80
      Protocol:  HTTP
Events:          <none>

virtualservice

[root@elasticsearch02 istio]# kubectl describe virtualservice frontend-route -n ic-test
Name:         frontend-route
Namespace:    ic-test
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"frontend-route","namespace":"ic-...
API Version:  networking.istio.io/v1beta1
Kind:         VirtualService
Metadata:
  Creation Timestamp:  2024-01-24T03:08:55Z
  Generation:          6
  Managed Fields:
    API Version:  networking.istio.io/v1alpha3
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .:
          f:kubectl.kubernetes.io/last-applied-configuration:
      f:spec:
        .:
        f:gateways:
        f:hosts:
        f:http:
    Manager:         kubectl
    Operation:       Update
    Time:            2024-01-24T03:08:55Z
  Resource Version:  190328684
  UID:               94c655d1-c48a-45b6-8f90-e0bf214702eb
Spec:
  Gateways:
    istio-system/weather-gateway
  Hosts:
    vue.xip.com
  Http:
    Match:
      Port:  80
    Route:
      Destination:
        Host:  frontend
        Port:
          Number:  3000
        Subset:    v1
Events:            <none>

在ingress中可視化體現,多個域名都通過istio ingress去管理入口流量

自動sidecar注入配置

要在 Istio 中開啓自動的 sidecar 注入,你可以通過以下步驟來實現:

  1. 安裝 Istio:首先需要安裝 Istio 到你的 Kubernetes 集羣中。你可以使用 istioctl 或者 Helm 來進行安裝,具體安裝步驟可以參考 Istio 官方文檔。

  2. 開啓自動 sidecar 注入:在安裝 Istio 時,你可以選擇開啓自動 sidecar 注入功能。在 Istio 1.5 版本後,默認情況下自動 sidecar 注入功能是開啓的,但在一些特定情況下可能需要手動確認是否已經開啓。

  3. 確認命名空間標籤:確保你要啓用自動 sidecar 注入的命名空間已經添加了 Istio 的注入標籤。你可以通過以下命令來查看:

    kubectl get namespace your-namespace -o=jsonpath='{.metadata.labels}'
    

    如果沒有 istio-injection 標籤,你可以使用以下命令來爲命名空間添加標籤:

    kubectl label namespace your-namespace istio-injection=enabled
    
  4. 部署新的 Pod:一旦確認自動 sidecar 注入功能已經開啓,並且命名空間已經添加了 Istio 的注入標籤,當你部署新的 Pod 時,Istio 將會自動爲其注入 sidecar 容器。

通過以上步驟,你應該能夠成功地開啓 Istio 中的自動 sidecar 注入功能,並確保所有的服務都能夠通過 Envoy 代理進行流量管理和控制。

istio中的對象彙總

在 Istio 中,ServiceEntry、DestinationRule 和 VirtualService 是用來定義和配置流量管理的重要概念。下面簡要介紹它們的作用:

  1. ServiceEntry

    • 作用:ServiceEntry 允許你將外部服務引入到 Istio 網格中,或者定義對外部服務的訪問策略。
    • 場景:當你需要讓 Istio 管理對外部服務的訪問時,可以使用 ServiceEntry 將外部服務引入到 Istio 網格中,並對其進行流量控制和安全策略的定義。
  2. DestinationRule

    • 作用:DestinationRule 定義了對特定服務的流量策略,包括負載均衡、TLS設置等。
    • 場景:在 Istio 中,你可以使用 DestinationRule 來定義對服務的流量策略,比如指定不同版本的服務之間的流量比例、超時設置、負載均衡策略等。
  3. VirtualService

    • 作用:VirtualService 定義了請求應該如何路由到目標服務,以及對請求的修改和轉發。
    • 場景:通過 VirtualService,你可以定義基於請求的流量路由規則,比如根據請求的路徑、主機名等條件將請求路由到不同的服務版本,還可以定義請求的修改和轉發。

這些概念一起構成了 Istio 中強大的流量管理功能,使得你能夠靈活地定義和控制服務之間的通信和流量行爲。通過合理使用這些概念,你可以實現諸如流量控制、故障恢復、A/B 測試、藍綠部署等高級的流量管理策略。

除了 ServiceEntry、DestinationRule 和 VirtualService,Istio 還有一些其他重要的對象和資源,用於定義和配置服務網格中的各種功能和策略。以下是一些常見的 Istio 對象:

  1. Gateway:用於定義 Istio 網格的入口點,允許流量進入網格並將其路由到適當的服務。

  2. Sidecar:在 Istio 中,每個部署的應用程序都會有一個稱爲 sidecar 的邊車代理,它負責處理應用程序的所有網絡通信,並與 Istio 控制平面進行交互。

  3. AuthorizationPolicy:用於定義對服務之間流量的訪問控制策略,包括認證、授權和安全策略等。

  4. PeerAuthentication:用於定義服務之間的雙向 TLS 認證策略,確保服務之間的通信是安全的。

  5. RequestAuthentication:用於定義對服務端點的請求認證策略,可以限制來自客戶端的請求必須滿足特定的認證條件。

  6. EnvoyFilter:允許你爲 Envoy 代理定義自定義的過濾器和配置,以實現更靈活的流量控制和處理。

  7. ServiceRoleServiceRoleBinding:用於定義對服務的角色和權限控制,可以限制哪些服務具有對其他服務的訪問權限。

這些對象一起構成了 Istio 的配置模型,通過對這些對象的定義和配置,你可以實現對服務網格中流量管理、安全性、可觀察性等方面的精細控制。因此,熟悉並理解這些對象是使用 Istio 進行微服務架構管理的關鍵。

在istio中,Gateway,DestinationRule和VirtualService三者之間的關係可以用以下文字圖示表示:

         +-------------------+
         |      Gateway      |
         +---------+---------+
                   |
                   v
         +---------+---------+
         |  VirtualService  |
         +---------+---------+
                   |
                   v
         +---------+---------+
         |  DestinationRule |
         +-------------------+

在這個示意圖中,Gateway負責接收外部流量,並將其轉發到內部服務。VirtualService定義了流量的路由規則,指定了如何將接收到的流量發送到不同的目標服務或版本。而DestinationRule定義了服務之間的通信策略,包括負載均衡、故障恢復等。因此,這三者之間存在一定的依賴關係,Gateway接收流量並將其傳遞給VirtualService,而VirtualService再根據定義的規則將流量分發給後端服務,同時DestinationRule定義了這些後端服務之間的通信策略。

Envoy Sidecar模式

當你將Istio部署到Kubernetes集羣中時,它在外部流量轉發的過程中起到了以下作用:

  1. 流量管理:Istio通過Envoy sidecar代理來管理流量,可以實現流量控制、負載均衡、故障恢復和A/B測試等功能。

  2. 安全:Istio提供了服務間的認證、加密通信和訪問控制,確保了服務之間的安全通信。

  3. 監控:Istio收集並展示了整個服務網格的指標、日誌和跟蹤數據,幫助你更好地監控和調試服務。

  4. 策略執行:Istio可以通過定義網絡策略來限制流量,例如路由規則、重試策略、超時設置等。

下面是一個簡單的圖示,描述了在Kubernetes集羣中使用Istio的流量管理過程:

外部流量 -> Ingress -> Istio Ingress Gateway -> Envoy Sidecar -> 後端服務

在這個示意圖中,外部流量首先經過Kubernetes的Ingress,然後被Istio Ingress Gateway接管,並通過Envoy sidecar代理進行流量管理,最終到達後端服務。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章