iT邦幫忙

2024 iThome 鐵人賽

DAY 24
0
Kubernetes

從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構系列 第 24

Day 24 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - istio 服務發現與負載平衡

  • 分享至 

  • xImage
  •  

服務發現

在Istio的Control Palne中,Pilot負責服務發現的相關工作。在Kubernetes 環境中,當有服務在Kubernetes 叢集中部屬時,Kubernetes 的服務註冊機制會記錄該服務的相關訊息。此時,Pilot會從Kubernetest API Server獲取這些訊息,並將這些資訊分發至各個Envoy代理中,且每個Envoy代理會更新配置已加入新部屬的服務訊息。

負載平衡

負載平衡是一種流量分配的機制,好的負載平衡策略可以提升效能,使用狀態適合的服務處理對應的流量,istio有提供基本的負載平衡策略,可以分別嘗試不同策略的效果,並選擇合適自己的策略。

這邊提供一個範例服務helloworld-v1與helloworld-v2

  • Deployment
apiVersion: v1
kind: Service
metadata:
  name: helloworld-service
spec:
  type: ClusterIP
  selector:
    app: helloworld
  ports:
    - protocol: TCP
      name: http
      port: 5000
      targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v1
  labels:
    app: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v1
  template:
    metadata:
      labels:
        app: helloworld
        version: v1
    spec:
      containers:
      - name: helloworld-v1
        image: allenku0/hello-api:v1
        ports:
        - containerPort: 5000
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloworld-v2
  labels:
    app: helloworld
spec:
  replicas: 1
  selector:
    matchLabels:
      app: helloworld
      version: v2
  template:
    metadata:
      labels:
        app: helloworld
        version: v2
    spec:
      containers:
      - name: helloworld-v2
        image: allenku0/hello-api:v2
        ports:
        - containerPort: 5000
        resources:
          limits:
            cpu: 500m
          requests:
            cpu: 200m
  • VirtualService
    VirtualService:用於定義流量路由的規則,控制進入 Service Mesh 中的流量路由
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: hello-vs
spec:
  hosts:
  - "*"
  gateways:
  - hello-gateway
  http:
  - match:
    - uri:
        prefix: /api
    route:
    - destination:
        host: helloworld-service
        port:
          number: 5000
      
  • DestinationRule
    DestinationRule 負責定義服務的策略,如負載平衡、Connection Pool與融斷等。
  1. Round Robin
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: example-destination
spec:
  host: example-service
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN
  1. Random
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: example-destination
spec:
  host: example-service
  trafficPolicy:
    loadBalancer:
      simple: RANDOM
  1. Least Request
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: example-destination
spec:
  host: example-service
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN

上一篇
Day 23 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - istio 在 Kubernetes 安裝
下一篇
Day 25 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - istio 流量控制(金絲雀部屬)
系列文
從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構26
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言