gpt4 book ai didi

kubernetes - 如何在前后服务之间使用Istio虚拟服务

转载 作者:行者123 更新时间:2023-12-02 12:01:05 25 4
gpt4 key购买 nike

我对Istio完全陌生,球场看起来非常令人兴奋。但是,我无法使其正常工作,这可能意味着我没有正确使用它。
我的目标是在2个服务之间实现 session 关联,这就是为什么我最初使用Istio的原因。但是,我做了一个非常基本的测试,它似乎不起作用:
我有一个kubernetes演示应用程序,它是一项前台服务,一种有状态服务和一种无状态服务。从浏览器,我访问前端服务,该前端服务使用K8s服务名称作为url:http://api-statefulhttp://api-stateless在有状态或无状态服务上调度请求。

我想声明一个虚拟服务来拦截从前台服务发送到有状态服务的请求。我并没有将其声明为网关,因为我知道网关位于K8s集群的外部边界。
我在带有Istio 1.6的Windows上使用Docker。

我在下面复制我的yaml文件。我想做的基本测试:将api状态状态的流量重新路由到api状态状态,以验证是否考虑了虚拟服务。而且它不起作用。你看错了吗?虚拟服务使用不正确吗?我的Kiali控制台在安装过程中未检测到任何问题。

####################################################################
######################### STATEFUL BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
name: stateful-deployment
spec:
replicas: 3
selector:
matchLabels:
app: stateful-backend
tier: backend
template:
metadata:
labels:
app: stateful-backend
tier: backend
spec:
containers:
- name: pocbackend
image: pocbackend:2.0
ports:
- name: http
containerPort: 3000
---
# Service for Stateful containers, listening on port 3000
apiVersion: v1
kind: Service
metadata:
name: api-stateful
spec:
selector:
app: stateful-backend
tier: backend
ports:
- protocol: TCP
port: 3002
targetPort: http
---
#####################################################################
######################### STATELESS BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
name: stateless-backend
spec:
replicas: 3
selector:
matchLabels:
app: stateless-backend
tier: backend
template:
metadata:
labels:
app: stateless-backend
tier: backend
spec:
containers:
- name: pocbackend
image: pocbackend:2.0
ports:
- name: http
containerPort: 3000
---
# Service for Stateless containers, listening on port 3000
apiVersion: v1
kind: Service
metadata:
name: api-stateless
spec:
selector:
app: stateless-backend
tier: backend
ports:
- protocol: TCP
port: 3001
targetPort: http
---
#############################################################
######################### FRONT END #########################
# deployment of the container pocfrontend listening to port 3500
apiVersion: apps/v1
kind: Deployment
metadata:
name: front-deployment
spec:
replicas: 2
selector:
matchLabels:
app: frontend
tier: frontend
template:
metadata:
labels:
app: frontend
tier: frontend
spec:
containers:
- name: pocfrontend
image: pocfrontend:2.0
ports:
- name: http
containerPort: 3500
---
# Service exposing frontend on node port 85
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: NodePort
selector:
app: frontend
tier: frontend
ports:
- protocol: TCP
port: 3500
targetPort: http
nodePort: 30000
---
##############################################################
############ ISTIO PROXY FOR API-STATEFUL SERVIC E############
##############################################################
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: api-stateful-proxy
spec:
hosts:
- api-stateful
http:
- route:
- destination:
host: api-stateless

最佳答案

如注释中所述,可以使用带有粘性 session 配置的DestinationRule进行修复。

可以在istio documentation中找到它的示例:

LoadBalancerSettings

Load balancing policies to apply for a specific destination. See Envoy’s load balancing documentation for more details.

For example, the following rule uses a round robin load balancing policy for all traffic going to the ratings service.

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: bookinfo-ratings
spec:
host: ratings.prod.svc.cluster.local
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN

The following example sets up sticky sessions for the ratings service hashing-based load balancer for the same ratings service using the the User cookie as the hash key.

 apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: bookinfo-ratings
spec:
host: ratings.prod.svc.cluster.local
trafficPolicy:
loadBalancer:
consistentHash:
httpCookie:
name: user
ttl: 0s

关于kubernetes - 如何在前后服务之间使用Istio虚拟服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62217822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com