[英]How to expose redis to outside with istio sidecar?
我在 k8s 1.15.0、istio 1.4.3 上使用 redis,它在網絡內運行良好。
但是,當我嘗試使用 istio 網關和 sidecar 將其公開給外部網絡時,它失敗了。
然后我刪除了 istio sidecar 並在 k8s 中啟動了 redis 服務器,它起作用了。
搜索后,我將 DestinationRule 添加到配置中,但沒有幫助。
那么它有什么問題呢? 感謝您提供任何提示!
這是我的redis.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: docker.io/redis:5.0.5-alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 16379
protocol: TCP
name: redis-port
volumeMounts:
- name: redis-data
mountPath: /data
- name: redis-conf
mountPath: /etc/redis
command:
- "redis-server"
args:
- "/etc/redis/redis.conf"
- "--protected-mode"
- "no"
volumes:
- name: redis-conf
configMap:
name: redis-conf
items:
- key: redis.conf
path: redis.conf
- name: redis-data
nfs:
path: /data/redis
server: 172.16.8.34
---
apiVersion: v1
kind: Service
metadata:
name: redis-svc
labels:
app: redis-svc
spec:
type: ClusterIP
ports:
- name: redis-port
port: 16379
protocol: TCP
selector:
app: redis
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: redis-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: tcp
protocol: TCP
hosts:
- "redis.basic.svc.cluster.local"
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: redis-svc
spec:
host: redis-svc
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: redis-vs
spec:
hosts:
- "redis.basic.svc.cluster.local"
gateways:
- redis-gateway
tcp:
- route:
- destination:
host: redis-svc.basic.svc.cluster.local
port:
number: 16379
更新:
這就是我的要求
[root]# redis-cli -h redis.basic.svc.cluster.local -p 80
redis.basic.svc.cluster.local:80> get Test
Error: Protocol error, got "H" as reply type byte
在使用 istio 公開 TCP 應用程序的情況下,幾乎沒有什么需要不同的。
hosts:
需要為"*"
因為TCP
協議僅適用於IP:PORT
。 L4 中沒有標題。
需要TCP
端口match
Your VirtualService
匹配GateWay
。 我建議以獨特的方式命名它並匹配Deployment
端口名稱。
我建議避免使用端口80
因為它已經在默認入口配置中使用,並且可能導致端口沖突,所以我將其更改為11337
。
所以你的GateWay
應該是這樣的:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: redis-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 11337
name: redis-port
protocol: TCP
hosts:
- "*"
你的VirtualService
是這樣的:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: redis-vs
spec:
hosts:
- "*"
gateways:
- redis-gateway
tcp:
- match:
- port: 11337
route:
- destination:
host: redis-svc
port:
number: 16379
請注意,為了清楚起見,我刪除了名稱空間。
然后使用以下命令將我們的自定義端口添加到默認入口網關:
kubectl edit svc istio-ingressgateway -n istio-system
並添加以下其他端口定義:
- name: redis-port
nodePort: 31402
port: 11337
protocol: TCP
targetPort: 16379
要訪問暴露的應用程序,請使用我們剛剛設置的 istio 網關外部 IP 和端口。
要獲取您的網關外部 IP,您可以使用:
export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
redis-cli -h $INGRESS_HOST -p 11337
如果您的istio-ingressgateway
沒有分配外部 IP,請使用您的節點 IP 地址和端口31402
。
希望這可以幫助。
謝謝肯定的回答。
但我認為redis.basic.svc.cluster.local
在 DNS 主機之外,可以通過 VirtualService 和VirtualService
進行匹配。 host
是具有完整命名空間路徑的服務redis-svc
路由。
也許不是這個原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.