k3s/docs/user-guide/downward-api.md

112 lines
3.8 KiB
Markdown
Raw Normal View History

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">
<h2>PLEASE NOTE: This document applies to the HEAD of the source tree</h2>
If you are using a released version of Kubernetes, you should
refer to the docs that go with that version.
<strong>
The latest 1.0.x release of this document can be found
[here](http://releases.k8s.io/release-1.0/docs/user-guide/downward-api.md).
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-04-28 01:57:58 +00:00
# Downward API
2015-07-07 00:22:07 +00:00
It is sometimes useful for a container to have information about itself, but we
want to be careful not to over-couple containers to Kubernetes. The downward
API allows containers to consume information about themselves or the system and
expose that information how they want it, without necessarily coupling to the
2015-04-28 01:57:58 +00:00
kubernetes client or REST API.
2015-07-07 00:22:07 +00:00
An example of this is a "legacy" app that is already written assuming
that a particular environment variable will hold a unique identifier. While it
is often possible to "wrap" such applications, this is tedious and error prone,
and violates the goal of low coupling. Instead, the user should be able to use
the Pod's name, for example, and inject it into this well-known variable.
2015-04-28 01:57:58 +00:00
2015-07-07 00:22:07 +00:00
## Capabilities
2015-04-28 01:57:58 +00:00
2015-07-13 02:03:06 +00:00
The following information is available to a `Pod` through the downward API:
2015-04-28 01:57:58 +00:00
2015-07-07 00:22:07 +00:00
* The pod's name
* The pod's namespace
2015-04-28 01:57:58 +00:00
2015-07-07 00:22:07 +00:00
More information will be exposed through this same API over time.
2015-04-28 01:57:58 +00:00
2015-07-07 00:22:07 +00:00
## Exposing pod information into a container
2015-04-28 01:57:58 +00:00
2015-07-07 00:22:07 +00:00
Containers consume information from the downward API using environment
variables. In the future, containers will also be able to consume the downward
API via a volume plugin.
### Environment variables
Most environment variables in the Kubernetes API use the `value` field to carry
simple values. However, the alternate `valueFrom` field allows you to specify
a `fieldRef` to select fields from the pod's definition. The `fieldRef` field
is a structure that has an `apiVersion` field and a `fieldPath` field. The
`fieldPath` field is an expression designating a field of the pod. The
`apiVersion` field is the version of the API schema that the `fieldPath` is
written in terms of. If the `apiVersion` field is not specified it is
defaulted to the API version of the enclosing object.
The `fieldRef` is evaluated and the resulting value is used as the value for
the environment variable. This allows users to publish their pod's name in any
environment variable they want.
## Example
This is an example of a pod that consumes its name and namespace via the
downward API:
2015-04-28 01:57:58 +00:00
2015-05-10 02:19:44 +00:00
```yaml
2015-06-05 19:47:15 +00:00
apiVersion: v1
2015-05-10 02:19:44 +00:00
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh", "-c", "env" ]
env:
2015-07-07 00:22:07 +00:00
- name: MY_POD_NAME
2015-05-10 02:19:44 +00:00
valueFrom:
fieldRef:
fieldPath: metadata.name
2015-07-07 00:22:07 +00:00
- name: MY_POD_NAMESPACE
2015-05-10 02:19:44 +00:00
valueFrom:
fieldRef:
fieldPath: metadata.namespace
restartPolicy: Never
2015-04-28 01:57:58 +00:00
```
2015-07-07 00:22:07 +00:00
Some more thorough examples:
2015-07-14 16:37:37 +00:00
* [environment variables](environment-guide/)
* [downward API](downward-api/)
2015-07-07 00:22:07 +00:00
2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2015-07-14 16:37:37 +00:00
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/downward-api.md?pixel)]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->