安装 Istio

1
2
3
4
5
6
[root@master-01 ~]# istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete

创建命名空间

1
2
[root@master-01 ~]# kubectl create ns tnmk
namespace/tnmk created

Istio 默认自动注入 Sidecar. 为 tnmk 命名空间打上标签 istio-injection=enabled

1
2
[root@master-01 ~]# kubectl label namespace tnmk istio-injection=enabled
namespace/tnmk labeled

bookinfo.yaml

使用 kubectl 部署应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@master-01 ~]# kubectl apply -f bookinfo.yaml -n tnmk
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created

确认所有的服务和 Pod 都已经启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@master-01 ~]# kubectl get services -n tnmk
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
details ClusterIP 172.101.98.173 <none> 9080/TCP 112s
productpage ClusterIP 172.101.139.235 <none> 9080/TCP 112s
ratings ClusterIP 172.101.24.172 <none> 9080/TCP 112s
reviews ClusterIP 172.101.40.212 <none> 9080/TCP 112s
[root@master-01 ~]# kubectl get pods -n tnmk
NAME READY STATUS RESTARTS AGE
details-v1-7d88846999-hggx9 2/2 Running 0 3m23s
productpage-v1-7795568889-tqjrv 2/2 Running 0 3m23s
ratings-v1-754f9c4975-tqrqj 2/2 Running 0 3m23s
reviews-v1-55b668fc65-nns2c 2/2 Running 0 3m23s
reviews-v2-858f99c99-l8vv5 2/2 Running 0 3m23s
reviews-v3-7886dd86b9-wpkkl 2/2 Running 0 3m23s

要确认 Bookinfo 应用是否正在运行,请在某个 Pod 中用 curl 命令对应用发送请求,例如 ratings

1
2
[root@master-01 ~]# kubectl exec -it -n tnmk $(kubectl get pod -l app=ratings -n tnmk -o jsonpath='{.items[0].metadata.name}') -c ratings -- curl productpage:9080/productpage | grep -o "<title>.*</title>"
<title>Simple Bookstore App</title>

为应用程序定义 Ingress 网关

bookinfo-gateway.yaml

1
2
3
[root@master-01 ~]# kubectl apply -f bookinfo-gateway.yaml -n tnmk
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

查看规则

1
kubectl get VirtualService -A