owned this note changed 4 years ago
Linked with GitHub

體驗 Istio 強大的 Service Mesh 能力

歡迎來到 Kubernetes Summit'20 共筆

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

共筆入口:https://hackmd.io/@k8ssummit/20
手機版請點選上方 按鈕展開議程列表。

這是 Will 保哥Kubernetes Summit 2020 實戰工作坊 體驗 Istio 強大的 Service Mesh 能力 的課前準備文件。請有意參加這場實戰工作坊的朋友,請務必事先安裝好實作環境!

回報實作進度: https://github.com/doggy8088/IstioHOL/issues

課前準備

前置條件 (Prerequisites)

本次實戰工作坊需要學員參考本篇文章進行環境設定,以確保實作順利:

安裝 KinD 命令列工具

  • On Mac (homebrew)

    ​​​​brew install kind
    
  • On Windows

    使用 Chocolatey 安裝 Kind 套件

    ​​​​choco install kind -y
    

    如果你已經安裝過 Docker Desktop for Windows 的話,請改用以下命令安裝:

    ​​​​choco install kind -i -y
    

    也可透過 PowerShell 下載 Windows 執行檔

    ​​​​curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.9.0/kind-windows-amd64
    ​​​​Move-Item .\kind-windows-amd64.exe c:\some-dir-in-your-PATH\kind.exe
    
  • On Linux

    ​​​​curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.9.0/kind-linux-amd64
    ​​​​chmod +x ./kind
    ​​​​sudo mv ./kind /usr/local/bin/kind
    
  • 驗證安裝版本

    ​​​​kind --version
    
    kind version 0.9.0
    

安裝 Kubectl 命令列工具

  • On Mac (homebrew)

    ​​​​brew install kubectl
    
  • On Windows

    ​​​​choco install kubernetes-cli -y
    
  • On Linux

    ​​​​curl -sLO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
    ​​​​chmod +x ./kubectl
    ​​​​sudo mv ./kubectl /usr/local/bin/kubectl
    
  • 驗證安裝版本

    ​​​​kubectl version --client --short
    
    Client Version: v1.19.1
    

安裝 kubectx 與 kubens 工具

  • 安裝 kubectx 與 kubens 工具

    ​​​​sudo apt install pkg-config -y
    
    ​​​​wget https://github.com/ahmetb/kubectx/releases/download/v0.9.1/kubectx_v0.9.1_linux_x86_64.tar.gz
    ​​​​wget https://github.com/ahmetb/kubectx/releases/download/v0.9.1/kubens_v0.9.1_linux_x86_64.tar.gz
    
    ​​​​sudo tar zxvf kubectx_v0.9.1_linux_x86_64.tar.gz -C /usr/local/bin kubectx
    ​​​​sudo tar zxvf kubens_v0.9.1_linux_x86_64.tar.gz -C /usr/local/bin kubens
    
    ​​​​sudo git clone https://github.com/ahmetb/kubectx.git /etc/kubectx
    ​​​​COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
    ​​​​sudo ln -sf /etc/kubectx/completion/kubens.bash $COMPDIR/kubens
    ​​​​sudo ln -sf /etc/kubectx/completion/kubectx.bash $COMPDIR/kubectx
    
    ​​​​rm kubectx_v0.9.1_linux_x86_64.tar.gz kubens_v0.9.1_linux_x86_64.tar.gz
    

安裝 k9s 工具

  • 安裝 k9s 工具

    ​​​​wget https://github.com/derailed/k9s/releases/download/v0.21.7/k9s_Linux_x86_64.tar.gz
    ​​​​sudo tar -zxvf k9s_Linux_x86_64.tar.gz -C /usr/local/bin k9s
    
  • 驗證安裝版本

    ​​​​k9s info
    

建立 KinD 叢集

建立一個有 1 個 control-plane nodes 與 1 個 worker nodes 的 KinD 叢集:

  1. 先建立 kind-2nodes.yaml 檔案

    ​​​​kind: Cluster
    ​​​​apiVersion: kind.x-k8s.io/v1alpha4
    ​​​​nodes:
    ​​​​- role: control-plane
    ​​​​- role: worker
    ​​​​  extraPortMappings:
    ​​​​  - containerPort: 30080
    ​​​​    hostPort: 80
    ​​​​  - containerPort: 30443
    ​​​​    hostPort: 443
    ​​​​  - containerPort: 32034
    ​​​​    hostPort: 32034
    ​​​​  - containerPort: 30291
    ​​​​    hostPort: 30291
    ​​​​  - containerPort: 31538
    ​​​​    hostPort: 31538
    ​​​​  - containerPort: 31952
    ​​​​    hostPort: 31952
    ​​​​  - containerPort: 31813
    ​​​​    hostPort: 31813
    
  2. 建立叢集

    ​​​​kind create cluster --config ./kind-2nodes.yaml
    
  3. 如果你從 Windows 建立叢集,但想在 WSL 2 操作 kubectl 的話,請執行以下命令:

    ​​​​kind export kubeconfig
    
  4. 測試 kubectl 叢集連線

    ​​​​kubectl cluster-info
    ​​​​kubectl get nodes
    
    ​​​​$ kubectl cluster-info
    ​​​​Kubernetes master is running at https://127.0.0.1:25783
    ​​​​KubeDNS is running at https://127.0.0.1:25783/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    
    ​​​​To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
    
    ​​​​$ kubectl get nodes
    ​​​​NAME                  STATUS   ROLES    AGE   VERSION
    ​​​​kind-control-plane   Ready    master   22m   v1.19.1
    ​​​​kind-worker          Ready    <none>   21m   v1.19.1
    

測試 Kubernetes 與 Networking 可否正常運作

  1. 建立 mynginx.yaml 檔案

    ​​​​cat <<EOF > mynginx.yaml
    ​​​​apiVersion: v1
    ​​​​kind: Service
    ​​​​metadata:
    ​​​​  name: web-nginx
    ​​​​  labels:
    ​​​​    app.kubernetes.io/name: nginx
    ​​​​spec:
    ​​​​  type: NodePort
    ​​​​  ports:
    ​​​​    - name: http
    ​​​​      port: 80
    ​​​​      targetPort: http
    ​​​​      nodePort: 30080
    ​​​​  selector:
    ​​​​    app.kubernetes.io/name: nginx
    ​​​​---
    ​​​​apiVersion: apps/v1
    ​​​​kind: Deployment
    ​​​​metadata:
    ​​​​  name: web-nginx
    ​​​​  labels:
    ​​​​    app.kubernetes.io/name: nginx
    ​​​​spec:
    ​​​​  selector:
    ​​​​    matchLabels:
    ​​​​      app.kubernetes.io/name: nginx
    ​​​​  replicas: 2
    ​​​​  template:
    ​​​​    metadata:
    ​​​​      labels:
    ​​​​        app.kubernetes.io/name: nginx
    ​​​​    spec:
    ​​​​      containers:
    ​​​​        - name: nginx
    ​​​​          image: docker.io/bitnami/nginx:1.19.1-debian-10-r23
    ​​​​          ports:
    ​​​​            - name: http
    ​​​​              containerPort: 8080
    ​​​​EOF
    
  2. 建立 mynginx 服務

    ​​​​kubectl apply -f mynginx.yaml
    
  3. 測試服務連線是否正常

    http://localhost/

    Welcome to nginx!

  4. 移除 mynginx 服務

    ​​​​kubectl delete -f ./mynginx.yaml
    

Istio 新手上路

安裝 Istio 服務

  1. Istio

    ​​​​curl -sL https://istio.io/downloadIstio | sh -
    ​​​​cd istio-1.*
    ​​​​export PATH=$PWD/bin:$PATH
    
    ​​​​Downloading istio-1.7.2 from https://github.com/istio/istio/releases/download/1.7.2/istio-1.7.2-linux-amd64.tar.gz ...
    
    ​​​​Istio 1.7.2 Download Complete!
    
    ​​​​Istio has been successfully downloaded into the istio-1.7.2 folder on your system.
    
    ​​​​Next Steps:
    ​​​​See https://istio.io/latest/docs/setup/install/ to add Istio to your Kubernetes cluster.
    
    ​​​​To configure the istioctl client tool for your workstation,
    ​​​​add the /home/will/build/istio-1.7.2/bin directory to your environment path variable with:
    ​​​​        export PATH="$PATH:/home/will/build/istio-1.7.2/bin"
    
    ​​​​Begin the Istio pre-installation check by running:
    ​​​​        istioctl x precheck
    
    ​​​​Need more information? Visit https://istio.io/latest/docs/setup/install/
    
  2. 安裝 Istio 之前的檢查

    ​​​​istioctl x precheck
    
  3. 安裝 Istio 服務 (Installation Guides)

    我們將用 Install with Istioctl 文件的說明進行安裝。

    測試環境可以用 demo 這個 Profile 來進行安裝,但正式環境不建議使用 demo Profile 喔!

    ​​​​istioctl install --set profile=demo
    

    更多 Profile 請參考 Installation Configuration Profiles 文件。

  4. 設定 default 這個 namespace 啟用自動注入 Sidecar 的 Label

    ​​​​kubectl label namespace default istio-injection=enabled
    

    列出目前所有 namespace 的 istio-injection 狀態

    ​​​​kubectl get namespace -L istio-injection
    
  5. 部署 Istio 範例應用程式 (bookinfo)

    ​​​​kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
    
    ​​​​kubectl get services
    
    ​​​​kubectl get pods
    
    ​​​​kubectl exec -it $(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}') -c ratings -- curl productpage:9080/productpage | grep -o "<title>.*</title>"
    
  6. 開放應用程式給外部網路連線

    ​​​​kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
    

    驗證 Istio 設定是否正確 (因為太容易設定錯誤了,特別寫了一個工具專門用來檢查)

    ​​​​istioctl analyze
    
  7. 決定 ingress 的 IP 與 Ports

    先取得 istio-system 負責對外的 istio-ingressgateway 服務資訊

    ​​​​kubectl get svc istio-ingressgateway -n istio-system
    

    由於 KinD 內的服務沒辦法透過 type: LoadBalancer 取得 IP 地址,因此必須手動改成 type: NodePort 才行!

    Service | Kubernetes > Type NodePort

    請修改 spec.typeNodePort,並調整 .spec.ports[*].nodePort 的 Port 埠號。

    • name: http2nodePort 調整為 30080
    • name: httpsnodePort 調整為 30443
  8. 在 Windows 10 開啟瀏覽器,透過 Istio Gateway 瀏覽網頁!

    當你連到 http://localhost/ 將會看到 HTTP 404 錯誤頁面。

    當你連到 http://localhost/productpage 就會看到 BookInfo Sample 的首頁!

    重新整理 http://localhost/productpage 網頁可以看到不同的 Book Reviews 內容(來自不同的 Pod 執行結果)

服務監控

安裝 Grafana 與 Prometheus 服務

  1. 安裝 GrafanaPrometheus

    ​​​​kubectl apply -f samples/addons/prometheus.yaml -f samples/addons/grafana.yaml
    
  2. 開啟 Grafana dashboard 查看 Istio 相關監控數據

    透過以下命令可以啟動一個 Proxy 端點,讓你可以透過本機進行瀏覽

    請注意:每次執行 istioctl dashboard 可能會產生不同的 Ports

    ​​​​$ istioctl dashboard grafana
    ​​​​http://localhost:3000
    ​​​​Failed to open browser; open http://localhost:3000 in your browser.
    

    istio / Istio Control Plane Dashboard

  3. 壓力測試

    ​​​​kubectl exec -it $(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}') -c ratings -- bash
    ​​​​curl -s productpage:9080/productpage | grep -o "<title>.*</title>"
    
    ​​​​while true; do curl -s productpage:9080/productpage | grep -o "<title>.*</title>"; done
    

流量監控

安裝 Kiali 服務

  1. 安裝 Kiali 為 Istio Addons

    ​​​​kubectl apply -f samples/addons/kiali.yaml
    

    如果執行時有出現錯誤訊息,請重新執行一遍。

  2. 開啟 Kiali dashboard 查看 Istio 的所有流量資訊

    透過以下命令可以啟動一個 Proxy 端點,讓你可以透過本機進行瀏覽

    請注意:每次執行 istioctl dashboard kiali 可能會產生不同的 Ports

    ​​​​$ istioctl dashboard kiali
    ​​​​http://localhost:20001/kiali
    ​​​​Failed to open browser; open http://localhost:20001/kiali in your browser.
    

    參考下圖進行報表顯示設定,對網站進行壓力測試後,就會看見流量的即時動畫!

    Kiali Console - Graph

  3. 壓力測試

    ​​​​kubectl exec -it $(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}') -c ratings -- bash
    ​​​​curl -s productpage:9080/productpage | grep -o "<title>.*</title>"
    
    ​​​​while true; do curl -s productpage:9080/productpage | grep -o "<title>.*</title>"; done
    

清空資源

移除 bookinfo 應用程式

kubectl delete -f samples/bookinfo/platform/kube/bookinfo.yaml

移除 Istio 安裝

kubectl label namespace default istio-injection-
kubectl get namespace -L istio-injection

istioctl x uninstall --purge
kubectl delete namespace istio-system

刪除 kind 叢集

kind delete cluster
tags: k8ssummit20 k8s
Select a repo