How do I get the created pod to run an application (command and arguments) while having a deployment and service referencing it?
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
You can use the java -jar
command in the docker file itself , ENTRYPOINT
which 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 command
and 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 containers
error , it happens because you are trying to modify part of an existing pod object that is containers
not 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.
kubectl get deploy reggo -o yaml --export > deployment.yaml
- By deleting an existing deployment
kubectl delete deploy reggo
- Edit deployment.yaml to add the correct command
- Apply yaml to the cluster
kubectl apply -f deployment.yaml