@@ -9,24 +9,24 @@ content_type: task
9
9
<!-- overview -->
10
10
11
11
This page shows how to use ` kubectl exec ` to get a shell to a
12
- running Container .
12
+ running container .
13
13
14
14
15
15
16
16
17
17
## {{% heading "prerequisites" %}}
18
18
19
19
20
- {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
20
+ {{< include "task-tutorial-prereqs.md" >}}
21
21
22
22
23
23
24
24
25
25
<!-- steps -->
26
26
27
- ## Getting a shell to a Container
27
+ ## Getting a shell to a container
28
28
29
- In this exercise, you create a Pod that has one Container . The Container
29
+ In this exercise, you create a Pod that has one container . The container
30
30
runs the nginx image. Here is the configuration file for the Pod:
31
31
32
32
{{< codenew file="application/shell-demo.yaml" >}}
@@ -37,117 +37,121 @@ Create the Pod:
37
37
kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml
38
38
```
39
39
40
- Verify that the Container is running:
40
+ Verify that the container is running:
41
41
42
42
``` shell
43
43
kubectl get pod shell-demo
44
44
```
45
45
46
- Get a shell to the running Container :
46
+ Get a shell to the running container :
47
47
48
48
``` shell
49
- kubectl exec -it shell-demo -- /bin/bash
49
+ kubectl exec --stdin --tty shell-demo -- /bin/bash
50
50
```
51
- {{< note >}}
52
-
53
- The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments.
54
51
52
+ {{< note >}}
53
+ The double dash (` -- ` ) separates the arguments you want to pass to the command from the kubectl arguments.
55
54
{{< /note >}}
56
55
57
56
In your shell, list the root directory:
58
57
59
58
``` shell
60
- root@shell-demo:/# ls /
59
+ # Run this inside the container
60
+ ls /
61
61
```
62
62
63
63
In your shell, experiment with other commands. Here are
64
64
some examples:
65
65
66
66
``` shell
67
- root@shell-demo:/# ls /
68
- root@shell-demo:/# cat /proc/mounts
69
- root@shell-demo:/# cat /proc/1/maps
70
- root@shell-demo:/# apt-get update
71
- root@shell-demo:/# apt-get install -y tcpdump
72
- root@shell-demo:/# tcpdump
73
- root@shell-demo:/# apt-get install -y lsof
74
- root@shell-demo:/# lsof
75
- root@shell-demo:/# apt-get install -y procps
76
- root@shell-demo:/# ps aux
77
- root@shell-demo:/# ps aux | grep nginx
67
+ # You can run these example commands inside the container
68
+ ls /
69
+ cat /proc/mounts
70
+ cat /proc/1/maps
71
+ apt-get update
72
+ apt-get install -y tcpdump
73
+ tcpdump
74
+ apt-get install -y lsof
75
+ lsof
76
+ apt-get install -y procps
77
+ ps aux
78
+ ps aux | grep nginx
78
79
```
79
80
80
81
## Writing the root page for nginx
81
82
82
83
Look again at the configuration file for your Pod. The Pod
83
- has an ` emptyDir ` volume, and the Container mounts the volume
84
+ has an ` emptyDir ` volume, and the container mounts the volume
84
85
at ` /usr/share/nginx/html ` .
85
86
86
87
In your shell, create an ` index.html ` file in the ` /usr/share/nginx/html `
87
88
directory:
88
89
89
90
``` shell
90
- root@shell-demo:/# echo Hello shell demo > /usr/share/nginx/html/index.html
91
+ # Run this inside the container
92
+ echo ' Hello shell demo' > /usr/share/nginx/html/index.html
91
93
```
92
94
93
95
In your shell, send a GET request to the nginx server:
94
96
95
97
``` shell
96
- root@shell-demo:/# apt-get update
97
- root@shell-demo:/# apt-get install curl
98
- root@shell-demo:/# curl localhost
98
+ # Run this in the shell inside your container
99
+ apt-get update
100
+ apt-get install curl
101
+ curl http://localhost/
99
102
```
100
103
101
104
The output shows the text that you wrote to the ` index.html ` file:
102
105
103
- ``` shell
106
+ ```
104
107
Hello shell demo
105
108
```
106
109
107
110
When you are finished with your shell, enter ` exit ` .
108
111
109
- ## Running individual commands in a Container
112
+ ``` shell
113
+ exit # To quit the shell in the container
114
+ ```
115
+
116
+ ## Running individual commands in a container
110
117
111
118
In an ordinary command window, not your shell, list the environment
112
- variables in the running Container :
119
+ variables in the running container :
113
120
114
121
``` shell
115
122
kubectl exec shell-demo env
116
123
```
117
124
118
- Experiment running other commands. Here are some examples:
125
+ Experiment with running other commands. Here are some examples:
119
126
120
127
``` shell
121
- kubectl exec shell-demo ps aux
122
- kubectl exec shell-demo ls /
123
- kubectl exec shell-demo cat /proc/1/mounts
128
+ kubectl exec shell-demo -- ps aux
129
+ kubectl exec shell-demo -- ls /
130
+ kubectl exec shell-demo -- cat /proc/1/mounts
124
131
```
125
132
126
133
127
134
128
135
<!-- discussion -->
129
136
130
- ## Opening a shell when a Pod has more than one Container
137
+ ## Opening a shell when a Pod has more than one container
131
138
132
- If a Pod has more than one Container , use ` --container ` or ` -c ` to
133
- specify a Container in the ` kubectl exec ` command. For example,
139
+ If a Pod has more than one container , use ` --container ` or ` -c ` to
140
+ specify a container in the ` kubectl exec ` command. For example,
134
141
suppose you have a Pod named my-pod, and the Pod has two containers
135
- named main-app and helper-app . The following command would open a
136
- shell to the main-app Container .
142
+ named _ main-app _ and _ helper-app _ . The following command would open a
143
+ shell to the _ main-app _ container .
137
144
138
145
``` shell
139
- kubectl exec -it my-pod --container main-app -- /bin/bash
146
+ kubectl exec -i -t my-pod --container main-app -- /bin/bash
140
147
```
141
148
142
-
149
+ {{< note >}}
150
+ The short options ` -i ` and ` -t ` are the same as the long options ` --stdin ` and ` --tty `
151
+ {{< /note >}}
143
152
144
153
145
154
## {{% heading "whatsnext" %}}
146
155
147
156
148
- * [ kubectl exec] ( /docs/reference/generated/kubectl/kubectl-commands/#exec )
149
-
150
-
151
-
152
-
153
-
157
+ * Read about [ kubectl exec] ( /docs/reference/generated/kubectl/kubectl-commands/#exec )
0 commit comments