forked from kubernetes/examples
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
372 changed files
with
23,461 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
reviewers: | ||
- brendandburns | ||
- thockin | ||
- zmerlynn | ||
approvers: | ||
- brendandburns | ||
- eparis | ||
- thockin | ||
- zmerlynn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
# examples | ||
Kubernetes application example tutorials | ||
# Kubernetes Examples: releases.k8s.io/HEAD | ||
|
||
Home for examples to be moved from kubernetes/kubernetes/examples | ||
This directory contains a number of examples of how to run | ||
real applications with Kubernetes. | ||
|
||
Demonstrations of how to use specific Kubernetes features can be found in our [documents](../docs/). | ||
|
||
|
||
### Maintained Examples | ||
|
||
Maintained Examples are expected to be updated with every Kubernetes | ||
release, to use the latest and greatest features, current guidelines | ||
and best practices, and to refresh command syntax, output, changed | ||
prerequisites, as needed. | ||
|
||
|Name | Description | Notable Features Used | Complexity Level| | ||
------------- | ------------- | ------------ | ------------ | | ||
|[Guestbook](guestbook/) | PHP app with Redis | Replication Controller, Service | Beginner | | ||
|[WordPress](mysql-wordpress-pd/) | WordPress with MySQL | Deployment, Persistent Volume with Claim | Beginner| | ||
|[Cassandra](storage/cassandra/) | Cloud Native Cassandra | Daemon Set | Intermediate | ||
|
||
* Note: Please add examples to the list above that are maintained. | ||
|
||
See [Example Guidelines](guidelines.md) for a description of what goes | ||
in this directory, and what examples should contain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
licenses(["notice"]) | ||
|
||
load( | ||
"@io_bazel_rules_go//go:def.bzl", | ||
"go_library", | ||
"go_test", | ||
) | ||
|
||
filegroup( | ||
name = "config", | ||
srcs = glob([ | ||
"**/*.yaml", | ||
"**/*.yml", | ||
"**/*.json", | ||
]) + [ | ||
"pod", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "sources", | ||
srcs = glob([ | ||
"**/*", | ||
]), | ||
) | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["doc.go"], | ||
tags = ["automanaged"], | ||
) | ||
|
||
go_test( | ||
name = "go_default_xtest", | ||
srcs = ["examples_test.go"], | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//pkg/api:go_default_library", | ||
"//pkg/api/testapi:go_default_library", | ||
"//pkg/api/validation:go_default_library", | ||
"//pkg/apis/apps:go_default_library", | ||
"//pkg/apis/apps/validation:go_default_library", | ||
"//pkg/apis/batch:go_default_library", | ||
"//pkg/apis/extensions:go_default_library", | ||
"//pkg/apis/extensions/validation:go_default_library", | ||
"//pkg/capabilities:go_default_library", | ||
"//pkg/registry/batch/job:go_default_library", | ||
"//plugin/pkg/scheduler/api:go_default_library", | ||
"//plugin/pkg/scheduler/api/latest:go_default_library", | ||
"//vendor/github.com/golang/glog:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/util/validation/field:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "package-srcs", | ||
srcs = glob(["**"]), | ||
tags = ["automanaged"], | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
filegroup( | ||
name = "all-srcs", | ||
srcs = [ | ||
":package-srcs", | ||
"//examples/explorer:all-srcs", | ||
"//examples/guestbook-go:all-srcs", | ||
"//examples/https-nginx:all-srcs", | ||
"//examples/sharing-clusters:all-srcs", | ||
], | ||
tags = ["automanaged"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
## Kubernetes DNS example | ||
|
||
This is a toy example demonstrating how to use kubernetes DNS. | ||
|
||
### Step Zero: Prerequisites | ||
|
||
This example assumes that you have forked the repository and [turned up a Kubernetes cluster](../../docs/getting-started-guides/). Make sure DNS is enabled in your setup, see [DNS doc](https://github.com/kubernetes/dns). | ||
|
||
```sh | ||
$ cd kubernetes | ||
$ hack/dev-build-and-up.sh | ||
``` | ||
|
||
### Step One: Create two namespaces | ||
|
||
We'll see how cluster DNS works across multiple [namespaces](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/), first we need to create two namespaces: | ||
|
||
```sh | ||
$ kubectl create -f examples/cluster-dns/namespace-dev.yaml | ||
$ kubectl create -f examples/cluster-dns/namespace-prod.yaml | ||
``` | ||
|
||
Now list all namespaces: | ||
|
||
```sh | ||
$ kubectl get namespaces | ||
NAME LABELS STATUS | ||
default <none> Active | ||
development name=development Active | ||
production name=production Active | ||
``` | ||
|
||
For kubectl client to work with each namespace, we define two contexts: | ||
|
||
```sh | ||
$ kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} | ||
$ kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME} | ||
``` | ||
|
||
You can view your cluster name and user name in kubernetes config at ~/.kube/config. | ||
|
||
### Step Two: Create backend replication controller in each namespace | ||
|
||
Use the file [`examples/cluster-dns/dns-backend-rc.yaml`](dns-backend-rc.yaml) to create a backend server [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) in each namespace. | ||
|
||
```sh | ||
$ kubectl config use-context dev | ||
$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml | ||
``` | ||
|
||
Once that's up you can list the pod in the cluster: | ||
|
||
```sh | ||
$ kubectl get rc | ||
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS | ||
dns-backend dns-backend ddysher/dns-backend name=dns-backend 1 | ||
``` | ||
|
||
Now repeat the above commands to create a replication controller in prod namespace: | ||
|
||
```sh | ||
$ kubectl config use-context prod | ||
$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml | ||
$ kubectl get rc | ||
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS | ||
dns-backend dns-backend ddysher/dns-backend name=dns-backend 1 | ||
``` | ||
|
||
### Step Three: Create backend service | ||
|
||
Use the file [`examples/cluster-dns/dns-backend-service.yaml`](dns-backend-service.yaml) to create | ||
a [service](https://kubernetes.io/docs/concepts/services-networking/service/) for the backend server. | ||
|
||
```sh | ||
$ kubectl config use-context dev | ||
$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml | ||
``` | ||
|
||
Once that's up you can list the service in the cluster: | ||
|
||
```sh | ||
$ kubectl get service dns-backend | ||
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE | ||
dns-backend 10.0.2.3 <none> 8000/TCP name=dns-backend 1d | ||
``` | ||
|
||
Again, repeat the same process for prod namespace: | ||
|
||
```sh | ||
$ kubectl config use-context prod | ||
$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml | ||
$ kubectl get service dns-backend | ||
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE | ||
dns-backend 10.0.2.4 <none> 8000/TCP name=dns-backend 1d | ||
``` | ||
|
||
### Step Four: Create client pod in one namespace | ||
|
||
Use the file [`examples/cluster-dns/dns-frontend-pod.yaml`](dns-frontend-pod.yaml) to create a client [pod](https://kubernetes.io/docs/concepts/workloads/pods/pod/) in dev namespace. The client pod will make a connection to backend and exit. Specifically, it tries to connect to address `http://dns-backend.development.cluster.local:8000`. | ||
|
||
```sh | ||
$ kubectl config use-context dev | ||
$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml | ||
``` | ||
|
||
Once that's up you can list the pod in the cluster: | ||
|
||
```sh | ||
$ kubectl get pods dns-frontend | ||
NAME READY STATUS RESTARTS AGE | ||
dns-frontend 0/1 ExitCode:0 0 1m | ||
``` | ||
|
||
Wait until the pod succeeds, then we can see the output from the client pod: | ||
|
||
```sh | ||
$ kubectl logs dns-frontend | ||
2015-05-07T20:13:54.147664936Z 10.0.236.129 | ||
2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000 | ||
2015-05-07T20:13:54.147733438Z <Response [200]> | ||
2015-05-07T20:13:54.147738295Z Hello World! | ||
``` | ||
|
||
Please refer to the [source code](images/frontend/client.py) about the log. First line prints out the ip address associated with the service in dev namespace; remaining lines print out our request and server response. | ||
|
||
If we switch to prod namespace with the same pod config, we'll see the same result, i.e. dns will resolve across namespace. | ||
|
||
```sh | ||
$ kubectl config use-context prod | ||
$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml | ||
$ kubectl logs dns-frontend | ||
2015-05-07T20:13:54.147664936Z 10.0.236.129 | ||
2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000 | ||
2015-05-07T20:13:54.147733438Z <Response [200]> | ||
2015-05-07T20:13:54.147738295Z Hello World! | ||
``` | ||
|
||
|
||
#### Note about default namespace | ||
|
||
If you prefer not using namespace, then all your services can be addressed using `default` namespace, e.g. `http://dns-backend.default.svc.cluster.local:8000`, or shorthand version `http://dns-backend:8000` | ||
|
||
|
||
### tl; dr; | ||
|
||
For those of you who are impatient, here is the summary of the commands we ran in this tutorial. Remember to set first `$CLUSTER_NAME` and `$USER_NAME` to the values found in `~/.kube/config`. | ||
|
||
```sh | ||
# create dev and prod namespaces | ||
kubectl create -f examples/cluster-dns/namespace-dev.yaml | ||
kubectl create -f examples/cluster-dns/namespace-prod.yaml | ||
|
||
# create two contexts | ||
kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} | ||
kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME} | ||
|
||
# create two backend replication controllers | ||
kubectl config use-context dev | ||
kubectl create -f examples/cluster-dns/dns-backend-rc.yaml | ||
kubectl config use-context prod | ||
kubectl create -f examples/cluster-dns/dns-backend-rc.yaml | ||
|
||
# create backend services | ||
kubectl config use-context dev | ||
kubectl create -f examples/cluster-dns/dns-backend-service.yaml | ||
kubectl config use-context prod | ||
kubectl create -f examples/cluster-dns/dns-backend-service.yaml | ||
|
||
# create a pod in each namespace and get its output | ||
kubectl config use-context dev | ||
kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml | ||
kubectl logs dns-frontend | ||
|
||
kubectl config use-context prod | ||
kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml | ||
kubectl logs dns-frontend | ||
``` | ||
|
||
|
||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS --> | ||
[]() | ||
<!-- END MUNGE: GENERATED_ANALYTICS --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
apiVersion: v1 | ||
kind: ReplicationController | ||
metadata: | ||
name: dns-backend | ||
labels: | ||
name: dns-backend | ||
spec: | ||
replicas: 1 | ||
selector: | ||
name: dns-backend | ||
template: | ||
metadata: | ||
labels: | ||
name: dns-backend | ||
spec: | ||
containers: | ||
- name: dns-backend | ||
image: gcr.io/google_containers/example-dns-backend:v1 | ||
ports: | ||
- name: backend-port | ||
containerPort: 8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
kind: Service | ||
apiVersion: v1 | ||
metadata: | ||
name: dns-backend | ||
spec: | ||
ports: | ||
- port: 8000 | ||
selector: | ||
name: dns-backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: dns-frontend | ||
labels: | ||
name: dns-frontend | ||
spec: | ||
containers: | ||
- name: dns-frontend | ||
image: gcr.io/google_containers/example-dns-frontend:v1 | ||
command: | ||
- python | ||
- client.py | ||
- http://dns-backend.development.svc.cluster.local:8000 | ||
imagePullPolicy: Always | ||
restartPolicy: Never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
FROM python:2.7-slim | ||
|
||
COPY . /dns-backend | ||
WORKDIR /dns-backend | ||
|
||
CMD ["python", "server.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
TAG = v1 | ||
PREFIX = gcr.io/google_containers | ||
IMAGE = example-dns-backend | ||
|
||
all: push | ||
|
||
image: | ||
docker build --pull -t $(PREFIX)/$(IMAGE):$(TAG) . | ||
|
||
push: image | ||
gcloud docker -- push $(PREFIX)/$(IMAGE) | ||
|
||
clean: |
Oops, something went wrong.