前言
本系列文章主要從源碼(35e2b904)出發(fā),對istio做深入剖析,讓大家對istio有更深的認(rèn)知,從而方便平時排查問題。不了解Service Mesh和Istio的同學(xué)請先閱讀敖小劍老師如下文章進(jìn)行概念上的理解:
本文主要對istio在ubuntu16.04下環(huán)境搭建做簡單介紹,Mac用戶和其他linux發(fā)行版用戶請根據(jù)bash腳本做相應(yīng)調(diào)整。
概述
Istio為希臘文,意思是“啟航”,和“kubernetes(舵手)”遙相呼應(yīng),是一個開源的微服務(wù)管理、保護(hù)、監(jiān)控的基礎(chǔ)設(shè)施。Istio發(fā)音“意絲帝歐”,重音在“意”上。
前兩篇文章主要對istio開發(fā)環(huán)境以及通過服務(wù)治理配置生效流程做了介紹??紤]到有些用戶可能沒有接觸過Istio,本文會對Istio整體架構(gòu)、內(nèi)部各組件做介紹。
Istio是邏輯上分為數(shù)據(jù)平面(Data Plane)和控制平面(Control Plane)。
- 數(shù)據(jù)平面的含義是什么?官網(wǎng)是這么描述的:
The data plane is composed of a set of intelligent proxies (Envoy) deployed as sidecars that mediate and control all network communication between microservices.
數(shù)據(jù)平面由一組作為sidecar智能代理(Envoy)和它掌控網(wǎng)絡(luò)通信的的微服務(wù)實例組成。sidecar的意思是邊三輪,一個Envoy實例掌控一個微服務(wù)實例的入口和出口流量,像邊三輪一樣。
- 對于控制平面,官網(wǎng)給出的定義是:
The control plane is responsible for managing and configuring proxies to route traffic, as well as enforcing policies at runtime.
控制平面由Pliot、Mixer、Istio-Auth組成,負(fù)責(zé)管理和配置代理的路由流量以及運行時服務(wù)治理策略的執(zhí)行。
整體架構(gòu)
Istio架構(gòu)圖如下所示:

Envoy
是一個高性能輕量級代理,它掌控了service的入口流量和出口流量,它提供了很多內(nèi)置功能,如動態(tài)負(fù)載服務(wù)發(fā)現(xiàn)、負(fù)載均衡、TLS終止、HTTP/2 & gRPC流量代理、熔斷、健康檢查等功能。Mixer
翻譯過來是混音器,Mixer負(fù)責(zé)在整個Service Mesh中實施訪問控制和使用策略。Mixer是一個可擴(kuò)展組件,內(nèi)部提供了多個模塊化的適配器(adapter)。
Envoy提供request級別的屬性(attributes)數(shù)據(jù)。這些數(shù)據(jù)交由Mixer進(jìn)行評估和處理,Mixer中的各種適配器(adapter)基于這些屬性數(shù)據(jù),來實現(xiàn)日志記錄、監(jiān)控指標(biāo)采集展示、配額管理、ACL檢查等功能。Pilot
翻譯過來是領(lǐng)航員,Pliot對Envoy的生命周期進(jìn)行管理,同時提供了智能路由(如A/B測試、金絲雀部署)、流量管理(超時、重試、熔斷)功能。Pliot接收用戶指定的高級路由規(guī)則配置,轉(zhuǎn)換成Envoy的配置,使這些規(guī)則生效。Istio-Auth
服務(wù)間認(rèn)證和終端用戶認(rèn)證功能。
源碼結(jié)構(gòu)介紹
Istio源碼托管于Github,項目使用Go開發(fā)。
以35e2b904為例,istio代碼結(jié)構(gòu)如下所示:
.
├── addons
├── bin
├── broker
├── docker
├── galley
├── install
├── istioctl
├── mixer
├── pilot
├── pkg
├── prow
├── release
├── samples
├── security
├── tests
├── tools
├── vendor
├── codecov.requirement
├── codecov.skip
├── CONTRIBUTING.md
├── downloadIstio.sh
├── Gopkg.lock
├── Gopkg.toml
├── istio.deps
├── istio.VERSION
├── istio.yaml
├── LICENSE
├── lintconfig_base.json
├── Makefile
├── OWNERS
└── README.md
部分包\文件\文件夾功能列表如下:
| Package/Directory/File | Introduction |
|---|---|
| addons | 一些插件,比如展示metrics的grafana和繪制服務(wù)調(diào)用圖的servicegraph |
| bin | 存放初始化依賴、編譯、插件證書檢查、代碼生成的腳本 |
| broker | Istio對Open Service Broker的一種實現(xiàn),該API使得外部服務(wù)能自動訪問Istio服務(wù)。broker目前還處于研發(fā)階段。 |
| galley | 提供了Istio的配置管理功能,目前還處于研發(fā)階段。 |
| install | 生成各環(huán)境(ansible、consul、ereka、kubernetes等)安裝istio時需要yaml配置清單。 |
| istioctl | istio終端控制工具(類似kubectl之于kubernetes),用戶通過istioctl來修改istio運行時配置,執(zhí)行服務(wù)治理策略。 |
| mixer | “混音器”,參與到tracffic處理流程。通過對envoy上報的attributes進(jìn)行處理,結(jié)合內(nèi)部的adapters實現(xiàn)日志記錄、監(jiān)控指標(biāo)采集展示、配額管理、ACL檢查等功能。 |
| pilot | “領(lǐng)航員”,pliot對Envoy的生命周期進(jìn)行管理,同時提供了智能路由(如A/B測試、金絲雀部署)、流量管理(超時、重試、熔斷)功能。 |
| pkg | 頂級公共包,包含istio版本處理、tracing、日志記錄、緩存管理等。 |
| release | 包含Istio在各平臺上進(jìn)行編譯的腳本。 |
| samples | Istio提供的微服務(wù)樣例,比如bookinfo。 |
| security | Istio用戶身份驗證、服務(wù)間認(rèn)證。 |
| tests | 測試用例、腳本等。 |
| vendor | dep生成的第三方依賴。 |
| Gopkg.* | dep需要version constraint和version lock文件。 |
| Makefile | Istio Makefile,編譯docker鏡像時會引用tools/istio-docker.mk這個Makefile。 |
開發(fā)環(huán)境搭建
安裝docker
# 參考 https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce-1
配置docker代理[可選]
新建/etc/systemd/system/docker.service.d/http-proxy.conf,添加如下配置
[Service]
Environment="HTTP_PROXY=http://<your_proxy>/" "HTTPS_PROXY=http://<your_proxy>/" "NO_PROXY=192.168.0.0/16,127.0.0.0/8" # your_proxy替換成你自己的代理
安裝virtualbox
# virtualbox需要裝最新的5.2.10, https://www.virtualbox.org/wiki/Linux_Downloads
# Mac OS
wget https://download.virtualbox.org/virtualbox/5.2.10/VirtualBox-5.2.10-122088-OSX.dmg
# ubuntu AMD64
wget https://download.virtualbox.org/virtualbox/5.2.10/virtualbox-5.2_5.2.10-122088~Ubuntu~xenial_amd64.deb && sudo apt install ./virtualbox-5.2_5.2.10-122088~Ubuntu~xenial_amd64.deb
# centos6 AMD64
wget https://download.virtualbox.org/virtualbox/5.2.10/VirtualBox-5.2-5.2.10_122088_el6-1.x86_64.rpm && yum install ./VirtualBox-5.2-5.2.10_122088_el7-1.x86_64.rpm
# centos7 AMD64
wget https://download.virtualbox.org/virtualbox/5.2.10/VirtualBox-5.2-5.2.10_122088_el7-1.x86_64.rpm && yum install ./VirtualBox-5.2-5.2.10_122088_el7-1.x86_64.rpm
安裝k8s集群
使用vagrant安裝k8s集群[建議]
參考jimmysong的vagrant教程kubernetes-vagrant-centos-cluster,其中節(jié)點個數(shù)根據(jù)自己機(jī)器配置酌情更改(參考kubernetes-vagrant-centos-cluster)。
使用minikube安裝k8s集群[可選]
- 安裝minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ # 下載minikube
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/ # 下載kubectl
- 啟動k8s
# 注minikube可以不使用virtualbox啟動k8s,不過需要dockerc處于橋接模式,否則可能導(dǎo)致網(wǎng)絡(luò)連接不通,配置過程請自行搜索
minikube start \
--extra-config=controller-manager.ClusterSigningCertFile="/var/lib/localkube/certs/ca.crt" \
--extra-config=controller-manager.ClusterSigningKeyFile="/var/lib/localkube/certs/ca.key" \
--extra-config=apiserver.Admission.PluginNames=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
--kubernetes-version=v1.9.4 \
--logtostderr
配置Istio repo
- 配置github并拉取代碼
# 配置github代理
git config --global http.https://github.com.proxy http://proxy:1080
# 在github上fork Istio,并clone到自己公共的$GOPATH/src/istio.io/下
mkdir -p $GOPATH/src/istio.io
cd $GOPATH/src/istio.io
git clone https://github.com/$YOU/istio
cd istio
git remote add upstream 'https://github.com/istio/istio'
git config --global --add http.followRedirects 1
- 配置環(huán)境變量
未避免污染全局環(huán)境變量,強(qiáng)烈建議安裝autoenv。
cd $GOPATH/src/istio.io/istio
在istio根目錄下新增.env文件,配置如下:
export GOPATH=$YOUR_GOPATH # $YOUR_GOPATH為你的GOPATH路徑
export PATH=$PATH:$GOPATH/bin
export ISTIO=$GOPATH/src/istio.io # eg. ~/go/src/istio.io
# Please change HUB to the desired HUB for custom docker container
# builds.
export HUB="docker.io/$USER" # $USER為你的dockerhub賬號
# The Istio Docker build system will build images with a tag composed of
# $USER and timestamp. The codebase doesn't consistently use the same timestamp
# tag. To simplify development the development process when later using
# updateVersion.sh you may find it helpful to set TAG to something consistent
# such as $USER.
export TAG=$USER # TAG為你編譯Istio各組建后打包鏡像的tag,建議Mac/ubuntu當(dāng)前賬號
# If your github username is not the same as your local user name (saved in the
# shell variable $USER), then replace "$USER" below with your github username
export GITHUB_USER=$USER # GITHUB_USER為你的github賬號
# Specify which Kube config you'll use for testing. This depends on whether
# you're using Minikube or your own Kubernetes cluster for local testing
# For a GKE cluster:
export KUBECONFIG=${HOME}/.kube/config
# Alternatively, for Minikube:
# export KUBECONFIG=${GOPATH}/src/istio.io/istio/.circleci/config
export ISTIO_DOCKER_HUB="docker.io/$USER" # make build時使用此變量
export ISTIO_VERSION=$USER # make build時使用此變量
使當(dāng)前.env生效:
cd .
Istio編譯組件并測試
- 編譯鏡像
# make init # 初始化,檢查目錄結(jié)構(gòu)、Go版本號、初始化環(huán)境變量、檢查vendor等
make docker # 對各組件(istioctl、mixer、pilot、istio-auth等)進(jìn)行二進(jìn)制包編譯、測試、鏡像編譯
make push # 推送鏡像到dockerhub
# 其他指令
make pilot docker.pilot # 編譯pilot組件和鏡像
make app docker.app # 編譯app組件和鏡像
make proxy docker.proxy # 編譯proxy組件和鏡像
make proxy_init docker.proxy_init # 編譯proxy_init組件和鏡像
make proxy_debug docker.proxy_debug # 編譯proxy_debug組件和鏡像
make sidecar_injector docker.sidecar_injector # 編譯sidecar_injector組件和鏡像
make proxyv2 docker.proxyv2 # 編譯proxyv2組件和鏡像
make push.docker.pilot # 推送pilot鏡像到dockerhub,其他組件類似
- 其他腳本
cd $GOPATH/src/istio.io/istio
./bin/get_workspace_status # 查看當(dāng)前工作目錄狀態(tài),包括環(huán)境變量等
install/updateVersion.sh -a ${HUB},${TAG} # 使用當(dāng)前環(huán)境變量生成Istio清單
samples/bookinfo/build_push_update_images.sh # 使用當(dāng)前環(huán)境變量編譯并推送bookinfo鏡像
- 測試鏡像
作者
鄭偉,小米信息部技術(shù)架構(gòu)組
招聘
小米信息部武漢研發(fā)中心,信息部是小米公司整體系統(tǒng)規(guī)劃建設(shè)的核心部門,支撐公司國內(nèi)外的線上線下銷售服務(wù)體系、供應(yīng)鏈體系、ERP體系、內(nèi)網(wǎng)OA體系、數(shù)據(jù)決策體系等精細(xì)化管控的執(zhí)行落地工作,服務(wù)小米內(nèi)部所有的業(yè)務(wù)部門以及 40 家生態(tài)鏈公司。
同時部門承擔(dān)微服務(wù)體系建設(shè)落地及各類后端基礎(chǔ)平臺研發(fā)維護(hù),語言涉及 Go、PHP、Java,長年虛位以待對微服務(wù)、基礎(chǔ)架構(gòu)有深入理解和實踐、或有大型電商后端系統(tǒng)研發(fā)經(jīng)驗的各路英雄。
歡迎投遞簡歷:jin.zhang(a)xiaomi.com
更多技術(shù)文章:小米信息部技術(shù)團(tuán)隊