当我在GKE中将NodePort服务指定为Inress后端时,我得到两个后端 [英] When I specify NodePort service as Ingress backend in GKE, I get two backends
问题描述
--概述
我在GKE(type=nodeport)上自定义安装了istio。安装命令如下
istioctl install --set profile=default --set values.gateways.istio-ingressgateway.type=NodePort
我正在构建Inress并将NodePort服务指定为后端。我发现另一个后端被GCP指定为默认设置,即使我指定了NodePort。因此,我无法通过TCP/IP连接到GCP LoadBalancer。如果我为入口设置与Pod的ReadinessProbe相同的端口,等等,运行状况检查也会在那里进行。 有什么办法可以解决这个问题吗?
--详细信息
- 有两个后端的负载均衡详细信息(顶级后端服务是GCP中的默认配置)
- 节点端口配置
# This is a value that is automatically set by istio
$ k get svc istio-ingressgateway -n istio-system -o yaml
ports:
- name: status-port
nodePort: 32476
port: 15021
protocol: TCP
targetPort: 15021
- name: http2
nodePort: 32241
port: 80
protocol: TCP
targetPort: 8080
- name: https
nodePort: 31739
port: 443
protocol: TCP
targetPort: 8443
- name: tcp-istiod
nodePort: 32488
port: 15012
protocol: TCP
targetPort: 15012
- name: tls
nodePort: 32741
port: 15443
protocol: TCP
targetPort: 15443
- istio-ingressateway(Pod)就绪设置
$ k get po istio-ingressgateway-6f8bbbbd8c-qmkln -n istio-system -o yaml
:
readinessProbe:
failureThreshold: 30
httpGet:
path: /healthz/ready
port: 15021
scheme: HTTP
initialDelaySeconds: 1
periodSeconds: 2
successThreshold: 1
timeoutSeconds: 1
- 入口设置
#
spec:
rules:
- host: www.custom.com
http:
paths:
- backend:
serviceName: istio-ingressgateway
servicePort: 80
- backend:
serviceName: istio-ingressgateway
servicePort: 15021
- 结果
kubectl get svc
istio-system istio-ingressgateway NodePort 10.47.13.185 <none> 15021:31761/TCP,80:31561/TCP,443:31257/TCP,15012:31841/TCP,15443:32172/TCP 9h
推荐答案
我已将此答案分成几个部分:
- 使用
GKE
创建Ingress
资源时有两个后端。 - 问题中的入口定义。
- 将
Cloud
Armor与Istio
集成。
使用GKE
创建Ingress
资源时有两个后端
这工作正常,因为使用GKE
创建的Ingress
将有两个后端:
YAML
清单中指定的那个。- 默认后台:
default-http-backend
。
作为示例,您可以按照以下步骤操作:
$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
- 为
nginx
创建Ingress
资源,但使用hello
路径(例如)
之后,您应该会看到类似的设置:
第一个后端服务正在使用实例组向默认后端发送与Ingress
资源不匹配的请求。
第二个后端服务正在使用NEG(网络终结点组)发送与Ingress
资源(在此示例中为nginx
Deployment
)匹配的请求。
我已将红色方块标记为";将其连接到Kubernetes资源(查看端口):
$ kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.20.6.229 <none> 80:32612/TCP 51m
$ kubectl get svc -n kube-system default-http-backend
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default-http-backend NodePort 10.20.15.39 <none> 80:30603/TCP 121m
问题中的入口定义
问题中包含的Ingress
定义的一部分是/以引用具有2个不同后端的相同路径的方式创建的:
http:
paths:
- backend:
serviceName: istio-ingressgateway
servicePort: 80
- backend:
serviceName: istio-ingressgateway
servicePort: 15021
以上示例:
- 有两个不同的后端。
- 配置为两个不同的后端共享同一路径。
- 请求(正如我已复制的那样)进入";
15021
";后端,该后端仅用于运行状况检查。
要解决此问题,您需要删除15021
后端,并使用backendConfig
资源配置运行状况检查和安全策略(稍后将详细介绍)。
附注!
YAML
清单正在使用陈旧且即将废弃的方式来描述Ingress
。有关更多参考信息,请参阅本文档:
将Cloud Armor与Istio集成
您可以通过以下方式将Cloud Armor与Istio配合使用:
- 创建安全策略。
- 创建
backendConfig
作为IstioService
的先决条件。 - 安装Istio并修改其
Service
。 - 创建指向
istio-ingressgateway
的Ingress
资源。 - 使用示例进行测试。
创建安全策略
出于示例目的,可以创建仅阻止单个IP地址的安全策略。可以通过gcloud
或Cloud Console
(Web UI)完成:
我们假设创建的名为deny-single
的安全策略阻止了单个IP地址。
创建backendConfig
作为IstioService
的先决条件。
您需要创建backendConfig
来配置运行状况检查并强制实施安全策略:
apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: ingress-backendconfig
namespace: istio-system
spec:
healthCheck:
requestPath: /healthz/ready
port: 15021
type: HTTP
securityPolicy:
name: deny-single # <-- IMPORTANT
安装Istio并修改其Service
您需要将以下批注添加到istio-ingressgateway
的Service
:
cloud.google.com/backend-config: '{"default": "ingress-backendconfig"}'
cloud.google.com/neg: '{"ingress":true}'
此批注将通知GCP
安全策略同时应用要将通信传递到istio-ingressgateway
所需的运行状况检查。
创建Ingress
资源以指向istio-ingressgateway
将请求从HTTP(S) Load Balancer
发送到istio-ingressgateway
的基本Ingress
定义可以如下:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: istio-ingress
namespace: istio-system
spec:
rules:
- http:
paths:
- path: /*
pathType: ImplementationSpecific
backend:
service:
name: istio-ingressgateway
port:
number: 80
使用示例进行测试
若要检查安装程序是否正常工作,您可以派生Bookinfo Application。
使用两个不同的IP地址测试:
$ curl ifconfig.me
217.AAA.BBB.CCC
$ curl 34.XXX.YYY.ZZZ/productpage
<!doctype html><meta charset="utf-8"><meta name=viewport content="width=device-width, initial-scale=1"><title>403</title>403 Forbidden%
$ curl ifconfig.me
94.EEE.FFF.GGG
$ curl 34.XXX.YYY.ZZZ/productpage
<html>
<head>
<title>Simple Bookstore App</title>
<-- REDACTED -->
附注!
您收到的";关闭端口&可能与istio-ingressgateway
配置为侦听特定路径(如/productpage
而不是/
)有关。(如果请求专门针对端口80
,而不是15021
)
其他资源:
- Alwaysupalwayson: Posts: 2021: 04: Cloud armor
- Cloud.google.com: Kubernetes Engine: Docs: How to: Standalone NEG
- Istio.io
这篇关于当我在GKE中将NodePort服务指定为Inress后端时,我得到两个后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!