Interactive deployment of an image

You can also use the kubectl run command to start an interactive session with a Pod. This can be exceptionally useful to log in and see what is available in a container image, or within the context of the software you've copied into a container image.

For example, if you wanted to run a shell to look around inside the base Alpine container image that I used for the Python example, you could run the following command:

kubectl run -i -t alpine-interactive --image=alpine -- sh

The -i option is what tells it to make the session interactive, and the -t option (which is almost always used with the -i option) indicates that it should allocate a TTY session (a Terminal session) for the interactive output. The trailing -- sh is an override to provide a specific command to be invoked with this session, in this case sh, asking to execute the shell.

When you invoke this command, it still sets up a deployment, and when you exit the interactive shell, the output will tell you how can you reattach to that same interactive shell. The output will look something like the following:

Session ended, resume using 'kubectl attach alpine-interactive-1535083360-4nxj8 -c alpine-interactive -i -t' command when the pod is running

If you want to kill that deployment, you will need to run the following command:

kubectl delete deployment alpine-interactive

This technique is immensely useful for getting a container image up and running within the Kubernetes cluster, and giving you shell access to interact with it. If you are used to using Python, Node.js, or similar dynamic languages, then the ability to get your libraries all loaded and a REPL active for you to interrogate or interact with to interactively poke at the running environment can be incredibly useful.

As an example, we can do this with the same Python image that we used for our Flask application. To bring it up as an interactive session that you can later delete, use the following command:

kubectl run -i -t python-interactive --image=quay.io/kubernetes-for-developers/flask:latest --command -- /bin/sh

This command can take a little time to complete, as it will wait while Kubernetes downloads the image and starts it, using the command we put in (/bin/sh) instead of the entrypoint that we defined for it originally. In a short while, you should see some output in your Terminal window akin to the following:

If you don't see a command prompt, try pressing enter.
/ #

At this point, you can invoke Python and interact with the Python REPL directly, loading code and doing whatever you need. The following are some example commands to show you how this might work:

cd /opt/exampleapp
/opt/exampleapp # python3
Python 3.6.1 (default, May 2 2017, 15:16:41)
[GCC 6.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ
environ({'KUBERNETES_PORT': 'tcp://10.0.0.1:443', 'KUBERNETES_SERVICE_PORT': '443', 'HOSTNAME': 'python-interactive-666665880-hwvvp', 'SHLVL': '1', 'OLDPWD': '/', 'HOME': '/root', 'TERM': 'xterm', 'KUBERNETES_PORT_443_TCP_ADDR': '10.0.0.1', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'KUBERNETES_PORT_443_TCP_PORT': '443', 'KUBERNETES_PORT_443_TCP_PROTO': 'tcp', 'KUBERNETES_PORT_443_TCP': 'tcp://10.0.0.1:443', 'KUBERNETES_SERVICE_PORT_HTTPS': '443', 'PWD': '/opt/exampleapp', 'KUBERNETES_SERVICE_HOST': '10.0.0.1'})
>>> import flask
>>> help(flask.app)
Help on module flask.app in flask:
NAME
flask.app
DESCRIPTION
flask.app
~~~~~~~~~
This module implements the central WSGI application object.
:copyright: (c) 2015 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
CLASSES
flask.helpers._PackageBoundObject(builtins.object)
Flask
class Flask(flask.helpers._PackageBoundObject)
| The flask object implements a WSGI application and acts as the central
| object. It is passed the name of the module or package of the
| application. Once it is created it will act as a central registry for
| the view functions, the URL rules, template configuration and much more.
|
| The name of the package is used to resolve resources from inside the
| package or the folder the module is contained in depending on if the
| package parameter resolves to an actual python package (a folder with
>>> exit()
/opt/exampleapp #

Once you are done interacting with this deployment, you can exit the shell by pressing Ctrl + D or by typing exit.

Session ended, resume using 'kubectl attach python-interactive-666665880-hwvvp -c python-interactive -i -t' command when the pod is running

This is leaving the deployment running, so you can reattach to it using the preceding command, or you can delete the deployment and recreate it again when/if you want it. To delete it, you would use the following command:

kubectl delete deployment python-interactive
deployment "python-interactive" deleted