<p>A little more information is needed about specific operations you are doing, but generally your options are:</p>
<ol>
<li><p>For one-time operations: use <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="noreferrer">kubectl exec</a> to get into containers and do commands (works similar to <code>docker exec</code>).</p>
</li>
<li><p>Also for one-time operations, spawn separate pods with shell access to interact with existing pods, something along the lines:</p>
</li>
</ol>
<pre><code>kubectl run -it --rm --restart=Never --image busybox tempbusybox -- sh
</code></pre>
<ol start="3">
<li><p>For reproducible (not one-time) operations, you may use <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="noreferrer">kubernetes jobs</a>.</p>
</li>
<li><p>In some cases you may use <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="noreferrer">init containers</a> for setup tasks.</p>
</li>
</ol>

CC BY-SA 4.0
2020-09-26T02:37:45.747Z
2020-09-26T02:37:45.747Z
taleodor
<p>Currently I have a setup on AWS with 4 EC2 machines. They provide a web interface that among other things also has a database search. My recent project was to move the database search to AWS Elastic Search. That took a while but in the end it was fine, because to create the initial index I simply had to SSH into one of the machines, run the command to create the index and I was done.</p>
<p>I could not have done it without SSH'ing into it because the production environment (meaning DB and ES instances) is not directly accessible from dev PCs (which makes sense to me).</p>
<p>So, being quite new to Kubernetes my question is: if we were to migrate these EC2 instances and switch to a Kubernetes kind of setup, how would I run the initial command to create the ES index?</p>
<p>I know I can connect to a pod when it's a single container but these would most likely have containers at least for nginx, redis and django (the one I'd want to connect).</p>

<kubernetes><ssh>
How to run a single one-time command on a kubernetes instance/pod
ChatterOne
# How to run a single one-time command on a kubernetes instance/pod

(Asked by: username_0 on 2020-09-25)

Currently I have a setup on AWS with 4 EC2 machines. They provide a web interface that among other things also has a database search. My recent project was to move the database search to AWS Elastic Search. That took a while but in the end it was fine, because to create the initial index I simply had to SSH into one of the machines, run the command to create the index and I was done.

I could not have done it without SSH'ing into it because the production environment (meaning DB and ES instances) is not directly accessible from dev PCs (which makes sense to me).

So, being quite new to Kubernetes my question is: if we were to migrate these EC2 instances and switch to a Kubernetes kind of setup, how would I run the initial command to create the ES index?

I know I can connect to a pod when it's a single container but these would most likely have containers at least for nginx, redis and django (the one I'd want to connect).

**Tags:** kubernetes, ssh


---

## 1 answers

### Answer by username_1 on 2020-09-26. Score: 11 | Accepted answer

A little more information is needed about specific operations you are doing, but generally your options are:

For one-time operations: use kubectl exec to get into containers and do commands (works similar to docker exec).

Also for one-time operations, spawn separate pods with shell access to interact with existing pods, something along the lines:


kubectl run -it --rm --restart=Never --image busybox tempbusybox -- sh


For reproducible (not one-time) operations, you may use kubernetes jobs.

In some cases you may use init containers for setup tasks.


---

