From 3574999fa34d54c47f43efd9eaff7e1c571c7910 Mon Sep 17 00:00:00 2001 From: Amy Unruh Date: Mon, 10 Aug 2015 11:00:44 -0700 Subject: [PATCH] Use GCR images from 'google-samples' project; allow switch on whether dns service is supported, or to use env vars to get service host info. Test change to reflect php filename change. --- examples/guestbook/README.md | 56 ++++++++++++++++--- examples/guestbook/frontend-controller.yaml | 10 +++- examples/guestbook/php-redis/Dockerfile | 13 +++-- examples/guestbook/php-redis/controllers.js | 4 +- .../php-redis/{index.php => guestbook.php} | 20 +++++-- examples/guestbook/php-redis/index.html | 2 +- .../guestbook/redis-slave-controller.yaml | 10 +++- examples/guestbook/redis-slave/run.sh | 6 +- test/e2e/kubectl.go | 2 +- 9 files changed, 97 insertions(+), 26 deletions(-) rename examples/guestbook/php-redis/{index.php => guestbook.php} (58%) diff --git a/examples/guestbook/README.md b/examples/guestbook/README.md index c93cbece6b4b8..f6e7102370556 100644 --- a/examples/guestbook/README.md +++ b/examples/guestbook/README.md @@ -136,6 +136,12 @@ redis-master-dz33o 1/1 Running 0 2h (Note that an initial `docker pull` to grab a container image may take a few minutes, depending on network conditions. A pod will be reported as `Pending` while its image is being downloaded.) +`kubectl get pods` will show only the pods in the default [namespace](../../docs/user-guide/namespaces.md). To see pods in all namespaces, run: + +``` +kubectl get pods -o wide --all-namespaces=true +``` + #### Optional Interlude You can get information about a pod, including the machine that it is running on, via `kubectl describe pods/`. E.g., for the redis master, you should see something like the following (your pod name will be different): @@ -256,9 +262,15 @@ Kubernetes supports two primary modes of finding a service— environment variab The services in a Kubernetes cluster are discoverable inside other containers [via environment variables](../../docs/user-guide/services.md#environment-variables). An alternative is to use the [cluster's DNS service](../../docs/user-guide/services.md#dns), if it has been enabled for the cluster. This lets all pods do name resolution of services automatically, based on the service name. -We'll use the DNS service for this example. E.g., you can see the service name, `redis-master`, accessed as a `host` value in the PHP script in [Step 5](#step-five-create-the-frontend-replicated-pods). -**Note**: **If your cluster does not have the DNS service enabled, then this example will not work out of the box.** You will need to edit `examples/guestbook/php-redis/index.php` to use environment variables for service discovery instead, then rebuild the container image from the `Dockerfile` in that directory. (However, this is unlikely to be necessary. You can check for the DNS service in the list of the clusters' services.) +This example has been configured to use the DNS service by default. + +If your cluster does not have the DNS service enabled, then you can use environment variables by setting the +`GET_HOSTS_FROM` env value in both +`examples/guestbook/redis-slave-controller.yaml` and `examples/guestbook/frontend-controller.yaml` +from `dns` to `env` before you start up the app. +(However, this is unlikely to be necessary. You can check for the DNS service in the list of the clusters' services by +running `kubectl --namespace=kube-system get rc`, and looking for a controller prefixed `kube-dns`.) ### Step Three: Fire up the replicated slave pods @@ -291,7 +303,15 @@ spec: spec: containers: - name: worker - image: kubernetes/redis-slave:v2 + image: gcr.io/google_samples/gb-redisslave:v1 + env: + - name: GET_HOSTS_FROM + value: dns + # If your cluster config does not include a dns service, then to + # instead access an environment variable to find the master + # service's host, comment out the 'value: dns' line above, and + # uncomment the line below. + # value: env ports: - containerPort: 6379 ``` @@ -393,7 +413,15 @@ spec: spec: containers: - name: php-redis - image: gcr.io/google_containers/example-guestbook-php-redis:v3 + image: gcr.io/google_samples/gb-frontend:v2 + env: + - name: GET_HOSTS_FROM + value: dns + # If your cluster config does not include a dns service, then to + # instead access environment variables to find service host + # info, comment out the 'value: dns' line above, and uncomment the + # line below. + # value: env ports: - containerPort: 80 ``` @@ -435,33 +463,43 @@ redis-slave-iqkhy 1/1 Running 0 2h You should see a single redis master pod, two redis slaves, and three frontend pods. -The code for the PHP server that the frontends are running looks like this: +The code for the PHP server that the frontends are running is in `guestbook/php-redis/guestbook.php`. It looks like this: ```php 'tcp', - 'host' => 'redis-master', + 'host' => $host, 'port' => 6379, ]); $client->set($_GET['key'], $_GET['value']); print('{"message": "Updated"}'); } else { + $host = 'redis-slave'; + if (getenv('GET_HOSTS_FROM') == 'env') { + $host = getenv('REDIS_SLAVE_SERVICE_HOST'); + } $client = new Predis\Client([ 'scheme' => 'tcp', - 'host' => 'redis-slave', + 'host' => $host, 'port' => 6379, ]); diff --git a/examples/guestbook/frontend-controller.yaml b/examples/guestbook/frontend-controller.yaml index c5e666f058a23..ae8d24986bcce 100644 --- a/examples/guestbook/frontend-controller.yaml +++ b/examples/guestbook/frontend-controller.yaml @@ -15,6 +15,14 @@ spec: spec: containers: - name: php-redis - image: gcr.io/google_containers/example-guestbook-php-redis:v3 + image: gcr.io/google_samples/gb-frontend:v2 + env: + - name: GET_HOSTS_FROM + value: dns + # If your cluster config does not include a dns service, then to + # instead access environment variables to find service host + # info, comment out the 'value: dns' line above, and uncomment the + # line below. + # value: env ports: - containerPort: 80 diff --git a/examples/guestbook/php-redis/Dockerfile b/examples/guestbook/php-redis/Dockerfile index 3cf7c2cfa20f8..093cd7cbdc0f7 100644 --- a/examples/guestbook/php-redis/Dockerfile +++ b/examples/guestbook/php-redis/Dockerfile @@ -1,7 +1,10 @@ -FROM brendanburns/php +FROM php:5-apache -ADD index.php /var/www/index.php -ADD controllers.js /var/www/controllers.js -ADD index.html /var/www/index.html +RUN apt-get update +RUN apt-get install -y php-pear +RUN pear channel-discover pear.nrk.io +RUN pear install nrk/Predis -CMD /run.sh +ADD guestbook.php /var/www/html/guestbook.php +ADD controllers.js /var/www/html/controllers.js +ADD index.html /var/www/html/index.html diff --git a/examples/guestbook/php-redis/controllers.js b/examples/guestbook/php-redis/controllers.js index 48481fd5d4676..1e4b55042469a 100644 --- a/examples/guestbook/php-redis/controllers.js +++ b/examples/guestbook/php-redis/controllers.js @@ -9,7 +9,7 @@ RedisController.prototype.onRedis = function() { this.scope_.messages.push(this.scope_.msg); this.scope_.msg = ""; var value = this.scope_.messages.join(); - this.http_.get("index.php?cmd=set&key=messages&value=" + value) + this.http_.get("guestbook.php?cmd=set&key=messages&value=" + value) .success(angular.bind(this, function(data) { this.scope_.redisResponse = "Updated."; })); @@ -21,7 +21,7 @@ redisApp.controller('RedisCtrl', function ($scope, $http, $location) { $scope.controller.location_ = $location; $scope.controller.http_ = $http; - $scope.controller.http_.get("index.php?cmd=get&key=messages") + $scope.controller.http_.get("guestbook.php?cmd=get&key=messages") .success(function(data) { console.log(data); $scope.messages = data.data.split(","); diff --git a/examples/guestbook/php-redis/index.php b/examples/guestbook/php-redis/guestbook.php similarity index 58% rename from examples/guestbook/php-redis/index.php rename to examples/guestbook/php-redis/guestbook.php index 18bff077579fe..2ea63f0a0b0bf 100644 --- a/examples/guestbook/php-redis/index.php +++ b/examples/guestbook/php-redis/guestbook.php @@ -1,27 +1,37 @@ 'tcp', - 'host' => 'redis-master', + 'host' => $host, 'port' => 6379, ]); - + $client->set($_GET['key'], $_GET['value']); print('{"message": "Updated"}'); } else { + $host = 'redis-slave'; + if (getenv('GET_HOSTS_FROM') == 'env') { + $host = getenv('REDIS_SLAVE_SERVICE_HOST'); + } $client = new Predis\Client([ 'scheme' => 'tcp', - 'host' => 'redis-slave', + 'host' => $host, 'port' => 6379, ]); diff --git a/examples/guestbook/php-redis/index.html b/examples/guestbook/php-redis/index.html index fe457984874b3..4ffb4ed2ab587 100644 --- a/examples/guestbook/php-redis/index.html +++ b/examples/guestbook/php-redis/index.html @@ -4,7 +4,7 @@ - +
diff --git a/examples/guestbook/redis-slave-controller.yaml b/examples/guestbook/redis-slave-controller.yaml index 74fec7c02508f..6e5dde18aa764 100644 --- a/examples/guestbook/redis-slave-controller.yaml +++ b/examples/guestbook/redis-slave-controller.yaml @@ -15,6 +15,14 @@ spec: spec: containers: - name: worker - image: kubernetes/redis-slave:v2 + image: gcr.io/google_samples/gb-redisslave:v1 + env: + - name: GET_HOSTS_FROM + value: dns + # If your cluster config does not include a dns service, then to + # instead access an environment variable to find the master + # service's host, comment out the 'value: dns' line above, and + # uncomment the line below. + # value: env ports: - containerPort: 6379 diff --git a/examples/guestbook/redis-slave/run.sh b/examples/guestbook/redis-slave/run.sh index bf48f27c0158f..9f79ccef17a58 100755 --- a/examples/guestbook/redis-slave/run.sh +++ b/examples/guestbook/redis-slave/run.sh @@ -14,4 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -redis-server --slaveof redis-master 6379 +if [[ ${GET_HOSTS_FROM:-dns} == "env" ]]; then + redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379 +else + redis-server --slaveof redis-master 6379 +fi diff --git a/test/e2e/kubectl.go b/test/e2e/kubectl.go index 82c649e7d3bbc..26dd7d8f9d087 100644 --- a/test/e2e/kubectl.go +++ b/test/e2e/kubectl.go @@ -746,7 +746,7 @@ func makeRequestToGuestbook(c *client.Client, cmd, value string, ns string) (str Namespace(ns). Resource("services"). Name("frontend"). - Suffix("/index.php"). + Suffix("/guestbook.php"). Param("cmd", cmd). Param("key", "messages"). Param("value", value).