Skip to content

Commit dd735dd

Browse files
committed
First draft of deployment-oriented sections of the user guide.
1 parent 3f2ac78 commit dd735dd

6 files changed

+805
-0
lines changed
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Kubernetes User Guide: Managing Applications: Configuring and launching containers
2+
3+
## Configuration in Kubernetes
4+
5+
In addition to the imperative-style commands, such as `kubectl run` and `kubectl expose`, described [elsewhere](quick-start.md), Kubernetes supports declarative configuration. Often times, configuration files are preferable to imperative commands, since they can be checked into version control and changes to the files can be code reviewed, which is especially important for more complex configurations, producing a more robust, reliable and archival system.
6+
7+
In the declarative style, all configuration is stored in YAML or JSON configuration files, using Kubernetes's API resource schemas as the configuration schemas. `kubectl` can create, update, delete, and get API resources. The `apiVersion` (currently “v1”), resource `kind`, and resource `name` are used by `kubectl` to construct the appropriate API path to invoke for the specified operation.
8+
9+
## Launching a container using a configuration file
10+
11+
Kubernetes executes containers in [*Pods*](../../docs/pods.md). A pod containing a simple Hello World container can be specified in YAML as follows:
12+
13+
```yaml
14+
apiVersion: v1
15+
kind: Pod
16+
metadata:
17+
name: hello-world
18+
spec: # specification of the pod’s contents
19+
restartPolicy: Never
20+
containers:
21+
- name: hello
22+
image: "ubuntu:14.04"
23+
command: ["/bin/echo","hello”,”world"]
24+
```
25+
The value of `metadata.name`, `hello-world`, will be the name of the pod resource created, and must be unique within the cluster, whereas `containers[0].name` is just a nickname for the container within that pod. `image` is the name of the Docker image, which Kubernetes expects to be able to pull from a registry, the [Docker Hub](https://registry.hub.docker.com/) by default.
26+
27+
`restartPolicy: Never` indicates that we just want to run the container once and then terminate the pod.
28+
29+
The [`command`](../../docs/containers.md#containers-and-commands) overrides the Docker container’s `Entrypoint`. Command arguments (corresponding to Docker’s `Cmd`) may be specified using `args`, as follows:
30+
31+
```yaml
32+
command: ["/bin/echo"]
33+
args: ["hello","world"]
34+
```
35+
36+
This pod can be created using the `create` command:
37+
```bash
38+
$ kubectl create -f hello-world.yaml
39+
pods/hello-world
40+
```
41+
`kubectl` prints the resource type and name of the resource created when successful.
42+
43+
## Validating configuration
44+
45+
If you’re not sure you specified the resource correctly, you can ask `kubectl` to validate it for you:
46+
```bash
47+
$ kubectl create -f hello-world.yaml --validate
48+
```
49+
50+
Let’s say you specified `entrypoint` instead of `command`. You’d see output as follows:
51+
```
52+
I0709 06:33:05.600829 14160 schema.go:126] unknown field: entrypoint
53+
I0709 06:33:05.600988 14160 schema.go:129] this may be a false alarm, see https://github.com/GoogleCloudPlatform/kubernetes/issues/6842
54+
pods/hello-world
55+
```
56+
`kubectl create --validate` currently warns about problems it detects, but creates the resource anyway, unless a required field is absent or a field value is invalid. Unknown API fields are ignored, so be careful. This pod was created, but with no `command`, which is an optional field, since the image may specify an `Entrypoint`.
57+
58+
## Environment variables and variable expansion
59+
60+
Kubernetes [does not automatically run commands in a shell](https://github.com/GoogleCloudPlatform/kubernetes/wiki/User-FAQ#use-of-environment-variables-on-the-command-line) (not all images contain shells). If you would like to run your command in a shell, such as to expand environment variables (specified using `env`), you could do the following:
61+
```yaml
62+
apiVersion: v1
63+
kind: Pod
64+
metadata:
65+
name: hello-world
66+
spec: # specification of the pod’s contents
67+
restartPolicy: Never
68+
containers:
69+
- name: hello
70+
image: "ubuntu:14.04"
71+
env:
72+
- name: MESSAGE
73+
value: "hello world"
74+
command: ["/bin/sh","-c"]
75+
args: ["/bin/echo \"${MESSAGE}\""]
76+
```
77+
78+
However, a shell isn’t necessary just to expand environment variables. Kubernetes will do it for you if you use [`$(ENVVAR)` syntax](../../docs/design/expansion.md):
79+
```yaml
80+
command: ["/bin/echo"]
81+
args: ["$(MESSAGE)"]
82+
```
83+
## Viewing pod status
84+
85+
You can see the pod you created (actually all of your cluster's pods) using the `get` command.
86+
87+
If you’re quick, it will look as follows:
88+
```bash
89+
$ kubectl get pods
90+
NAME READY STATUS RESTARTS AGE
91+
hello-world 0/1 Pending 0 0s
92+
```
93+
Initially, a newly created pod is unscheduled -- no node has been selected to run it. Scheduling happens after creation, but is fast, so you normally shouldn’t see pods in an unscheduled state unless there’s a problem.
94+
95+
After the pod has been scheduled, the image may need to be pulled to the node on which it was scheduled, if it hadn’t be pulled already. After a few seconds, you should see the container running:
96+
```bash
97+
$ kubectl get pods
98+
NAME READY STATUS RESTARTS AGE
99+
hello-world 1/1 Running 0 5s
100+
```
101+
The `READY` column shows how many containers in the pod are running.
102+
103+
Almost immediately after it starts running, this command will terminate. `kubectl` shows that the container is no longer running and displays the exit status:
104+
```bash
105+
$ kubectl get pods
106+
NAME READY STATUS RESTARTS AGE
107+
hello-world 0/1 ExitCode:0 0 15s
108+
```
109+
## Viewing pod output
110+
111+
You probably want to see the output of the command you ran. As with [`docker logs`](https://docs.docker.com/userguide/usingdocker/), `kubectl logs` will show you the output:
112+
```bash
113+
$ kubectl logs hello-world
114+
hello world
115+
```
116+
## Deleting pods
117+
When you’re done looking at the output, you should delete the pod:
118+
```bash
119+
$ kubectl delete pod hello-world
120+
pods/hello-world
121+
```
122+
As with `create`, `kubectl` prints the resource type and name of the resource deleted when successful.
123+
124+
You can also use the resource/name format to specify the pod:
125+
```bash
126+
$ kubectl delete pods/hello-world
127+
pods/hello-world
128+
```
129+
130+
Terminated pods aren’t currently automatically deleted, so that you can observe their final status, so be sure to clean up your dead pods.
131+
132+
On the other hand, containers and their logs are eventually deleted automatically in order to free up disk space on the nodes.
133+
134+
135+
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/configuring-containers.md?pixel)]()
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Kubernetes User Guide: Managing Applications: Deploying continuously running applications
2+
3+
You previously read about how to quickly deploy a simple replicated application using [`kubectl run`](quick-start.md) and how to configure and launch single-run containers using pods (configuring-containers.md). Here, you’ll use the configuration-based approach to deploy a continuously running, replicated application.
4+
5+
## Launching a set of replicas using a configuration file
6+
7+
Kubernetes creates and manages sets of replicated containers (actually, replicated [Pods](../../docs/pods.md)) using [*Replication Controllers*](../../docs/replication-controller.md).
8+
9+
A replication controller simply ensures that a specified number of pod "replicas" are running at any one time. If there are too many, it will kill some. If there are too few, it will start more. It’s analogous to Google Compute Engine’s [Instance Group Manager](https://cloud.google.com/compute/docs/instance-groups/manager/) or AWS’s [Auto-scaling Group](http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/AutoScalingGroup.html) (with no scaling policies).
10+
11+
The replication controller created to run nginx by `kubctl run` in the [Quick start](quick-start.md) could be specified using YAML as follows:
12+
```yaml
13+
apiVersion: v1
14+
kind: ReplicationController
15+
metadata:
16+
name: my-nginx
17+
spec:
18+
replicas: 2
19+
template:
20+
metadata:
21+
labels:
22+
app: nginx
23+
spec:
24+
containers:
25+
- name: nginx
26+
image: nginx
27+
ports:
28+
- containerPort: 80
29+
```
30+
Some differences compared to specifying just a pod are that the `kind` is `ReplicationController`, the number of `replicas` desired is specified, and the pod specification is under the `template` field. The names of the pods don’t need to be specified explicitly because they are generated from the name of the replication controller.
31+
32+
This replication controller can be created using `create`, just as with pods:
33+
```bash
34+
$ kubectl create -f nginx-rc.yaml
35+
replicationcontrollers/my-nginx
36+
```
37+
38+
Unlike in the case where you directly create pods, a replication controller replaces pods that are deleted or terminated for any reason, such as in the case of node failure. For this reason, we recommend that you use a replication controller for a continuously running application even if your application requires only a single pod, in which case you can omit `replicas` and it will default to a single replica.
39+
40+
## Viewing replication controller status
41+
42+
You can view the replication controller you created using `get`:
43+
```bash
44+
$ kubectl get rc
45+
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
46+
my-nginx nginx nginx app=nginx 2
47+
```
48+
This tells you that your controller will ensure that you have two nginx replicas.
49+
50+
You can see those replicas using `get`, just as with pods you created directly:
51+
```bash
52+
$ kubectl get pods
53+
NAME READY STATUS RESTARTS AGE
54+
my-nginx-065jq 1/1 Running 0 51s
55+
my-nginx-buaiq 1/1 Running 0 51s
56+
```
57+
## Deleting replication controllers
58+
59+
When you want to kill your application, delete your replication controller, as in the [Quick start](quick-start.md):
60+
```bash
61+
$ kubectl delete rc my-nginx
62+
replicationcontrollers/my-nginx
63+
```
64+
65+
By default, this will also cause the pods managed by the replication controller to be deleted. If there were a large number of pods, this may take a while to complete. If you want to leave the pods running, specify `--cascade=false`.
66+
67+
If you try to delete the pods before deleting the replication controller, it will just replace them, as it is supposed to do.
68+
69+
## Labels
70+
71+
Kubernetes uses user-defined key-value attributes called [*labels*](../../docs/labels.md) to categorize and identify sets of resources, such as pods and replication controllers. The example above specified a single label in the pod template, with key `app` and value `nginx`. All pods created carry that label, which can be viewed using `-L`:
72+
```bash
73+
$ kubectl get pods -L app
74+
NAME READY STATUS RESTARTS AGE APP
75+
my-nginx-afv12 0/1 Running 0 3s nginx
76+
my-nginx-lg99z 0/1 Running 0 3s nginx
77+
```
78+
79+
The labels from the pod template are copied to the replication controller’s labels by default, as well -- all resources in Kubernetes support labels:
80+
```bash
81+
$ kubectl get rc my-nginx -L app
82+
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS APP
83+
my-nginx nginx nginx app=nginx 2 nginx
84+
```
85+
86+
More importantly, the pod template’s labels are used to create a [`selector`](../../docs/labels.md#label-selectors) that will match pods carrying those labels. You can see this field by requesting it using the [Go template output format of `kubectl get`](../../docs/kubectl_get.md):
87+
```bash
88+
$ kubectl get rc my-nginx -o template --template="{{.spec.selector}}"
89+
map[app:nginx]
90+
```
91+
92+
You could also specify the `selector` explicitly, such as if you wanted to specify labels in the pod template that you didn’t want to select on, but you should ensure that the selector will match the labels of the pods created from the pod template, and that it won’t match pods created by other replication controllers. The most straightforward way to ensure the latter is to create a unique label value for the replication controller, and to specify it in both the pod template’s labels and in the selector.
93+
94+
95+
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/deploying-applications.md?pixel)]()

0 commit comments

Comments
 (0)