istio 學習之 手動注入sidecar

 

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到底經歷了什麼呢

  1. 我們可以先觀察這個注入後yml文件
  2. 在觀察注入後的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 做的事情

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章