简体   繁体   English

从 java 应用程序到 kubernetes pod 执行命令

[英]Execute command from java application to kubernetes pod

I have been searching web to execute a command in a Kubernetes pod using my java application.我一直在搜索 web 以使用我的 java 应用程序在 Kubernetes pod 中执行命令。 The results were blurry, Hence I would like to know If there is a way for a java application running in one pod to execute a command on another pod?结果很模糊,因此我想知道是否有办法让在一个 pod 中运行的 java 应用程序在另一个 pod 上执行命令?

[Is] there is a way for a java application running in one pod to execute a command on another pod? [是否] 在一个 pod 中运行的 java 应用程序可以在另一个 pod 上执行命令吗?

No. You need to create a network interface to the second application and make gRPC or HTTP calls to it, or build both programs into the same image so that you can launch the second program as an ordinary subprocess.不可以。您需要为第二个应用程序创建一个网络接口并对它进行 gRPC 或 HTTP 调用,或者将两个程序构建到同一个映像中,以便您可以将第二个程序作为普通子进程启动。

This is true of containers in general.一般情况下,容器都是如此。 You'd have the same problem trying to run the two parts in non-Kubernetes Docker containers (recommended for experimentation and development) or running two containers in the same Kubernetes pod (not recommended).尝试在非 Kubernetes Docker 容器(推荐用于实验和开发)中运行这两个部分或在同一个 Kubernetes pod 中运行两个容器(不推荐)时,您会遇到同样的问题。

(There's a "but" involving using the Kubernetes API, but this is a rather complex setup, it involves Kubernetes-specific code in your application, and it requires service-account and permissions setup in the Kubernetes deployment. I'd avoid that, especially for your application's core data flow.) (有一个“但是”涉及使用 Kubernetes API,但这是一个相当复杂的设置,它涉及应用程序中特定于 Kubernetes 的代码,它需要在 Z30136395F0187979241983 中设置服务帐户和权限。特别是对于您的应用程序的核心数据流。)

I suppose you can communicate with the pod with a service or kube-dns which resolves svcName.service.ns.cluster.local as ClusterIP and use another websocket to that pod which listens your commands and etc.我想您可以使用将svcName.service.ns.cluster.local解析为 ClusterIP 的服务或 kube-dns 与 pod 通信,并使用另一个 websocket 到该 pod 来监听您的命令等。

Can not help you with java sockets or detailed code structure but I am pretty sure that this can help.无法帮助您 java sockets 或详细的代码结构,但我很确定这会有所帮助。

Edit1: example for service yaml files. Edit1:服务 yaml 文件的示例。

For service of podA:对于 podA 的服务:

apiVersion: v1
kind: Service
metadata:
  name: svc-nodeport-httpd
spec:
  type: NodePort
  ports:
  - port: 3050
    targetPort: 80
    nodePort: 31000
  selector:
    app: apache_webserver

which creates a pod-port to node-port connection or它创建一个 pod-port 到 node-port 的连接或

ClusterIP.集群IP。 This is Suggested!这是推荐的!

apiVersion: v1
kind: Service
metadata:
  name: "myapp-service"
  namespace: "namespace"
spec:
  ports:
  - name: appPort
    port: 8065
    protocol: TCP
    targetPort: http
  selector:
    app: "myapp"
  type: ClusterIP

which creates a service which can be used for internal traffic.它创建了一个可用于内部流量的服务。

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

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