mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Updated guestbook based on suggestions in the pull request
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
This commit is contained in:
parent
13f79b00a0
commit
b7d1b1ac3c
174
examples/guestbook-go/README.md
Normal file
174
examples/guestbook-go/README.md
Normal file
@ -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://<host-ip>: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
|
||||
```
|
9
examples/guestbook-go/_src/Dockerfile
Normal file
9
examples/guestbook-go/_src/Dockerfile
Normal file
@ -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/
|
42
examples/guestbook-go/_src/README.md
Normal file
42
examples/guestbook-go/_src/README.md
Normal file
@ -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
|
10
examples/guestbook-go/_src/guestbook/Dockerfile
Normal file
10
examples/guestbook-go/_src/guestbook/Dockerfile
Normal file
@ -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
|
86
examples/guestbook-go/_src/main.go
Normal file
86
examples/guestbook-go/_src/main.go
Normal file
@ -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")
|
||||
}
|
34
examples/guestbook-go/_src/public/index.html
Normal file
34
examples/guestbook-go/_src/public/index.html
Normal file
@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta charset="utf-8">
|
||||
<meta content="width=device-width" name="viewport">
|
||||
<link href="/style.css" rel="stylesheet">
|
||||
<title>Guestbook</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="header">
|
||||
<h1>Guestbook</h1>
|
||||
</div>
|
||||
|
||||
<div id="guestbook-entries">
|
||||
<p>Waiting for database connection...</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<form id="guestbook-form">
|
||||
<input autocomplete="off" id="guestbook-entry-content" type="text">
|
||||
<a href="#" id="guestbook-submit">Submit</a>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p><h2 id="guestbook-host-address"></h2></p>
|
||||
<p><a href="/env">/env</a>
|
||||
<a href="/info">/info</a></p>
|
||||
</div>
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
|
||||
<script src="/script.js"></script>
|
||||
</body>
|
||||
</html>
|
46
examples/guestbook-go/_src/public/script.js
Normal file
46
examples/guestbook-go/_src/public/script.js
Normal file
@ -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("<p>" + val + "</p>");
|
||||
});
|
||||
}
|
||||
|
||||
var handleSubmission = function(e) {
|
||||
e.preventDefault();
|
||||
var entryValue = entryContentElement.val()
|
||||
if (entryValue.length > 0) {
|
||||
entriesElement.append("<p>...</p>");
|
||||
$.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);
|
||||
});
|
||||
})();
|
||||
});
|
61
examples/guestbook-go/_src/public/style.css
Normal file
61
examples/guestbook-go/_src/public/style.css
Normal file
@ -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;
|
||||
}
|
35
examples/guestbook-go/_src/script/build.sh
Executable file
35
examples/guestbook-go/_src/script/build.sh
Executable file
@ -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
|
25
examples/guestbook-go/_src/script/clean.sh
Executable file
25
examples/guestbook-go/_src/script/clean.sh
Executable file
@ -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
|
24
examples/guestbook-go/_src/script/push.sh
Executable file
24
examples/guestbook-go/_src/script/push.sh
Executable file
@ -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}"
|
40
examples/guestbook-go/_src/script/release.sh
Executable file
40
examples/guestbook-go/_src/script/release.sh
Executable file
@ -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."
|
@ -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 }]
|
||||
}],
|
||||
}
|
||||
},
|
||||
|
@ -3,5 +3,6 @@
|
||||
"kind": "Service",
|
||||
"id": "guestbook",
|
||||
"port": 3000,
|
||||
"containerPort": "http-server",
|
||||
"selector": { "name": "guestbook" }
|
||||
}
|
||||
|
@ -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" }
|
||||
}
|
||||
|
@ -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" }
|
||||
}
|
||||
|
@ -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" }
|
||||
}
|
||||
|
@ -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" }
|
||||
}
|
||||
|
@ -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
|
@ -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
|
@ -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
|
@ -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
|
@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
# Usage: ./push.sh [TAG]
|
||||
|
||||
docker push gurpartap/guestbook-example:${1:-latest}
|
@ -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."
|
@ -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")
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width">
|
||||
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
|
||||
<link rel="stylesheet" href="/style.css">
|
||||
<title>Guestbook</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="section" id="header">
|
||||
<div class="color-overlay"></div>
|
||||
<div class="container">
|
||||
<h1>Guestbook</h1>
|
||||
<h2><script type="text/javascript">document.write(document.URL);</script></h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="content">
|
||||
<div class="container">
|
||||
<div class="section-text-container" id="guestbook-entries">
|
||||
<p>Waiting for database connection...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section" id="content">
|
||||
<div class="container">
|
||||
<div class="section-text-container">
|
||||
<form>
|
||||
<input id="guestbook-entry-content" required="true" autocomplete="off" type="text" placeholder="" value="">
|
||||
<a class="pure-button default-button" id="guestbook-submit" href="#">Submit</a>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
|
||||
<script src="/script.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,34 +0,0 @@
|
||||
$( document ).ready(function() {
|
||||
|
||||
appendGuestbookEntries = function( data ) {
|
||||
$( "#guestbook-entries" ).empty();
|
||||
$.each( data, function( key, val ) {
|
||||
$( "#guestbook-entries" ).append( "<p>" + val + "</p>" );
|
||||
});
|
||||
}
|
||||
|
||||
handleSubmission = function() {
|
||||
value = $( "#guestbook-entry-content" ).val()
|
||||
if (value.length > 0) {
|
||||
$( "#guestbook-entries" ).append( "<p>...</p>" );
|
||||
$.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); });
|
||||
|
||||
})();
|
||||
|
||||
});
|
@ -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%; }
|
Loading…
Reference in New Issue
Block a user