Istio 1.5.1部署及實戰(zhàn)

Istio

官方文檔 https://istio.io/zh/docs
部分資料參考:https://jicki.me/istio/2020/04/13/Istio-1.5/
https://jimmysong.io/istio-handbook/concepts/traffic-management-basic.html

一、基礎概念及原理

Istio 是什么?(衣撕丟)

  • 云平臺令使用它們的公司受益匪淺。但不可否認的是,上云會給 DevOps 團隊帶來壓力。為了可移植性,開發(fā)人員必須使用微服務來構建應用,同時運維人員也正在管理著極端龐大的混合云和多云的部署環(huán)境。 Istio 允許您連接、保護、控制和觀察服務。
  • 從較高的層面來說,Istio 有助于降低這些部署的復雜性,并減輕開發(fā)團隊的壓力。它是一個完全開源的服務網(wǎng)格,作為透明的一層接入到現(xiàn)有的分布式應用程序里。它也是一個平臺,擁有可以集成任何日志、遙測和策略系統(tǒng)的 API 接口。Istio 多樣化的特性使您能夠成功且高效地運行分布式微服務架構,并提供保護、連接和監(jiān)控微服務的統(tǒng)一方法。

服務網(wǎng)格 又是什么?

  • 服務網(wǎng)格 - 用來描述組成這些應用程序的微服務網(wǎng)絡以及它們之間的交互。隨著服務網(wǎng)格的規(guī)模和復雜性不斷的增長,它將會變得越來越難以理解和管理。它的需求包括服務發(fā)現(xiàn)、負載均衡、故障恢復、度量和監(jiān)控等。服務網(wǎng)格通常還有更復雜的運維需求,比如 A/B 測試、金絲雀發(fā)布、速率限制、訪問控制和端到端認證。

Istio 架構

image.png
  • Istio 服務網(wǎng)格從邏輯上分為數(shù)據(jù)平面和控制平面。
    數(shù)據(jù)平面? 由一組智能代理(Envoy)組成,被部署為 sidecar。這些代理通過一個通用的策略和遙測中心(Mixer)傳遞和控制微服務之間的所有網(wǎng)絡通信。
    控制平面? 管理并配置代理來進行流量路由。此外,控制平面配置 Mixer 來執(zhí)行策略和收集遙測數(shù)據(jù)。

Istio 組件

Envoy

  • Istio 使用 Envoy 代理的擴展版本。

  • Envoy是用 C++ 開發(fā)的高性能代理,用于協(xié)調(diào)服務網(wǎng)格中所有服務的入站和出站流量。

  • Envoy 代理是唯一與數(shù)據(jù)平面流量交互的 Istio 組件。

  • Envoy 代理被部署為服務的 sidecar,在邏輯上為服務增加了 Envoy 的許多內(nèi)置特性:
    1.動態(tài)服務發(fā)現(xiàn)
    2.負載均衡
    3.TLS 終端
    4.HTTP/2 與 gRPC 代理
    5.熔斷器
    6.健康檢查
    7.基于百分比流量分割的分階段發(fā)布
    8.故障注入
    9.豐富的指標

  • sidecar 代理模型
    sidecar ? 允許 Istio 提取大量關于流量行為的信號作為屬性。反之,Istio 可以在 Mixer 中使用這些屬性來執(zhí)行決 策,并將它們發(fā)送到監(jiān)控系統(tǒng),以提供整個網(wǎng)格的行為信息。
    sidecar ? 還允許您向現(xiàn)有的部署添加 Istio 功能,而不需要重新設計架構或重寫代碼。

  • Envoy 代理在 istio 中可以實現(xiàn)
    1.流量控制功能:? 通過豐富的 HTTP、gRPC、WebSocket 和 TCP 流量路由規(guī)則來執(zhí)行細粒度的流量控制。
    2.網(wǎng)絡彈性特性:? 重試設置、故障轉移、熔斷器和故障注入。
    3.安全性和身份驗證特性:? 執(zhí)行安全性策略以及通過配置 API 定義的訪問控制和速率限制。

Mixer

  • Mixer ? 是一個平臺無關的組件。Mixer 在整個服務網(wǎng)格中執(zhí)行訪問控制和策略使用,并從 Envoy 代理和其他服務收集遙測數(shù)據(jù)。代理提取請求級別屬性,并將其發(fā)送到 Mixer 進行評估。您可以在我們的 Mixer 配置文檔中找到更多關于屬性提取和策略評估的信息。
  • Mixer ? 包括一個靈活的插件模型。該模型使 Istio 能夠與各種主機環(huán)境和后端基礎設施進行交互。因此,Istio 從這些細節(jié)中抽象出 Envoy 代理和 Istio 管理的服務。

Pilot

  • Pilot ? 主要是為 Envoy sidecar 提供服務發(fā)現(xiàn)、用于智能路由的流量管理功能(例如,A/B 測試、金絲雀發(fā)布等)以及彈性功能(超時、重試、熔斷器等)。
  • Pilot ? 將控制流量行為的高級路由規(guī)則轉換為特定于環(huán)境的配置,并在運行時將它們傳播到 sidecar。
  • Pilot ? 將特定于平臺的服務發(fā)現(xiàn)機制抽象出來,并將它們合成為任何符合 Envoy API 的 sidecar 都可以使用的標準格式。
  • 平臺適配器 與 Envoy 交互圖 (平臺 ? 支持 kubernetes、Consul、gcp、Nomad等)
    1.平臺啟動一個服務的新實例,該實例通知其平臺適配器。
    2.平臺適配器使用 Pilot 抽象模型注冊實例。
    3.Pilot 將流量規(guī)則和配置派發(fā)給 Envoy 代理,來傳達此次更改。
    4.可以使用 Istio 的流量管理 API 通過 Pilot 優(yōu)化 Envoy 配置,以便對服務網(wǎng)格中的流量進行更細粒度地控制。


    image.png

Citadel

  • Citadel ? 通過內(nèi)置的身份和證書管理,可以支持強大的服務到服務以及最終用戶的身份驗證。
  • Citadel ? 可以用來升級服務網(wǎng)格中的未加密流量。
  • Citadel、operator ? 結合使用可以執(zhí)行基于服務身份的策略,而不是相對不穩(wěn)定的 3 層或 4 層網(wǎng)絡標識。
  • Citadel ? 使用 Istio 的授權特性來控制誰可以訪問您的服務。

Galley

  • Galley ? 是 Istio 的配置驗證、提取、處理和分發(fā)組件。它負責將其余的 Istio 組件與從底層平臺(例如 Kubernetes)獲取用戶配置的細節(jié)隔離開來。

二、安裝Istio Install

我這里 Kubernetes 版本為1.18 目前只有這么一個集群
  • 安裝 Istio 環(huán)境準備
    1.搭建 Kubernetes 集群, ( 請按照官方提供的兼容測試版本安裝 目前支持 1.14, 1.15, 1.16 )
    2.下載 Istio 項目包. 項目包內(nèi)包括(安裝文件、示例和 istioctl 命令行工具。)
    3.安裝 Istio. 通過 istioctl 客戶端工具直接安裝istio 到 Kubernetes 中.

搭建 Kubernetes

這一部分這里就省略了, 如需這一部分 文檔, 請參考其他的博文。

下載 Istio 項目包

在 macOS 或 Linux 系統(tǒng)中, 也可以通過以下命令下載最新版本的 Istio

# 新建目錄
mkdir -p /opt/istio && cd /opt/istio

# 設置安裝版本
export ISTIO_VERSION=1.5.1

# 下載文件
curl -L https://istio.io/downloadIstio | sh -

#輸出如下:

Istio 1.5.1 Download Complete!

Istio has been successfully downloaded into the istio-1.5.1 folder on your system.

Next Steps:
See https://istio.io/docs/setup/kubernetes/install/ to add Istio to your Kubernetes cluster.

To configure the istioctl client tool for your workstation,
add the /opt/istio/istio-1.5.1/bin directory to your environment path variable with:
         export PATH="$PATH:/opt/istio/istio-1.5.1/bin"

Begin the Istio pre-installation verification check by running:
         istioctl verify-install 

Need more information? Visit https://istio.io/docs/setup/kubernetes/install/ 

#配置環(huán)境變量
vi /etc/profile
# 添加
export PATH="$PATH:/opt/istio/istio-1.5.1/bin"

# 生效配置
source /etc/profile

#驗證安裝
istioctl verify-install

Checking the cluster to make sure it is ready for Istio installation...

#1. Kubernetes-api
-----------------------
Can initialize the Kubernetes client.
Can query the Kubernetes API Server.

#2. Kubernetes-version
-----------------------
Istio is compatible with Kubernetes: v1.18.0.

#3. Istio-existence
-----------------------
Istio will be installed in the istio-system namespace.

#4. Kubernetes-setup
-----------------------
Can create necessary Kubernetes configurations: Namespace,ClusterRole,ClusterRoleBinding,CustomResourceDefinition,Role,ServiceAccount,Service,Deployments,ConfigMap. 

#5. SideCar-Injector
-----------------------
This Kubernetes cluster supports automatic sidecar injection. To enable automatic sidecar injection see https://istio.io/docs/setup/kubernetes/additional-setup/sidecar-injection/#deploying-an-app

-----------------------
Install Pre-Check passed! The cluster is ready for Istio installation.

#配置 istioctl 命令自動補全
#  創(chuàng)建目錄
mkdir -p /usr/share/istio

# 拷貝補全腳本
cp tools/istioctl.bash /usr/share/istio/


# 導入自動補全
source /usr/share/istio/istioctl.bash



# 添加到 bashrc 中
vi ~/.bashrc
# 添加如下:
# istio
source /usr/share/istio/istioctl.bash


# 測試tab補全

[root@k8s-node-1 istio-1.5.1]# istioctl 
analyze          authz            dashboard        experimental     manifest         profile          proxy-status     upgrade          verify-install   
authn            convert-ingress  deregister       kube-inject      operator         proxy-config     register         validate         version  
  • 目錄結構說明
  • bin? 目錄包含 istioctl 的客戶端文件。istioctl 工具用于手動注入 Envoy sidecar 代理。
  • manifest.yaml? 文件的 sha碼。
  • samples? 目錄包含 istio 的實例應用程序。
  • tools? 目錄包含 一些腳本
    -- convert_RbacConfig_to_ClusterRbacConfig.sh
    -- dump_kubernetes.sh
    -- _istioctl
    -- istioctl.bash? istio 命令tab自動補全的腳本
  • install? 目錄包含如下目錄: (istio除了支持 kubernetes 之外還支持 consul 和 gcp)
    -- consul? 目錄
    -- gcp? 目錄
    -- kubernetes? 目錄包含 kuebrnetes 服務相關的 YAML 文件。
    -- tools ? 目錄

安裝 istio

  • istio 包含兩種安裝方式
    1.通過 istioctl 客戶端命令安裝 (推薦)
    2.通過 helm 進行安裝
# 通過如下命令進行安裝 (manifest 是資源清單, profile 指定類型的清單)
istioctl manifest apply --set profile=demo 

輸出如下:
- Applying manifest for component Base...
? Finished applying manifest for component Base.
- Applying manifest for component Pilot...
? Finished applying manifest for component Pilot.
- Applying manifest for component EgressGateways...
- Applying manifest for component IngressGateways...
- Applying manifest for component AddonComponents...
? Finished applying manifest for component EgressGateways.
? Finished applying manifest for component IngressGateways.
? Finished applying manifest for component AddonComponents.

? Installation complete
#查看部署情況

#如果集群運行在一個不支持外部負載均衡器的環(huán)境中(例如:minikube),istio-ingressgateway 的 EXTERNAL-IP 將顯示為 <pending> 狀態(tài)。請使用服務的 NodePort 或 端口轉發(fā)來訪問網(wǎng)關。
kubectl get pods -n istio-system
NAME                                   READY   STATUS    RESTARTS   AGE
grafana-5f6f8cbf75-lqnjg               1/1     Running   0          17m
istio-egressgateway-74896c8487-mjlg8   1/1     Running   0          17m
istio-ingressgateway-54d494869-7npql   1/1     Running   0          17m
istio-tracing-9dd6c4f7c-x5kcp          1/1     Running   0          17m
istiod-756bd84654-n2k6m                1/1     Running   0          18m
kiali-869c6894c5-64vmw                 1/1     Running   0          17m
prometheus-c89875c74-rgzdx             2/2     Running   0          17m


kubectl get svc -n istio-system 
NAME                        TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                                                                                                                                      AGE
grafana                     ClusterIP      10.254.35.48    <none>        3000/TCP                                                                                                                                     158m
istio-egressgateway         ClusterIP      10.254.10.123   <none>        80/TCP,443/TCP,15443/TCP                                                                                                                     158m
istio-ingressgateway        LoadBalancer   10.254.26.46    <pending>     15020:32142/TCP,80:30000/TCP,443:32701/TCP,15029:30413/TCP,15030:30781/TCP,15031:31714/TCP,15032:32419/TCP,31400:30673/TCP,15443:31123/TCP   158m
istio-pilot                 ClusterIP      10.254.54.205   <none>        15010/TCP,15011/TCP,15012/TCP,8080/TCP,15014/TCP,443/TCP                                                                                     158m
istiod                      ClusterIP      10.254.3.7      <none>        15012/TCP,443/TCP                                                                                                                            158m
jaeger-agent                ClusterIP      None            <none>        5775/UDP,6831/UDP,6832/UDP                                                                                                                   158m
jaeger-collector            ClusterIP      10.254.2.80     <none>        14267/TCP,14268/TCP,14250/TCP                                                                                                                158m
jaeger-collector-headless   ClusterIP      None            <none>        14250/TCP                                                                                                                                    158m
jaeger-query                ClusterIP      10.254.61.2     <none>        16686/TCP                                                                                                                                    158m
kiali                       ClusterIP      10.254.7.135    <none>        20001/TCP                                                                                                                                    158m
prometheus                  ClusterIP      10.254.8.200    <none>        9090/TCP                                                                                                                                     158m
tracing                     ClusterIP      10.254.39.104   <none>        80/TCP                                                                                                                                       158m
zipkin                      ClusterIP      10.254.32.230   <none>        9411/TCP                                                                                                                                     158m
  • 組件說明
  • tracing? 全鏈路監(jiān)控。
  • istio-pilot? 服務發(fā)現(xiàn)與服務配置。
  • kiali? 可視化服務網(wǎng)格展示。
    服務拓撲圖
    分布式跟蹤
    指標度量收集和圖標
    配置校驗
    健康檢查和顯示
    服務發(fā)現(xiàn)
  • prometheus? 大家都懂的監(jiān)控。
  • grafana? prometheus監(jiān)控的展示webui。
  • istio-ingressgateway? 出口網(wǎng)關。
  • istio-egressgateway? 入口網(wǎng)關。
  • jaeger?
    jaeger-agent? jaeger client的一個代理程序,client將收集到的調(diào)用鏈數(shù)據(jù)發(fā)給agent,然后由agent發(fā)給collector;
    jaeger-collector? 負責接收jaeger client或者jaeger agent上報上來的調(diào)用鏈數(shù)據(jù),然后做一些校驗,比如時間范圍是否合法等,最終會經(jīng)過內(nèi)部的處理存儲到后端存儲;
    jaeger-query? 專門負責調(diào)用鏈查詢的一個服務。
  • 修改 istio-ingressgateway 網(wǎng)絡類型 為 NodePort
kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'

kubectl get svc -n istio-system istio-ingressgateway 
NAME                   TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)                                                                                                                                      AGE
istio-ingressgateway   NodePort   10.254.26.46   <none>        15020:32142/TCP,80:30000/TCP,443:32701/TCP,15029:30413/TCP,15030:30781/TCP,15031:31714/TCP,15032:32419/TCP,31400:30673/TCP,15443:31123/TCP   176m

#驗證 istio版本
[root@k8s-node-1 ~]# istioctl version
client version: 1.5.1
control plane version: 1.5.1
data plane version: 1.5.1 (3 proxies)

#Kiali 組件
#Kiali 以 web ui 的方式可視化服務網(wǎng)格。
#查看 kiali svc
[root@k8s-node-1 kubeadm]# kubectl get svc -n istio-system  kiali
NAME    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE
kiali   ClusterIP   10.254.7.135   <none>        20001/TCP   4h24m
#配置訪問(我這里的node節(jié)點為云主機,所以我配置了一個 ingress)
[root@k8s-node-1 ~]# cat kiali-ingress.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: kiali-ingress
  namespace: istio-system
spec:
  rules:
  - host: kiali.aaa.me
    http:
      paths:
      - backend:
          serviceName: kiali
          servicePort: 20001

#創(chuàng)建ingress服務
[root@k8s-node-1 kubeadm]# kubectl apply -f kiali-ingress.yaml 
ingress.extensions/kiali-ingress created

# 查看服務
[root@k8s-node-1 ~]# kubectl get ingress -n istio-system 
NAME            CLASS    HOSTS             ADDRESS       PORTS   AGE
kiali-ingress   <none>   kiali.jicki.me    10.254.8.81   80      2m31s

Istio Profile

Profile 相關的介紹以及具體的區(qū)別

  • istioctl profile list 命令可查看當前版本的 profile
[root@k8s-node-1 istio]# istioctl profile list
Istio configuration profiles:
    minimal
    remote
    separate
    default
    demo
    empty
  • profile? 包含如下:
    1.remote 遠程kubernetes部署, 以及多kubernetes集群
    2.separatei 獨立部署,不建議使用,后續(xù)可能刪除
    3.default 默認安裝, 根據(jù)IstioControlPlaneAPI的默認設置啟用組件, 建議用于生產(chǎn)部署
    4.demo 演示實例,展示istio 所有功能且資源需求適中的配置
    5.empty 不部署任何內(nèi)容。用于導出空的配置文件。
    6.minimal 最小化安裝。


  • istioctl profile dump profileName 可以打印或者導出profile配置
    這里導出的文件就是kubernetes 的 YAML 編排文件。api 為 istio 的 api。
    這里可以導出 default 然后根據(jù)自己的環(huán)境自定義適合自己的 profile。
[root@k8s-node-1 istio]# istioctl profile dump default > default.yaml

istio injection

  • injection 注入后的變化
    1.原生 pod ? –> pods 包含 程序 項目。
    2.注入以后 pod ? –> pods 包含 程序 項目、istio-init、istio-proxy。
    2.1.istio-init? 用于初始化網(wǎng)絡配置, iptables 路由配置。
    2.2.istio-proxy? 用于當前pod 與集群內(nèi)部其他資源進行交互。
  • 可以被 injection 的服務
    1.Deployment - 注入后會添加 istio-init、istio-proxy 。
    2.ReplicaSet - 注入后會添加 istio-init、istio-proxy 。
    3.DeamonSet - 注入后會添加 istio-init、istio-proxy 。
    4.Pod - 注入后會添加 istio-init、istio-proxy 。
    5.Job - 注入后會添加 istio-init、istio-proxy 。
    6.Service - 注入后不會添加任何組件。
    7.Secrets - 注入后不會添加任何組件。
    8.ConfigMap - 注入后不會添加任何組件。
    9.deployment yaml 編排文件注入
    istioctl kube-inject -f nginx-test.yaml 對 Deployment編排文件進行注入(會修改yaml文件內(nèi)容)
    kubectl apply -f <(istioctl kube-inject -f nginx-test.yaml) 注入并 創(chuàng)建 服務到kubernetes中, 這樣操作不會改變原有的編排文件。

三、實操使用

對某個namespaces實現(xiàn)injection 自動注入

# 注入
[root@k8s-node-1 ~]# kubectl label namespace default istio-injection=enabled
namespace/jicki labeled


# 檢測注入情況
[root@k8s-node-1 ~]# istioctl analyze -n default
? No validation issues found when analyzing namespace: default.


# 查看 namespace 的 labels
[root@k8s-node-1 ~]# kubectl get ns --show-labels
NAME              STATUS   AGE     LABELS
default           Active   9d     istio-injection=enabled

手動注入

kubectl apply -f <(istioctl kube-inject -f  file_name.yaml)

實戰(zhàn)1、實現(xiàn)對流量控制

#創(chuàng)建pod
vim  nginx-test.yaml
apiVersion: apps/v1
kind: Deployment 
metadata: 
  name: nginx-1
  labels:
    web: nginx-1
spec: 
  replicas: 1
  selector:
    matchLabels:
      web: nginx-1
  template: 
    metadata: 
      labels: 
        app: nginx 
        web: nginx-1
        version: v1.0.0
    spec: 
      containers: 
        - name: nginx 
          image: nginx:alpine 
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: http
          command: ["/bin/sh", "-c", "echo 'hello nginx-1' > /usr/share/nginx/html/index.html; nginx  -g  'daemon off;'"]
---
apiVersion: apps/v1
kind: Deployment 
metadata: 
  name: nginx-2
  labels:
    web: nginx-2
spec: 
  replicas: 1
  selector:
    matchLabels:
      web: nginx-2
  template: 
    metadata: 
      labels: 
        app: nginx 
        web: nginx-2
        version: v1.0.1
    spec: 
      containers: 
        - name: nginx 
          image: nginx:alpine 
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: http
          command: ["/bin/sh", "-c", "echo 'hello nginx-2' > /usr/share/nginx/html/index.html; nginx  -g  'daemon off;'"]
---
apiVersion: apps/v1
kind: Deployment 
metadata: 
  name: nginx-3
  labels:
    web: nginx-3
spec: 
  replicas: 1
  selector:
    matchLabels:
      web: nginx-3
  template: 
    metadata: 
      labels: 
        app: nginx 
        web: nginx-3
        version: v1.0.2
    spec: 
      containers: 
        - name: nginx 
          image: nginx:alpine 
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: http
          command: ["/bin/sh", "-c", "echo 'hello nginx-3' > /usr/share/nginx/html/index.html; nginx  -g  'daemon off;'"]
---
apiVersion: apps/v1
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-1
  labels:
    web: nginx-1
spec:
  ports:
    - port: 80
      name: http
      targetPort: 80
      protocol: TCP
  selector:
    web: nginx-1
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-2
  labels:
    web: nginx-2
spec:
  ports:
    - port: 80
      name: http
      targetPort: 80
      protocol: TCP
  selector:
    web: nginx-2
---
apiVersion: apps/v1
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-3
  labels:
    web: nginx-3
spec:
  ports:
    - port: 80
      name: http
      targetPort: 80
      protocol: TCP
  selector:
    web: nginx-3
---
apiVersion: v1 
kind: Service
metadata: 
  name: nginx-svc 
  labels:
    app: nginx
spec: 
  ports: 
    - port: 80
      name: http
      targetPort: 80
      protocol: TCP 
  selector: 
    app: nginx
  • 創(chuàng)建服務(后續(xù)實驗都用該服務)
[root@k8s-node-1 istio]# kubectl apply -f <(istioctl kube-inject -f nginx-test.yaml)
deployment.apps/nginx-deployment created

#查看生成的pod
[root@master demo-file]# kubectl get po
NAME                              READY   STATUS    RESTARTS   AGE
nginx-1-6cf586866c-5gj2n          2/2     Running   0          5h53m
nginx-2-dffbd7fc4-jcs62           2/2     Running   0          5h53m
nginx-3-58db9d749-jk52v           2/2     Running   0          5h35m

#觀察邊車服務過程注入
[root@master demo-file]# kubectl describe po nginx-1-6cf586866c-5gj2n
#略

#查看 istio 對外服務的端口以及相關進程
#可以發(fā)現(xiàn)除了80端口還有其他5個額外的端口
[root@master demo-file]# kubectl exec -it nginx-1-6cf586866c-5gj2n -c istio-proxy -- netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:15006           0.0.0.0:*               LISTEN      15/envoy            
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:15090           0.0.0.0:*               LISTEN      15/envoy            
tcp        0      0 127.0.0.1:15000         0.0.0.0:*               LISTEN      15/envoy            
tcp        0      0 0.0.0.0:15001           0.0.0.0:*               LISTEN      15/envoy            
tcp6       0      0 :::15020                :::*                    LISTEN      1/pilot-agent 

#查看配置的三個 service 的 endpoints
[root@master demo-file]# kubectl get ep
NAME           ENDPOINTS                                          AGE
ceph.com-rbd   <none>                                             2d23h
kubernetes     172.24.49.78:6443                                  4d8h
nginx-svc      10.254.65.113:80,10.254.70.81:80,10.254.70.82:80   6h
nginx-svc-1    10.254.70.81:80                                    6h
nginx-svc-2    10.254.65.113:80                                   6h
nginx-svc-3    10.254.70.82:80                                    5h41m
  • 配置一個 Virtual Service
[root@master demo-file]# cat nginx-test-load.yaml 
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-svc-vs
spec:
  hosts: 
  - "*"
  gateways:
  - istio-system/ingressgateway
  http:
  - route:
    - destination:
        host: nginx-svc-1.default.svc.cluster.local
      weight: 20
    - destination:
        host: nginx-svc-2.default.svc.cluster.local
      weight: 50
    - destination:
        host: nginx-svc-3.default.svc.cluster.local
      weight: 30

#VirtualService 服務并非是 kubernetes 中實際的 service 服務。
#kubectl get virtualservices 使用這個命令查看 Virtual Service
#創(chuàng)建Virtual Service
kubectl apply -f nginx-test-load.yaml
#查看Virtual Service
[root@master demo-file]# kubectl get virtualservices
NAME           GATEWAYS                        HOSTS   AGE
nginx-svc-vs   [istio-system/ingressgateway]   [*]     6h2m
  • 創(chuàng)建gateway及ingress實現(xiàn)從外部訪問(從內(nèi)部訪問可略過此步)
[root@master demo-file]# cat nginx-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: app-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"

[root@master demo-file]# cat nginx-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  generation: 1
  labels:
    cattle.io/creator: norman
  name: myapp-gateway
  namespace: istio-system
spec:
  rules:
  - host: wp.abc.com
    http:
      paths:
      - backend:
          serviceName: istio-ingressgateway
          servicePort: 80
#創(chuàng)建gateway及ingress
kubectl apply -f nginx-ingress.yaml
kubectl apply -f nginx-gateway.yaml
  • 模擬訪問
while true;
do
wget -q -O - http://wp.abc.com/;
done
  • 通過kiali查看流量情況


實戰(zhàn)2、實現(xiàn)對特定流量控制

  • 修改Virtual Service配置
[root@master demo-file]# cat nginx-test-headers-user.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-svc-vs
spec:
  hosts: 
  #- nginx-svc.default.svc.cluster.local
  - "*"
  gateways:
  - istio-system/ingressgateway
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: nginx-svc-2.default.svc.cluster.local
  - route:
    - destination:
        host: nginx-svc-1.default.svc.cluster.local
#應用配置
kubectl apply -f nginx-test-headers-user.yaml
  • 訪問測試
[root@master demo-file]# curl wp.abc.com
hello nginx-1
[root@master demo-file]# curl wp.abc.com -Hend-user:jason
hello nginx-2

實戰(zhàn)3、實現(xiàn)對特定流量控制,并附件其他動作

  • 修改Virtual Service配置
[root@master demo-file]# cat nginx-test-headers-user-sleep2s.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-svc-vs
spec:
  hosts: 
  - "*"
  gateways:
  - istio-system/ingressgateway
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    fault:
      delay:
        percent: 100    #觸發(fā)占比100%
        fixedDelay: 2s
    route:
    - destination:
        host: nginx-svc-2.default.svc.cluster.local
  - route:
    - destination:
        host: nginx-svc-1.default.svc.cluster.local
#應用配置
kubectl apply -f  nginx-test-headers-user-sleep2s.yaml
  • 訪問測試
[root@master demo-file]# curl wp.abc.com
hello nginx-1
[root@master demo-file]# curl wp.abc.com -Hend-user:jason
#等待兩秒,返回數(shù)據(jù)
hello nginx-2

實戰(zhàn)4、實現(xiàn)故障注入

  • 修改Virtual Service配置
[root@master demo-file]# cat nginx-test-headers-user-httpstatus.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-svc-vs
spec:
  hosts: 
  - "*"
  gateways:
  - istio-system/ingressgateway
  http:
  - fault:
      abort:
        httpStatus: 500
        percentage:
          value: 90      #觸發(fā)占比90%
    match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: nginx-svc-2.default.svc.cluster.local
  - route:
    - destination:
        host: nginx-svc-1.default.svc.cluster.local

#應用配置
kubectl apply -f nginx-test-headers-user-httpstatus.yaml
  • 訪問測試
[root@master demo-file]# curl wp.abc.com
hello nginx-1
[root@master demo-file]# curl wp.abc.com -Hend-user:jason
fault filter abort
#返回狀態(tài)碼為500。

更多實戰(zhàn)內(nèi)容、可從官網(wǎng)獲取。

Istio Upgrade

注意事項
升級 Istio 之前, 請確認是否支持升級 istioctl manifest versions 查看支持版本。

升級過程中可能發(fā)生流量中斷。為了縮短流量中斷時間, 請確保每個組件(Citadel 除外)至少運行有兩個副本。同時, 確保 PodDistruptionBudgets 配置最小可用性為 1。

確保 istio profile 與 需要升級的版本所配置的 profile 一致。

升級步驟
1.?下載需要升級的 istio 版本 curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.5.1 sh -

2.?替換 istioctl 二進制文件, 或者 更改 istio 的環(huán)境變量PATH路徑到新版本的目錄中。

3.?如果配置了 istioctl 自動補全,還需要替換為 新的 自動補全腳本。

4.?使用新的 istioctl 導出新版本的 profile 文件 istioctl profile dump demo > demo.yaml

5.?修改 demo.yaml 文件, 將其中 jwtPolicy 身份驗證機構修改為 first-party-jwt。Istio 將默認使用第三方令牌。

5.1? 驗證是否支持 第三方令牌。kubectl get --raw /api/v1 | jq '.resources[] | select(.name | index("serviceaccounts/token"))'

5.1.1?jwtPolicy = third-party-jwt 使用第三方令牌 更安全 Istio 默認使用這個選項。

5.1.2? jwtPolicy = first-party-jwt 使用第一方令牌 屬性比較不安全。

6.?istioctl upgrade -f demo.yaml 命令進行升級。

7.?觀察 kubernets 中 istio-system 的服務更新完成。

8.?重新注入環(huán)境中部署的服務, 用以更新注入數(shù)據(jù)。

8.1?對于自動注入的情況可使用如下命令:

8.1.1?kubectl rollout restart deployment

8.1.2?kubectl rollout restart statefulset

8.1.3?kubectl rollout restart daemonset

8.2?對于手動注入的情況( 需要重新 apply 一下服務) :

8.2.1?kubectl apply -f <(istioctl kube-inject -f nginx-test.yaml)
9.?檢查升級, 執(zhí)行 istioctl version 檢查

9.1?client version 版本是否為新版本。

9.2?control plane version 版本是否為新版本。

9.3?data plane version 中是否全部 proxies 都為新版本。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容