微軟近期開源了一個(gè)新的名為 Open Service Mesh 的項(xiàng)目并準(zhǔn)備捐贈(zèng)給 CNCF 。
基本介紹
Open Service Mesh (OSM) is a lightweight, extensible, Cloud Native service mesh that allows users to uniformly manage, secure, and get out-of-the-box observability features for highly dynamic microservice environments.
Open Service Mesh(OSM)是一個(gè)輕量級(jí)反症,可擴(kuò)展的云原生服務(wù)網(wǎng)格堪侯,它使用戶能夠統(tǒng)一管理,保護(hù)和獲得針對(duì)高度動(dòng)態(tài)微服務(wù)環(huán)境的開箱即用的可觀察性功能璃赡。
OSM 在 Kubernetes 上運(yùn)行基于 Envoy 的控制平面,可以使用 SMI API 進(jìn)行配置。它通過(guò)以 sidecar 的形式注入 Envoy 代理來(lái)工作辕录。
控制面負(fù)責(zé)持續(xù)配置代理,以配置策略和路由規(guī)則等都保持最新梢卸。代理主要負(fù)責(zé)執(zhí)行訪問(wèn)控制的規(guī)則走诞,路由控制,采集 metrics 等蛤高。(這和目前我們常見(jiàn)到的 Service Mesh 方案基本都一樣的)
顯著特性
- 基于 Service Mesh Interface (SMI) 的實(shí)現(xiàn)蚣旱,主要包括
Traffic Access Control
,Traffic Specs
和Traffic Split
戴陡。剩下的Traffic Metrics
正在開發(fā)中塞绿; - 服務(wù)間的通信加密使用 mTLS ;
- 定義和執(zhí)行服務(wù)間的訪問(wèn)控制策略恤批;
- 通過(guò) Prometheus 和 Grafana 完成其觀察性异吻;
- 可與外部證書管理服務(wù)進(jìn)行集成;
- Envoy sidecar 自動(dòng)注入;
上手體驗(yàn)
只做介紹未免太過(guò)無(wú)趣诀浪,而且說(shuō)實(shí)話棋返,這么多 service mesh 實(shí)現(xiàn),不親自上手試試看雷猪,感覺(jué)不出來(lái)太多差異的睛竣。
這里我使用 KIND 作為我本地的實(shí)驗(yàn)環(huán)境。
安裝
安裝過(guò)程很簡(jiǎn)單求摇,直接去 Release 頁(yè)面 下載預(yù)編譯好的二進(jìn)制文件射沟。可將二進(jìn)制文件加入到 $PATH
中月帝。
(MoeLove) ? ~ wget -q https://github.com/openservicemesh/osm/releases/download/v0.1.0/osm-v0.1.0-linux-amd64.tar.gz
(MoeLove) ? ~ tar -xzvf osm-v0.1.0-linux-amd64.tar.gz
linux-amd64/
linux-amd64/LICENSE
linux-amd64/README.md
linux-amd64/osm
(MoeLove) ? ~ cd linux-amd64
(MoeLove) ? linux-amd64 ls
LICENSE osm README.md
在進(jìn)行 osm 資源和服務(wù)的正式安裝前躏惋,先做個(gè)必要的檢查:
(MoeLove) ? linux-amd64 ./osm check --pre-install
ok: initialize Kubernetes client
ok: query Kubernetes API
ok: Kubernetes version
ok: can create namespaces
ok: can create customresourcedefinitions
ok: can create clusterroles
ok: can create clusterrolebindings
ok: can create mutatingwebhookconfigurations
ok: can create serviceaccounts
ok: can create services
ok: can create deployments
ok: can create configmaps
ok: can read secrets
ok: can modify iptables
All checks successful!
可以看到主要是和權(quán)限相關(guān)的一些檢查。接下來(lái)就正式對(duì) ocm 相關(guān)資源進(jìn)行部署嚷辅。
默認(rèn)使用的鏡像簿姨,托管在 DockerHub 上,如果需要配置加速的小伙伴簸搞,可傳遞 --container-registry
更改源地址扁位,以便于加速安裝進(jìn)度。
(MoeLove) ? linux-amd64 ./osm install
OSM installed successfully in namespace [osm-system] with mesh name [osm]
(MoeLove) ? linux-amd64 kubectl -n osm-system get pods
NAME READY STATUS RESTARTS AGE
osm-controller-d499d6cc7-88659 0/1 ContainerCreating 0 12s
osm-grafana-58ff65dfb7-svztv 0/1 ContainerCreating 0 12s
osm-prometheus-5756769877-zj6f6 0/1 ContainerCreating 0 12s
zipkin-6df4b57677-dcq8q 0/1 ContainerCreating 0 12s
可以看到默認(rèn)安裝完成后趁俊,都在 osm-system
命名空間下域仇,有 4 個(gè) Pods
- osm-controller:控制謬
- osm-grafana:Dashboard 相關(guān),可通過(guò)
osm dashboard
命令喚起寺擂; - osm-prometheus:采集 metrics 暇务;
- zipkin:鏈路追蹤
還有對(duì)應(yīng)的 service 記錄.
(MoeLove) ? linux-amd64 kubectl -n osm-system get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
osm-controller ClusterIP 10.97.115.1 <none> 15128/TCP,443/TCP 7m46s
osm-grafana ClusterIP 10.110.209.86 <none> 3000/TCP 7m46s
osm-prometheus ClusterIP 10.97.10.65 <none> 7070/TCP 7m46s
zipkin ClusterIP 10.103.150.158 <none> 9411/TCP 7m46s
以及一系列的 CRD
(MoeLove) ? linux-amd64 kubectl -n osm-system get crd
NAME CREATED AT
backpressures.policy.openservicemesh.io 2020-08-06T16:14:03Z
httproutegroups.specs.smi-spec.io 2020-08-06T16:14:03Z
tcproutes.specs.smi-spec.io 2020-08-06T16:14:03Z
trafficsplits.split.smi-spec.io 2020-08-06T16:14:03Z
traffictargets.access.smi-spec.io 2020-08-06T16:14:03Z
實(shí)踐
- 創(chuàng)建實(shí)驗(yàn)用的 namespace, 并通過(guò)
osm namespace add
將其納入管理范圍中:
(MoeLove) ? ~ kubectl create ns bookstore
namespace/bookstore created
(MoeLove) ? ~ kubectl create ns bookbuyer
namespace/bookbuyer created
(MoeLove) ? ~ kubectl create ns bookthief
namespace/bookthief created
(MoeLove) ? ~ kubectl create ns bookwarehouse
namespace/bookwarehouse created
(MoeLove) ? ~ osm namespace add bookstore bookbuyer bookthief bookwarehouse
Namespace [bookstore] succesfully added to mesh [osm]
Namespace [bookbuyer] succesfully added to mesh [osm]
Namespace [bookthief] succesfully added to mesh [osm]
Namespace [bookwarehouse] succesfully added to mesh [osm]
- 部署實(shí)驗(yàn)應(yīng)用程序
# 在項(xiàng)目的代碼目錄中執(zhí)行
(MoeLove) ? osm git:(main) kubectl apply -f docs/example/manifests/apps
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
namespace/bookbuyer configured
serviceaccount/bookbuyer created
service/bookbuyer created
deployment.apps/bookbuyer created
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
namespace/bookstore configured
service/bookstore created
service/bookstore-v1 created
serviceaccount/bookstore-v1 created
deployment.apps/bookstore-v1 created
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
namespace/bookthief configured
serviceaccount/bookthief created
service/bookthief created
deployment.apps/bookthief created
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
namespace/bookwarehouse configured
serviceaccount/bookwarehouse created
service/bookwarehouse created
deployment.apps/bookwarehouse created
trafficsplit.split.smi-spec.io/bookstore-split created
- 本地訪問(wèn)
你可以通過(guò) kubectl port-foward
在本地對(duì)剛才部署的應(yīng)用進(jìn)行訪問(wèn)。示例中也提供了相應(yīng)的啟動(dòng)腳本 scripts/port-forward-all.sh
怔软,注意這里需要先安裝 GNU parallel 垦细,例如: dnf install parallel
.
(MoeLove) ? osm git:(main) ? ./scripts/port-forward-all.sh
Academic tradition requires you to cite works you base your article on.
If you use programs that use GNU Parallel to process data for an article in a
scientific publication, please cite:
O. Tange (2018): GNU Parallel 2018, Mar 2018, ISBN 9781387509881,
DOI https://doi.org/10.5281/zenodo.1146014
This helps funding further development; AND IT WON'T COST YOU A CENT.
If you pay 10000 EUR you should feel free to use GNU Parallel without citing.
More about funding GNU Parallel and the citation notice:
https://www.gnu.org/software/parallel/parallel_design.html#Citation-notice
To silence this citation notice: run 'parallel --citation' once.
訪問(wèn)本地的 8080~8083
端口即可看到示例項(xiàng)目。例如:
備注:這里是因?yàn)槲业膽?yīng)用程序已經(jīng)運(yùn)行一段時(shí)間了挡逼,如果是新部署的括改,所有數(shù)字皆為 0
- 訪問(wèn)控制策略
我們來(lái)看看如何調(diào)整訪問(wèn)控制的策略
kind: TrafficTarget
apiVersion: access.smi-spec.io/v1alpha2
metadata:
name: bookstore-v1
namespace: bookstore
spec:
destination:
kind: ServiceAccount
name: bookstore-v1
namespace: bookstore
rules:
- kind: HTTPRouteGroup
name: bookstore-service-routes
matches:
- buy-a-book
- books-bought
sources:
- kind: ServiceAccount
name: bookbuyer
namespace: bookbuyer
#- kind: ServiceAccount
#name: bookthief
#namespace: bookthief
---
apiVersion: specs.smi-spec.io/v1alpha3
kind: HTTPRouteGroup
metadata:
name: bookstore-service-routes
namespace: bookstore
spec:
matches:
- name: books-bought
pathRegex: /books-bought
methods:
- GET
headers:
- host: "bookstore.bookstore"
- "user-agent": ".*-http-client/*.*"
- "client-app": "bookbuyer"
- name: buy-a-book
pathRegex: ".*a-book.*new"
methods:
- GET
headers:
- host: "bookstore.bookstore"
這里定義了兩個(gè) SMI 中的資源 TrafficTarget
和 HTTPRouteGroup
,用來(lái)控制入口流量家坎。
(MoeLove) ? osm git:(main) ? kubectl apply -f docs/example/manifests/access/
通過(guò)以上命令創(chuàng)建這兩個(gè)資源嘱能。然后再次打開我們的示例應(yīng)用程序,就會(huì)看到對(duì)應(yīng)的計(jì)數(shù)正在逐步增加(因?yàn)檎?qǐng)求被放行了) 虱疏。
以上示例來(lái)自于項(xiàng)目倉(cāng)庫(kù) 中的示例惹骂。
Dashboard
通過(guò) osm dashboard
可直接喚起本地瀏覽器,并 port-foward 將 Grafana 打開做瞪。
總結(jié)
Open Service Mesh 相對(duì)來(lái)說(shuō)析苫,確實(shí)很輕量。 所需要的訪問(wèn)控制,流量切割等功能通過(guò)自己創(chuàng)建 SMI 資源來(lái)控制衩侥。
并且国旷,在同一個(gè)集群內(nèi)可存在多組 mesh ,osm 安裝的時(shí)候茫死,可指定 mesh 名稱跪但。
此外,這個(gè)項(xiàng)目也是微軟在 Service Mesh 方向的又一個(gè)大動(dòng)作了峦萎。目標(biāo)也許是 Istio 屡久。讓我們拭目以待。
歡迎訂閱我的文章公眾號(hào)【MoeLove】