在Istio的Control Palne中,Pilot負責服務發現的相關工作。在Kubernetes 環境中,當有服務在Kubernetes 叢集中部屬時,Kubernetes 的服務註冊機制會記錄該服務的相關訊息。此時,Pilot會從Kubernetest API Server獲取這些訊息,並將這些資訊分發至各個Envoy代理中,且每個Envoy代理會更新配置已加入新部屬的服務訊息。
負載平衡是一種流量分配的機制,好的負載平衡策略可以提升效能,使用狀態適合的服務處理對應的流量,istio有提供基本的負載平衡策略,可以分別嘗試不同策略的效果,並選擇合適自己的策略。
這邊提供一個範例服務helloworld-v1與helloworld-v2
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
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
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: example-destination
spec:
host: example-service
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: example-destination
spec:
host: example-service
trafficPolicy:
loadBalancer:
simple: RANDOM
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: example-destination
spec:
host: example-service
trafficPolicy:
loadBalancer:
simple: LEAST_CONN