Bài học

Ngày 80 - Cơ bản về quản lý lưu lượng Istio

Thực hành routing theo header và phân chia lưu lượng Bookinfo.

- 16 phút đọc

Nội dung khóa học

Ngày 80 - Cơ bản về quản lý lưu lượng Istio
Mục lục

Traffic management

Sau Ngày 79, chúng ta tìm hiểu routing, traffic splitting/shifting, phát hành phiên bản, mirroring và load balancing. Ví dụ dùng Bookinfo và Istio 1.16.1 từ Ngày 78; kiểm tra tài liệu phiên bản hiện hành trước khi thực hành.

Một luồng ví dụ là client → ingress → ProductPage → Reviews → Ratings. Lưu lượng nội bộ cũng có thể bắt đầu từ một workload trong mesh mà không đi qua ingress.

Gateway và sidecar

Istio ingress gateway nhận request bên ngoài, còn sidecar proxy xử lý lưu lượng của workload trong mô hình sidecar. Gateway và VirtualService phối hợp để chọn host, port và tuyến đích. TLS chỉ được bảo vệ khi được cấu hình phù hợp; ví dụ nguồn dùng HTTP port 80.

Xem file gateway:

cat istio-1.16.1/samples/bookinfo/networking/bookinfo-gateway.yaml
marinow@mwm1mbp networking % cat bookinfo-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"

Gateway chọn ingress bằng label, lắng nghe HTTP port 80 cho host wildcard. Service phía sau thường là ClusterIP trong ví dụ, nhưng việc dùng gateway không tự chuyển loại của mọi Service thành ClusterIP.

VirtualService và DestinationRule

VirtualService xác định quy tắc routing. Ví dụ nguồn trỏ các service tới subset v1:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - route:
    - destination:
        host: ratings
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: details
spec:
  hosts:
  - details
  http:
  - route:
    - destination:
        host: details
        subset: v1
---

DestinationRule định nghĩa subset theo label của backend và có thể cấu hình chính sách cho đích:

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3

ServiceEntry có thể bổ sung đích bên ngoài vào service registry của mesh; chính sách outbound cụ thể phụ thuộc cấu hình.

Routing theo phiên bản

Áp dụng DestinationRule và VirtualService mẫu:

cd istio-1.16.1
kubectl apply -f samples/bookinfo/networking/destination-rule-all.yaml
kubectl apply -f samples/bookinfo/networking/virtual-service-all-v1.yaml

Kiểm tra:

kubectl get vs && kubectl get dr
marinow@mwm1mbp istio-1.16.1 % kubectl get vs && kubectl get dr
NAME          GATEWAYS               HOSTS             AGE
bookinfo      ["bookinfo-gateway"]   ["*"]             4d15h
productpage                          ["productpage"]   14h
reviews                              ["reviews"]       14h
ratings                              ["ratings"]       14h
details                              ["details"]       14h
NAME          HOST          AGE
productpage   productpage   96s
reviews       reviews       96s
ratings       ratings       96s
details       details       96s

Khi refresh Bookinfo, ví dụ route tới reviews v1:

Reviews v1
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
---

Nguồn thay rule để header end-user: jason đi tới v2, các request khác tới v1:

cat samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1
kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml

Đăng nhập bằng jason trong ứng dụng mẫu để quan sát:

Reviews v2

Header là dữ liệu request; không nên xem giá trị header do client cung cấp là danh tính đã xác thực nếu không có kiểm soát phù hợp.

Chia lưu lượng

Xóa rule trước:

kubectl delete -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml

Xem cấu hình nguồn:

cat samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 70
    - destination:
        host: reviews
        subset: v3
      weight: 30

Nguồn mô tả tỷ lệ v1/v3 là 70/30, dù tên file chứa 50-v3. Nội dung file thực tế có thể khác theo phiên bản Istio; kiểm tra trước khi áp dụng:

kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-50-v3.yaml

Gửi request thử:

for i in {1..10}; do curl -s http://bookinfo.io/productpage | grep "reviews-v"; done
    <u>reviews-v3-6dc9897554-8pgtx</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v3-6dc9897554-8pgtx</u>
    <u>reviews-v3-6dc9897554-8pgtx</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>
    <u>reviews-v1-9c6bb6658-lvzsr</u>

Tỷ lệ được cấu hình là xác suất phân phối, không đảm bảo mỗi nhóm 10 request luôn có đúng 7 và 3. Kiểm tra số lượng mẫu đủ lớn và metric thực tế khi đánh giá.

Tóm tắt

Chúng ta đã xem Gateway, sidecar, VirtualService, DestinationRule và ServiceEntry, rồi thử routing theo header và trọng số. Tìm hiểu thêm tại 70 Days of Service Mesh. Tiếp theo là Ngày 81.

Bài viết được chuyển ngữ từ Day 80 — 90DaysOfDevOps 2023, thuộc dự án 90DaysOfDevOps; ghi công Michael Cade và các cộng tác viên. Bản tiếng Việt có hiệu đính, bổ sung ghi chú và được chia sẻ theo giấy phép Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International. Các ảnh minh họa được lấy từ bài gốc.