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

一、基礎(chǔ)概念及原理

Istio 是什么初家?(衣撕丟)

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

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

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

Istio 架構(gòu)

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

Istio 組件

Envoy

  • Istio 使用 Envoy 代理的擴(kuò)展版本。

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

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

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

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

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

Mixer

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

Pilot

  • Pilot ? 主要是為 Envoy sidecar 提供服務(wù)發(fā)現(xiàn)杈帐、用于智能路由的流量管理功能(例如她君,A/B 測試恰矩、金絲雀發(fā)布等)以及彈性功能(超時寺枉、重試琴儿、熔斷器等)从祝。
  • Pilot ? 將控制流量行為的高級路由規(guī)則轉(zhuǎn)換為特定于環(huán)境的配置眷篇,并在運(yùn)行時將它們傳播到 sidecar例诀。
  • Pilot ? 將特定于平臺的服務(wù)發(fā)現(xiàn)機(jī)制抽象出來朽肥,并將它們合成為任何符合 Envoy API 的 sidecar 都可以使用的標(biāo)準(zhǔn)格式菇民。
  • 平臺適配器 與 Envoy 交互圖 (平臺 ? 支持 kubernetes尽楔、Consul、gcp第练、Nomad等)
    1.平臺啟動一個服務(wù)的新實例阔馋,該實例通知其平臺適配器。
    2.平臺適配器使用 Pilot 抽象模型注冊實例娇掏。
    3.Pilot 將流量規(guī)則和配置派發(fā)給 Envoy 代理呕寝,來傳達(dá)此次更改。
    4.可以使用 Istio 的流量管理 API 通過 Pilot 優(yōu)化 Envoy 配置婴梧,以便對服務(wù)網(wǎng)格中的流量進(jìn)行更細(xì)粒度地控制壁涎。


    image.png

Citadel

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

Galley

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

二崭歧、安裝Istio Install

我這里 Kubernetes 版本為1.18 目前只有這么一個集群
  • 安裝 Istio 環(huán)境準(zhǔ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

# 設(shè)置安裝版本
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 命令自動補(bǔ)全
#  創(chuàng)建目錄
mkdir -p /usr/share/istio

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


# 導(dǎo)入自動補(bǔ)全
source /usr/share/istio/istioctl.bash



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


# 測試tab補(bǔ)全

[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  
  • 目錄結(jié)構(gòu)說明
  • bin? 目錄包含 istioctl 的客戶端文件叔营。istioctl 工具用于手動注入 Envoy sidecar 代理。
  • manifest.yaml? 文件的 sha碼所宰。
  • samples? 目錄包含 istio 的實例應(yīng)用程序绒尊。
  • tools? 目錄包含 一些腳本
    -- convert_RbacConfig_to_ClusterRbacConfig.sh
    -- dump_kubernetes.sh
    -- _istioctl
    -- istioctl.bash? istio 命令tab自動補(bǔ)全的腳本
  • install? 目錄包含如下目錄: (istio除了支持 kubernetes 之外還支持 consul 和 gcp)
    -- consul? 目錄
    -- gcp? 目錄
    -- kubernetes? 目錄包含 kuebrnetes 服務(wù)相關(guān)的 YAML 文件。
    -- tools ? 目錄

安裝 istio

  • istio 包含兩種安裝方式
    1.通過 istioctl 客戶端命令安裝 (推薦)
    2.通過 helm 進(jìn)行安裝
# 通過如下命令進(jìn)行安裝 (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
#查看部署情況

#如果集群運(yùn)行在一個不支持外部負(fù)載均衡器的環(huán)境中(例如:minikube)仔粥,istio-ingressgateway 的 EXTERNAL-IP 將顯示為 <pending> 狀態(tài)婴谱。請使用服務(wù)的 NodePort 或 端口轉(zhuǎn)發(fā)來訪問網(wǎng)關(guān)。
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? 服務(wù)發(fā)現(xiàn)與服務(wù)配置谭羔。
  • kiali? 可視化服務(wù)網(wǎng)格展示。
    服務(wù)拓?fù)鋱D
    分布式跟蹤
    指標(biāo)度量收集和圖標(biāo)
    配置校驗
    健康檢查和顯示
    服務(wù)發(fā)現(xiàn)
  • prometheus? 大家都懂的監(jiān)控麦向。
  • grafana? prometheus監(jiān)控的展示webui口糕。
  • istio-ingressgateway? 出口網(wǎng)關(guān)。
  • istio-egressgateway? 入口網(wǎng)關(guān)磕蛇。
  • jaeger?
    jaeger-agent? jaeger client的一個代理程序景描,client將收集到的調(diào)用鏈數(shù)據(jù)發(fā)給agent,然后由agent發(fā)給collector秀撇;
    jaeger-collector? 負(fù)責(zé)接收jaeger client或者jaeger agent上報上來的調(diào)用鏈數(shù)據(jù)超棺,然后做一些校驗,比如時間范圍是否合法等呵燕,最終會經(jīng)過內(nèi)部的處理存儲到后端存儲棠绘;
    jaeger-query? 專門負(fù)責(zé)調(diào)用鏈查詢的一個服務(wù)。
  • 修改 istio-ingressgateway 網(wǎng)絡(luò)類型 為 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ù)網(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é)點為云主機(jī),所以我配置了一個 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服務(wù)
[root@k8s-node-1 kubeadm]# kubectl apply -f kiali-ingress.yaml 
ingress.extensions/kiali-ingress created

# 查看服務(wù)
[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 相關(guān)的介紹以及具體的區(qū)別

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


  • istioctl profile dump profileName 可以打印或者導(dǎo)出profile配置
    這里導(dǎo)出的文件就是kubernetes 的 YAML 編排文件让虐。api 為 istio 的 api。
    這里可以導(dǎo)出 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)絡(luò)配置, iptables 路由配置。
    2.2.istio-proxy? 用于當(dāng)前pod 與集群內(nèi)部其他資源進(jìn)行交互笼才。
  • 可以被 injection 的服務(wù)
    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編排文件進(jìn)行注入(會修改yaml文件內(nèi)容)
    kubectl apply -f <(istioctl kube-inject -f nginx-test.yaml) 注入并 創(chuàng)建 服務(wù)到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)建服務(wù)(后續(xù)實驗都用該服務(wù))
[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

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

#查看 istio 對外服務(wù)的端口以及相關(guān)進(jìn)程
#可以發(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 服務(wù)并非是 kubernetes 中實際的 service 服務(wù)。
#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
#應(yīng)用配置
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
#應(yīng)用配置
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

#應(yīng)用配置
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 之前, 請確認(rèn)是否支持升級 istioctl manifest versions 查看支持版本。

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

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

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

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

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

4.?使用新的 istioctl 導(dǎo)出新版本的 profile 文件 istioctl profile dump demo > demo.yaml

5.?修改 demo.yaml 文件, 將其中 jwtPolicy 身份驗證機(jī)構(gòu)修改為 first-party-jwt劫流。Istio 將默認(rèn)使用第三方令牌怎虫。

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

5.1.1?jwtPolicy = third-party-jwt 使用第三方令牌 更安全 Istio 默認(rèn)使用這個選項困介。

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

6.?istioctl upgrade -f demo.yaml 命令進(jìn)行升級。

7.?觀察 kubernets 中 istio-system 的服務(wù)更新完成座哩。

8.?重新注入環(huán)境中部署的服務(wù), 用以更新注入數(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 一下服務(wù)) :

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 都為新版本姜骡。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末导坟,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子圈澈,更是在濱河造成了極大的恐慌惫周,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件康栈,死亡現(xiàn)場離奇詭異递递,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)啥么,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進(jìn)店門登舞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人悬荣,你說我怎么就攤上這事菠秒。” “怎么了氯迂?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵践叠,是天一觀的道長。 經(jīng)常有香客問我嚼蚀,道長禁灼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任驰坊,我火速辦了婚禮匾二,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拳芙。我一直安慰自己察藐,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布舟扎。 她就那樣靜靜地躺著分飞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪睹限。 梳的紋絲不亂的頭發(fā)上譬猫,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天,我揣著相機(jī)與錄音羡疗,去河邊找鬼染服。 笑死,一個胖子當(dāng)著我的面吹牛叨恨,可吹牛的內(nèi)容都是我干的柳刮。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼秉颗!你這毒婦竟也來了痢毒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤蚕甥,失蹤者是張志新(化名)和其女友劉穎哪替,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體菇怀,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凭舶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了敏释。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片库快。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡摸袁,死狀恐怖钥顽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情靠汁,我是刑警寧澤蜂大,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站蝶怔,受9級特大地震影響奶浦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜踢星,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一澳叉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沐悦,春花似錦成洗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至副签,卻和暖如春遥椿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背淆储。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工冠场, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人本砰。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓碴裙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子青团,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容