Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a container in Kubernetes without creating Deployment or Job?

Tags:

kubernetes

I'm trying to run an interactive Pod (container) in Kubernetes that does not create a Job or Deployment and deletes itself after completing.

The purpose of the container is to give our developers an easy way to access our database, which doesn't have a public IP address.

Currently, we are using this command:

kubectl run -i --tty proxy-pgclient --image=private-registry.com/pgclient --restart=Never --env="PGPASSWORD=foobar" -- psql -h dbhost.local -p 5432 -U pg_admin -W postgres

which works the first time you run it, however, after exiting the session if you try to run the above again to connect to the database again, we get:

Error from server: jobs.extensions "proxy-pgclient" already exists

Forcing the developer to delete the job with:

kubectl delete job proxy-pgclient

before they can run the command and connect again.

Is there any way of starting up an interactive container (Pod) in Kubernetes without creating a Job or Deployment object and having that container be deleted when the interactive session is closed?

like image 454
srkiNZ84 Avatar asked May 23 '16 03:05

srkiNZ84


People also ask

Can you run containers without Kubernetes?

Can You Use Docker Without Kubernetes? The short and simple answer is yes, Docker can function without Kubernetes. You see, Docker is a standalone software designed to run containerized applications. Since container creation is part of Docker, you don't need any separate software for Docker to execute.

Does kubectl run create a deployment or pod?

kubectl run was earlier used to create deployments as well. However, with Kubernetes 1.18, kubectl run was updated to only create pods and it lost its deployment-specific options as well. If you are looking to create a deployment, you should instead use the kubectl create deployment command.


2 Answers

Adding the "--rm" flag to the original command resulted in the Job (and Pod) being deleted at the completion of the interactive session, which is what I was after. The command then becomes:

kubectl run -i --tty --rm proxy-pgclient --image=private-registry.com/pgclient --restart=Never --env="PGPASSWORD=foobar" -- psql -h dbhost.local -p 5432 -U pg_admin -W postgres
like image 196
srkiNZ84 Avatar answered Jan 04 '23 00:01

srkiNZ84


There isn't a short kubectl command that will do exactly what you want. Instead, you can create a yaml/json file with your pod description and run kubectl create -f pod.yaml. Your pod can be set to never restart, so it will terminate once it exits.

like image 36
Robert Bailey Avatar answered Jan 04 '23 02:01

Robert Bailey