2014-06-06 23:40:48 +00:00
## GuestBook example
This example shows how to build a simple multi-tier web application using Kubernetes and Docker.
The example combines a web frontend, a redis master for storage and a replicated set of redis slaves.
### Step Zero: Prerequisites
2014-09-11 20:10:49 +00:00
This example assumes that you have forked the repository and [turned up a Kubernetes cluster ](https://github.com/GoogleCloudPlatform/kubernetes#contents ):
2014-06-06 23:40:48 +00:00
2014-09-10 20:56:56 +00:00
```shell
$ cd kubernetes
$ hack/dev-build-and-up.sh
```
2014-11-20 20:00:58 +00:00
### Step One: Turn up the redis master
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
Use the file `examples/guestbook/redis-master.json` which describes a single pod running a redis key-value server in a container:
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
```js
2014-06-06 23:40:48 +00:00
{
2014-11-20 20:00:58 +00:00
"id": "redis-master",
2014-07-30 23:52:58 +00:00
"kind": "Pod",
"apiVersion": "v1beta1",
2014-06-06 23:40:48 +00:00
"desiredState": {
"manifest": {
2014-06-30 23:16:06 +00:00
"version": "v1beta1",
2014-11-20 20:00:58 +00:00
"id": "redis-master",
2014-06-06 23:40:48 +00:00
"containers": [{
"name": "master",
"image": "dockerfile/redis",
2014-11-20 20:00:58 +00:00
"cpu": 100,
2014-06-06 23:40:48 +00:00
"ports": [{
"containerPort": 6379,
2014-11-20 20:00:58 +00:00
"hostPort": 6379
2014-06-06 23:40:48 +00:00
}]
}]
}
},
"labels": {
"name": "redis-master"
}
}
```
2014-11-20 20:00:58 +00:00
Create the redis pod in your Kubernetes cluster by running:
2014-06-06 23:40:48 +00:00
2014-06-09 21:37:43 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh -c examples/guestbook/redis-master.json create pods
2014-06-06 23:40:48 +00:00
```
2014-06-09 05:38:45 +00:00
Once that's up you can list the pods in the cluster, to verify that the master is running:
2014-06-06 23:40:48 +00:00
```shell
2014-07-23 06:56:43 +00:00
cluster/kubecfg.sh list pods
2014-06-06 23:40:48 +00:00
```
2014-11-20 20:00:58 +00:00
You'll see a single redis master pod. It will also display the machine that the pod is running on once it gets placed (may take up to thirty seconds):
2014-06-09 21:05:14 +00:00
2014-06-15 23:12:13 +00:00
```
2014-11-20 20:00:58 +00:00
ID Image(s) Host Labels Status
2014-09-03 02:22:01 +00:00
---------- ---------- ---------- ---------- ----------
2014-11-20 20:00:58 +00:00
redis-master dockerfile/redis kubernetes-minion-3.c.briandpe-api.internal name=redis-master Running
2014-06-09 21:05:14 +00:00
```
If you ssh to that machine, you can run `docker ps` to see the actual pod:
2014-06-09 21:37:43 +00:00
```shell
2014-06-13 22:03:17 +00:00
$ gcutil ssh --zone us-central1-b kubernetes-minion-3
2014-06-09 21:05:14 +00:00
$ sudo docker ps
me@kubernetes-minion-3:~$ sudo docker ps
2014-11-20 20:00:58 +00:00
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3eed3e5e6d1 dockerfile/redis:latest redis-server /etc/re 8 minutes ago Up 8 minutes k8s_master.9c0a9146_redis-master.etcd_6296f4bd-70fa-11e4-8469-0800279696e1_45331ebc
2014-06-06 23:40:48 +00:00
```
2014-06-09 21:05:14 +00:00
(Note that initial `docker pull` may take a few minutes, depending on network conditions.)
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
### Step Two: Turn up the master service
2014-06-09 21:05:14 +00:00
A Kubernetes 'service' is a named load balancer that proxies traffic to one or more containers. The services in a Kubernetes cluster are discoverable inside other containers via environment variables. Services find the containers to load balance based on pod labels.
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
The pod that you created in Step One has the label `name=redis-master` . The selector field of the service determines which pods will receive the traffic sent to the service. Use the file `examples/guestbook/redis-master-service.json` :
2014-06-09 21:05:14 +00:00
2014-06-09 21:37:43 +00:00
```js
2014-06-06 23:40:48 +00:00
{
2014-11-20 20:00:58 +00:00
"id": "redis-master",
2014-07-30 23:52:58 +00:00
"kind": "Service",
"apiVersion": "v1beta1",
2014-10-21 04:16:21 +00:00
"port": 6379,
2014-08-25 23:37:55 +00:00
"containerPort": 6379,
2014-06-19 00:03:38 +00:00
"selector": {
2014-06-06 23:40:48 +00:00
"name": "redis-master"
2014-11-20 20:00:58 +00:00
},
"labels": {
"name": "redis-master"
2014-06-06 23:40:48 +00:00
}
}
```
2014-11-20 20:00:58 +00:00
to create the service by running:
2014-06-06 23:40:48 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh -c examples/guestbook/redis-master-service.json create services
2014-11-20 20:00:58 +00:00
ID Labels Selector Port
2014-07-30 23:52:58 +00:00
---------- ---------- ---------- ----------
2014-11-20 20:00:58 +00:00
redis-master name=redis-master name=redis-master 6379
2014-06-06 23:40:48 +00:00
```
2014-10-21 04:16:21 +00:00
This will cause all pods to see the redis master apparently running on < ip > :6379.
2014-07-30 23:52:58 +00:00
2014-10-21 04:16:21 +00:00
Once created, the service proxy on each minion is configured to set up a proxy on the specified port (in this case port 6379).
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
### Step Three: Turn up the replicated slave pods
2014-06-09 21:05:14 +00:00
Although the redis master is a single pod, the redis read slaves are a 'replicated' pod. In Kubernetes, a replication controller is responsible for managing multiple instances of a replicated pod.
2014-11-20 20:00:58 +00:00
Use the file `examples/guestbook/redis-slave-controller.json` :
2014-06-06 23:40:48 +00:00
2014-06-09 21:37:43 +00:00
```js
2014-09-03 02:22:01 +00:00
{
2014-07-30 23:52:58 +00:00
"id": "redisSlaveController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "redisslave"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "redisSlaveController",
"containers": [{
"name": "slave",
"image": "brendanburns/redis-slave",
2014-11-20 20:00:58 +00:00
"cpu": 200,
"ports": [{"containerPort": 6379, "hostPort": 6380}]
2014-07-30 23:52:58 +00:00
}]
}
},
2014-11-20 20:00:58 +00:00
"labels": {
"name": "redisslave",
"uses": "redis-master",
}
2014-07-30 23:52:58 +00:00
}},
"labels": {"name": "redisslave"}
}
2014-06-06 23:40:48 +00:00
```
2014-09-01 04:13:37 +00:00
to create the replication controller by running:
2014-06-06 23:40:48 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh -c examples/guestbook/redis-slave-controller.json create replicationControllers
2014-11-20 20:00:58 +00:00
ID Image(s) Selector Replicas
2014-06-15 23:12:13 +00:00
---------- ---------- ---------- ----------
redisSlaveController brendanburns/redis-slave name=redisslave 2
2014-06-06 23:40:48 +00:00
```
2014-11-20 20:00:58 +00:00
The redis slave configures itself by looking for the Kubernetes service environment variables in the container environment. In particular, the redis slave is started with the following command:
2014-06-06 23:40:48 +00:00
2014-06-09 21:37:43 +00:00
```shell
2014-11-20 20:00:58 +00:00
redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST:-$SERVICE_HOST} $REDIS_MASTER_SERVICE_PORT
2014-06-06 23:40:48 +00:00
```
2014-06-09 05:38:45 +00:00
Once that's up you can list the pods in the cluster, to verify that the master and slaves are running:
2014-06-06 23:40:48 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh list pods
2014-11-20 20:00:58 +00:00
ID Image(s) Host Labels Status
---------- ---------- ---------- ---------- ----------
redis-master dockerfile/redis kubernetes-minion-3.c.briandpe-api.internal name=redis-master Running
e4469b52-70e7-11e4-9154-0800279696e1 brendanburns/redis-slave kubernetes-minion-3.c.briandpe-api.internal name=redisslave,replicationController=redisSlaveController,uses=redis-master Running
e446dfc0-70e7-11e4-9154-0800279696e1 brendanburns/redis-slave kubernetes-minion-4.c.briandpe-api.internal name=redisslave,replicationController=redisSlaveController,uses=redis-master Running
2014-06-09 21:05:14 +00:00
```
You will see a single redis master pod and two redis slave pods.
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
### Step Four: Create the redis slave service
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
Just like the master, we want to have a service to proxy connections to the read slaves. In this case, in addition to discovery, the slave service provides transparent load balancing to clients. The service specification for the slaves is in `examples/guestbook/redis-slave-service.json` :
2014-06-06 23:40:48 +00:00
2014-06-09 21:37:43 +00:00
```js
2014-06-06 23:40:48 +00:00
{
"id": "redisslave",
2014-07-30 23:52:58 +00:00
"kind": "Service",
"apiVersion": "v1beta1",
2014-10-21 04:16:21 +00:00
"port": 6379,
2014-08-25 23:37:55 +00:00
"containerPort": 6379,
2014-06-06 23:40:48 +00:00
"labels": {
2014-07-30 23:52:58 +00:00
"name": "redisslave"
2014-06-19 00:03:38 +00:00
},
"selector": {
2014-07-30 23:52:58 +00:00
"name": "redisslave"
2014-06-06 23:40:48 +00:00
}
}
```
2014-11-20 20:00:58 +00:00
This time the selector for the service is `name=redisslave` , because that identifies the pods running redis slaves. It may also be helpful to set labels on your service itself as we've done here to make it easy to locate them with the `cluster/kubecfg.sh -l "label=value" list services` command.
2014-06-06 23:40:48 +00:00
2014-11-20 20:00:58 +00:00
Now that you have created the service specification, create it in your cluster by running:
2014-06-09 21:05:14 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh -c examples/guestbook/redis-slave-service.json create services
2014-11-20 20:00:58 +00:00
ID Labels Selector Port
2014-07-30 23:52:58 +00:00
---------- ---------- ---------- ----------
2014-11-20 20:00:58 +00:00
redisslave name=redisslave name=redisslave 6379
2014-06-06 23:40:48 +00:00
```
2014-11-20 20:00:58 +00:00
### Step Five: Create the frontend pod
2014-06-06 23:40:48 +00:00
2014-06-12 22:39:19 +00:00
This is a simple PHP server that is configured to talk to either the slave or master services depending on whether the request is a read or a write. It exposes a simple AJAX interface, and serves an angular-based UX. Like the redis read slaves it is a replicated service instantiated by a replication controller.
2014-06-06 23:40:48 +00:00
2014-07-30 23:52:58 +00:00
The pod is described in the file `examples/guestbook/frontend-controller.json` :
2014-06-09 21:05:14 +00:00
2014-06-09 21:37:43 +00:00
```js
2014-06-09 21:05:14 +00:00
{
"id": "frontendController",
2014-07-30 23:52:58 +00:00
"kind": "ReplicationController",
"apiVersion": "v1beta1",
2014-06-09 21:05:14 +00:00
"desiredState": {
"replicas": 3,
2014-06-19 00:20:34 +00:00
"replicaSelector": {"name": "frontend"},
2014-06-09 21:05:14 +00:00
"podTemplate": {
"desiredState": {
"manifest": {
2014-06-30 23:16:06 +00:00
"version": "v1beta1",
"id": "frontendController",
2014-06-09 21:05:14 +00:00
"containers": [{
2014-07-30 23:52:58 +00:00
"name": "php-redis",
2014-06-09 21:05:14 +00:00
"image": "brendanburns/php-redis",
2014-11-20 20:00:58 +00:00
"cpu": 100,
"memory": 50000000,
2014-06-27 23:48:35 +00:00
"ports": [{"containerPort": 80, "hostPort": 8000}]
2014-06-09 21:05:14 +00:00
}]
}
},
2014-11-20 20:00:58 +00:00
"labels": {
"name": "frontend",
"uses": "redisslave,redis-master"
}
2014-06-09 21:05:14 +00:00
}},
"labels": {"name": "frontend"}
}
2014-06-06 23:40:48 +00:00
```
2014-07-30 23:52:58 +00:00
Using this file, you can turn up your frontend with:
2014-06-06 23:40:48 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh -c examples/guestbook/frontend-controller.json create replicationControllers
2014-11-20 20:00:58 +00:00
ID Image(s) Selector Replicas
2014-06-15 23:12:13 +00:00
---------- ---------- ---------- ----------
frontendController brendanburns/php-redis name=frontend 3
2014-06-06 23:40:48 +00:00
```
2014-08-19 05:19:23 +00:00
Once that's up (it may take ten to thirty seconds to create the pods) you can list the pods in the cluster, to verify that the master, slaves and frontends are running:
2014-06-06 23:40:48 +00:00
2014-06-15 23:12:13 +00:00
```shell
2014-07-23 06:56:43 +00:00
$ cluster/kubecfg.sh list pods
2014-11-20 20:00:58 +00:00
ID Image(s) Host Labels Status
---------- ---------- ---------- ---------- ----------
redis-master dockerfile/redis kubernetes-minion-3.c.briandpe-api.internal name=redis-master Running
e4469b52-70e7-11e4-9154-0800279696e1 brendanburns/redis-slave kubernetes-minion-3.c.briandpe-api.internal name=redisslave,replicationController=redisSlaveController,uses=redis-master Running
e446dfc0-70e7-11e4-9154-0800279696e1 brendanburns/redis-slave kubernetes-minion-4.c.briandpe-api.internal name=redisslave,replicationController=redisSlaveController,uses=redis-master Running
6b584847-70ee-11e4-9154-0800279696e1 brendanburns/php-redis kubernetes-minion-3.c.briandpe-api.internal name=frontend,replicationController=frontendController,uses=redisslave,redis-master Running
6b59e6d5-70ee-11e4-9154-0800279696e1 brendanburns/php-redis kubernetes-minion-2.c.briandpe-api.internal name=frontend,replicationController=frontendController,uses=redisslave,redis-master Running
6b57a25d-70ee-11e4-9154-0800279696e1 brendanburns/php-redis kubernetes-minion-1.c.briandpe-api.internal name=frontend,replicationController=frontendController,uses=redisslave,redis-master Running
2014-06-06 23:40:48 +00:00
```
2014-06-09 21:05:14 +00:00
You will see a single redis master pod, two redis slaves, and three frontend pods.
2014-06-06 23:40:48 +00:00
The code for the PHP service looks like this:
2014-06-09 21:05:14 +00:00
2014-06-09 21:37:43 +00:00
```php
2014-06-06 23:40:48 +00:00
< ?
set_include_path('.:/usr/share/php:/usr/share/pear:/vendor/predis');
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'predis/autoload.php';
if (isset($_GET['cmd']) === true) {
header('Content-Type: application/json');
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => 'tcp',
2014-11-20 20:00:58 +00:00
'host' => getenv('REDIS_MASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
'port' => getenv('REDIS_MASTER_SERVICE_PORT'),
2014-06-06 23:40:48 +00:00
]);
$client->set($_GET['key'], $_GET['value']);
print('{"message": "Updated"}');
} else {
2014-11-20 20:00:58 +00:00
$read_port = getenv('REDIS_MASTER_SERVICE_PORT');
2014-06-06 23:40:48 +00:00
if (isset($_ENV['REDISSLAVE_SERVICE_PORT'])) {
$read_port = getenv('REDISSLAVE_SERVICE_PORT');
}
$client = new Predis\Client([
'scheme' => 'tcp',
2014-11-20 20:00:58 +00:00
'host' => getenv('REDIS_MASTER_SERVICE_HOST') ?: getenv('SERVICE_HOST'),
2014-06-06 23:40:48 +00:00
'port' => $read_port,
]);
$value = $client->get($_GET['key']);
print('{"data": "' . $value . '"}');
}
} else {
phpinfo();
} ?>
```
2014-09-03 02:22:01 +00:00
To play with the service itself, find the name of a frontend, grab the external IP of that host from the [Google Cloud Console][cloud-console] or the `gcutil` tool, and visit `http://<host-ip>:8000` .
2014-06-15 23:12:13 +00:00
```shell
$ gcutil listinstances
```
2014-06-27 23:48:35 +00:00
You may need to open the firewall for port 8000 using the [console][cloud-console] or the `gcutil` tool. The following command will allow traffic from any source to instances tagged `kubernetes-minion` :
2014-06-15 23:12:13 +00:00
```shell
2014-06-27 23:48:35 +00:00
$ gcutil addfirewall --allowed=tcp:8000 --target_tags=kubernetes-minion kubernetes-minion-8000
2014-06-15 23:12:13 +00:00
```
2014-06-27 23:48:35 +00:00
2014-11-20 20:00:58 +00:00
If you are running Kubernetes locally, you can just visit http://localhost:8000.
For details about limiting traffic to specific sources, see the [gcutil documentation][gcutil-docs].
2014-06-07 09:11:23 +00:00
[cloud-console]: https://console.developer.google.com
2014-06-15 23:13:49 +00:00
[gcutil-docs]: https://developers.google.com/compute/docs/gcutil/reference/firewall#addfirewall
2014-09-10 20:56:56 +00:00
### Step Six: Cleanup
To turn down a Kubernetes cluster:
```shell
$ cluster/kube-down.sh
```