1.在Kubernetes集群中完成Bookinfo样例程序的部署,在master节点/root目录下编写YAML文件istio.yaml创建请求路由,具体要求如下:
(1)路由名称:bookinfo-virtualservice;
(2)将所有流量路由到每个微服务的 v1 版本。
完成后使用该YAML文件创建请求路由。
解:该题是为了实现部署每一个流量都走向微服务的v1版本,所以不仅要定义
DestinationRule
来匹配微服务不同版本的不同label标签来指向不同目地,也需要定义VirtualService
,指向流量路由。
#定义目标规则
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: details
spec:
host: details
subsets:
- name: v1
labels:
version: v1
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: productpage
spec:
host: productpage
subsets:
- name: v1
labels:
version: v1
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: ratings
spec:
host: ratings
subsets:
- name: v1
labels:
version: v1
---
#定义请求路由
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: details
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
---
2.在Kubernetes集群中完成Bookinfo样例程序的部署,然后在maser节点/root目录下编写YAML文件istio.yaml配置HTTP请求超时,具体要求如下:
(1)路由名称:reviews;
(2)将请求路由到reviews服务的v2版本;
(3)对reviews服务的调用增加一个半秒的请求超时。
完成后使用该YAML文件配置HTTP请求超时。
“用来设置请求资源和服务器返回的时间,保证一个请求占用固定时间,超出后报504超时!这样可以保证一个请求占用过长时间。”
官网:https://istio.io/latest/zh/docs/tasks/traffic-management/request-timeouts/#understanding-what-happened
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
timeout: 0.5s
3.在Kubernetes集群中完成Bookinfo示例程序的部署,然后对productpage服务进行链路追踪,具体要求如下:
(1)安装Jaeger服务到集群中;
(2)使用curl工具向productpage服务发送1000个请求以生成追踪信息,每秒请求一次;
完成后通过Jaeger服务追踪最近一次访问productpage的详细信息。
#获取productpage的IP地址
kubectl get svc
#不通curl做不了上诉要求,所以这里写了一简单的循环
i=1 ; while [ $i -le 1000 ] ;do curl 'http://10.96.95.235:9080/productpage' && echo $i && let i++ ;sleep 1;done;
4.在Kubernetes集群中完成Bookinfo示例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml为ratings服务注入HTTP延迟故障,具体要求如下:
(1)注入规则名称:ratings;
(2)为用户jason在reviews:v2和ratings服务之间注入一个7秒的延迟。
完成后使用该YAML文件创建故障注入规则。
#配置ratings虚拟服务,登陆用户jason注入一个7s的延迟
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: ratings
spec:
hosts:
- ratings
http:
- match:
- headers:
end-user:
exact: jason
fault:
delay:
percentage:
value: 100
fixedDelay: 7s
route:
- destination:
host: ratings
---
#配置reviews虚拟服务登陆用户jason指定路由到v2版本
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
5.在Kubernetes集群中完成Bookinfo示例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml创建基于权重的路由,具体要求如下:
(1)虚拟服务名称:reviews;
(2)将30%的流量路由到reviews服务的v1版本;
(3)将70%的流量路由到reviews服务的v3版本。
完成后使用该YAML文件创建权重路由。
#配置目标规则
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
---
#配置虚拟服务
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 30
- destination:
host: reviews
subset: v3
weight: 70
1.在Kubernetes集群中完成httpbin样例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml创建默认路由策略,具体要求如下:
(1)路由策略名称:httpbin;
(2)将100%流量路由到服务的v1版本;
(3)将100%的相同流量镜像到服务的v2版本。
完成后使用该YAML文件创建路由策略。
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: httpbin
spec:
host: httpbin
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- httpbin
http:
- mirror:
host: httpbin
subset: v2
mirror_percent: 100
route:
- destination:
host: httpbin
subset: v1
weight: 100
2.在Kubernetes集群中完成httpbin样例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml配置熔断器,具体要求如下:
(1)目标规则名称:httpbin;
(2)将TLS流量策略 mode:ISTIO_MUTUAL 添加到目标规则;
(3)要求当并发的连接和请求数超过一个,在istio-proxy进行进一步的请求和连接时,后续请求或连接将被阻止。
完成后使用该YAML文件配置熔断器。
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: httpbin
spec:
host: httpbin
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
connectionPool: #连接池设置,当连接池中连接不够用时,服务调用会返回 503 响应码。
tcp:
maxConnections: 1 #最大连接数
connectTimeout: 30ms #连接超时
http:
http1MaxPendingRequests: 1 #对目标的最大挂起 HTTP 请求数。
maxRequestsPerConnection: 1 #每个连接到后端的最大请求数。
3.在Kubernetes集群中完成HTTPBin样例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml创建Ingress,具体要求如下:
(1)在端口80上配置Ingress以实现HTTP流量;
(2)Ingress名称:httpbin-ingress;
(3)允许流量流向路径/status和/delay;
(4)对外访问的域名:httpbin.example.com。
完成后使用该YAML文件创建Ingress。
部署ingress
https://github.com/kubernetes/ingress-nginx/blob/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml
kubectl apply -f deploy.yaml
#修改镜像地址
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.1
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: httpbin-ingress
spec:
defaultBackend:
service:
name: httpbin
port:
number: 8000
rules:
- host: "httpbin.example.com"
http:
paths:
- path: /status
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 8000
- path: /delay
pathType: Prefix
backend:
service:
name: httpbin
port:
number: 8000
4.在Kubernetes集群中完成HTTPBin样例程序的部署,然后在master节点/root目录下编写YAML文件istio.yaml创建Ingress Gateway,具体要求如下:
(1)以NodePort方式配置Ingress端口;
(2)在80端口为HTTP流量配置一个Gateway,名称:httpbin-gateway;
(3)为Gateway的入口流量配置路由,允许流量流向路径/status和/delay;
(4)对外访问的域名:httpbin.example.com。
完成后使用该YAML文件创建Ingress Gateway。
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: httpbin-gateway
protocol: HTTP
hosts:
- httpbin.example.com
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: httpbin-gateway-vs
spec:
gateways:
- httpbin-gateway
hosts:
- httpbin.example.com
http:
- match:
- uri:
prefix: /status
- uri:
prefix: /delay
- uri:
prefix: /headers
route:
- destination:
host: httpbin
port:
number: 8000
#测试访问
curl -H 'Host:httpbin.example.com' http://10.18.4.10:53460/status/200
curl -H 'Host:httpbin.example.com' http://10.18.4.10:53460/delay/1
curl -H 'Host:httpbin.example.com' http://10.18.4.10:53460/headers
5.在Kubernetes集群中完成HTTPBin服务的部署,在master节点/root目录下编写YAML文件istio.yaml创建Egress控制Istio服务网格的出口流量,具体要求如下:
(1)虚拟服务名称:httpbin-ext;
(2)设置调用外部服务httpbin.org的超时时间为3秒;
(3)响应时间超过3秒的httpbin.org服务将被切断。
完成后使用该YAML文件创建Egress控制Istio服务网格的出口流量。
基本概念
由于默认情况下,来自 Istio-enable Pod 的所有出站流量都会重定向到其 Sidecar 代理,集群外部 URL 的可访问性取决于代理的配置。默认情况下,Istio 将 Envoy 代理配置为允许传递未知服务的请求。尽管这为入门 Istio 带来了方便,但是,通常情况下,配置更严格的控制是更可取的。
- 允许 Envoy 代理将请求传递到未在网格内配置过的服务。
- 配置 service entries 以提供对外部服务的受控访问。
- 对于特定范围的 IP,完全绕过 Envoy 代理。
官网信息:https://istio.io/latest/zh/docs/tasks/traffic-management/egress/egress-control/
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin-ext
spec:
hosts:
- httpbin.org
http:
- timeout: 3s
route:
- destination:
host: httpbin.org
weight: 100
出口网关必备小知识:
容器不能访问外网的原因是/etc/resolv.conf
默认的DNS服务器是coredns,导致并不能指定域名去访问外网。
如何修改Pod的DNS:https://www.jianshu.com/p/b9153181e372