简体   繁体   English

如何设置 kubernetes 服务和 pod 的 IP 地址和端口以将一些信息发送到 pod?

[英]How would I set up kubernetes Services and pods' IP addresses and ports to send some information into a pod?

The issue of ClusterIP, pod IP, nodePort, and targetPort are still a little confusing to me. ClusterIP、pod IP、nodePort、targetPort 的问题还是让我有点摸不着头脑。

I want to set up a small test case to better evaluate use cases, but I am having a bit of trouble.我想建立一个小测试用例来更好地评估用例,但我遇到了一些麻烦。 At the moment, I am working with kubernetes for docker on mac.目前,我正在使用 kubernetes 在 mac 上为 docker 工作。

What I'd like:我想要什么:

  • A pod with an application on it that listens on a port (say, 8080).一个 pod,上面有一个应用程序,它侦听一个端口(比如 8080)。
  • Pod is running on the docker-desktop node that comes with Kubernetes on Docker Pod 在 Docker 上的 Kubernetes 附带的 docker-desktop 节点上运行
  • A way to send requests from my local machine to the docker-desktop node that the pod can receive on its 8080 port, and a way to get back information the pod provides in response to the request一种从我的本地机器向 docker-desktop 节点发送请求的方法,pod 可以在其 8080 端口上接收该节点,以及一种获取 pod 为响应请求而提供的信息的方法

I am pretty sure I need something like a Service to act as the middleware between the pod and me, but I am not sure how to set something like this up.我很确定我需要像 Service 这样的东西来充当 Pod 和我之间的中间件,但我不确定如何设置这样的东西。

Something like the kubernetes-dashboard api, where I can access the pod with:类似于 kubernetes-dashboard api 之类的东西,我可以通过以下方式访问 pod:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/pod?namespace=default

Here, know kubernetes-dashboard is replaced by the namespace for my pods, and https is replaced with whatever port name I have configured in a Service, but I am not sure about the rest.在这里,知道kubernetes-dashboard被我的 Pod 的命名空间替换,并且https被替换为我在服务中配置的任何端口名称,但我不确定 rest。

You should define a Service with NodePort type to access the service from the host machine.您应该定义一个具有NodePort类型的服务以从主机访问该服务。

Follow the reference --> https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/按照参考-> https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/

The kubernetes dashboard example that you are referring you are actually using kubectl proxy or kubectl port forward to access it.您所指的 kubernetes 仪表板示例实际上是在使用kubectl proxykubectl port forward来访问它。 In this case Kubernetes API Server works as proxy and forwards the request to pod.在这种情况下 Kubernetes API 服务器充当代理并将请求转发到 pod。

You can just create a clusterIP type service and use the kubectl proxy or kubectl port forward mechanism to access it.您可以只创建一个 clusterIP 类型的服务并使用kubectl proxykubectl port forward机制来访问它。 Here is a guide on this. 是一个指南。

The service would look like below as an example该服务将如下所示作为示例

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

You could also use NodePort type service to expose the pod.您还可以使用 NodePort 类型的服务来公开 pod。 In this case you don't need to use kubectl proxy or kubectl port forward在这种情况下,您不需要使用kubectl proxykubectl port forward

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 8080
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

You access it using http://<NODEIP>:30007 where <NODEIP> is any of the kubernetes node's IP.您可以使用http://<NODEIP>:30007访问它,其中<NODEIP>是 kubernetes 节点的 IP 中的任何一个。

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

相关问题 我如何设置一个钩子以在Kubernetes Pod重新启动时发送电子邮件? - How do I set up a hook to send an email on Kubernetes pod restart? Kubernetes:获取网络上其他Pod的IP地址 - Kubernetes: Getting the IP Addresses of Other Pods on the Network Kubernetes Pod与内部/外部IP地址的互通 - Kubernetes Pods intercommunication with internal/external IP addresses 在 Kubernetes 中,我怎样才能有一种访问模式来允许一个 pod 一次写入而多个 pod 只读? - In Kubernetes, how can i have an access mode to allow one pod at a time to write and many pods to read only? 2个端口,用于1个Ingres /服务/ statefulsets / pod - 2 ports for 1 Ingres / services / statefulsets/ pods Kubernetes-如何在 kubernetes 中将数据从一个 Pod 发送到另一个 Pod - Kubernetes-How to send data from a pod to another pod in kubernetes 如何在kubernetes pod中初始化系统服务? - How to initialize systemd services in kubernetes pod? 如何在 kubernetes 中定义和共享 pods 环境变量到 pod 的应用程序 - how to define and share pods environment variable to pod's apps in kubernetes 如何在minikube节点上的kubernetes pod中设置sysctl密钥? - How do I set sysctl key in kubernetes pod on a minikube node? 如何在 Kubernetes pod 中设置 no_proxy - How to set no_proxy in Kubernetes pods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM