﻿# 如何利用 kind 與 WSL 2 測試 Open Service Mesh (OSM) 範例應用程式

昨天在 [Kubernetes Summit 2020](https://k8s.ithome.com.tw/) 主持了一場[實戰工作坊](https://kubernetessummit.ithome.com.tw/2020/hol.html)，幫助大家體驗 [Istio](https://istio.io/) 強大的 Service Mesh 能力。今天在家抽空玩了一下原先由微軟主導的 [Open Service Mesh (OSM)](https://openservicemesh.io/)，這套最近貢獻給 [CNCF](https://www.cncf.io/) (雲端原生運算基金會)，[正式成為 CNCF 沙盒專案的成員](https://openservicemesh.io/blog/open-service-mesh-osm-accepted-into-cncf-as-a-sandbox-project/)之一，十分有發展潛力。本篇文章就帶大家試試這套 [SMI](https://smi-spec.io/) 服務網格介面 (Service Mesh Interface) 實作。

### 前置條件 (Prerequisites)

本篇雖然文章主要採用 Windows 10 與 WSL 2 進行實作，但事實上你只要有 Docker Engine 與 Shell 環境就可以進行操作：

-   Windows
    -   作業系統：Windows 10 版本 2004 (Build 19041) 以上
    -   安裝 [Docker Desktop for Windows](https://hub.docker.com/editions/community/docker-ce-desktop-windows) 穩定版本 (Stable)
    -   請先安裝好 [Chocolatey](https://chocolatey.org/install) 套件管理器
    -   執行的 [Ubuntu 20.04 LTS](https://www.microsoft.com/zh-tw/p/ubuntu-2004-lts/9n6svws3rx71) 作業系統環境請記得啟用 WSL 2 功能 (Optional)
        -   請記得透過 `wsl.exe --set-default-version 2` 將之後安裝的 Distro 預設都調整為 WSL 2 版本
        -   官方文件: [Windows Subsystem for Linux Installation Guide for Windows 10](https://learn.microsoft.com/en-us/windows/wsl/install-win10)
        -   可以參考 [使用 WSL 2 打造優質的多重 Linux 開發環境](/post/2020/07/26/Multiple-Linux-Dev-Environment-build-on-WSL-2) 文章進行調整環境
        -   可以到 **Microsoft Store** 安裝 [Ubuntu 20.04 LTS](https://www.microsoft.com/zh-tw/p/ubuntu-2004-lts/9n6svws3rx71) 應用程式
-   macOS
    -   作業系統：10.13 以上版本 (Catalina, Mojave, or High Sierra)
    -   安裝 [Docker Desktop on Mac](https://docs.docker.com/docker-for-mac/install/) 穩定版本 (Stable)
    -   請先安裝好 [Homebrew](https://brew.sh/zh-tw/) 套件管理器
-   Linux
    -   作業系統: Ubuntu 20.04 LTS
    -   請參考 [Install Docker Engine on Ubuntu](https://docs.docker.com/engine/install/ubuntu/) 進行安裝

### 實作步驟

-   安裝各項建置工具 (`make`)
    
    ```bash
    sudo apt-get install build-essential -y
    ```
    
-   安裝 Go 建置環境
    
    ```bash
    curl -sLO https://golang.org/dl/go1.15.2.linux-amd64.tar.gz
    sudo tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
    export PATH=$PATH:/usr/local/go/bin
    go version
    ```
    
-   安裝 kind 工具
    
    ```bash
    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
    ```
    
-   下載 OSM CLI 工具
    
    ```bash
    curl -sLO https://github.com/openservicemesh/osm/releases/download/v0.4.0/osm-v0.4.0-linux-amd64.tar.gz
    tar zxvf osm-v0.4.0-linux-amd64.tar.gz --strip-components=1 -C /usr/local/bin/ linux-amd64/osm
    ```
    
-   下載 OSM 專案原始碼
    
    ```bash
    git clone https://github.com/openservicemesh/osm.git
    cd osm
    ```
    
-   建立 kind 叢集
    
    預設透過以下命令建立的 [kind](https://kind.sigs.k8s.io/) 叢集名稱為 `osm`，執行命令為 `./scripts/kind-with-registry.sh`，他會額外建立一個 Docker Registry 供 kind 叢集使用：
    
    ```bash
    make kind-up
    ```
    
    如果你想建立含有 [Docker Registry](https://docs.docker.com/registry/) 的 kind 叢集，可以參考 kind 網站的 [Local Registry](https://kind.sigs.k8s.io/docs/user/local-registry/) 文件。
    
    建立完成後，你可以用 `kind get clusters` 查詢所有已建立的 [kind](https://kind.sigs.k8s.io/) 叢集名稱。
    
-   建立範例應用程式
    
    先準備好 `.env` 環境變數檔 ([Declare default environment variables in file](https://docs.docker.com/compose/env-file/))
    
    ```bash
    cat .env.example > .env
    ```
    
    > 請修改 `.env` 檔案的內容，特別將 `export ENABLE_GRAFANA=true` 環境變數啟用（請取消這行的註解）
    
    正式建立 OSM 內建的 Demo 應用程式
    
    ```bash
    make kind-demo
    ```
    
    上述命令也會包含 `make kind-up` 動作，但如果你已經建立，他會自動跳過建立 kind 叢集的動作。
    
-   建立 Web UI 連線
    
    預設該應用程式所有的 Service Type 都是以 `ClusterIP` 為主，並沒有對外連接的端點。因此你可以透過 `kubectl port-forward` 命令，快速建立本機網路可連接的端點。以下是 OSM 內建的命令，可一次開啟所有 Web UI 的連線。
    
    > 相關連結：[Use Port Forwarding to Access Applications in a Cluster](https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/)
    
    ```bash
    /scripts/port-forward-all.sh
    ```
    
    -   想看 **Bookbuyer** 網站，可開啟 [http://localhost:8080/](http://localhost:8080/) 網址
    -   想看 **Bookstore v1** 網站，可開啟 [http://localhost:8081/](http://localhost:8081/) 網址
    -   想看 **Bookstore v2** 網站，可開啟 [http://localhost:8082/](http://localhost:8082/) 網址
    -   想看 **BookThief** 網站，可開啟 [http://localhost:8083/](http://localhost:8083/) 網址
    -   想看 **[Jaeger](https://www.jaegertracing.io/)** 介面，可開啟 [http://localhost:16686/](http://localhost:16686/) 網址
    -   想看 **[Grafana](https://grafana.com/)** 介面，可開啟 [http://localhost:3000/](http://localhost:3000/) 網址 (預設帳號密碼為 `admin`/`admin`)
    -   OSM controller 提供了一個簡易的偵錯用的 Web 端點，請開啟 [http://localhost:9091/debug](http://localhost:9091/debug) 網址
-   刪除 kind 叢集
    
    ```bash
    make kind-reset
    ```
    

### 相關連結

-   [Open Service Mesh](https://openservicemesh.io/)
    -   [openservicemesh/osm](https://github.com/openservicemesh/osm/) (GitHub)
-   [SMI | A standard interface for service meshes on Kubernetes](https://smi-spec.io/)
-   [微軟推自家服務網格Open Service Mesh | iThome](https://www.ithome.com.tw/news/139247)
