How do I get the created pod to run an application (command and arguments) while having a deployment and service referencing it?


tyrant

Context :
Technologies : Java, Docker Toolbox, Minikube.
I have a java web application (already packaged as web-tool.jar) and I want to have all the benefits of kubernetes at runtime.
To instruct kubernetes to fetch the image locally, I use the image tag:

docker build -t despot/web-tool:1.0 .

Then make it available to minikube by:

docker save despot/web-tool:1.0 | (eval $(minikube docker-env) && docker load)

The docker file is:

FROM openjdk:11-jre
ADD target/web-tool-1.0-SNAPSHOT.jar app.jar
EXPOSE 1111
EXPOSE 2222

1. How can I create a pod, run a java application, and have a deployment and service referencing it at the same time?

1.1. Can I create a deployment to propagate commands and arguments when creating a pod? (Best for me as I make sure to create deployments and services before creating pods)
1.2. If 1.1. is not possible, can I kubectl apply some pod configuration with commands and parameters to the already created deployment/pod/service? (Worse solution is an extra manual step)
1.3. If 1.2. is not feasible, is it possible to create a deployment/service and attach it to an already running pod (starting with "kubectl run ... java -jar app.jar reg")?

What I've tried is :
a) create a deployment (start a pod automatically) and expose (create a service):

kubectl create deployment reggo --image=despot/web-tool:1.0

With this, a pod is created in a CrashLoopBackoff state because it doesn't have a foreground process running.

b) Try the following, hoping the deployment accepts commands and arguments that will be propagated to pod creation (1.1.):

kubectl create deployment reggo --image=despot/web-tool:1.0 -- java -jar app.jar reg

Same result for pods, since the deployment doesn't accept commands and arguments.

c) Trying to apply pod config using command and args after deploy creates pod, so I run command from a), find pod's id (reggo-858ccdcddd-mswzs) using (kubectl get pods) and then I execute:

kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: reggo-858ccdcddd-mswzs
spec:
  containers:
  - name: reggo-858ccdcddd-mswzs
    command: ["java"]
    args: ["-jar", "app.jar", "reg"]
EOF

but I got:


Warning: kubectl apply should be used for resources Pod "reggo-858ccdcddd-mswzs" created by kubectl create --save-config or kubectl apply is invalid:
* spec.containers[0].image:
required value * spec.containers : Forbidden: pod update cannot add or remove containers

This makes me think that I cannot execute the command by applying the command/parameter configuration.

Solution (using Arghya's answer ) :

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reggo
spec:
  selector:
    matchLabels:
      app: reggo-label
  template:
    metadata:
      labels:
        app: reggo-label
    spec:
     containers:
     - name: reggo
       image: "despot/web-tool:1.0"
       command: ["java"]
       args: ["-jar", "app.jar", "reg"]
       ports:
       - containerPort: 1111
EOF

and execute:

kubectl expose deployment reggo  --type=NodePort --port=1111
Agia Sadhu

You can use the java -jarcommand in the docker file itself , ENTRYPOINTwhich tells Docker to run the java application.

FROM openjdk:11-jre
ADD target/web-tool-1.0-SNAPSHOT.jar app.jar
EXPOSE 1111
EXPOSE 2222
ENTRYPOINT ["java", "-jar", "app.jar", "reg"]

Alternatively, the same can be achieved via the commandand section in the kubernetes yamlargs

containers:
- name: myapp
  image: myregistry.azurecr.io/myapp:0.1.7
  command: ["java"]
  args: ["-jar", "app.jar", "reg"]

Now coming to the point of Forbidden: pod updates may not add or remove containerserror , it happens because you are trying to modify part of an existing pod object that is containersnot allowed . Instead of doing this, you can take the entire deployment yaml and open it in an editor and edit it to add a command section, then delete the existing deployment, and finally apply the modified deployment yaml to the cluster.

  1. kubectl get deploy reggo -o yaml --export > deployment.yaml
  2. By deleting an existing deploymentkubectl delete deploy reggo
  3. Edit deployment.yaml to add the correct command
  4. Apply yaml to the clusterkubectl apply -f deployment.yaml

Related


How do I run a command when starting a Pod in Kubernetes?

Karthik Rao V. I want to execute a command during pod creation. I see two options available: kubectl run busybox --image=busybox --restart=Never -- sleep 3600 kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600" What is the difference

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I get notified when a pod or service or endpoint changes?

Blankman Is there a way to be notified if I have services, pods, etc. that can be queried using selectors that change their ip addresses? For example, let's say my application needs a list of Pod's IP addresses or Service's IP addresses. Since a container can

How do I run a command while logging the output to a file?

Ben Morel I have a console application that needs to run as part of the deployment of a new application version on the server. This console application is designed to output to the console and cannot be changed. I want to run it normally, but log stdout and st

How do I run a command while logging the output to a file?

Ben Morel I have a console application that needs to be run as part of the deployment of a new application version on the server. This console application is designed to output to the console and cannot be changed. I want to run it normally, but log stdout and

What command can I run to get the oldest pod name?

Jar 99 I want to get the name of the oldest pod in the script. It appears that I should be able to run kubectl get po --no-headers=trueAGE-sort, then sort by AGE, then pipe to head -n1|awk '{print $1}', but can't seem to sort by job. I am running kubectl 1.7.9

What command can I run to get the oldest pod name?

Jar 99 I want to get the name of the oldest pod in the script. It appears that I should be able to run kubectl get po --no-headers=trueAGE-sort, then sort by AGE, then pipe to head -n1|awk '{print $1}', but can't seem to sort by job. I am running kubectl 1.7.9

How do I get the command to run after logging in and starting x?

user xset led 3What if I need to run after every login ? I am using Manjaro from gnome. hammock If you're using a display manager (eg Gnome-native GDM) , look at ~/.xprofile , where you put the command to invoke the x server after it has started but before any

How do I get the command to run after logging in and starting x?

user xset led 3What if I need to run after every login ? I am using Manjaro from gnome. hammock If you're using a display manager (eg Gnome-native GDM) , look at ~/.xprofile , where you put the command to invoke the x server after it has started but before any

How do I feed a command with arguments to Bash?

d3pd I need to type a UTC timestamp (like "2017-01-25T1422Z") very regularly, so I set it as a shortcut key in Ubuntu ( Ctrl+ Shift+ d) . Specifically, the custom shortcut keys are set to the following commands: bash /home/user/scripts/type_time_UTC.sh The sc

How do I pass arguments to the "source" command?

newbie My sourcecommand in CShell looks like this: source /directory/of/script/script.csh Between script.cshexecution and my password awaits confirmation of identity. I'm tired of having to enter my password every time source. So since my password is constant

How do I feed a command with arguments to Bash?

d3pd I need to type a UTC timestamp (like "2017-01-25T1422Z") very regularly, so I set it as a shortcut key in Ubuntu ( Ctrl+ Shift+ d) . Specifically, the custom shortcut keys are set to the following commands: bash /home/user/scripts/type_time_UTC.sh The sc

How do I pass arguments to the "source" command?

newbie My sourcecommand in CShell looks like this: source /directory/of/script/script.csh Between script.cshexecution and my password awaits confirmation of identity. I'm tired of having to enter my password every time source. So since my password is constant

How to run Interop.Excel.Application with command arguments?

Michael Fiala I need to pass parameters to a new instance of excel and then read the parameters from the new instance via Addin. I want to open new excel application with this structure: new Microsoft.Office.Interop.Excel.Application() Is there a way how to p

How to run an application with command line arguments in Mac OS

Andrew On a Mac, is there any easy way to append command line arguments to an application? For example, to run Opera in kiosk mode or use a different profile in Firefox, I can type $ /Applications/Opera.app/Contents/MacOS/Opera -kioskmode $ /Applications/Firef