1. 程式人生 > 實用技巧 >ISTIO 學習筆記(一)之 安裝

ISTIO 學習筆記(一)之 安裝

一、背景

serviceMesh 目前 在服務治理方面作為新興技術 已逐步被使用,其對應的 產品為 Istio。

二、部署

1、前提條件

在物理伺服器上或虛擬機器中部署好 kubernetes叢集。 (參考:kubernetes 學習筆記之安裝)

2、部署 Istio

Istio目前最新版本為1.7.3,但是由於1.7.3提供的 demo profile 不支援預設安裝 grafana、kiali、prometheus、tracing 等第三方元件,但 1.6.12支援,且1.6.12和1.7.3 在架構上是一致的,所以選擇安裝1.7.3。

(1) 下載包

https://github.com/istio/istio/releases

中下載

(2)配置 istioctl

將istio-1.6.12-linux-amd64.tar.gz上傳到安裝kubernetes叢集的物理機或虛擬機器上,執行以下命令。

#切換到包所在目錄

cd /root/wf

#解壓包

tar -zxvf  istio-1.6.12-linux-amd64.tar.gz

#重新命名

mv istio-1.6.12 istio

#新增環境變數

vi /etc/profile

export PATH=/root/wf/istio/bin:$PATH

#修改的環境變數生效

source /etc/profile

#驗證istioctl命令是否存在

istioctl

(3)安裝 istio

a)檢視 profile demo

說明:

  • addonComponents:表示要安裝的第三方元件,這裡顯示:istiocoredns不預設安裝,grafana\kiali\prometheus\tracing預設安裝;
  • components:表示istio內部元件,這裡顯示,citadel\cni不預設安裝。

b)執行部署命令

為了最大限度的能夠使用istio的特性功能,這裡選擇安裝所有支援的addonComponents及components。

istioctl manifest apply --set profile=demo --set addonComponents.istiocoredns.enabled=true
--set components.citadel.enabled=true --set components.cni.enabled=true

c)配置 kiali kubernetes 叢集外可訪問

  • 在kubernetes中建立gateway及vritualservice,檔案資訊如下:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: kiali
  namespace: istio-system
spec:
  gateways:
  - kiali-gateway  #該虛擬服務的路由規則適用於流入kiali-gateway閘道器的流量
  hosts:
  - "*"
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: kiali
        port:
          number: 20001
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: kiali-gateway
  namespace: istio-system
spec:
  selector:
    istio: ingressgateway   #使用 istio 提供的 ingressgateway服務
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
          - "*"
  • gateway 及 vritualservice(PS:其實vritualservice可以直接用於所有叢集入口流量路由的配置)
#部署gateway\virtualservice
kubectl apply -f /root/wf/docker/kiali-virtualservice.yaml
#檢視istio-system 安裝的 istio-ingressgateway 對外提供的訪問埠
kubectl get svc -n istio-system

圖中顯示 istio-ingressgateway 80 對外對應的埠為30932

  • 配置 grafana 到 kiali 中
#檢視kiali的configmap內容,其實可以看到kiali掛載的配置檔案config.yaml 中grafana.url值為空
kubectl get cm -n istio-system kiali -o yaml
#編輯configmap
kubectl edit cm –n istio-system kiali

配置完成後,可以在services/workload下的 Inbound Metrics/ Outbound Metrics 看到內容。

(3)部署 bookinfo 示例程式

  • 部署
#檢視叢集中所有的名稱空間
kubectl get namespaces
#檢視default名稱空間的資訊
kubectl describe ns default
#給default名稱空間新增標籤,該標籤新增成功則部署在該名稱空間中的pod會自動注入sidecar容器
kubectl label namespace default istio-injection=enabled
#部署bookinfo 官方示例程式
kubectl apply-f /root/wf/istio/samples/bookinfo/platform/kube/ bookinfo.yaml
#檢查是否bookinfo相關的pod都是running狀態
kubectl get pods 
#當發現通過以上部署pod時,未建立pod且原因 為 sidecar容器自動失敗,此時可以採用手動注入方式
#解除安裝bookinfo示例程式
kubectl delete -f /root/wf/istio/samples/bookinfo/platform/kube/ bookinfo.yaml
#手動注入sidecar容器
istioctl kube-inject -f /root/wf/istio/samples/bookinfo/platform/kube/ bookinfo.yaml |kubectl apply -f -
  • 配置 bookinfo kubernetes 叢集外可訪問

在配置kiali的時候已經建立了針對kubernetes叢集外訪問叢集內服務的gateway,因此這裡直接使用這個gateway,現在要做的是建立virtualservice,內容如下:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  hosts:
  - "*"
  gateways:
  - istio-system/kiali-gateway
  http:
  - match:
    - uri:
        exact: /productpage
    - uri:
        prefix: /static
    - uri:
        exact: /login
    - uri:
        exact: /logout
    - uri:
        prefix: /api/v1/products
    route:
    - destination:
        host: productpage
        port:
            number: 9080

執行命令:

kubectl apply –f /root/wf/istio/samples/bookinfo/networking/bookinfo-gateway.yaml

在瀏覽器輸入 http://x.x.x.x:30932/productpage 即可訪問叢集內的 bookinfo系統,不斷重新整理會發現頁面右側的星級狀態一直在發生變化。

三、部署過程中遇到的問題

1、映象拉取不成功

(1)在通過istioctl manifest部署的時候,會遇到timeout的情況,導致部署不成功。

排查方法:

#istio內部元件及第三方元件預設安裝在 istio-system名稱空間
#檢視該名稱空間上的所有pod,會看到部分不是running的狀態
kubectl get pods –n istio-system
#檢視失敗的pod的描述資訊,在events 一欄可以看到錯誤資訊,這裡會提示是imagepull失敗,xxx 為 pod name
kubectl describe pod xxx –n istio-system

解決辦法:

方法一:讓kubernetes 所屬伺服器能夠連上外網(這種是最好的,這樣部署的時候,會自動從外網拉取所需映象檔案);

方法二:在方法一不可行的情況下使用,提前下載好所有需要的image.

a)在kubernetes伺服器上,通過以下命令獲取所有需要下載的image

kubectl get deployment –n istio-system –o yaml > test.yaml

開啟test.yaml檔案,其中image 的值就是需要的image.

grafana/grafana:6.7.4
docker.io/istio/proxyv2:1.6.12
docker.io/jaegertracing/all-in-one:1.16
coredns/coredns:1.6.2
istio/coredns-plugin:0.2-istio-1.1
docker.io/istio/pilot:1.6.12
quay.io/kiali/kiali:v1.18
docker.io/prom/prometheus:v2.15.1
docker.io/istio/install-cni:1.6.12

b)使用一臺可以連線外網的伺服器,且該伺服器上已經安裝了docker;

c)在可連線外網的伺服器上執行以下命令,save image

docker pull xxxx
docker images
docker save –o /root/wf/kiali.tar quay.io/kiali/kiali:v1.18

d) 在kubernetes 伺服器上,load image 到docker中(PS: kubernetes叢集的master 和worker node所屬伺服器都要操作)

將連線外網上save下來 image檔案都上傳到kubernetes所屬伺服器上,並執行以下命令。

#載入映象檔案
docker load -i kiali.tar
#顯示本地docker中所有的映象檔案
docker images

e)重新部署 istio

#解除安裝
istioctl manifest generate --set profile=demo --set addonComponents.istiocoredns.enabled=true --set components.citadel.enabled=true --set components.cni.enabled=true | kubectl delete -f -
#安裝
istioctl manifest apply --set profile=demo --set addonComponents.istiocoredns.enabled=true --set components.citadel.enabled=true --set components.cni.enabled=true

(2)部署 bookinfo 中也會出現 pod 狀態部署 running,且是由於映象拉取失敗造成的,按照以上方式提前準備好映象。

#解除安裝
kubectl delete -f /root/wf/istio/samples/bookinfo/platform/kube/ bookinfo.yaml
#部署
kubectl apply-f /root/wf/istio/samples/bookinfo/platform/kube/ bookinfo.yaml

2、部署 bookinfo 發現對應的 pod 沒有建立

排查方法:

#檢視default名稱空間中所有已建立的 deployment
kubectl getdeployment 
#檢視default名稱空間中 name為details-v1 的deployment資訊
kubectl describe deployment details-v1
#檢視副本資源資料資訊
kubectl describe rs/details-v1-5974b67c8

有時副本資源報的錯誤資訊如下:

通過如下命令也可以檢視到錯誤日誌:

kubectl logs kube-controller-manager-master1 -n kube-system

或者

kubectl logs kube-apiserver-master1 -n kube-system

解決方法:

以上是網路外掛存在問題,在istio的deployment部署檔案中新增 hostNetwork:true,表示使用宿主機網路,即沒有問題。但是具體原因,好需要研究。