istio 創建pod的時候會給默認自動注入的命名空間 注入sidecar ,sidecar中包含envoy組件和pilot-agent組件
,這兩個共同組成sidecar。
這次的目的就是為了觀察istio 注入的過程。
首先我們新創建一個test 命名空間
[root@istio-master test]# kubectl create namespace test
namespace/test created
寫一個nginx.yaml
[root@istio-master test]# vi nginx.yaml [root@istio-master test]# cat nginx.yaml apiVersion: apps/v1 kind: Deployment metadata: name: test-nginx labels: name: test-nginx spec: replicas: 1 selector: matchLabels: app: test-nginx template: metadata: labels: app: test-nginx spec: containers: - name: nginx image: nginx:1.14-alpine imagePullPolicy: IfNotPresent ports: - containerPort: 80
執行這個yml文件,創建一個nginx的pod
[root@istio-master test]# kubectl create -f nginx.yaml -n test
deployment.apps/test-nginx created
查看這個pod已經創建完畢
[root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE test-nginx-9ddbd4d55-c676x 1/1 Running 0 22s
對這個pod進行手動注入sidecar。並且觀察pod的變化
[root@istio-master test]# istioctl kube-inject -f nginx.yaml | kubectl apply -f - -n test Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply deployment.apps/test-nginx configured [root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE test-nginx-7f945c7759-4g47d 0/2 PodInitializing 0 7s test-nginx-9ddbd4d55-c676x 1/1 Running 0 11m [root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE test-nginx-7f945c7759-4g47d 0/2 PodInitializing 0 10s test-nginx-9ddbd4d55-c676x 1/1 Running 0 11m [root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE test-nginx-7f945c7759-4g47d 2/2 Running 0 17s test-nginx-9ddbd4d55-c676x 0/1 Terminating 0 11m [root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE test-nginx-7f945c7759-4g47d 2/2 Running 0 23s test-nginx-9ddbd4d55-c676x 0/1 Terminating 0 11m [root@istio-master test]# kubectl get po -n test NAME READY STATUS RESTARTS AGE
可以觀察到pod的變化 由最初的一個消失,變成兩個帶有兩個container的pod,那么這個pod到底經歷了什么呢
- 我們可以先觀察這個注入后yml文件
- 在觀察注入后的pod的詳情
第一步觀察 注入后的yml 和之前發生了什么變化
[root@istio-master test]# istioctl kube-inject -f nginx.yaml > nginx-inject.yaml
然后觀察這個yml文件,因為這個文件很多我就截圖來看下yml多了什么東西
這個截圖還是我們原來的nginx 的鏡像沒有太大變化
下面這個是新生成的istio-proxy
這個鏡像是一個isito-init 這個只是用於給nginx和isito-proxy創建一個網絡環境配置ip地址然后就消失了,並不會占用資源
為了證明 ngnix 和 isito-proxy 在同一個網絡環境,我們再進入pod中的container 看看是否一樣
我們知道原來的nginx 只有一個 80 端口。那么現在肯定增加了好幾個端口,來看看吧
[root@istio-master test]# kubectl exec -it test-nginx-7f945c7759-4g47d -n test -c nginx -- 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:15021 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro tcp 0 0 0.0.0.0:15090 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:15000 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:15001 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:15006 0.0.0.0:* LISTEN - tcp 0 0 :::15020 :::* LISTEN -
增加了好多 用來控制流量以及與代理進行通信的端口,這些都是istio-init 這個pod 做的事情