一、 istio gateway使用demo
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: ngdemo-gatewaynamespace: ssx
spec:selector:istio: ingressgateway # use Istio default gateway implementationservers:- port:number: 80name: httpprotocol: HTTPhosts:- "tomcat.shenshuxin.cn"
EOFkubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: ngdemo-virtualservicenamespace: ssx
spec:hosts:- "tomcat.shenshuxin.cn"gateways:- ngdemo-gatewayhttp:- match:- uri:prefix: /v1route:- destination:port:number: 8082host: demo-tomcat-for-ingress-name- route:- destination:port:number: 8081host: demo-tomcat-for-ingress-name
EOF
curl -HHost:tomcat.shenshuxin.cn “http://node101:32318”
端口號是ingressgateway服務的nodeport
查看方式:kubectl get service -n istio-system | grep istio-ingressgateway
找到80端口對應的nodeport即可
二、istio部署測試服務之間的調用通信
部署兩個tomcat服務pod并且配置serivce服務
注意部署的兩個deployment需要指定一下版本標簽version: ??
apiVersion: apps/v1
kind: Deployment
metadata:name: demo-tomcat-for-istio-name1namespace: ssx
spec:replicas: 1selector:matchLabels:app: demo-tomcat-for-istio-dmtemplate:metadata:labels:app: demo-tomcat-for-istio-dmversion: vv11spec:containers:- image: 'docker.io/library/tomcat:8'imagePullPolicy: IfNotPresentname: demo-tomcat-cports:- containerPort: 8080---
apiVersion: apps/v1
kind: Deployment
metadata:name: demo-tomcat-for-istio-name2namespace: ssx
spec:replicas: 1selector:matchLabels:app: demo-tomcat-for-istio-dmtemplate:metadata:labels:app: demo-tomcat-for-istio-dmversion: vv22spec:containers:- image: 'docker.io/library/tomcat:8'imagePullPolicy: IfNotPresentname: demo-tomcat-cports:- containerPort: 8080---
apiVersion: v1
kind: Service
metadata:labels:app: demo-tomcat-for-istio-sv-lbname: demo-tomcat-for-istio-namenamespace: ssx
spec:ports:- name: tomcat8080port: 8081protocol: TCPtargetPort: 8080selector:app: demo-tomcat-for-istio-dmtype: ClusterIP
通過istio的虛擬服務進行流量管理
注意這里的hosts名稱(demo-tomcat-for-istio-name)要和上面的service配置的一致,這樣istio才可以進行流量管理。
這里設置了請求轉發策略,并且設置自定義響應頭
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: demo-tomcat-istio-vsnamespace: ssx
spec:hosts:- demo-tomcat-for-istio-namehttp:- headers:request:set:test: "true"route:- destination:host: demo-tomcat-for-istio-namesubset: vv11weight: 10headers:response:set:ssxppp: abc- destination:host: demo-tomcat-for-istio-namesubset: vv22headers:response:set:ssxppp: 123weight: 90---apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:name: demo-tomcat-istio-drnamespace: ssx
spec:host: demo-tomcat-for-istio-namesubsets:- name: vv11labels:version: vv11- name: vv22labels:version: vv22
驗證
隨便找一個集群中的通過istio代理的服務,執行curl命令:
# curl -I demo-tomcat-for-istio-name.ssx:8081
HTTP/1.1 200 OK
accept-ranges: bytes
etag: W/"8-1691939281480"
last-modified: Sun, 13 Aug 2023 15:08:01 GMT
content-type: text/html
content-length: 8
date: Tue, 15 Aug 2023 00:54:15 GMT
x-envoy-upstream-service-time: 2
server: envoy
ssxppp: fs
調用的方式是service名稱.命名空間名稱:端口號。
curl -I
命令是只顯示響應頭