一般的服務(wù)不需要做過多修改,開啟自動(dòng)注入后,istio會(huì)自動(dòng)注入siderbar等依賴服務(wù)和組件。
1.設(shè)置命名空間自動(dòng)注入
這里新建一個(gè)命名空間(參照前面的文章)進(jìn)行實(shí)驗(yàn),并設(shè)置成默認(rèn)空間
kubectl config set-context $(kubectl config current-context) --namespace=dwaynt-istio
開啟siderbar默認(rèn)注入,指定dwaynt-istio命名空間:
kubectl label namespace dwaynt-istio istio-injection=enabled
2.創(chuàng)建服務(wù)
Kubectl apply 部署應(yīng)用。Siderbar會(huì)自動(dòng)注入到該服務(wù)。
helloworld.yaml:

3.定義ingressgateway、VirtualService
Istio中使用ingressgateway作為入口,創(chuàng)建istio-gress.yaml,創(chuàng)建gateway規(guī)則,注意VirtualService中的route host指的是服務(wù)的hostname,同一個(gè)namespace里面就是service-name。port number要和istio初始化時(shí)使用的配置(manifests/profiles/demo.yaml)istio-ingressgateway中的設(shè)置對應(yīng)(我選用的是demo配置),http協(xié)議80端口。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
? name: spring-boot-istio-gateway
spec:
? selector:
? ? istio: ingressgateway
? servers:
? ? - port:
? ? ? ? number: 80
? ? ? ? name: http-hellogateway
? ? ? ? protocol: HTTP
? ? ? hosts:
? ? ? ? - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
? name: helloworld-istio
spec:
? hosts:
? ? - "www.helloworld.com"
? gateways:
? ? - spring-boot-istio-gateway
? http:
? ? - match:
? ? ? ? - uri:
? ? ? ? ? ? prefix: /helloworld
? ? ? route:
? ? ? ? - destination:
? ? ? ? ? ? host: helloworld-master
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 17077
4.映射外網(wǎng)端口
kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'

export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o 'jsonpath={.items[0].status.hostIP}')
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
瀏覽器通過ip+port訪問,路由設(shè)置了host,本地綁定域名,通過域名訪問
http://www.helloworld.com:31779/helloworld
5.問題
upstream connect error or disconnect/reset before headers. reset reason: connection failure, transport failure reason: TLS error: 268436501:SSL routines:OPENSSL_internal:SSLV3_ALERT_CERTIFICATE_EXPIRED
解決方法:
重啟istio服務(wù)
kubectl delete pods istio-egressgateway-65bdddf685-mzzmg istio-ingressgateway-7b545cdbc7-68pgx istiod-864977fd6c-qj75n ??-n istio-system
6.集群內(nèi)服務(wù)調(diào)用
6.1coredns方式
查看集群內(nèi)dns(找一個(gè)在集群內(nèi)的服務(wù)查看容器內(nèi)的設(shè)置):

增加到本地dns(在啟動(dòng)coredns時(shí)一般會(huì)自動(dòng)修改/etc/resolve.conf,ubuntu18.04 systemd服務(wù)會(huì)刷新,所以需要修改該服務(wù)的配置):

測試服務(wù)請求:
服務(wù)地址一般為<service-name>.<namespace>.svc.cluster.local:port

7.服務(wù)調(diào)用例子(微服務(wù)調(diào)用另一個(gè)微服務(wù))
增加一個(gè)helloworld-istio服務(wù),和前面的helloworld-master服務(wù)區(qū)分,helloworld-istio調(diào)用helloworld-master服務(wù):
@RestController
public class AController {
????@Autowired
????RestTemplate restTemplate;
????@RequestMapping(value = "/hellosecond/helloworld", method = RequestMethod.GET)
????public Response hello() {
????????Response res = new Response();
????????String result = restTemplate.getForObject("http://helloworld-master.dwaynt-istio.svc.cluster.local:17077/helloworld", String.class);
????????res.setMsg("helloworld-istio-second:" +
????????????????"get from master:" + result);
????????return res;
????}
}
helloworld-istio.yaml示例:
apiVersion: v1
kind: Service
metadata:
? name: helloworld-istio
? namespace: dwaynt-istio
? labels:
? ? verison: "1.0.0"
? ? env: "test"
spec:
? selector:
? ? app: helloworld-istio
? ? release: master
? ports:
? ? ? - name: http
? ? ? ? port: 17078
? ? ? ? targetPort: 17002
---
apiVersion: apps/v1
kind: Deployment
metadata:
? name: helloworld-istio
? namespace: dwaynt-istio
spec:
? replicas: 1
? selector:
? ? matchLabels:
? ? ? app: helloworld-istio
? ? ? release: master
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: helloworld-istio
? ? ? ? release: master
? ? ? ? version: "1.0.0"
? ? spec:
? ? ? containers:
? ? ? ? - name: demo-hello-world-istio
? ? ? ? ? image: dw/demo-hello-world-istio
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - name: http
? ? ? ? ? ? ? containerPort: 17078
增加helloworld-istio服務(wù)的路由:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
? name: helloworld-istio
spec:
? hosts:
? ? - "www.helloworld.com"
? gateways:
? ? - spring-boot-istio-gateway
? http:
? ? - match:
? ? ? ? - uri:
? ? ? ? ? ? prefix: /hellosecond
? ? ? route:
? ? ? ? - destination:
? ? ? ? ? ? host: helloworld-istio
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 17078
? ? - match:
? ? ? ? - uri:
? ? ? ? ? ? prefix: /helloworld
? ? ? route:
? ? ? ? - destination:
? ? ? ? ? ? host: helloworld-master
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 17077
瀏覽器訪問測試:

查看鏈路:

8.服務(wù)簡單治理
8.1限流與熔斷
DestinationRule組件,設(shè)置限流和熔斷規(guī)則
參考:http://dljz.nicethemes.cn/news/show-99078.html
注意,對于HTTP1而言,限制并發(fā)數(shù)=maxConnections*maxRequestsPerConnection,對于HTTP2而言,限制并發(fā)數(shù)=http2MaxRequests,outlierDetection是異常檢測,熔斷機(jī)制,baseEjectionTime拒絕服務(wù)時(shí)間,maxEjectionPercent拒絕比例。
Yaml例子參考,設(shè)置hello-istio服務(wù)的限流和熔斷:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
? name: hello-rule
spec:
? host: helloworld-istio
? trafficPolicy:
? ? connectionPool:
? ? ? tcp:
? ? ? ? maxConnections: 1
? ? ? http:
? ? ? ? http1MaxPendingRequests: 1
? ? ? ? maxRequestsPerConnection: 1
? ? ? ? http2MaxRequests: 1
? ? outlierDetection:
? ? ? consecutive5xxErrors: 1
? ? ? interval: 1s
? ? ? baseEjectionTime: 30s
? ? ? maxEjectionPercent: 100
8.2灰度控制
Helloworld-istio服務(wù)增加一個(gè)2.0.0版本:
---
apiVersion: apps/v1
kind: Deployment
metadata:
? name: helloworld-istio-2
? namespace: dwaynt-istio
spec:
? replicas: 1
? selector:
? ? matchLabels:
? ? ? app: helloworld-istio
? ? ? release: master
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: helloworld-istio
? ? ? ? release: master
? ? ? ? version: "2.0.0"
? ? spec:
? ? ? containers:
? ? ? ? - name: demo-hello-world-istio
? ? ? ? ? image: dw/demo-hello-world-istio-2
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - name: http
? ? ? ? ? ? ? containerPort: 17078
應(yīng)用yaml:
kubectl apply -f helloworld.yaml
多次訪問http://www.helloworld.com:31779/hellosecond/helloworld,查看調(diào)用鏈路。默認(rèn)情況下負(fù)載是輪詢,平均負(fù)載壓力:

按比例灰度訪問:
修改destinationrule,增加subset:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
? name: hello-rule
spec:
? host: helloworld-istio
? trafficPolicy:
? ? connectionPool:
? ? ? tcp:
? ? ? ? maxConnections: 1
? ? ? http:
? ? ? ? http1MaxPendingRequests: 1
? ? ? ? maxRequestsPerConnection: 1
? ? ? ? http2MaxRequests: 1
? ? outlierDetection:
? ? ? consecutive5xxErrors: 1
? ? ? interval: 1s
? ? ? baseEjectionTime: 30s
? ? ? maxEjectionPercent: 100
? subsets:
? - labels:
? ? ? version: "1.0.0"
? ? name: v1
? - labels:
? ? ? version: "2.0.0"
? ? name: v2
修改virtualservice,增加比例,v1:v2按照4:1配置:
? ? ? route:
? ? ? ? - destination:
? ? ? ? ? ? host: helloworld-istio
? ? ? ? ? ? subset: v1
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 17078
? ? ? ? ? weight: 80
? ? ? ? - destination:
? ? ? ? ? ? host: helloworld-istio
? ? ? ? ? ? subset: v2
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 17078
? ? ? ? ? weight: 20
測試,測試100次,基本是4:1的負(fù)載比例:
