Skip to content

Commit

Permalink
Merge pull request kubernetes#12497 from amygdala/gb2
Browse files Browse the repository at this point in the history
Auto commit by PR queue bot
  • Loading branch information
k8s-merge-robot committed Sep 9, 2015
2 parents 5a01a76 + 3574999 commit c8526ad
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 26 deletions.
56 changes: 47 additions & 9 deletions examples/guestbook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<pod_name>`. E.g., for the redis master, you should see something like the following (your pod name will be different):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
<?
set_include_path('.:/usr/share/php:/usr/share/pear:/vendor/predis');
set_include_path('.:/usr/local/lib/php');
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'predis/autoload.php';
require 'Predis/Autoloader.php';
Predis\Autoloader::register();
if (isset($_GET['cmd']) === true) {
$host = 'redis-master';
if (getenv('GET_HOSTS_FROM') == 'env') {
$host = getenv('REDIS_MASTER_SERVICE_HOST');
}
header('Content-Type: application/json');
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => '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,
]);
Expand Down
10 changes: 9 additions & 1 deletion examples/guestbook/frontend-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 8 additions & 5 deletions examples/guestbook/php-redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions examples/guestbook/php-redis/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}));
Expand All @@ -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(",");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
<?

set_include_path('.:/usr/share/php:/usr/share/pear:/vendor/predis');
set_include_path('.:/usr/local/lib/php');

error_reporting(E_ALL);
ini_set('display_errors', 1);

require 'predis/autoload.php';
require 'Predis/Autoloader.php';

Predis\Autoloader::register();

if (isset($_GET['cmd']) === true) {
$host = 'redis-master';
if (getenv('GET_HOSTS_FROM') == 'env') {
$host = getenv('REDIS_MASTER_SERVICE_HOST');
}
header('Content-Type: application/json');
if ($_GET['cmd'] == 'set') {
$client = new Predis\Client([
'scheme' => '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,
]);

Expand Down
2 changes: 1 addition & 1 deletion examples/guestbook/php-redis/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="controllers.js"></script>
<script src="ui-bootstrap-tpls-0.10.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.0/ui-bootstrap-tpls.js"></script>
</head>
<body ng-controller="RedisCtrl">
<div style="width: 50%; margin-left: 20px">
Expand Down
10 changes: 9 additions & 1 deletion examples/guestbook/redis-slave-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion examples/guestbook/redis-slave/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/e2e/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,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).
Expand Down

0 comments on commit c8526ad

Please sign in to comment.