From 4123b6484046b8391b7623ba070b61279c7d8d12 Mon Sep 17 00:00:00 2001 From: Gurpartap Singh Date: Fri, 12 Sep 2014 22:54:54 +0530 Subject: [PATCH 1/4] Add the source for Go based Guestbook --- examples/guestbook-go/src/main.go | 104 +++++++++++++++++++ examples/guestbook-go/src/public/index.html | 39 +++++++ examples/guestbook-go/src/public/script.js | 34 +++++++ examples/guestbook-go/src/public/style.css | 107 ++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 examples/guestbook-go/src/main.go create mode 100644 examples/guestbook-go/src/public/index.html create mode 100644 examples/guestbook-go/src/public/script.js create mode 100644 examples/guestbook-go/src/public/style.css diff --git a/examples/guestbook-go/src/main.go b/examples/guestbook-go/src/main.go new file mode 100644 index 00000000000..098feaa176d --- /dev/null +++ b/examples/guestbook-go/src/main.go @@ -0,0 +1,104 @@ +package main + +import ( + "encoding/json" + "net/http" + "os" + "strings" + + "github.com/codegangsta/negroni" + "github.com/gorilla/mux" + "github.com/xyproto/simpleredis" +) + +var pool *simpleredis.ConnectionPool + +func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { + members, err := simpleredis.NewList(pool, mux.Vars(req)["key"]).GetAll() + if err != nil { + panic(err) + } + + membersJSON, err := json.MarshalIndent(members, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Header().Set("Content-Type", "application/json") + rw.Write([]byte(membersJSON)) +} + +func ListPushHandler(rw http.ResponseWriter, req *http.Request) { + set := simpleredis.NewList(pool, mux.Vars(req)["key"]) + err := set.Add(mux.Vars(req)["value"]) + if err != nil { + panic(err) + } + + members, err := set.GetAll() + if err != nil { + panic(err) + } + + membersJSON, err := json.MarshalIndent(members, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Header().Set("Content-Type", "application/json") + rw.Write([]byte(membersJSON)) +} + +func InfoHandler(rw http.ResponseWriter, req *http.Request) { + info, err := pool.Get(0).Do("INFO") + if err != nil { + panic(err) + } + + infoString := string(info.([]uint8)) + + rw.WriteHeader(200) + rw.Write([]byte(infoString)) +} + +func EnvHandler(rw http.ResponseWriter, req *http.Request) { + getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string { + items := make(map[string]string) + for _, item := range data { + key, val := getkeyval(item) + items[key] = val + } + return items + } + environment := getenvironment(os.Environ(), func(item string) (key, val string) { + splits := strings.Split(item, "=") + key = splits[0] + val = strings.Join(splits[1:], "=") + return + }) + + envJSON, err := json.MarshalIndent(environment, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Write([]byte(envJSON)) +} + +func main() { + pool = simpleredis.NewConnectionPoolHost(os.Getenv("SERVICE_HOST") + ":" + os.Getenv("REDIS_MASTER_SERVICE_SERVICE_PORT")) + defer pool.Close() + + r := mux.NewRouter() + r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler) + r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler) + r.Path("/info").Methods("GET").HandlerFunc(InfoHandler) + r.Path("/env").Methods("GET").HandlerFunc(EnvHandler) + + n := negroni.Classic() + n.UseHandler(r) + n.Run(":3000") +} diff --git a/examples/guestbook-go/src/public/index.html b/examples/guestbook-go/src/public/index.html new file mode 100644 index 00000000000..e1301d79796 --- /dev/null +++ b/examples/guestbook-go/src/public/index.html @@ -0,0 +1,39 @@ + + + + + + + + + Guestbook + + + +
+
+
+

Waiting for database connection...

+
+
+
+
+
+
+
+ + Submit +
+
+
+
+ + + + diff --git a/examples/guestbook-go/src/public/script.js b/examples/guestbook-go/src/public/script.js new file mode 100644 index 00000000000..2383a791acc --- /dev/null +++ b/examples/guestbook-go/src/public/script.js @@ -0,0 +1,34 @@ +$( document ).ready(function() { + + appendGuestbookEntries = function( data ) { + $( "#guestbook-entries" ).empty(); + $.each( data, function( key, val ) { + $( "#guestbook-entries" ).append( "

" + val + "

" ); + }); + } + + handleSubmission = function() { + value = $( "#guestbook-entry-content" ).val() + if (value.length > 0) { + $( "#guestbook-entries" ).append( "

...

" ); + $.getJSON( "rpush/guestbook/" + value, appendGuestbookEntries); + } + return false; + } + + // Event handlers. + $( "#guestbook-submit" ).click(handleSubmission); + $( "#guestbook-entry-content" ).keypress(function (e) { + if (e.which == 13) { + return handleSubmission(); + } + }); + + // Poll every second. + (function fetchGuestbook(){ + + $.getJSON("lrange/guestbook").done(appendGuestbookEntries).always(function() { setTimeout(fetchGuestbook, 1000); }); + + })(); + +}); diff --git a/examples/guestbook-go/src/public/style.css b/examples/guestbook-go/src/public/style.css new file mode 100644 index 00000000000..20e25aac27d --- /dev/null +++ b/examples/guestbook-go/src/public/style.css @@ -0,0 +1,107 @@ +html, body { + background: #EEE; } + +html, button, input, select, textarea, +.pure-g [class*="pure-u"], +.pure-g-r [class*="pure-u"] { + font-family: "Helvetica", sans-serif; + color: #585A5C; + line-height: 1.4; + position: relative; } + +h1, h2, p, a, .pure-button { + margin: 0; + color: #585A5C; + font-weight: 400; } + h1 strong, h2 strong, h3 strong, p strong, li strong, a strong, .pure-button strong { + font-weight: 700; } + +h1 { + font-size: 5em; + font-weight: 300; + color: #585A5C; } + +h2 { + font-size: 2.25em; + font-weight: 300; + color: #585A5C; } + +p { + font-size: 1.2em; + line-height: 1.7; } + +input { + font-size: 1.125em; + outline: none; } + +.default-button { + position: relative; + font-weight: 400; + padding: 0.75em 2em; + white-space: normal; + background: #1699e3; + border-radius: 1000px; + font-size: 1.25em; + color: #FFF; + text-transform: uppercase; } + .default-button strong { + font-weight: 700; } + +.section { + position: relative; + zoom: 1; + width: 100%; + overflow: hidden; } + +.container { + position: relative; + overflow: hidden; + margin: 0 auto; + padding: 0 1em; + max-width: 64em; + text-align: center; } + +.section-text-container { + max-width: 48em; + margin: 0 auto; } + +.color-overlay { + position: absolute; + width: 100%; + height: 100%; + top: 0; + left: 0; + opacity: 0.9; + -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=" 90 ")"; + filter: alpha(opacity=90); + zoom: 1; } + +#header { + padding: 2em 0 0 0; } + #header h1, #header h2, #header a { + color: #585A5C; } + #header .color-overlay { + background: #eee; } + #header .container { + overflow: visible; } + +#content { + background: #eee; + padding: 1em 0; } + #content form { + text-align: center; + margin: 3em auto 0 auto; + max-width: 20em; } + #content input { + font-size: 1.25em; + display: block; + width: 80%; + padding: 0.75em 10%; + margin-bottom: 1em; + border-radius: 1000px; + text-align: center; + border: 0; + box-shadow: inset 0 0 0 2px #ccc; + -webkit-appearance: none; } + #content button { + width: 100%; } From b8fb169d22eb289fff7ee0bef02bc427eee57768 Mon Sep 17 00:00:00 2001 From: Gurpartap Singh Date: Fri, 12 Sep 2014 22:56:40 +0530 Subject: [PATCH 2/4] Add Dockerfiles for compiling the guestbook and building a minimal image --- examples/guestbook-go/src/Dockerfile | 6 ++++++ .../guestbook-go/src/busybox-image/Dockerfile | 10 ++++++++++ .../src/busybox-image/script/build.sh | 7 +++++++ .../src/busybox-image/script/clean.sh | 6 ++++++ .../src/busybox-image/script/push.sh | 4 ++++ .../src/busybox-image/script/release.sh | 17 +++++++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 examples/guestbook-go/src/Dockerfile create mode 100644 examples/guestbook-go/src/busybox-image/Dockerfile create mode 100755 examples/guestbook-go/src/busybox-image/script/build.sh create mode 100755 examples/guestbook-go/src/busybox-image/script/clean.sh create mode 100755 examples/guestbook-go/src/busybox-image/script/push.sh create mode 100755 examples/guestbook-go/src/busybox-image/script/release.sh diff --git a/examples/guestbook-go/src/Dockerfile b/examples/guestbook-go/src/Dockerfile new file mode 100644 index 00000000000..9b758281aba --- /dev/null +++ b/examples/guestbook-go/src/Dockerfile @@ -0,0 +1,6 @@ +FROM google/golang:latest + +RUN go get github.com/Gurpartap/guestbook-example && \ + cp /gopath/src/github.com/Gurpartap/guestbook-example/busybox-image/Dockerfile /gopath + +CMD docker build --rm --force-rm -t gurpartap/guestbook-example /gopath diff --git a/examples/guestbook-go/src/busybox-image/Dockerfile b/examples/guestbook-go/src/busybox-image/Dockerfile new file mode 100644 index 00000000000..9cb481b5536 --- /dev/null +++ b/examples/guestbook-go/src/busybox-image/Dockerfile @@ -0,0 +1,10 @@ +FROM busybox:ubuntu-14.04 + +ADD ./bin/guestbook-example /app/guestbook-example +ADD ./src/github.com/Gurpartap/guestbook-example/public/index.html /app/public/index.html +ADD ./src/github.com/Gurpartap/guestbook-example/public/script.js /app/public/script.js +ADD ./src/github.com/Gurpartap/guestbook-example/public/style.css /app/public/style.css + +WORKDIR /app +CMD ["./guestbook-example"] +EXPOSE 3000 diff --git a/examples/guestbook-go/src/busybox-image/script/build.sh b/examples/guestbook-go/src/busybox-image/script/build.sh new file mode 100755 index 00000000000..01e6c7bcb22 --- /dev/null +++ b/examples/guestbook-go/src/busybox-image/script/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# Usage: ./build.sh + +docker build --rm --force-rm -t gurpartap/guestbook-example-build . +docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \ + -v $(which docker):/usr/local/bin/docker \ + -ti --name guestbook-example-build gurpartap/guestbook-example-build diff --git a/examples/guestbook-go/src/busybox-image/script/clean.sh b/examples/guestbook-go/src/busybox-image/script/clean.sh new file mode 100755 index 00000000000..0b43c36496d --- /dev/null +++ b/examples/guestbook-go/src/busybox-image/script/clean.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# Usage: ./clean.sh + +docker rm -f guestbook-example-build 2> /dev/null +docker rmi -f gurpartap/guestbook-example-build +docker rmi -f gurpartap/guestbook-example diff --git a/examples/guestbook-go/src/busybox-image/script/push.sh b/examples/guestbook-go/src/busybox-image/script/push.sh new file mode 100755 index 00000000000..4fb56af82e8 --- /dev/null +++ b/examples/guestbook-go/src/busybox-image/script/push.sh @@ -0,0 +1,4 @@ +#!/bin/sh +# Usage: ./push.sh [TAG] + +docker push gurpartap/guestbook-example:${1:-latest} diff --git a/examples/guestbook-go/src/busybox-image/script/release.sh b/examples/guestbook-go/src/busybox-image/script/release.sh new file mode 100755 index 00000000000..9b9aec847c3 --- /dev/null +++ b/examples/guestbook-go/src/busybox-image/script/release.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Usage: ./release.sh [TAG] + +set +e + +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +echo " ---> Building..." +sh $DIR/build.sh + +echo " ---> Pushing gurpartap/guestbook-example:${1:-latest}..." +sh $DIR/push.sh $1 + +echo " ---> Cleaning up..." +sh $DIR/clean.sh + +echo " ---> Done." From 13f79b00a068b30132c6006b3ff8bbbfd80b4e16 Mon Sep 17 00:00:00 2001 From: Gurpartap Singh Date: Fri, 12 Sep 2014 22:58:32 +0530 Subject: [PATCH 3/4] Add Go guestbook's k8s config json files --- .../guestbook-go/guestbook-controller.json | 24 ++++++++++++++++++ examples/guestbook-go/guestbook-service.json | 7 ++++++ examples/guestbook-go/redis-master-pod.json | 17 +++++++++++++ .../guestbook-go/redis-master-service.json | 7 ++++++ .../guestbook-go/redis-slave-controller.json | 25 +++++++++++++++++++ .../guestbook-go/redis-slave-service.json | 8 ++++++ 6 files changed, 88 insertions(+) create mode 100644 examples/guestbook-go/guestbook-controller.json create mode 100644 examples/guestbook-go/guestbook-service.json create mode 100644 examples/guestbook-go/redis-master-pod.json create mode 100644 examples/guestbook-go/redis-master-service.json create mode 100644 examples/guestbook-go/redis-slave-controller.json create mode 100644 examples/guestbook-go/redis-slave-service.json diff --git a/examples/guestbook-go/guestbook-controller.json b/examples/guestbook-go/guestbook-controller.json new file mode 100644 index 00000000000..79f178d6cdc --- /dev/null +++ b/examples/guestbook-go/guestbook-controller.json @@ -0,0 +1,24 @@ +{ + "apiVersion": "v1beta1", + "kind": "ReplicationController", + "id": "guestbook-controller", + "desiredState": { + "replicas": 3, + "replicaSelector": { "name": "guestbook" }, + "podTemplate": { + "desiredState": { + "manifest": { + "version": "v1beta1", + "id": "guestbook-controller", + "containers": [{ + "image": "gurpartap/guestbook-example", + "name": "php-redis", + "ports": [{ "containerPort": 3000, "hostPort": 3000 }] + }], + } + }, + "labels": { "name": "guestbook" } + }, + }, + "labels": { "name": "guestbook" } +} diff --git a/examples/guestbook-go/guestbook-service.json b/examples/guestbook-go/guestbook-service.json new file mode 100644 index 00000000000..a809c89efd2 --- /dev/null +++ b/examples/guestbook-go/guestbook-service.json @@ -0,0 +1,7 @@ +{ + "apiVersion": "v1beta1", + "kind": "Service", + "id": "guestbook", + "port": 3000, + "selector": { "name": "guestbook" } +} diff --git a/examples/guestbook-go/redis-master-pod.json b/examples/guestbook-go/redis-master-pod.json new file mode 100644 index 00000000000..8b80720cd63 --- /dev/null +++ b/examples/guestbook-go/redis-master-pod.json @@ -0,0 +1,17 @@ +{ + "apiVersion": "v1beta1", + "kind": "Pod", + "id": "redis-master-pod", + "desiredState": { + "manifest": { + "version": "v1beta1", + "id": "redis-master-pod", + "containers": [{ + "name": "master", + "image": "gurpartap/redis", + "ports": [{ "containerPort": 6379, "hostPort": 6379 }] + }] + } + }, + "labels": { "name": "redis-master" } +} diff --git a/examples/guestbook-go/redis-master-service.json b/examples/guestbook-go/redis-master-service.json new file mode 100644 index 00000000000..a6fc6064679 --- /dev/null +++ b/examples/guestbook-go/redis-master-service.json @@ -0,0 +1,7 @@ +{ + "apiVersion": "v1beta1", + "kind": "Service", + "id": "redis-master-service", + "port": 6379, + "selector": { "name": "redis-master" } +} diff --git a/examples/guestbook-go/redis-slave-controller.json b/examples/guestbook-go/redis-slave-controller.json new file mode 100644 index 00000000000..4955bc6876c --- /dev/null +++ b/examples/guestbook-go/redis-slave-controller.json @@ -0,0 +1,25 @@ +{ + "apiVersion": "v1beta1", + "kind": "ReplicationController", + "id": "redis-slave-controller", + "desiredState": { + "replicas": 2, + "replicaSelector": { "name": "redis-slave" }, + "podTemplate": { + "desiredState": { + "manifest": { + "version": "v1beta1", + "id": "redis-slave-controller", + "containers": [{ + "name": "redis-slave", + "image": "gurpartap/redis", + "command": ["redis-server", "/etc/redis/redis.conf", "--slaveof", "$SERVICE_HOST", "$REDIS_MASTER_SERVICE_SERVICE_PORT"], + "ports": [{ "containerPort": 6379, "hostPort": 6379 }] + }] + } + }, + "labels": { "name": "redis-slave" } + } + }, + "labels": { "name": "redis-slave" } +} diff --git a/examples/guestbook-go/redis-slave-service.json b/examples/guestbook-go/redis-slave-service.json new file mode 100644 index 00000000000..aefbb05c2db --- /dev/null +++ b/examples/guestbook-go/redis-slave-service.json @@ -0,0 +1,8 @@ +{ + "apiVersion": "v1beta1", + "kind": "Service", + "id": "redis-slave-service", + "port": 6379, + "labels": { "name": "redis-slave" }, + "selector": { "name": "redis-slave" } +} From b7d1b1ac3c70818d81c6bbdd829b7c69103d0ed0 Mon Sep 17 00:00:00 2001 From: Gurpartap Singh Date: Fri, 12 Sep 2014 23:05:02 +0530 Subject: [PATCH 4/4] Updated guestbook based on suggestions in the pull request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port of current guestbook's README.md Fix guestbook pod and service names Add go boilerplate Use role label for redis pods Give service.containerPort a name based value This gives better env variable keys like REDIS_MASTER_SERVICE_REDIS_SERVER_ADDR, etc. Avoid unnecessarily long names for service (esp in env vars) Adding guestbook deploy/destroy scripts for k8s These are probably only useful for quick testing. Maybe remove them before merging the pull request. Part of avoiding long names for services Update Dockerfiles to git clone from Google's repo Use correct service names while deleting them Fix the script usage path. K8s is not go gettable. Use git clone instead. Using my fork in the Dockerfile to release and update to the docker hub image. Doesn't delete all pods if you remove controller too soon Run the command in a shell to substitute env vars. Workaround for GoogleCloudPlatform/kubernetes#1309 GoogleCloudPlatform in lieu of my fork in Dckrfile Some directory structure changes for guestbook src README that explains the build process for source Strip down the html and css to absolute essentials Reformat JS according to Google's guidelines Also added code to set random colors for elements. Handle repetitive error checks using a common func Also uses @roberthbailey’s stripped down code for reading env vars infoString isn't really a string. Use info instead Remove deploy.sh/destroy.sh scripts Bind submit instead of keypress to capture submit Add links to /env and /info in the footer Reformat the JS Incorporating suggestions by @filbranden License boilerplate and some fixes to release.sh Update README.md Update README.md Add building on boot2docker info to README Accept docker bin path as a param for building Use kubernetes user to host the image on registry Don't get included in k8s's recursive build deps https://github.com/GoogleCloudPlatform/kubernetes/pull/1299#discussion_r 17638061 --- examples/guestbook-go/README.md | 174 ++++++++++++++++++ examples/guestbook-go/_src/Dockerfile | 9 + examples/guestbook-go/_src/README.md | 42 +++++ .../guestbook-go/_src/guestbook/Dockerfile | 10 + examples/guestbook-go/_src/main.go | 86 +++++++++ examples/guestbook-go/_src/public/index.html | 34 ++++ examples/guestbook-go/_src/public/script.js | 46 +++++ examples/guestbook-go/_src/public/style.css | 61 ++++++ examples/guestbook-go/_src/script/build.sh | 35 ++++ examples/guestbook-go/_src/script/clean.sh | 25 +++ examples/guestbook-go/_src/script/push.sh | 24 +++ examples/guestbook-go/_src/script/release.sh | 40 ++++ .../guestbook-go/guestbook-controller.json | 6 +- examples/guestbook-go/guestbook-service.json | 1 + examples/guestbook-go/redis-master-pod.json | 6 +- .../guestbook-go/redis-master-service.json | 5 +- .../guestbook-go/redis-slave-controller.json | 10 +- .../guestbook-go/redis-slave-service.json | 7 +- examples/guestbook-go/src/Dockerfile | 6 - .../guestbook-go/src/busybox-image/Dockerfile | 10 - .../src/busybox-image/script/build.sh | 7 - .../src/busybox-image/script/clean.sh | 6 - .../src/busybox-image/script/push.sh | 4 - .../src/busybox-image/script/release.sh | 17 -- examples/guestbook-go/src/main.go | 104 ----------- examples/guestbook-go/src/public/index.html | 39 ---- examples/guestbook-go/src/public/script.js | 34 ---- examples/guestbook-go/src/public/style.css | 107 ----------- 28 files changed, 605 insertions(+), 350 deletions(-) create mode 100644 examples/guestbook-go/README.md create mode 100644 examples/guestbook-go/_src/Dockerfile create mode 100644 examples/guestbook-go/_src/README.md create mode 100644 examples/guestbook-go/_src/guestbook/Dockerfile create mode 100644 examples/guestbook-go/_src/main.go create mode 100644 examples/guestbook-go/_src/public/index.html create mode 100644 examples/guestbook-go/_src/public/script.js create mode 100644 examples/guestbook-go/_src/public/style.css create mode 100755 examples/guestbook-go/_src/script/build.sh create mode 100755 examples/guestbook-go/_src/script/clean.sh create mode 100755 examples/guestbook-go/_src/script/push.sh create mode 100755 examples/guestbook-go/_src/script/release.sh delete mode 100644 examples/guestbook-go/src/Dockerfile delete mode 100644 examples/guestbook-go/src/busybox-image/Dockerfile delete mode 100755 examples/guestbook-go/src/busybox-image/script/build.sh delete mode 100755 examples/guestbook-go/src/busybox-image/script/clean.sh delete mode 100755 examples/guestbook-go/src/busybox-image/script/push.sh delete mode 100755 examples/guestbook-go/src/busybox-image/script/release.sh delete mode 100644 examples/guestbook-go/src/main.go delete mode 100644 examples/guestbook-go/src/public/index.html delete mode 100644 examples/guestbook-go/src/public/script.js delete mode 100644 examples/guestbook-go/src/public/style.css diff --git a/examples/guestbook-go/README.md b/examples/guestbook-go/README.md new file mode 100644 index 00000000000..2e83713608d --- /dev/null +++ b/examples/guestbook-go/README.md @@ -0,0 +1,174 @@ +## 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 + +This example assumes that you have forked the repository and [turned up a Kubernetes cluster](https://github.com/GoogleCloudPlatform/kubernetes#contents): + +```shell +$ cd kubernetes +$ hack/dev-build-and-up.sh +``` + +### Step One: Turn up the redis master. + +Use the file `examples/guestbook-go/redis-master-pod.json` which describes a single pod running a redis key-value server in a container. + +Create the redis pod in your Kubernetes cluster using the `kubecfg` CLI: + +```shell +$ cluster/kubecfg.sh -c examples/guestbook-go/redis-master-pod.json create pods +``` + +Once that's up you can list the pods in the cluster, to verify that the master is running: + +```shell +$ cluster/kubecfg.sh list pods +``` + +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). + +``` + ID Image(s) Host Labels Status +---------- ---------- ---------- ---------- ---------- +redis-master-pod gurpartap/redis kubernetes-minion-3.c.briandpe-api.internal name=redis,role=master Running +``` + +If you ssh to that machine, you can run `docker ps` to see the actual pod: + +```shell +$ gcutil ssh --zone us-central1-b kubernetes-minion-3 +$ sudo docker ps + +me@kubernetes-minion-3:~$ sudo docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS +e443647cd064 gurpartap/redis:latest redis-server /etc/r 22 minutes ago Up 22 minutes +``` + +(Note that initial `docker pull` may take a few minutes, depending on network conditions.) + +### Step Two: Turn up the master service. +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. + +The pod that you created in Step One has the label `name=redis` and `role=master`. The selector field of the service determines which pods will receive the traffic sent to the service. Use the file `examples/guestbook-go/redis-master-service.json` + +To create the service with the `kubecfg` cli: + +```shell +$ cluster/kubecfg.sh -c examples/guestbook-go/redis-master-service.json create services +ID Labels Selector Port +---------- ---------- ---------- ---------- +redis-master name=redis,role=master 6379 +``` + +This will cause all pods to see the redis master apparently running on localhost:6379. + +Once created, the service proxy on each minion is configured to set up a proxy on the specified port (in this case port 6379). + +### Step Three: Turn up the replicated slave pods. +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. + +Use the file `examples/guestbook-go/redis-slave-controller.json` + +to create the replication controller by running: + +```shell +$ cluster/kubecfg.sh -c examples/guestbook-go/redis-slave-controller.json create replicationControllers + ID Image(s) Selector Replicas +---------- ---------- ---------- ---------- +redis-slave-controller gurpartap/redis name=redis,role=slave 2 +``` + +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: + +```shell +redis-server --slaveof $SERVICE_HOST $REDIS_MASTER_SERVICE_PORT +``` + +Once that's up you can list the pods in the cluster, to verify that the master and slaves are running: + +```shell +$ cluster/kubecfg.sh list pods + ID Image(s) Host Labels Status +---------- ---------- ---------- ---------- ---------- +redis-master-pod gurpartap/redis kubernetes-minion-3.c.briandpe-api.internal name=redis,role=master Running +4d65822107fcfd52 gurpartap/redis kubernetes-minion-3.c.briandpe-api.internal name=redis,role=slave,replicationController=redis-slave-controller Running +78629a0f5f3f164f gurpartap/redis kubernetes-minion-4.c.briandpe-api.internal name=redis,role=slave,replicationController=redis-slave-controller Running +``` + +You will see a single redis master pod and two redis slave pods. + +### Step Four: Create the redis slave service. + +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-go/redis-slave-service.json` + +This time the selector for the service is `name=redis,role=slave`, 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 `kubecfg -l "label=value" list services` command. + +Now that you have created the service specification, create it in your cluster with the `kubecfg` CLI: + +```shell +$ cluster/kubecfg.sh -c examples/guestbook-go/redis-slave-service.json create services + ID Labels Selector Port +---------- ---------- ---------- ---------- +redis-slave name=redis-slave name=redis,role=slave 6379 +``` + +### Step Five: Create the guestbook pod. + +This is a simple Go net/http ([negroni](https://github.com/codegangsta/negroni) based) 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 JSON interface, and serves a jQuery-Ajax based UX. Like the redis read slaves it is a replicated service instantiated by a replication controller. + +The pod is described in the file `examples/guestbook-go/guestbook-controller.json`: + +Using this file, you can turn up your guestbook with: + +```shell +$ cluster/kubecfg.sh -c examples/guestbook-go/guestbook-controller.json create replicationControllers + ID Image(s) Selector Replicas +---------- ---------- ---------- ---------- +guestbook-controller gurpartap/redis name=guestbook 3 +``` + +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 guestbook frontends are running: + +```shell +$ cluster/kubecfg.sh list pods + ID Image(s) Host Labels Status +---------- ---------- ---------- ---------- ---------- +redis-master-pod gurpartap/redis kubernetes-minion-3.c.briandpe-api.internal name=redis,role=master Running +4d65822107fcfd52 gurpartap/redis kubernetes-minion-3.c.briandpe-api.internal name=redis,role=slave,replicationController=redis-slave-controller Running +380704bb7b4d7c03 kubernetes/guestbook kubernetes-minion-3.c.briandpe-api.internal name=guestbook,replicationController=guestbook-controller Running +55104dc76695721d kubernetes/guestbook kubernetes-minion-2.c.briandpe-api.internal name=guestbook,replicationController=guestbook-controller Running +365a858149c6e2d1 kubernetes/guestbook kubernetes-minion-1.c.briandpe-api.internal name=guestbook,replicationController=guestbook-controller Running +78629a0f5f3f164f gurpartap/redis kubernetes-minion-4.c.briandpe-api.internal name=redis,role=slave,replicationController=redis-slave-controller Running +``` + +You will see a single redis master pod, two redis slaves, and three guestbook pods. + +To play with the service itself, find the name of a guestbook, grab the external IP of that host from the [Google Cloud Console][cloud-console] or the `gcutil` tool, and visit `http://:3000`. + +```shell +$ gcutil listinstances +``` + +You may need to open the firewall for port 3000 using the [console][cloud-console] or the `gcutil` tool. The following command will allow traffic from any source to instances tagged `kubernetes-minion`: + +```shell +$ gcutil addfirewall --allowed=tcp:3000 --target_tags=kubernetes-minion kubernetes-minion-3000 +``` + +If you are running Kubernetes locally, you can just visit http://localhost:3000 +For details about limiting traffic to specific sources, see the [gcutil documentation][gcutil-docs] + +[cloud-console]: https://console.developer.google.com +[gcutil-docs]: https://developers.google.com/compute/docs/gcutil/reference/firewall#addfirewall + +### Step Six: Cleanup + +To turn down a Kubernetes cluster: + +```shell +$ cluster/kube-down.sh +``` diff --git a/examples/guestbook-go/_src/Dockerfile b/examples/guestbook-go/_src/Dockerfile new file mode 100644 index 00000000000..84b1ff1550f --- /dev/null +++ b/examples/guestbook-go/_src/Dockerfile @@ -0,0 +1,9 @@ +FROM google/golang:latest + +RUN mkdir -p /gopath/src/github.com/GoogleCloudPlatform/ && cd /gopath/src/github.com/GoogleCloudPlatform/ && \ + git clone http://github.com/GoogleCloudPlatform/kubernetes && \ + cd /gopath/src/github.com/GoogleCloudPlatform/kubernetes/examples/guestbook-go/src/ && \ + go get && go build -o ../bin/guestbook && \ + cp ./guestbook/Dockerfile /gopath/src/github.com/GoogleCloudPlatform/kubernetes/examples/guestbook-go/ + +CMD docker build --rm --force-rm -t kubernetes/guestbook /gopath/src/github.com/GoogleCloudPlatform/kubernetes/examples/guestbook-go/ diff --git a/examples/guestbook-go/_src/README.md b/examples/guestbook-go/_src/README.md new file mode 100644 index 00000000000..3139742a8e7 --- /dev/null +++ b/examples/guestbook-go/_src/README.md @@ -0,0 +1,42 @@ +## Building and releasing Guestbook Image + +Guestbook build process employs the usage of docker-in-docker to build an image within another. This requires that the build image has access to the `docker` program's binary, which defaults to the docker available on your host machine. In the case of boot2docker, `DOCKER_BIN` must be set to the binary's location in the boot2docker's vm. + +Releasing the image requires that you have access to the docker registry user account which will host the image. + +To build and release the guestbook image: + + cd examples/guestbook-go/src + ./script/release.sh + +If you're using boot2docker, specify the `DOCKER_BIN` environment variable + + DOCKER_BIN="$(boot2docker ssh which docker)" ./script/release.sh + +#### Step by step + +If you may want to, you can build and push the image step by step. + +###### Start fresh before building + + ./script/clean.sh 2> /dev/null + +###### Build + +Builds a docker image that builds the app and packages it into a minimal docker image + + ./script/build.sh + +If you're using boot2docker, specify the `DOCKER_BIN` environment variable + + DOCKER_BIN="$(boot2docker ssh which docker)" ./script/build.sh + +###### Push + +Accepts an optional tag (defaults to "latest") + + ./script/push.sh [TAG] + +###### Clean up + + ./script/clean.sh diff --git a/examples/guestbook-go/_src/guestbook/Dockerfile b/examples/guestbook-go/_src/guestbook/Dockerfile new file mode 100644 index 00000000000..25805d2766c --- /dev/null +++ b/examples/guestbook-go/_src/guestbook/Dockerfile @@ -0,0 +1,10 @@ +FROM busybox:ubuntu-14.04 + +ADD ./bin/guestbook /app/guestbook +ADD ./src/public/index.html /app/public/index.html +ADD ./src/public/script.js /app/public/script.js +ADD ./src/public/style.css /app/public/style.css + +WORKDIR /app +CMD ["./guestbook"] +EXPOSE 3000 diff --git a/examples/guestbook-go/_src/main.go b/examples/guestbook-go/_src/main.go new file mode 100644 index 00000000000..78eaf8e06e7 --- /dev/null +++ b/examples/guestbook-go/_src/main.go @@ -0,0 +1,86 @@ +/* +Copyright 2014 Google Inc. All rights reserved. + +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. +*/ + +package main + +import ( + "encoding/json" + "net/http" + "os" + "strings" + + "github.com/codegangsta/negroni" + "github.com/gorilla/mux" + "github.com/xyproto/simpleredis" +) + +var pool *simpleredis.ConnectionPool + +func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { + key := mux.Vars(req)["key"] + list := simpleredis.NewList(pool, key) + members := HandleError(list.GetAll()).([]string) + membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte) + rw.Write(membersJSON) +} + +func ListPushHandler(rw http.ResponseWriter, req *http.Request) { + key := mux.Vars(req)["key"] + value := mux.Vars(req)["value"] + list := simpleredis.NewList(pool, key) + HandleError(nil, list.Add(value)) + ListRangeHandler(rw, req) +} + +func InfoHandler(rw http.ResponseWriter, req *http.Request) { + info := HandleError(pool.Get(0).Do("INFO")).([]byte) + rw.Write(info) +} + +func EnvHandler(rw http.ResponseWriter, req *http.Request) { + environment := make(map[string]string) + for _, item := range os.Environ() { + splits := strings.Split(item, "=") + key := splits[0] + val := strings.Join(splits[1:], "=") + environment[key] = val + } + + envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte) + rw.Write(envJSON) +} + +func HandleError(result interface{}, err error) (r interface{}) { + if err != nil { + panic(err) + } + return result +} + +func main() { + pool = simpleredis.NewConnectionPoolHost(os.Getenv("SERVICE_HOST") + ":" + os.Getenv("REDIS_MASTER_SERVICE_PORT")) + defer pool.Close() + + r := mux.NewRouter() + r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler) + r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler) + r.Path("/info").Methods("GET").HandlerFunc(InfoHandler) + r.Path("/env").Methods("GET").HandlerFunc(EnvHandler) + + n := negroni.Classic() + n.UseHandler(r) + n.Run(":3000") +} diff --git a/examples/guestbook-go/_src/public/index.html b/examples/guestbook-go/_src/public/index.html new file mode 100644 index 00000000000..62991ed3c63 --- /dev/null +++ b/examples/guestbook-go/_src/public/index.html @@ -0,0 +1,34 @@ + + + + + + + + Guestbook + + + + +
+

Waiting for database connection...

+
+ +
+
+ + Submit +
+
+ +
+

+

/env + /info

+
+ + + + diff --git a/examples/guestbook-go/_src/public/script.js b/examples/guestbook-go/_src/public/script.js new file mode 100644 index 00000000000..a0a545b0564 --- /dev/null +++ b/examples/guestbook-go/_src/public/script.js @@ -0,0 +1,46 @@ +$(document).ready(function() { + var headerTitleElement = $("#header h1"); + var entriesElement = $("#guestbook-entries"); + var formElement = $("#guestbook-form"); + var submitElement = $("#guestbook-submit"); + var entryContentElement = $("#guestbook-entry-content"); + var hostAddressElement = $("#guestbook-host-address"); + + var appendGuestbookEntries = function(data) { + entriesElement.empty(); + $.each(data, function(key, val) { + entriesElement.append("

" + val + "

"); + }); + } + + var handleSubmission = function(e) { + e.preventDefault(); + var entryValue = entryContentElement.val() + if (entryValue.length > 0) { + entriesElement.append("

...

"); + $.getJSON("rpush/guestbook/" + entryValue, appendGuestbookEntries); + } + return false; + } + + // colors = purple, blue, red, green, yellow + var colors = ["#549", "#18d", "#d31", "#2a4", "#db1"]; + var randomColor = colors[Math.floor(5 * Math.random())]; + (function setElementsColor(color) { + headerTitleElement.css("color", color); + entryContentElement.css("box-shadow", "inset 0 0 0 2px " + color); + submitElement.css("background-color", color); + })(randomColor); + + submitElement.click(handleSubmission); + formElement.submit(handleSubmission); + hostAddressElement.append(document.URL); + + // Poll every second. + (function fetchGuestbook() { + $.getJSON("lrange/guestbook").done(appendGuestbookEntries).always( + function() { + setTimeout(fetchGuestbook, 1000); + }); + })(); +}); diff --git a/examples/guestbook-go/_src/public/style.css b/examples/guestbook-go/_src/public/style.css new file mode 100644 index 00000000000..fd1c393fb08 --- /dev/null +++ b/examples/guestbook-go/_src/public/style.css @@ -0,0 +1,61 @@ +body, input { + color: #123; + font-family: "Gill Sans", sans-serif; +} + +div { + overflow: hidden; + padding: 1em 0; + position: relative; + text-align: center; +} + +h1, h2, p, input, a { + font-weight: 300; + margin: 0; +} + +h1 { + color: #BDB76B; + font-size: 3.5em; +} + +h2 { + color: #999; +} + +form { + margin: 0 auto; + max-width: 50em; + text-align: center; +} + +input { + border: 0; + border-radius: 1000px; + box-shadow: inset 0 0 0 2px #BDB76B; + display: inline; + font-size: 1.5em; + margin-bottom: 1em; + outline: none; + padding: .5em 5%; + width: 55%; +} + +form a { + background: #BDB76B; + border: 0; + border-radius: 1000px; + color: #FFF; + font-size: 1.25em; + font-weight: 400; + padding: .75em 2em; + text-decoration: none; + text-transform: uppercase; + white-space: normal; +} + +p { + font-size: 1.5em; + line-height: 1.5; +} diff --git a/examples/guestbook-go/_src/script/build.sh b/examples/guestbook-go/_src/script/build.sh new file mode 100755 index 00000000000..ccff8c1a742 --- /dev/null +++ b/examples/guestbook-go/_src/script/build.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Copyright 2014 Google Inc. All rights reserved. +# +# 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. + +# Usage: ./script/build.sh + +set -o errexit +set -o nounset +set -o pipefail + +if [[ "${DOCKER_BIN+set}" == "set" ]]; then + echo "Using DOCKER_BIN=\"${DOCKER_BIN}\" from the environment." +elif DOCKER_BIN=$(which docker); then + echo "Setting DOCKER_BIN=\"${DOCKER_BIN}\" from host machine." +else + echo "Could not find a working docker binary and none passed in DOCKER_BIN." >&2 + exit 1 +fi + +docker build --rm --force-rm -t kubernetes/guestbook-build . +docker run --rm -v "${DOCKER_BIN}:/usr/local/bin/docker" \ + -v "/var/run/docker.sock:/var/run/docker.sock" \ + -ti --name guestbook-build kubernetes/guestbook-build diff --git a/examples/guestbook-go/_src/script/clean.sh b/examples/guestbook-go/_src/script/clean.sh new file mode 100755 index 00000000000..957d2312d62 --- /dev/null +++ b/examples/guestbook-go/_src/script/clean.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Copyright 2014 Google Inc. All rights reserved. +# +# 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. + +# Usage: ./script/clean.sh + +set -o errexit +set -o nounset +set -o pipefail + +docker rm -f guestbook-build 2> /dev/null || true +docker rmi -f kubernetes/guestbook-build || true +docker rmi -f kubernetes/guestbook || true diff --git a/examples/guestbook-go/_src/script/push.sh b/examples/guestbook-go/_src/script/push.sh new file mode 100755 index 00000000000..533573dbc3c --- /dev/null +++ b/examples/guestbook-go/_src/script/push.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Copyright 2014 Google Inc. All rights reserved. +# +# 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. + +# Usage: ./script/push.sh [TAG] + +set -o errexit +set -o nounset +set -o pipefail + +guestbook_version=${1:-latest} +docker push "kubernetes/guestbook:${guestbook_version}" diff --git a/examples/guestbook-go/_src/script/release.sh b/examples/guestbook-go/_src/script/release.sh new file mode 100755 index 00000000000..7fc66e39008 --- /dev/null +++ b/examples/guestbook-go/_src/script/release.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Copyright 2014 Google Inc. All rights reserved. +# +# 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. + +# Usage: ./script/release.sh [TAG] + +set -o errexit +set -o nounset +set -o pipefail + +base_dir=$(dirname "$0") +base_dir=$(cd "${base_dir}" && pwd) + +guestbook_version=${1:-latest} + +echo " ---> Cleaning up before building..." +"${base_dir}/clean.sh" 2> /dev/null + +echo " ---> Building..." +"${base_dir}/build.sh" + +echo " ---> Pushing kubernetes/guestbook:${guestbook_version}..." +"${base_dir}/push.sh" "${guestbook_version}" + +echo " ---> Cleaning up..." +"${base_dir}/clean.sh" + +echo " ---> Done." diff --git a/examples/guestbook-go/guestbook-controller.json b/examples/guestbook-go/guestbook-controller.json index 79f178d6cdc..686104832f0 100644 --- a/examples/guestbook-go/guestbook-controller.json +++ b/examples/guestbook-go/guestbook-controller.json @@ -11,9 +11,9 @@ "version": "v1beta1", "id": "guestbook-controller", "containers": [{ - "image": "gurpartap/guestbook-example", - "name": "php-redis", - "ports": [{ "containerPort": 3000, "hostPort": 3000 }] + "image": "kubernetes/guestbook", + "name": "guestbook", + "ports": [{ "name": "http-server", "containerPort": 3000 }] }], } }, diff --git a/examples/guestbook-go/guestbook-service.json b/examples/guestbook-go/guestbook-service.json index a809c89efd2..3fb4212fb6c 100644 --- a/examples/guestbook-go/guestbook-service.json +++ b/examples/guestbook-go/guestbook-service.json @@ -3,5 +3,6 @@ "kind": "Service", "id": "guestbook", "port": 3000, + "containerPort": "http-server", "selector": { "name": "guestbook" } } diff --git a/examples/guestbook-go/redis-master-pod.json b/examples/guestbook-go/redis-master-pod.json index 8b80720cd63..1633cf359a7 100644 --- a/examples/guestbook-go/redis-master-pod.json +++ b/examples/guestbook-go/redis-master-pod.json @@ -7,11 +7,11 @@ "version": "v1beta1", "id": "redis-master-pod", "containers": [{ - "name": "master", + "name": "redis-master", "image": "gurpartap/redis", - "ports": [{ "containerPort": 6379, "hostPort": 6379 }] + "ports": [{ "name": "redis-server", "containerPort": 6379 }] }] } }, - "labels": { "name": "redis-master" } + "labels": { "name": "redis", "role": "master" } } diff --git a/examples/guestbook-go/redis-master-service.json b/examples/guestbook-go/redis-master-service.json index a6fc6064679..f635b578c01 100644 --- a/examples/guestbook-go/redis-master-service.json +++ b/examples/guestbook-go/redis-master-service.json @@ -1,7 +1,8 @@ { "apiVersion": "v1beta1", "kind": "Service", - "id": "redis-master-service", + "id": "redis-master", "port": 6379, - "selector": { "name": "redis-master" } + "containerPort": "redis-server", + "selector": { "name": "redis", "role": "master" } } diff --git a/examples/guestbook-go/redis-slave-controller.json b/examples/guestbook-go/redis-slave-controller.json index 4955bc6876c..d117f0dc411 100644 --- a/examples/guestbook-go/redis-slave-controller.json +++ b/examples/guestbook-go/redis-slave-controller.json @@ -4,7 +4,7 @@ "id": "redis-slave-controller", "desiredState": { "replicas": 2, - "replicaSelector": { "name": "redis-slave" }, + "replicaSelector": { "name": "redis", "role": "slave" }, "podTemplate": { "desiredState": { "manifest": { @@ -13,13 +13,13 @@ "containers": [{ "name": "redis-slave", "image": "gurpartap/redis", - "command": ["redis-server", "/etc/redis/redis.conf", "--slaveof", "$SERVICE_HOST", "$REDIS_MASTER_SERVICE_SERVICE_PORT"], - "ports": [{ "containerPort": 6379, "hostPort": 6379 }] + "command": ["sh", "-c", "redis-server /etc/redis/redis.conf --slaveof $SERVICE_HOST $REDIS_MASTER_SERVICE_PORT"], + "ports": [{ "name": "redis-server", "containerPort": 6379 }] }] } }, - "labels": { "name": "redis-slave" } + "labels": { "name": "redis", "role": "slave" } } }, - "labels": { "name": "redis-slave" } + "labels": { "name": "redis", "role": "slave" } } diff --git a/examples/guestbook-go/redis-slave-service.json b/examples/guestbook-go/redis-slave-service.json index aefbb05c2db..243b2675050 100644 --- a/examples/guestbook-go/redis-slave-service.json +++ b/examples/guestbook-go/redis-slave-service.json @@ -1,8 +1,9 @@ { "apiVersion": "v1beta1", "kind": "Service", - "id": "redis-slave-service", + "id": "redis-slave", "port": 6379, - "labels": { "name": "redis-slave" }, - "selector": { "name": "redis-slave" } + "containerPort": "redis-server", + "labels": { "name": "redis", "role": "slave" }, + "selector": { "name": "redis", "role": "slave" } } diff --git a/examples/guestbook-go/src/Dockerfile b/examples/guestbook-go/src/Dockerfile deleted file mode 100644 index 9b758281aba..00000000000 --- a/examples/guestbook-go/src/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM google/golang:latest - -RUN go get github.com/Gurpartap/guestbook-example && \ - cp /gopath/src/github.com/Gurpartap/guestbook-example/busybox-image/Dockerfile /gopath - -CMD docker build --rm --force-rm -t gurpartap/guestbook-example /gopath diff --git a/examples/guestbook-go/src/busybox-image/Dockerfile b/examples/guestbook-go/src/busybox-image/Dockerfile deleted file mode 100644 index 9cb481b5536..00000000000 --- a/examples/guestbook-go/src/busybox-image/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM busybox:ubuntu-14.04 - -ADD ./bin/guestbook-example /app/guestbook-example -ADD ./src/github.com/Gurpartap/guestbook-example/public/index.html /app/public/index.html -ADD ./src/github.com/Gurpartap/guestbook-example/public/script.js /app/public/script.js -ADD ./src/github.com/Gurpartap/guestbook-example/public/style.css /app/public/style.css - -WORKDIR /app -CMD ["./guestbook-example"] -EXPOSE 3000 diff --git a/examples/guestbook-go/src/busybox-image/script/build.sh b/examples/guestbook-go/src/busybox-image/script/build.sh deleted file mode 100755 index 01e6c7bcb22..00000000000 --- a/examples/guestbook-go/src/busybox-image/script/build.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -# Usage: ./build.sh - -docker build --rm --force-rm -t gurpartap/guestbook-example-build . -docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \ - -v $(which docker):/usr/local/bin/docker \ - -ti --name guestbook-example-build gurpartap/guestbook-example-build diff --git a/examples/guestbook-go/src/busybox-image/script/clean.sh b/examples/guestbook-go/src/busybox-image/script/clean.sh deleted file mode 100755 index 0b43c36496d..00000000000 --- a/examples/guestbook-go/src/busybox-image/script/clean.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -# Usage: ./clean.sh - -docker rm -f guestbook-example-build 2> /dev/null -docker rmi -f gurpartap/guestbook-example-build -docker rmi -f gurpartap/guestbook-example diff --git a/examples/guestbook-go/src/busybox-image/script/push.sh b/examples/guestbook-go/src/busybox-image/script/push.sh deleted file mode 100755 index 4fb56af82e8..00000000000 --- a/examples/guestbook-go/src/busybox-image/script/push.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# Usage: ./push.sh [TAG] - -docker push gurpartap/guestbook-example:${1:-latest} diff --git a/examples/guestbook-go/src/busybox-image/script/release.sh b/examples/guestbook-go/src/busybox-image/script/release.sh deleted file mode 100755 index 9b9aec847c3..00000000000 --- a/examples/guestbook-go/src/busybox-image/script/release.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# Usage: ./release.sh [TAG] - -set +e - -DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) - -echo " ---> Building..." -sh $DIR/build.sh - -echo " ---> Pushing gurpartap/guestbook-example:${1:-latest}..." -sh $DIR/push.sh $1 - -echo " ---> Cleaning up..." -sh $DIR/clean.sh - -echo " ---> Done." diff --git a/examples/guestbook-go/src/main.go b/examples/guestbook-go/src/main.go deleted file mode 100644 index 098feaa176d..00000000000 --- a/examples/guestbook-go/src/main.go +++ /dev/null @@ -1,104 +0,0 @@ -package main - -import ( - "encoding/json" - "net/http" - "os" - "strings" - - "github.com/codegangsta/negroni" - "github.com/gorilla/mux" - "github.com/xyproto/simpleredis" -) - -var pool *simpleredis.ConnectionPool - -func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { - members, err := simpleredis.NewList(pool, mux.Vars(req)["key"]).GetAll() - if err != nil { - panic(err) - } - - membersJSON, err := json.MarshalIndent(members, "", " ") - if err != nil { - panic(err) - } - - rw.WriteHeader(200) - rw.Header().Set("Content-Type", "application/json") - rw.Write([]byte(membersJSON)) -} - -func ListPushHandler(rw http.ResponseWriter, req *http.Request) { - set := simpleredis.NewList(pool, mux.Vars(req)["key"]) - err := set.Add(mux.Vars(req)["value"]) - if err != nil { - panic(err) - } - - members, err := set.GetAll() - if err != nil { - panic(err) - } - - membersJSON, err := json.MarshalIndent(members, "", " ") - if err != nil { - panic(err) - } - - rw.WriteHeader(200) - rw.Header().Set("Content-Type", "application/json") - rw.Write([]byte(membersJSON)) -} - -func InfoHandler(rw http.ResponseWriter, req *http.Request) { - info, err := pool.Get(0).Do("INFO") - if err != nil { - panic(err) - } - - infoString := string(info.([]uint8)) - - rw.WriteHeader(200) - rw.Write([]byte(infoString)) -} - -func EnvHandler(rw http.ResponseWriter, req *http.Request) { - getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string { - items := make(map[string]string) - for _, item := range data { - key, val := getkeyval(item) - items[key] = val - } - return items - } - environment := getenvironment(os.Environ(), func(item string) (key, val string) { - splits := strings.Split(item, "=") - key = splits[0] - val = strings.Join(splits[1:], "=") - return - }) - - envJSON, err := json.MarshalIndent(environment, "", " ") - if err != nil { - panic(err) - } - - rw.WriteHeader(200) - rw.Write([]byte(envJSON)) -} - -func main() { - pool = simpleredis.NewConnectionPoolHost(os.Getenv("SERVICE_HOST") + ":" + os.Getenv("REDIS_MASTER_SERVICE_SERVICE_PORT")) - defer pool.Close() - - r := mux.NewRouter() - r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler) - r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler) - r.Path("/info").Methods("GET").HandlerFunc(InfoHandler) - r.Path("/env").Methods("GET").HandlerFunc(EnvHandler) - - n := negroni.Classic() - n.UseHandler(r) - n.Run(":3000") -} diff --git a/examples/guestbook-go/src/public/index.html b/examples/guestbook-go/src/public/index.html deleted file mode 100644 index e1301d79796..00000000000 --- a/examples/guestbook-go/src/public/index.html +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - Guestbook - - - -
-
-
-

Waiting for database connection...

-
-
-
-
-
-
-
- - Submit -
-
-
-
- - - - diff --git a/examples/guestbook-go/src/public/script.js b/examples/guestbook-go/src/public/script.js deleted file mode 100644 index 2383a791acc..00000000000 --- a/examples/guestbook-go/src/public/script.js +++ /dev/null @@ -1,34 +0,0 @@ -$( document ).ready(function() { - - appendGuestbookEntries = function( data ) { - $( "#guestbook-entries" ).empty(); - $.each( data, function( key, val ) { - $( "#guestbook-entries" ).append( "

" + val + "

" ); - }); - } - - handleSubmission = function() { - value = $( "#guestbook-entry-content" ).val() - if (value.length > 0) { - $( "#guestbook-entries" ).append( "

...

" ); - $.getJSON( "rpush/guestbook/" + value, appendGuestbookEntries); - } - return false; - } - - // Event handlers. - $( "#guestbook-submit" ).click(handleSubmission); - $( "#guestbook-entry-content" ).keypress(function (e) { - if (e.which == 13) { - return handleSubmission(); - } - }); - - // Poll every second. - (function fetchGuestbook(){ - - $.getJSON("lrange/guestbook").done(appendGuestbookEntries).always(function() { setTimeout(fetchGuestbook, 1000); }); - - })(); - -}); diff --git a/examples/guestbook-go/src/public/style.css b/examples/guestbook-go/src/public/style.css deleted file mode 100644 index 20e25aac27d..00000000000 --- a/examples/guestbook-go/src/public/style.css +++ /dev/null @@ -1,107 +0,0 @@ -html, body { - background: #EEE; } - -html, button, input, select, textarea, -.pure-g [class*="pure-u"], -.pure-g-r [class*="pure-u"] { - font-family: "Helvetica", sans-serif; - color: #585A5C; - line-height: 1.4; - position: relative; } - -h1, h2, p, a, .pure-button { - margin: 0; - color: #585A5C; - font-weight: 400; } - h1 strong, h2 strong, h3 strong, p strong, li strong, a strong, .pure-button strong { - font-weight: 700; } - -h1 { - font-size: 5em; - font-weight: 300; - color: #585A5C; } - -h2 { - font-size: 2.25em; - font-weight: 300; - color: #585A5C; } - -p { - font-size: 1.2em; - line-height: 1.7; } - -input { - font-size: 1.125em; - outline: none; } - -.default-button { - position: relative; - font-weight: 400; - padding: 0.75em 2em; - white-space: normal; - background: #1699e3; - border-radius: 1000px; - font-size: 1.25em; - color: #FFF; - text-transform: uppercase; } - .default-button strong { - font-weight: 700; } - -.section { - position: relative; - zoom: 1; - width: 100%; - overflow: hidden; } - -.container { - position: relative; - overflow: hidden; - margin: 0 auto; - padding: 0 1em; - max-width: 64em; - text-align: center; } - -.section-text-container { - max-width: 48em; - margin: 0 auto; } - -.color-overlay { - position: absolute; - width: 100%; - height: 100%; - top: 0; - left: 0; - opacity: 0.9; - -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=" 90 ")"; - filter: alpha(opacity=90); - zoom: 1; } - -#header { - padding: 2em 0 0 0; } - #header h1, #header h2, #header a { - color: #585A5C; } - #header .color-overlay { - background: #eee; } - #header .container { - overflow: visible; } - -#content { - background: #eee; - padding: 1em 0; } - #content form { - text-align: center; - margin: 3em auto 0 auto; - max-width: 20em; } - #content input { - font-size: 1.25em; - display: block; - width: 80%; - padding: 0.75em 10%; - margin-bottom: 1em; - border-radius: 1000px; - text-align: center; - border: 0; - box-shadow: inset 0 0 0 2px #ccc; - -webkit-appearance: none; } - #content button { - width: 100%; }