2015-07-12 04:04:52 +00:00
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
<!-- BEGIN STRIP_FOR_RELEASE -->
2015-07-16 17:02:26 +00:00
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
2015-07-13 22:15:35 +00:00
2015-07-16 17:02:26 +00:00
< h2 > PLEASE NOTE: This document applies to the HEAD of the source tree< / h2 >
2015-07-12 04:04:52 +00:00
2015-07-16 17:02:26 +00:00
If you are using a released version of Kubernetes, you should
refer to the docs that go with that version.
2015-07-12 04:04:52 +00:00
2015-12-14 18:37:38 +00:00
<!-- TAG RELEASE_LINK, added by the munger automatically -->
2015-07-16 17:02:26 +00:00
< strong >
2015-11-03 18:17:57 +00:00
The latest release of this document can be found
[here ](http://releases.k8s.io/release-1.1/docs/user-guide/getting-into-containers.md ).
2015-07-16 17:02:26 +00:00
Documentation for other releases can be found at
[releases.k8s.io ](http://releases.k8s.io ).
< / strong >
--
2015-07-13 22:15:35 +00:00
2015-07-12 04:04:52 +00:00
<!-- END STRIP_FOR_RELEASE -->
<!-- END MUNGE: UNVERSIONED_WARNING -->
2015-09-07 14:34:03 +00:00
# Getting into containers: kubectl exec
2015-07-13 17:16:45 +00:00
Developers can use `kubectl exec` to run commands in a container. This guide demonstrates two use cases.
2015-07-17 22:35:41 +00:00
## Using kubectl exec to check the environment variables of a container
2015-07-14 16:37:37 +00:00
Kubernetes exposes [services ](services.md#environment-variables ) through environment variables. It is convenient to check these environment variables using `kubectl exec` .
2015-07-13 17:16:45 +00:00
We first create a pod and a service,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-13 17:16:45 +00:00
$ kubectl create -f examples/guestbook/redis-master-controller.yaml
$ kubectl create -f examples/guestbook/redis-master-service.yaml
```
2015-07-17 02:01:02 +00:00
2015-07-13 17:16:45 +00:00
wait until the pod is Running and Ready,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-13 17:16:45 +00:00
$ kubectl get pod
NAME READY REASON RESTARTS AGE
redis-master-ft9ex 1/1 Running 0 12s
```
2015-07-17 02:01:02 +00:00
2015-07-24 21:52:18 +00:00
then we can check the environment variables of the pod,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-13 17:16:45 +00:00
$ kubectl exec redis-master-ft9ex env
...
REDIS_MASTER_SERVICE_PORT=6379
REDIS_MASTER_SERVICE_HOST=10.0.0.219
...
```
2015-07-17 02:01:02 +00:00
2015-07-13 17:16:45 +00:00
We can use these environment variables in applications to find the service.
## Using kubectl exec to check the mounted volumes
2015-07-17 22:35:41 +00:00
2015-07-13 17:16:45 +00:00
It is convenient to use `kubectl exec` to check if the volumes are mounted as expected.
We first create a Pod with a volume mounted at /data/redis,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-16 00:20:39 +00:00
kubectl create -f docs/user-guide/walkthrough/pod-redis.yaml
2015-07-13 17:16:45 +00:00
```
2015-07-17 02:01:02 +00:00
2015-07-13 17:16:45 +00:00
wait until the pod is Running and Ready,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-13 17:16:45 +00:00
$ kubectl get pods
NAME READY REASON RESTARTS AGE
storage 1/1 Running 0 1m
```
2015-07-17 02:01:02 +00:00
2015-07-13 17:16:45 +00:00
we then use `kubectl exec` to verify that the volume is mounted at /data/redis,
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-13 17:16:45 +00:00
$ kubectl exec storage ls /data
redis
```
2015-07-10 23:21:05 +00:00
2015-07-11 00:58:51 +00:00
## Using kubectl exec to open a bash terminal in a pod
2015-07-17 22:35:41 +00:00
2015-07-11 00:58:51 +00:00
After all, open a terminal in a pod is the most direct way to introspect the pod. Assuming the pod/storage is still running, run
2015-07-17 02:01:02 +00:00
2015-07-18 23:13:42 +00:00
```console
2015-07-11 00:58:51 +00:00
$ kubectl exec -ti storage -- bash
root@storage:/data#
```
2015-07-17 02:01:02 +00:00
2015-07-11 00:58:51 +00:00
This gets you a terminal.
2015-07-10 23:21:05 +00:00
2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2015-07-10 23:21:05 +00:00
[![Analytics ](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/getting-into-containers.md?pixel )]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->