k3s/docs/user-guide/walkthrough/README.md

140 lines
4.8 KiB
Markdown
Raw Normal View History

2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
<!-- BEGIN STRIP_FOR_RELEASE -->
2015-07-15 00:28:47 +00:00
![WARNING](http://kubernetes.io/img/warning.png)
![WARNING](http://kubernetes.io/img/warning.png)
![WARNING](http://kubernetes.io/img/warning.png)
2015-07-13 22:15:35 +00:00
<h1>PLEASE NOTE: This document applies to the HEAD of the source
2015-07-14 00:13:09 +00:00
tree only. If you are using a released version of Kubernetes, you almost
certainly want the docs that go with that version.</h1>
<strong>Documentation for specific releases can be found at
[releases.k8s.io](http://releases.k8s.io).</strong>
2015-07-15 00:28:47 +00:00
![WARNING](http://kubernetes.io/img/warning.png)
![WARNING](http://kubernetes.io/img/warning.png)
![WARNING](http://kubernetes.io/img/warning.png)
2015-07-13 22:15:35 +00:00
2015-07-14 00:13:09 +00:00
<!-- END STRIP_FOR_RELEASE -->
<!-- END MUNGE: UNVERSIONED_WARNING -->
2014-10-09 19:38:24 +00:00
# Kubernetes 101 - Walkthrough
## Pods
2014-10-28 03:32:32 +00:00
The first atom of Kubernetes is a _pod_. A pod is a collection of containers that are symbiotically grouped.
2015-07-14 16:37:37 +00:00
See [pods](../../../docs/user-guide/pods.md) for more details.
### Intro
Trivially, a single container might be a pod. For example, you can express a simple web server as a pod:
```yaml
2015-06-10 20:25:54 +00:00
apiVersion: v1
kind: Pod
2015-05-08 00:55:36 +00:00
metadata:
name: www
spec:
containers:
- name: nginx
image: nginx
```
2014-10-28 06:25:55 +00:00
A pod definition is a declaration of a _desired state_. Desired state is a very important concept in the Kubernetes model. Many things present a desired state to the system, and it is Kubernetes' responsibility to make sure that the current state matches the desired state. For example, when you create a Pod, you declare that you want the containers in it to be running. If the containers happen to not be running (e.g. program failure, ...), Kubernetes will continue to (re-)create them for you in order to drive them to the desired state. This process continues until you delete the Pod.
2015-07-14 16:37:37 +00:00
See the [design document](../../../DESIGN.md) for more details.
### Volumes
2014-10-28 06:25:55 +00:00
Now that's great for a static web server, but what about persistent storage? We know that the container file system only lives as long as the container does, so we need more persistent storage. To do this, you also declare a ```volume``` as part of your pod, and mount it into a container:
```yaml
2015-06-10 20:25:54 +00:00
apiVersion: v1
kind: Pod
2015-05-08 00:55:36 +00:00
metadata:
name: storage
spec:
containers:
- name: redis
image: redis
volumeMounts:
# name must match the volume name below
- name: redis-persistent-storage
# mount path within the container
mountPath: /data/redis
volumes:
- name: redis-persistent-storage
emptyDir: {}
```
2015-05-08 00:55:36 +00:00
Ok, so what did we do? We added a volume to our pod:
```
volumes:
- name: redis-persistent-storage
emptyDir: {}
```
And we added a reference to that volume to our container:
2015-05-08 00:55:36 +00:00
```
volumeMounts:
# name must match the volume name below
- name: redis-persistent-storage
# mount path within the container
mountPath: /data/redis
```
In Kubernetes, ```emptyDir``` Volumes live for the lifespan of the Pod, which is longer than the lifespan of any one container, so if the container fails and is restarted, our persistent storage will live on.
If you want to mount a directory that already exists in the file system (e.g. ```/var/logs```) you can use the ```hostPath``` directive.
2015-07-14 16:37:37 +00:00
See [volumes](../../../docs/user-guide/volumes.md) for more details.
### Multiple Containers
_Note:
The examples below are syntactically correct, but some of the images (e.g. kubernetes/git-monitor) don't exist yet. We're working on turning these into working examples._
However, often you want to have two different containers that work together. An example of this would be a web server, and a helper job that polls a git repository for new updates:
```yaml
2015-06-10 20:25:54 +00:00
apiVersion: v1
kind: Pod
2015-05-08 00:55:36 +00:00
metadata:
name: www
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- mountPath: /srv/www
name: www-data
readOnly: true
- name: git-monitor
image: kubernetes/git-monitor
env:
- name: GIT_REPO
value: http://github.com/some/repo.git
volumeMounts:
- mountPath: /data
name: www-data
volumes:
- name: www-data
emptyDir: {}
```
Note that we have also added a volume here. In this case, the volume is mounted into both containers. It is marked ```readOnly``` in the web server's case, since it doesn't need to write to the directory.
Finally, we have also introduced an environment variable to the ```git-monitor``` container, which allows us to parameterize that container with the particular git repository that we want to track.
### What's next?
2015-07-14 00:13:09 +00:00
Continue on to [Kubernetes 201](k8s201.md) or
2015-07-14 16:37:37 +00:00
for a complete application see the [guestbook example](../../../examples/guestbook/README.md)
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/walkthrough/README.md?pixel)]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->