Bài học

Ngày 78 - Cài đặt và kiểm thử service mesh

Cài Istio, triển khai Bookinfo và kiểm tra ingress trong lab.

- 15 phút đọc

Nội dung khóa học

Ngày 78 - Cài đặt và kiểm thử service mesh
Mục lục

Lab Istio và Bookinfo

Sau Ngày 77, chúng ta cài Istio với demo profile, triển khai Bookinfo, kiểm tra sidecar và truy cập ứng dụng qua ingress gateway.

Bối cảnh phiên bản: Bài gốc dùng Istio 1.16.1 và Kubernetes 1.22–1.25. Đây là lab lịch sử, chưa chạy lại; hãy kiểm tra ma trận hỗ trợ và hướng dẫn Istio cho phiên bản hiện hành. Các lệnh thay đổi cluster và có thể tạo load balancer phát sinh phí; dùng cluster thử nghiệm và dọn tài nguyên khi hoàn tất.

Nguồn dùng cluster K3s ba node (mỗi node 2 CPU, 4 GB RAM), có cơ chế load balancer, cùng máy Linux hoặc macOS chạy istioctl. Các lựa chọn khác được nêu gồm kind, Minikube hoặc cluster cloud.

Cài Istio

Kiểm tra node và Pod:

kubectl get nodes -o wide
kubectl get pods -A

Nguồn tải installer:

curl -L https://istio.io/downloadIstio | sh -

Lệnh tải và chạy script từ internet; hãy đọc và xác minh nguồn, đồng thời chọn phiên bản phù hợp trước khi thực hiện. Script lấy phiên bản mới nhất nên thư mục có thể khác tên lịch sử bên dưới:

cd istio-1.16.1
export PATH=$PWD/bin:$PATH

Kiểm tra điều kiện:

istioctl x precheck
✔ No issues found when checking the cluster. Istio is safe to install or upgrade!
To get started, check out https://istio.io/latest/docs/setup/getting-started/

Cài demo profile:

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

Kiểm tra các thành phần:

kubectl get all -n istio-system
NAME                                        READY   STATUS    RESTARTS   AGE
pod/istiod-885df7bc9-f9k7c                  1/1     Running   0          31m
pod/istio-ingressgateway-6688c7f65d-szxf9   1/1     Running   0          31m
pod/istio-egressgateway-7475c75b68-mpxf7    1/1     Running   0          31m

NAME                           TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                                                                      AGE
service/istiod                 ClusterIP      10.43.249.242   <none>         15010/TCP,15012/TCP,443/TCP,15014/TCP                                        31m
service/istio-egressgateway    ClusterIP      10.43.75.47     <none>         80/TCP,443/TCP                                                               31m
service/istio-ingressgateway   LoadBalancer   10.43.51.40     bring.your.LB.IP   15021:31000/TCP,80:32697/TCP,443:30834/TCP,31400:30953/TCP,15443:30733/TCP   31m

NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/istiod                 1/1     1            1           31m
deployment.apps/istio-ingressgateway   1/1     1            1           31m
deployment.apps/istio-egressgateway    1/1     1            1           31m

NAME                                              DESIRED   CURRENT   READY   AGE
replicaset.apps/istiod-885df7bc9                  1         1         1       31m
replicaset.apps/istio-ingressgateway-6688c7f65d   1         1         1       31m
replicaset.apps/istio-egressgateway-7475c75b68    1         1         1       31m

External IP và tên Pod trong output là ví dụ và sẽ khác ở cluster của bạn. Demo profile dành cho thử nghiệm, không phải cấu hình production mặc định.

Inject sidecar và triển khai Bookinfo

Nguồn gắn nhãn namespace default để inject sidecar cho workload mới:

kubectl label namespace default istio-injection=enabled
kubectl get ns --show-labels

Thay đổi này ảnh hưởng các Pod mới trong namespace; dùng namespace lab riêng nếu phù hợp. Triển khai Bookinfo từ thư mục Istio đã tải:

kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
kubectl get all
NAME                                 READY   STATUS    RESTARTS   AGE
pod/details-v1-698b5d8c98-l8r5t      2/2     Running   0          7m30s
pod/reviews-v3-6dc9897554-8pgtx      2/2     Running   0          7m30s
pod/reviews-v1-9c6bb6658-lvzsr       2/2     Running   0          7m30s
pod/reviews-v2-8454bb78d8-9tm2j      2/2     Running   0          7m30s
pod/productpage-v1-bf4b489d8-q29jp   2/2     Running   0          7m29s
pod/ratings-v1-5967f59c58-s9f88      2/2     Running   0          7m30s

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/kubernetes    ClusterIP   10.43.0.1       <none>        443/TCP    80m
service/details       ClusterIP   10.43.198.240   <none>        9080/TCP   7m31s
service/ratings       ClusterIP   10.43.121.72    <none>        9080/TCP   7m30s
service/reviews       ClusterIP   10.43.173.247   <none>        9080/TCP   7m30s
service/productpage   ClusterIP   10.43.142.39    <none>        9080/TCP   7m30s

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/details-v1       1/1     1            1           7m30s
deployment.apps/reviews-v3       1/1     1            1           7m30s
deployment.apps/reviews-v1       1/1     1            1           7m30s
deployment.apps/reviews-v2       1/1     1            1           7m30s
deployment.apps/productpage-v1   1/1     1            1           7m30s
deployment.apps/ratings-v1       1/1     1            1           7m30s

NAME                                       DESIRED   CURRENT   READY   AGE
replicaset.apps/details-v1-698b5d8c98      1         1         1       7m30s
replicaset.apps/reviews-v3-6dc9897554      1         1         1       7m30s
replicaset.apps/reviews-v1-9c6bb6658       1         1         1       7m30s
replicaset.apps/reviews-v2-8454bb78d8      1         1         1       7m30s
replicaset.apps/productpage-v1-bf4b489d8   1         1         1       7m30s
replicaset.apps/ratings-v1-5967f59c58      1         1         1       7m30s

Các Pod có 2/2 container ready trong ví dụ, gồm ứng dụng và sidecar.

Kiểm tra kết nối nội bộ

Triển khai Pod sleep và kiểm tra:

kubectl apply -f samples/sleep/sleep.yaml
kubectl get pods
sleep-75bbc86479-xw5lw           2/2     Running   0          36s

Dùng tên Pod thực tế để gọi productpage:

kubectl exec sleep-75bbc86479-xw5lw -c sleep -- curl -sS productpage:9080
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
100  1683  100  1683    0     0  69202      0 --:--:-- --:--:-- --:--:-- 70125
<!DOCTYPE html>
<html>
<head>
    <title>Simple Bookstore App</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="static/bootstrap/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="static/bootstrap/css/bootstrap-theme.min.css">

</head>
<body>


<p>
    <h3>Hello! This is a simple bookstore application consisting of three services as shown below</h3>
</p>

Truy cập từ bên ngoài

Tạo cấu hình ingress gateway và virtual service:

kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

Xem external IP của service:

kubectl get svc -n istio-system

Nguồn đặt biến:

export INGRESS_HOST=[IP_from_Istio_ingress_gateway]
export INGRESS_PORT=80

Trong shell, thay [IP_from_Istio_ingress_gateway] bằng IP hoặc hostname thật. Nguồn gọi:

curl INGRESS_HOST:INGRESS_PORT/productpage

Hiệu đính: Lệnh nguồn thiếu $ trước tên biến; dùng curl "$INGRESS_HOST:$INGRESS_PORT/productpage". Đảm bảo load balancer và rule mạng cho phép kết nối.

Output minh họa của nguồn:

!DOCTYPE html>
<html>
<head>
    <title>Simple Bookstore App</title>

Tóm tắt

Chúng ta đã xem cài Istio, inject sidecar, triển khai Bookinfo và cấu hình ingress. Tìm hiểu thêm tại 70 Days of Service Mesh. Tiếp theo là Ngày 79.

Bài viết được chuyển ngữ từ Day 78 — 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.