Skip to content

Commit 3b6bb3f

Browse files
author
Phillip Wittrock
authored
Merge pull request #1526 from steveperry-53/task-cmd-arg
Write new task: Defining a Command and Arguments for a Container.
2 parents 150455f + d164720 commit 3b6bb3f

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

_data/tasks.yml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ toc:
66
section:
77
- title: Defining Environment Variables for a Container
88
path: /docs/tasks/configure-pod-container/define-environment-variable-container/
9+
- title: Defining a Command and Arguments for a Container
10+
path: /docs/tasks/configure-pod-container/define-command-argument-container/
911
- title: Accessing Applications in a Cluster
1012
section:
1113
- title: Using Port Forwarding to Access Applications in a Cluster
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: command-demo
5+
labels:
6+
purpose: demonstrate-command
7+
spec:
8+
containers:
9+
- name: command-demo-container
10+
image: debian
11+
command: ["printenv"]
12+
args: ["HOSTNAME", "KUBERNETES_PORT"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
---
3+
4+
{% capture overview %}
5+
6+
This page shows how to define commands and arguments when you run a container
7+
in a Kubernetes Pod.
8+
9+
{% endcapture %}
10+
11+
12+
{% capture prerequisites %}
13+
14+
{% include task-tutorial-prereqs.md %}
15+
16+
{% endcapture %}
17+
18+
19+
{% capture steps %}
20+
21+
### Defining a command and arguments when you create a Pod
22+
23+
When you create a Pod, you can define a command and arguments for the
24+
containers that run in the Pod. To define a command, include the `command`
25+
field in the configuration file. To define arguments for the command, include
26+
the `args` field in the configuration file. The command and arguments that
27+
you define cannot be changed after the Pod is created.
28+
29+
The command and arguments that you define in the configuration file
30+
override the default command and arguments provided by the container image.
31+
If you define args, but do not define a command, the default command is used
32+
with your new arguments. For more information, see
33+
[Commands and Capabilities](/docs/user-guide/containers/).
34+
35+
In this exercise, you create a Pod that runs one container. The configuration
36+
file for the Pod defines a command and two arguments:
37+
38+
{% include code.html language="yaml" file="commands.yaml" ghlink="/docs/tasks/configure-pod-container/commands.yaml" %}
39+
40+
1. Create a Pod based on the YAML configuration file:
41+
42+
export REPO=https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master
43+
kubectl create -f $REPO/docs/tasks/configure-pod-container/commands.yaml
44+
45+
1. List the running Pods:
46+
47+
kubectl get pods
48+
49+
The output shows that the container that ran in the command-demo Pod has
50+
completed.
51+
52+
1. To see the output of the command that ran in the container, view the logs
53+
from the Pod:
54+
55+
kubectl logs command-demo
56+
57+
The output shows the values of the HOSTNAME and KUBERNETES_PORT environment
58+
variables:
59+
60+
command-demo
61+
tcp://10.3.240.1:443
62+
63+
### Using environment variables to define arguments
64+
65+
In the preceding example, you defined the arguments directly by
66+
providing strings. As an alternative to providing strings directly,
67+
you can define arguments by using environment variables:
68+
69+
env:
70+
- name: MESSAGE
71+
value: "hello world"
72+
command: ["/bin/echo"]
73+
args: ["$(MESSAGE)"]
74+
75+
This means you can define an argument for a Pod using any of
76+
the techniques available for defining environment variables, including
77+
[ConfigMaps](/docs/user-guide/configmap/)
78+
and
79+
[Secrets](/docs/user-guide/secrets/).
80+
81+
NOTE: The environment variable appears in parentheses, `"$(VAR)"`. This is
82+
required for the variable to be expanded in the `command` or `args` field.
83+
84+
### Running a command in a shell
85+
86+
In some cases, you need your command to run in a shell. For example, your
87+
command might consist of several commands piped together, or it might be a shell
88+
script. To run your command in a shell, wrap it like this:
89+
90+
command: ["/bin/sh"]
91+
args: ["-c", "while true; do echo hello; sleep 10;done"]
92+
93+
{% endcapture %}
94+
95+
{% capture whatsnext %}
96+
97+
* Learn more about [containers and commands](/docs/user-guide/containers/).
98+
* Learn more about [configuring containers](/docs/user-guide/configuring-containers/).
99+
* Learn more about [running commands in a container](/docs/user-guide/getting-into-containers/).
100+
* See [Container](/docs/api-reference/v1/definitions/#_v1_container).
101+
102+
{% endcapture %}
103+
104+
105+
{% include templates/task.md %}

docs/tasks/index.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
The Tasks section of the Kubernetes documentation is a work in progress
55

6+
#### Configuring Pods and Containers
7+
8+
* [Defining Environment Variables for a Container](/docs/tasks/configure-pod-container/define-environment-variable-container/)
9+
* [Defining a Command and Arguments for a Container](/docs/tasks/configure-pod-container/define-command-argument-container/)
10+
611
#### Accessing Applications in a Cluster
712

813
* [Using Port Forwarding to Access Applications in a Cluster](/docs/tasks/access-application-cluster/port-forward-access-application-cluster/)

0 commit comments

Comments
 (0)