官方文档:Istio / 镜像

启动httpbin服务(演示服务)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# v1版本
cat <<EOF | istioctl kube-inject -f - | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin-v1
spec:
replicas: 1
selector:
  matchLabels:
    app: httpbin
    version: v1
template:
  metadata:
    labels:
      app: httpbin
      version: v1
  spec:
    containers:
    - image: docker.io/kennethreitz/httpbin
      imagePullPolicy: IfNotPresent
      name: httpbin
      command: ["gunicorn", "--access-logfile", "-", "-b", "0.0.0.0:80", "httpbin:app"]
      ports:
      - containerPort: 80
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# v2版本
cat <<EOF | istioctl kube-inject -f - | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin-v2
spec:
replicas: 1
selector:
  matchLabels:
    app: httpbin
    version: v2
template:
  metadata:
    labels:
      app: httpbin
      version: v2
  spec:
    containers:
    - image: docker.io/kennethreitz/httpbin
      imagePullPolicy: IfNotPresent
      name: httpbin
      command: ["gunicorn", "--access-logfile", "-", "-b", "0.0.0.0:80", "httpbin:app"]
      ports:
      - containerPort: 80
EOF

创建服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kubectl create -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: httpbin
labels:
  app: httpbin
spec:
ports:
- name: http
  port: 8000
  targetPort: 80
selector:
  app: httpbin
EOF

创建网关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gw
spec:
selector:
  app: httpbin
servers:
- port:
    number: 80
    name: http
    protocol: HTTP
  hosts:
  - "*"
EOF

创建目标规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: httpbin
spec:
host: httpbin
subsets:
- name: v1
  labels:
    version: v1
- name: v2
  labels:
    version: v2
EOF

创建虚拟服务

将流量全部转发到v1版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
gateways:
- bookinfo-gateway
hosts:
- "*"
http:
- match:
  - uri:
      exact: /headers
  route:
  - destination:
      host: httpbin
      subset: v1
    weight: 100
EOF

浏览器访问

流量镜像

将转发到v1的流量克隆一份到v2版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
gateways:
- bookinfo-gateway
hosts:
- "*"
http:
- match:
  - uri:
      exact: /headers
  route:
  - destination:
      host: httpbin
      subset: v1
    weight: 100
  mirror:
    # 镜像目标
    host: httpbin
    subset: v2
  # 镜像比例100%
  mirrorPercentage:
    value: 100.0
EOF

再次访问

重点注意这些被镜像的流量是『即发即弃』的,就是说镜像请求的响应会被丢弃,服务响应还是v1版本的