Kubernetes-Istio之Sidecar自动注入

前提: (官方提供)

1):确认使用的是Kubernetes服务器的受支持版本( 1.13、1.14、1.15):kubectl (官方提供,应该是1.13版本以上,我的是1.16版本)

kubectl version --short

Client Version: v1.16.2
Server Version: v1.16.2

2):  admissionregistration.k8s.io/v1beta1 应该启用

kubectl api-versions | grep admissionregistration.k8s.io/v1beta1

admissionregistration.k8s.io/v1beta1

3): 验证MutatingAdmissionWebhookValidatingAdmissionWebhook插件列在中kube-apiserver --enable-admission-plugins

4): 验证Kubernetes api服务器是否与webhook容器具有网络连接。例如,错误的http_proxy设置可能会干扰api服务器的操作

Sidecar自动注入:

使用Istio 提供的变异Webhook 接纳控制器,可以将Sidecar自动添加到适用的Kubernetes吊舱中 

 启用注入Webhook后,创建的所有新Pod都会自动添加一个Sidecar。

与手动注入不同,自动注入发生在容器级。您不会看到部署本身的任何更改。相反,您需要(通过kubectl describe检查各个容器,以查看注入的代理

部署应用(没开启自动注入):验证部署和Pod是否具有单个容器: nexus.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nexus
spec:
  selector:
    matchLabels:
       name: nexus
  replicas: 1
  template:
    metadata:
      labels:
        name: nexus
    spec:
      containers:
      - name: nexus
        image: sonatype/nexus3
        ports:
        - containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
   name: nexus
spec:
  ports:
    - port: 8081
      targetPort: 8081
#  # ClusterIP, NodePort, LoadBalancer
  type: NodePort
  selector:
    name: nexus

查看:

kubectl apply -f nexus.yml 

deployment.apps/nexus created
service/nexus created
root@master:/usr/local/k8s# kubectl get pods
NAME                     READY   STATUS              RESTARTS   AGE
nexus-69c9458b8f-wj9ns   1/1     Running                0       5s

 部署应用(开启自动注入):

default命名空间标记istio-injection=enabled:

kubectl label namespace default istio-injection=enabled
# 打印
namespace/default labeled

kubectl get namespace -L istio-injection
# 打印
NAME              STATUS   AGE   ISTIO-INJECTION
default           Active   37m   enabled  (打印出这个就标记成功)
istio-system      Active   34m   
kube-node-lease   Active   38m   
kube-public       Active   38m   
kube-system       Active   38m 

修改nexus.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nexus
spec:
  selector:
    matchLabels:
       name: nexus
  replicas: 1
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"          
      labels:
        name: nexus
    spec:
      containers:
      - name: nexus
        image: sonatype/nexus3
        ports:
        - containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
   name: nexus
spec:
  ports:
    - port: 8081
      targetPort: 8081
#  # ClusterIP, NodePort, LoadBalancer
  type: NodePort
  selector:
    name: nexus

部署:

 kubectl apply -f nexus.yml 

deployment.apps/nexus configured
service/nexus unchanged

 kubectl get pod

NAME                     READY   STATUS            RESTARTS   AGE
nexus-54cc6bd9cc-f6ghk   2/2     Running           0           4s
nexus-69c9458b8f-wj9ns   1/1     Running           0           2m

注入发生在容器创建时间。杀死正在运行的吊舱,并验证是否使用注入的边车创建了一个新吊舱。原始容器具有1/1 READY容器,而注入侧车的容器具有2/2 READY容器

查看已注入容器的详细状态。可以看到注入的istio-proxy容器和相应的内容:

kubectl describe pod <pod name>

 使用sidecar.istio.io/inject注释禁用边车注入:

  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"     # 修改成false     
      labels:
        name: nexus 

 

猜你喜欢

转载自www.cnblogs.com/zsifan/p/11830654.html