1. 程式人生 > >15分鐘在筆記本上搭建 Kubernetes + Istio開發環境

15分鐘在筆記本上搭建 Kubernetes + Istio開發環境

實例 整合 cpu iyu -- win yun power fault

技術分享圖片
11月13~15日,KubeCon 上海大會召開,雲原生是這個秋天最火熱的技術。很多同學來問如何上手 Kubernetes和Istio 服務網格開發。本文將幫助你利用Docker CE桌面版,15分鐘在筆記本上從零搭建 Kubernetes + Istio開發環境,開啟雲原生之旅。

說明:本文測試通過環境 Docker CE 18.09 (Kubernetes 1.10.3) 以及 Istio 1.0.4

先決條件,你需要一個 Docker for Mac或者Docker for Windows的安裝包,如果沒有請下載下載 Docker CE最新版本。由於Kubernetes大量的容器鏡像在 gcr.io, 無法在國內保證穩定的訪問。我們提供了一些工具腳本,幫助從阿裏雲鏡像服務下載所需鏡像

首先,

git clone https://github.com/AliyunContainerService/k8s-for-docker-desktop
cd k8s-for-docker-desktop
Docker for Mac 開啟 Kubernetes
為 Docker daemon 配置 Docker Hub 的中國官方鏡像加速 https://registry.docker-cn.com

技術分享圖片

可選操作: 為 Kubernetes 配置 CPU 和 內存資源,建議分配 4GB 或更多內存。

技術分享圖片

預先從阿裏雲Docker鏡像服務下載 Kubernetes 所需要的鏡像, 可以通過修改 images.properties 文件加載你自己需要的鏡像

./load_images.sh
開啟 Kubernetes,並等待 Kubernetes 開始運行

技術分享圖片

Docker for Windows 開啟 Kubernetes
為 Docker daemon 配置 Docker Hub 的中國官方鏡像加速 https://registry.docker-cn.com

技術分享圖片

可選操作: 為 Kubernetes 配置 CPU 和 內存資源,建議分配 4GB 或更多內存。

技術分享圖片

預先從阿裏雲Docker鏡像服務下載 Kubernetes 所需要的鏡像, 可以通過修改 images.properties 文件加載你自己需要的鏡像

使用 Bash shell

./load_images.sh

使用 PowerShell

.\load_images.ps1
說明: 如果因為安全策略無法執行 PowerShell 腳本,請在 “以管理員身份運行” 的 PowerShell 中執行 Set-ExecutionPolicy RemoteSigned 命令。

開啟 Kubernetes,並等待 Kubernetes 開始運行

技術分享圖片

配置 Kubernetes
可選操作: 切換Kubernetes運行上下文至 docker-for-desktop

kubectl config use-context docker-for-desktop
驗證 Kubernetes 集群狀態

kubectl cluster-info
kubectl get nodes
部署 Kubernetes dashboard

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

kubectl create -f kubernetes-dashboard.yaml
開啟 API Server 訪問代理

kubectl proxy
通過如下 URL 訪問 Kubernetes dashboard

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/overview?namespace=default

說明:如果測試 Istio,不需要安裝 Ingress,如果需要 Ingress 可以參考 https://github.com/AliyunContainerService/k8s-for-docker-desktop 中 Ingress相關章節

安裝 Helm
可以根據文檔安裝 helm https://github.com/helm/helm/blob/master/docs/install.md

在 Mac OS 上安裝

Use homebrew on Mac

brew install kubernetes-helm

Install Tiller into your Kubernetes cluster

helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.11.0 --skip-refresh

update charts repo (Optional)

helm repo update
在Windows上安裝

Use Chocolatey on Windows

choco install kubernetes-helm

Install Tiller into your Kubernetes cluster

helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.11.0 --skip-refresh

update charts repo (Optional)

helm repo update
配置 Istio
可以根據文檔安裝 Istio https://istio.io/docs/setup/kubernetes/

下載 Istio 1.0.4 並安裝 CLI
curl -L https://git.io/getLatestIstio | sh -
cd istio-1.0.4/
export PATH=$PWD/bin:$PATH
在Windows上,您可以手工下載Istio安裝包,或者把getLatestIstio.ps1拷貝到你希望下載 Istio 的目錄,並執行 - 說明:根據社區提供的安裝腳本修改而來

.\getLatestIstio.ps1
通過 Helm chart 安裝 Istio
helm install install/kubernetes/helm/istio --name istio --namespace istio-system
查看 istio 發布狀態
helm status istio
為 default 名空間開啟自動 sidecar 註入
kubectl label namespace default istio-injection=enabled
kubectl get namespace -L istio-injection
安裝 Book Info 示例
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
確認示例應用在運行中

export GATEWAY_URL=localhost:80
curl -o /dev/null -s -w "%{http_code}\n" http://${GATEWAY_URL}/productpage
可以通過瀏覽器訪問

http://localhost/productpage

說明:如果當前80端口已經被占用或保留,我們可以編輯 install/kubernetes/helm/istio/values.yaml 文件中
Gateway 端口進行調整,比如將 80 端口替換為 8888 端口

  ## You can add custom gateway ports
- port: 8888  # Changed from 80
  targetPort: 80
  name: http2
  nodePort: 31380

然後執行如下命令並生效

kubectl delete service istio-ingressgateway -n istio-system
helm upgrade istio install/kubernetes/helm/istio
學習 Istio
大家參照 Istio 開始學習吧, https://istio.io/zh/docs/examples/bookinfo/

技術分享圖片

刪除實例應用
samples/bookinfo/platform/kube/cleanup.sh
卸載 Istio
helm del --purge istio
kubectl delete -f install/kubernetes/helm/istio/templates/crds.yaml -n istio-system
總結
阿裏雲Kubernetes服務 (ACK) 已經對 Istio提供了支持,並將Istio和阿裏雲進行了深度整合,比如可以通過阿裏雲日誌服務支持基於Jaeger的全鏈路監控,基於雲監控提升Istio可觀測性和報警能力等。後續也將進一步簡化Kubernetes和Istio的開發者體驗,讓大家方便地把 Kubernetes + Istio 應用無縫遷移上雲。

15分鐘在筆記本上搭建 Kubernetes + Istio開發環境