Istio技術與實踐03:最佳實踐之sidecar自動注入

webhook自動注入:node

準備條件:nginx

  • 自動注入功能須要kubernetes 1.9或更高版本;git

  • kubernetes環境需支持MutatingAdmissionWebhook;github

$ kubectl api-versions | grep admissionregistrationweb

admissionregistration.k8s.io/v1beta1docker

  • 須要在kube-apiserver的啓動參數中加入;api

——admission-control=MutatingAdmissionWebhook,
ValidatingAdmissionWebhook
  • 確保master到node容器網絡通訊正常。網絡

自動注入控制:app

  • 可經過在sidecar-injector的configmap中設置policy=disabled字段來設置是否啓用自動注入(此處爲全局控制是否啓用自動注入功能);ide

$ kubectl get cm istio-sidecar-injector -nistio-system
apiVersion: v1
kind: ConfigMap
metadata:
name: istio-sidecar-injector
namespace: istio-system
data:
config: |-
    policy: enabled     //enabeld爲開啓,disabeld爲關閉
  • 爲須要自動注入的namespace打上標籤istio-injection: enabled(此處爲ns級別的自動注入控制)。

$ kubectl get namespace -L istio-injection
NAME           STATUS    AGE       ISTIO-INJECTION
default        Active    1h
istio-system   Active    1h
kube-public    Active    1h
kube-system    Active    1h
$ kubectl label namespace default istio-injection=enabled
namespace "default" labeled
$ kubectl get namespace -L istio-injection
NAME           STATUS    AGE       ISTIO-INJECTION
default        Active    1h        enabled
istio-system   Active    1h
kube-public    Active    1h
kube-system    Active    1h
  • 同時也能夠在deployment中經過設置annotation,sidecar.istio.io/inject=true來控制pod級別的自動注入。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test
spec:
replicas: 1
template:
metadata:
annotations:
sidecar.istio.io/inject: 「true」
// true爲啓用自動注入,false爲關閉自動注入
  • 定義webhook參數文件MutatingWebhookConfiguration,格式以下(在helm包的sidecarInject中)。

這裏的語義就是,監聽具備istio-injection: enabled的label的namespace下的pod資源,當發生rules(CREATE POD)的動做時,則調用services(istio-sidecar-injector.istio-system的/inject接口)。

apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingWebhookConfiguration
metadata:
 name: istio-sidecar-injector
namespace: {{ .Release.Namespace }}
labels:
app: istio-sidecar-injector
webhooks:
- name: sidecar-injector.istio.io
 clientConfig:
service:
 name: istio-sidecar-injector
namespace: {{ .Release.Namespace }}
 path: "/inject"
caBundle: ""
rules:
- operations: [ "CREATE" ]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
failurePolicy: Fail
namespaceSelector:
matchLabels:
istio-injection: enabled
  • webhook工做流程圖

介紹了自動注入的注意事項與原理,終於能夠測試下自動注入的結果了

  • 首先安裝Istio控制面,確保sidecar-inject安裝完成;

$ kubectl get po -nistio-system | grep sidecar-injector
istio-sidecar-injector-5fb5999bf8-59k79          1/1       Running   0
1d
  • 部署一個簡單的測試deploy,此處咱們以nginx爲例;

$ kubectl get po | grep nginx
nginx-v1-74c674fbd5-fl9bh         1/1       Running   0          22s
  • 咱們用步驟b).II中的方式爲default的namespace打上自動注入標籤,刪除pod,觀察pod狀態,能夠看到pod的容器數由1變爲2;

$ kubectl get po | grep nginx
nginx-v1-54fbccf6fd-ff4k2         2/2       Running       0          4s
nginx-v1-74c674fbd5-fl9bh         1/1       Terminating   0          5m
  • 能夠看到sidecar容器已經注入成功,咱們看下pod的描述信息,觀察下自動注入作了什麼。能夠看到,自動注入向pod中插入了一個初始化容器istio-init和一個sidecar容器istio-proxy(詳細參數能夠參考configmap:istio-sidecar-injector);

$ kubectl describe po nginx-v1-54fbccf6fd-ff4k2
Name:           nginx-v1-54fbccf6fd-ff4k2
Namespace:      default
Status:         Running
...

Init Containers:
istio-init:
Container ID:  
docker://96951306e214594d0c1e550f732a81781287f79f0e5a3262455f38535d42d61f
Image:         istio/proxy_init:0.8.0
...
Containers:
container-0:
Container ID:   
docker://237781c7ce1e8c1f49f68047142ce1738822bafbe504f836f51873cbb1ac1f5d
Image:          nginx:1.12-alpine-perl
Port:           80/TCP
State:          Running
...
istio-proxy:
Container ID:  
docker://7208d32552918a5853fd56171bdbab3de3ae734242d23b140f6e5c2a1a4bce64
Image:         istio/proxyv2:0.8.0
Args:
proxy
sidecar
    --configPath
/etc/istio/proxy
--binaryPath
/usr/local/bin/envoy
--serviceCluster
nginx
...

istioctl手動注入:

  • 下載istioctl工具並拷貝至環境,連接https://github.com/istio/istio/releases/ 

  • 將istioctl二進制拷貝至/usr/local/bin目錄下

mv -f istioctl /usr/local/bin
$ kubectl get cm -n istio-system | grep istio-sidecar-injector
istio-sidecar-injector                  1         15h
  • 準備須要注入的文件test.yaml

  • 執行istioctl會在原始內容的基礎上加入sidecar的配置內容,並輸出到控制檯。

$ kubectl apply -f <(istioctl kube-inject -f test.yaml)
  • 將istioctl處理以後的內容部署到kubernetes上

$ kubectl apply -f <(istioctl kube-inject -f test.yaml)
  • 能夠經過k8s命令查看pod詳細內容

$ kubectl describe pod test-c9f4b55c7-np4cf

總結:

這裏更推薦自動注入的方式來實現sidecar的注入,能夠經過在deployment的annotation中加入對應的key來實現自動注入的控制。自動注入實現的邏輯並不複雜,主要是對k8s中webhook的使用,以及經過模板,向deployment中注入相應的container資源等。

相關文章
相關標籤/搜索