繁体   English   中英

使用 nginx-Ingress 在 Kubernetes 中暴露集群外的 TCP 端口

[英]Exposing a TCP port out of cluster in Kubernetes using nginx-Ingress

所以我已经使用 Kubernetes 在谷歌云上设置了我的应用程序。 我有一个 Pod,我想将它暴露在需要 TCP 请求的集群之外。

我开始知道这可以通过ingress-nginx 实现,并对此进行了研究。 正如此处文档中所述,可以通过设置如下的 configMap 来完成:

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-configmap-example
data:
  9000: "default/my-service-name:7051

,但它的完整用法没有清楚地描述,我也无法在文档中正确找到完整的示例。

我已经安装了安装指南中提到的 ingress-nginx,但我不确定接下来要公开我的 Pod 的步骤是什么。

额外信息

  • 我想在集群外公开的 Pod 中的端口是7051
  • 我有一个 NodePort 服务,它以我的 Pod 端口为目标,可以与 Ingress 一起使用来公开。

因此,为了实现这一点,您可以这样做:

  1. 首先创建您添加到帖子中的 configMap。
apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-configmap-example
data:
  9000: "default/my-service-name:7051
  1. 然后通过将此标志添加到容器参数来编辑您的 nginx-ingress-controller 部署,如下所示:

     ... containers: - name: nginx-ingress-controller image: "quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.26.1" imagePullPolicy: "IfNotPresent" args: - /nginx-ingress-controller - --default-backend-service=nginx-ingress/nginx-ingress-default-backend - --election-id=ingress-controller-leader - --ingress-class=nginx - --configmap=nginx-ingress/nginx-ingress-controller - --tcp-services-configmap=default/tcp-configmap-example ...
  2. 通过向 LoadBalancer 添加端口来编辑 LoadBalancer 服务

    ... ports: - name: http port: 80 protocol: TCP targetPort: http - name: https port: 443 protocol: TCP targetPort: https - name: some-service-port port: 7051 protocol: TCP

希望能帮助到你!

如果您使用helm进行安装,则可以通过设置值来公开 tcp 端口。

# add helm repo
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

helm show values ingress-nginx/ingress-nginx将显示values.yaml文件以供参考,有两个字典用于暴露端口: tcpudp

# TCP service key:value pairs
# Ref: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/exposing-tcp-udp-services.md
##
tcp: {}
#  8080: "default/example-tcp-svc:9000"

# UDP service key:value pairs
# Ref: https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/exposing-tcp-udp-services.md
##
udp: {}
#  53: "kube-system/kube-dns:53"

要从命令行设置值:

# set `tcp` dictionary in values (other `helm install` options omitted, only left options regarding to exposing tcp ports)
helm install ingress-nginx ingress-nginx/ingress-nginx --set tcp.12345=some-namespace/some-service:80

在Google Cloud Platform内部,您可以使用type: LoadBalancer ,以将服务公开到集群之外。 您可以在此处看到使用服务公开应用程序的示例。

这是一个简单的示例:

$ kubectl run hello --image=test/hello-world
deployment "hello" created

$ kubectl expose deployment hello --port=8080 --type=LoadBalancer
service "hello" exposed

$ kubectl get service 
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
hello        LoadBalancer   10.11.251.34   35.192.25.112   8080:33107/TCP   2m

$ curl 35.192.25.112:8080
<html><head><title>hello world</title></head><body>hello world!</body></html>

您也可以按照Kubernetes文档中的说明进行操作, 公开一个外部IP地址以访问集群中的应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM