Execute command from java application to kubernetes pod

4/22/2021

I have been searching web to execute a command in a Kubernetes pod using my java application. 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 ?

-- R_S_C
java
kubernetes

2 Answers

4/22/2021

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.

Can not help you with java sockets or detailed code structure but I am pretty sure that this can help.

Edit1: example for service yaml files.

For service of 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

ClusterIP. 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.

-- Catastrophe
Source: StackOverflow

4/22/2021

Is there is a way for a java application running in one pod to execute a command on another 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.

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).

(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.)

-- David Maze
Source: StackOverflow