diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000000..6736efd943 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,35 @@ +# How to become a contributor and submit your own code + +## Contributor License Agreements + +We'd love to accept your sample apps and patches! Before we can take them, we +have to jump a couple of legal hurdles. + +Please fill out either the individual or corporate Contributor License Agreement +(CLA). + + * If you are an individual writing original source code and you're sure you + own the intellectual property, then you'll need to sign an [individual CLA] + (https://developers.google.com/open-source/cla/individual). + * If you work for a company that wants to allow you to contribute your work, + then you'll need to sign a [corporate CLA] + (https://developers.google.com/open-source/cla/corporate). + +Follow either of the two links above to access the appropriate CLA and +instructions for how to sign and return it. Once we receive it, we'll be able to +accept your pull requests. + +## Contributing A Patch + +1. Submit an issue describing your proposed change to the repo in question. +1. The repo owner will respond to your issue promptly. +1. If your proposed change is accepted, and you haven't already done so, sign a + Contributor License Agreement (see details above). +1. Fork the desired repo, develop and test your code changes. +1. Ensure that your code adheres to the existing style in the sample to which + you are contributing. Refer to the + [Google Cloud Platform Samples Style Guide] + (https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the + recommended coding standards for this organization. +1. Ensure that your code has an appropriate set of unit tests which all pass. +1. Submit a pull request. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/README.md b/README.md index e69de29bb2..e3018fe495 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,12 @@ +# Container Engine Samples + +Kubernetes samples for +[Google Container Engine](https://cloud.google.com/container-engine/). + +## Contributing changes + +* See [CONTRIBUTING.md](CONTRIBUTING.md) + +## Licensing + +* See [LICENSE](LICENSE) diff --git a/guestbook/README.md b/guestbook/README.md new file mode 100644 index 0000000000..8baa002e38 --- /dev/null +++ b/guestbook/README.md @@ -0,0 +1,685 @@ +## Guestbook Example + +This example shows how to build a simple, multi-tier web application using Kubernetes and [Docker](https://www.docker.com/). + +**Table of Contents** + + + - [Guestbook Example](#guestbook-example) + - [Prerequisites](#prerequisites) + - [Quick Start](#quick-start) + - [Step One: Start up the redis master](#step-one-start-up-the-redis-master) + - [Define a replication controller](#define-a-replication-controller) + - [Define a service](#define-a-service) + - [Create a service](#create-a-service) + - [Finding a service](#finding-a-service) + - [Create a replication controller](#create-a-replication-controller) + - [Optional Interlude](#optional-interlude) + - [Step Two: Start up the redis slave](#step-two-start-up-the-redis-slave) + - [Step Three: Start up the guestbook frontend](#step-three-start-up-the-guestbook-frontend) + - [Using 'type: LoadBalancer' for the frontend service (cloud-provider-specific)](#using-type-loadbalancer-for-the-frontend-service-cloud-provider-specific) + - [Step Four: Cleanup](#step-four-cleanup) + - [Troubleshooting](#troubleshooting) + - [Appendix: Accessing the guestbook site externally](#appendix-accessing-the-guestbook-site-externally) + - [Google Compute Engine External Load Balancer Specifics](#google-compute-engine-external-load-balancer-specifics) + + + +The example consists of: + +- A web frontend +- A [redis](http://redis.io/) master (for storage), and a replicated set of redis 'slaves'. + +The web frontend interacts with the redis master via javascript redis API calls. + +**Note**: If you are running this example on a [Google Container Engine](https://cloud.google.com/container-engine/) installation, see [this Container Engine guestbook walkthrough](https://cloud.google.com/container-engine/docs/tutorials/guestbook) instead. The basic concepts are the same, but the walkthrough is tailored to a Container Engine setup. + +### Prerequisites + +This example requires a running Kubernetes cluster. See the [Getting Started guides](../../docs/getting-started-guides/) for how to get started. And follow the [Prerequisites](../../docs/user-guide/prereqs.md) to make sure your `kubectl` is ok. As noted above, if you have a Google Container Engine cluster set up, go [here](https://cloud.google.com/container-engine/docs/tutorials/guestbook) instead. + +### Quick Start + +This section shows the simplest way to get the example work. If you want to know the details, you should skip this and read [the rest of the example](#step-one-start-up-the-redis-master). + +Start the guestbook with one command: + +```console +$ kubectl create -f examples/guestbook/all-in-one/guestbook-all-in-one.yaml +service "redis-master" created +replicationcontroller "redis-master" created +service "redis-slave" created +replicationcontroller "redis-slave" created +service "frontend" created +replicationcontroller "frontend" created +``` + +You also can start the guestbook by running: + +```console +$ kubectl create -f examples/guestbook/ +``` + +Then, list all your services: + +```console +$ kubectl get services +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE +frontend 10.0.93.211 80/TCP app=guestbook,tier=frontend 1h +redis-master 10.0.136.3 6379/TCP app=redis,role=master,tier=backend 1h +redis-slave 10.0.21.92 6379/TCP app=redis,role=slave,tier=backend 1h +``` + +Now you can access the guestbook on each node with frontend service's `:Port`, e.g. `10.0.93.211:80` in this guide. `` is a cluster-internal IP. If you want to access the guestbook from outside of the cluster, add `type: NodePort` to the frontend service `spec` field. Then you can access the guestbook with `:NodePort` from outside of the cluster. On cloud providers which support external load balancers, setting the type field to `type: LoadBalancer` will provision a load balancer for your service. There are several ways for you to access the guestbook. You may learn from [Accessing services running on the cluster](../../docs/user-guide/accessing-the-cluster.md#accessing-services-running-on-the-cluster). + +Clean up the guestbook: + +```console +$ kubectl delete -f examples/guestbook/all-in-one/guestbook-all-in-one.yaml +``` + +or + +```console +$ kubectl delete -f examples/guestbook/ +``` + + +### Step One: Start up the redis master + +Before continuing to the gory details, we also recommend you to read [Quick walkthrough](../../docs/user-guide/#quick-walkthrough), [Thorough walkthough](../../docs/user-guide/#thorough-walkthrough) and [Concept guide](../../docs/user-guide/#concept-guide). +**Note**: The redis master in this example is *not* highly available. Making it highly available would be an interesting, but intricate exercise— redis doesn't actually support multi-master deployments at this point in time, so high availability would be a somewhat tricky thing to implement, and might involve periodic serialization to disk, and so on. + +#### Define a replication controller + +To start the redis master, use the file `examples/guestbook/redis-master-controller.yaml`, which describes a single [pod](../../docs/user-guide/pods.md) running a redis key-value server in a container. + +Although we have a single instance of our redis master, we are using a [replication controller](../../docs/user-guide/replication-controller.md) to enforce that exactly one pod keeps running. E.g., if the node were to go down, the replication controller will ensure that the redis master gets restarted on a healthy node. (In our simplified example, this could result in data loss.) + +The file `examples/guestbook/redis-master-controller.yaml` defines the redis master replication controller: + + + +```yaml +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-master + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: master + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 1 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: master + # tier: backend + template: + metadata: + labels: + app: redis + role: master + tier: backend + spec: + containers: + - name: master + image: gcr.io/google_containers/redis:e2e # or just image: redis + resources: + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 6379 +``` + +[Download example](redis-master-controller.yaml?raw=true) + + +#### Define a service + +A Kubernetes [service](../../docs/user-guide/services.md) is a named load balancer that proxies traffic to one or more containers. This is done using the [labels](../../docs/user-guide/labels.md) metadata that we defined in the `redis-master` pod above. As mentioned, we have only one redis master, but we nevertheless want to create a service for it. Why? Because it gives us a deterministic way to route to the single master using an elastic IP. + +Services find the pods to load balance based on the pods' labels. +The selector field of the service description determines which pods will receive the traffic sent to the service, and the `port` and `targetPort` information defines what port the service proxy will run at. + +The file `examples/guestbook/redis-master-service.yaml` defines the redis master service: + + + +```yaml +apiVersion: v1 +kind: Service +metadata: + name: redis-master + labels: + app: redis + role: master + tier: backend +spec: + ports: + # the port that this service should serve on + - port: 6379 + targetPort: 6379 + selector: + app: redis + role: master + tier: backend +``` + +[Download example](redis-master-service.yaml?raw=true) + + +#### Create a service + +According to the [config best practices](../../docs/user-guide/config-best-practices.md), create a service before corresponding replication controllers so that the scheduler can spread the pods comprising the service. So we first create the service by running: + +```console +$ kubectl create -f examples/guestbook/redis-master-service.yaml +service "redis-master" created +``` + +Then check the list of services, which should include the redis-master: + +```console +$ kubectl get services +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE +redis-master 10.0.136.3 6379/TCP app=redis,role=master,tier=backend 1h +``` + +This will cause all pods to see the redis master apparently running on `:6379`. A service can map an incoming port to any `targetPort` in the backend pod. Once created, the service proxy on each node is configured to set up a proxy on the specified port (in this case port `6379`). + +`targetPort` will default to `port` if it is omitted in the configuration. For simplicity's sake, we omit it in the following configurations. + +The traffic flow from slaves to masters can be described in two steps, like so: + + - A *redis slave* will connect to "port" on the *redis master service* + - Traffic will be forwarded from the service "port" (on the service node) to the *targetPort* on the pod that the service listens to. + +For more details, please see [Connecting applications](../../docs/user-guide/connecting-applications.md). + +#### Finding a service + +Kubernetes supports two primary modes of finding a service— environment variables and DNS. + +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. + +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 cluster's services by +running `kubectl --namespace=kube-system get rc`, and looking for a controller prefixed `kube-dns`.) +Note that switching to env causes creation-order dependencies, since services need to be created before their clients that require env vars. + +#### Create a replication controller + +Second, create the redis master pod in your Kubernetes cluster by running: + +```console +$ kubectl create -f examples/guestbook/redis-master-controller.yaml +replicationcontroller "redis-master" created +``` + +You can see the replication controllers for your cluster by running: + +```console +$ kubectl get rc +CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS +redis-master master redis app=redis,role=master,tier=backend 1 +``` + +Then, you can list the pods in the cluster, to verify that the master is running: + +```console +$ kubectl get pods +``` + +You'll see all pods in the cluster, including the redis master pod, and the status of each pod. +The name of the redis master will look similar to that in the following list: + +```console +NAME READY STATUS RESTARTS AGE +... +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 --all-namespaces +``` + +For more details, please see [Configuring containers](../../docs/user-guide/configuring-containers.md) and [Deploying applications](../../docs/user-guide/deploying-applications.md). + +#### 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): + +```console +$ kubectl describe pods/redis-master-dz33o +... +Name: redis-master-dz33o +Image(s): redis +Node: kubernetes-minion-krxw/10.240.67.201 +Labels: app=redis,role=master,tier=backend +Status: Running +Replication Controllers: redis-master (1/1 replicas created) +Containers: + master: + Image: redis + State: Running + Started: Fri, 12 Jun 2015 12:53:46 -0700 + Ready: True + Restart Count: 0 +Conditions: + Type Status + Ready True +No events. +``` + +The `Node` is the name of the machine, e.g. `kubernetes-minion-krxw` in the example above. + +If you want to view the container logs for a given pod, you can run: + +```console +$ kubectl logs +``` + +These logs will usually give you enough information to troubleshoot. + +However, if you should want to SSH to the listed host machine, you can inspect various logs there directly as well. For example, with Google Compute Engine, using `gcloud`, you can SSH like this: + +```console +me@workstation$ gcloud compute ssh kubernetes-minion-krxw +``` + +Then, you can look at the Docker containers on the remote machine. You should see something like this (the specifics of the IDs will be different): + +```console +me@kubernetes-minion-krxw:~$ sudo docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +... +0ffef9649265 redis:latest "/entrypoint.sh redi" About a minute ago Up About a minute k8s_master.869d22f3_redis-master-dz33o_default_1449a58a-5ead-11e5-a104-688f84ef8ef6_d74cb2b5 +``` + +If you want to see the logs for a given container, you can run: + +```console +$ docker logs +``` + +### Step Two: Start up the redis slave + +Now that the redis master is running, we can start up its 'read slaves'. + +We'll define these as replicated pods as well, though this time— unlike for the redis master— we'll define the number of replicas to be 2. +In Kubernetes, a replication controller (RC) is responsible for managing multiple instances of a replicated pod. The replication controller will automatically launch new pods if the number of replicas falls below the specified number. +(This particular replicated pod is a great one to test this with -- you can try killing the Docker processes for your pods directly, then watch them come back online on a new node shortly thereafter.) + +Just like the master, we want to have a service to proxy connections to the redis slaves. In this case, in addition to discovery, the slave service will provide transparent load balancing to web app clients. + +This time we put the service and RC into one [file](../../docs/user-guide/managing-deployments.md#organizing-resource-configurations). Group related objects together in a single file. This is often better than separate files. +The specification for the slaves is in `examples/guestbook/all-in-one/redis-slave.yaml`: + + + +```yaml +apiVersion: v1 +kind: Service +metadata: + name: redis-slave + labels: + app: redis + role: slave + tier: backend +spec: + ports: + # the port that this service should serve on + - port: 6379 + selector: + app: redis + role: slave + tier: backend +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-slave + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: slave + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 2 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: slave + # tier: backend + template: + metadata: + labels: + app: redis + role: slave + tier: backend + spec: + containers: + - name: slave + image: gcr.io/google_samples/gb-redisslave:v1 + resources: + requests: + cpu: 100m + memory: 100Mi + 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 +``` + +[Download example](all-in-one/redis-slave.yaml?raw=true) + + +This time the selector for the service is `app=redis,role=slave,tier=backend`, because that identifies the pods running redis slaves. It is generally helpful to set labels on your service itself as we've done here to make it easy to locate them with the `kubectl get services -l "app=redis,role=slave,tier=backend"` command. For more information on the usage of labels, see [using-labels-effectively](../../docs/user-guide/managing-deployments.md#using-labels-effectively). + +Now that you have created the specification, create the service in your cluster by running: + +```console +$ kubectl create -f examples/guestbook/all-in-one/redis-slave.yaml +service "redis-slave" created +replicationcontroller "redis-slave" created + +$ kubectl get services +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE +redis-master 10.0.136.3 6379/TCP app=redis,role=master,tier=backend 1h +redis-slave 10.0.21.92 6379/TCP app=redis,role=slave,tier=backend 1h + +$ kubectl get rc +CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS +redis-master master redis app=redis,role=master,tier=backend 1 +redis-slave slave gcr.io/google_samples/gb-redisslave:v1 app=redis,role=slave,tier=backend 2 +``` + +Once the replication controller is up, you can list the pods in the cluster, to verify that the master and slaves are running. You should see a list that includes something like the following: + +```console +$ kubectl get pods +NAME READY STATUS RESTARTS AGE +... +redis-master-dz33o 1/1 Running 0 2h +redis-slave-35mer 1/1 Running 0 2h +redis-slave-iqkhy 1/1 Running 0 2h +``` + +You should see a single redis master pod and two redis slave pods. As mentioned above, you can get more information about any pod with: `kubectl describe pods/`. And also can view the resources on [kube-ui](../../docs/user-guide/ui.md). + +### Step Three: Start up the guestbook frontend + +A frontend pod is a simple PHP server that is configured to talk to either the slave or master services, depending on whether the client request is a read or a write. It exposes a simple AJAX interface, and serves an Angular-based UX. +Again we'll create a set of replicated frontend pods instantiated by a replication controller— this time, with three replicas. + +As with the other pods, we now want to create a service to group the frontend pods. +The RC and service are described in the file `frontend.yaml`: + + + +```yaml +apiVersion: v1 +kind: Service +metadata: + name: frontend + labels: + app: guestbook + tier: frontend +spec: + # if your cluster supports it, uncomment the following to automatically create + # an external load-balanced IP for the frontend service. + # type: LoadBalancer + ports: + # the port that this service should serve on + - port: 80 + selector: + app: guestbook + tier: frontend +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: frontend + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: guestbook + tier: frontend +spec: + # this replicas value is default + # modify it according to your case + replicas: 3 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # tier: frontend + template: + metadata: + labels: + app: guestbook + tier: frontend + spec: + containers: + - name: php-redis + image: gcr.io/google_samples/gb-frontend:v3 + resources: + requests: + cpu: 100m + memory: 100Mi + 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 +``` + +[Download example](all-in-one/frontend.yaml?raw=true) + + +#### Using 'type: LoadBalancer' for the frontend service (cloud-provider-specific) + +For supported cloud providers, such as Google Compute Engine or Google Container Engine, you can specify to use an external load balancer +in the service `spec`, to expose the service onto an external load balancer IP. +To do this, uncomment the `type: LoadBalancer` line in the `frontend.yaml` file before you start the service. + +[See the appendix below](#appendix-accessing-the-guestbook-site-externally) on accessing the guestbook site externally for more details. + +Create the service and replication controller like this: + +```console +$ kubectl create -f examples/guestbook/all-in-one/frontend.yaml +service "frontend" created +replicationcontroller "frontend" created +``` + +Then, list all your services again: + +```console +$ kubectl get services +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE +frontend 10.0.93.211 80/TCP app=guestbook,tier=frontend 1h +redis-master 10.0.136.3 6379/TCP app=redis,role=master,tier=backend 1h +redis-slave 10.0.21.92 6379/TCP app=redis,role=slave,tier=backend 1h +``` + +Also list all your replication controllers: + +```console +$ kubectl get rc +CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS +frontend php-redis kubernetes/example-guestbook-php-redis:v3 app=guestbook,tier=frontend 3 +redis-master master redis app=redis,role=master,tier=backend 1 +redis-slave slave gcr.io/google_samples/gb-redisslave:v1 app=redis,role=slave,tier=backend 2 +``` + +Once it's up (again, it may take up to thirty seconds to create the pods), you can list the pods with specified labels in the cluster, to verify that the master, slaves and frontends are all running. You should see a list containing pods with label 'tier' like the following: + +```console +$ kubectl get pods -L tier +NAME READY STATUS RESTARTS AGE TIER +frontend-4o11g 1/1 Running 0 2h frontend +frontend-u9aq6 1/1 Running 0 2h frontend +frontend-yga1l 1/1 Running 0 2h frontend +redis-master-dz33o 1/1 Running 0 2h backend +redis-slave-35mer 1/1 Running 0 2h backend +redis-slave-iqkhy 1/1 Running 0 2h backend +``` + +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 is in `examples/guestbook/php-redis/guestbook.php`. It looks like this: + +```php + 'tcp', + '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' => $host, + 'port' => 6379, + ]); + + $value = $client->get($_GET['key']); + print('{"data": "' . $value . '"}'); + } +} else { + phpinfo(); +} ?> +``` + +Note the use of the `redis-master` and `redis-slave` host names-- we're finding those services via the Kubernetes cluster's DNS service, as discussed above. All the frontend replicas will write to the load-balancing redis-slaves service, which can be highly replicated as well. + +### Step Four: Cleanup + +If you are in a live Kubernetes cluster, you can just kill the pods by deleting the replication controllers and services. Using labels to select the resources to delete is an easy way to do this in one command. + +```console +$ kubectl delete rc -l "app in (redis, guestbook)" +$ kubectl delete service -l "app in (redis, guestbook)" +``` + +To completely tear down a Kubernetes cluster, if you ran this from source, you can use: + +```console +$ /cluster/kube-down.sh +``` + +### Troubleshooting + +If you are having trouble bringing up your guestbook app, double check that your external IP is properly defined for your frontend service, and that the firewall for your cluster nodes is open to port 80. + +Then, see the [troubleshooting documentation](../../docs/troubleshooting.md) for a further list of common issues and how you can diagnose them. + + + +### Appendix: Accessing the guestbook site externally + +You'll want to set up your guestbook service so that it can be accessed from outside of the internal Kubernetes network. Above, we introduced one way to do that, using the `type: LoadBalancer` spec. + +More generally, Kubernetes supports two ways of exposing a service onto an external IP address: `NodePort`s and `LoadBalancer`s , as described [here](../../docs/user-guide/services.md#publishing-services---service-types). + +If the `LoadBalancer` specification is used, it can take a short period for an external IP to show up in `kubectl get services` output, but you should then see it listed as well, e.g. like this: + +```console +$ kubectl get services +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE +frontend 10.0.93.211 130.211.188.51 80/TCP app=guestbook,tier=frontend 1h +redis-master 10.0.136.3 6379/TCP app=redis,role=master,tier=backend 1h +redis-slave 10.0.21.92 6379/TCP app=redis,role=master,tier=backend 1h +``` + +Once you've exposed the service to an external IP, visit the IP to see your guestbook in action. E.g., `http://130.211.188.51:80` in the example above. + +You should see a web page that looks something like this (without the messages). Try adding some entries to it! + + + +If you are more advanced in the ops arena, you can also manually get the service IP from looking at the output of `kubectl get pods,services`, and modify your firewall using standard tools and services (firewalld, iptables, selinux) which you are already familiar with. + +#### Google Compute Engine External Load Balancer Specifics + +In Google Compute Engine, Kubernetes automatically creates forwarding rules for services with `LoadBalancer`. + +You can list the forwarding rules like this (the forwarding rule also indicates the external IP): + +```console +$ gcloud compute forwarding-rules list +NAME REGION IP_ADDRESS IP_PROTOCOL TARGET +frontend us-central1 130.211.188.51 TCP us-central1/targetPools/frontend +``` + +In Google Compute Engine, you also may need to open the firewall for port 80 using the [console][cloud-console] or the `gcloud` tool. The following command will allow traffic from any source to instances tagged `kubernetes-minion` (replace with your tags as appropriate): + +```console +$ gcloud compute firewall-rules create --allow=tcp:80 --target-tags=kubernetes-minion kubernetes-minion-80 +``` + +For GCE Kubernetes startup details, see the [Getting started on Google Compute Engine](../../docs/getting-started-guides/gce.md) + +For Google Compute Engine details about limiting traffic to specific sources, see the [Google Compute Engine firewall documentation][gce-firewall-docs]. + +[cloud-console]: https://console.developer.google.com +[gce-firewall-docs]: https://cloud.google.com/compute/docs/networking#firewalls diff --git a/guestbook/all-in-one/frontend.yaml b/guestbook/all-in-one/frontend.yaml new file mode 100644 index 0000000000..780f8da1a0 --- /dev/null +++ b/guestbook/all-in-one/frontend.yaml @@ -0,0 +1,59 @@ +apiVersion: v1 +kind: Service +metadata: + name: frontend + labels: + app: guestbook + tier: frontend +spec: + # if your cluster supports it, uncomment the following to automatically create + # an external load-balanced IP for the frontend service. + # type: LoadBalancer + ports: + # the port that this service should serve on + - port: 80 + selector: + app: guestbook + tier: frontend +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: frontend + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: guestbook + tier: frontend +spec: + # this replicas value is default + # modify it according to your case + replicas: 3 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # tier: frontend + template: + metadata: + labels: + app: guestbook + tier: frontend + spec: + containers: + - name: php-redis + image: gcr.io/google_samples/gb-frontend:v3 + resources: + requests: + cpu: 100m + memory: 100Mi + 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/guestbook/all-in-one/guestbook-all-in-one.yaml b/guestbook/all-in-one/guestbook-all-in-one.yaml new file mode 100644 index 0000000000..f101e2a18d --- /dev/null +++ b/guestbook/all-in-one/guestbook-all-in-one.yaml @@ -0,0 +1,176 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis-master + labels: + app: redis + tier: backend + role: master +spec: + ports: + # the port that this service should serve on + - port: 6379 + targetPort: 6379 + selector: + app: redis + tier: backend + role: master +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-master + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: master + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 1 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: master + # tier: backend + template: + metadata: + labels: + app: redis + role: master + tier: backend + spec: + containers: + - name: master + image: gcr.io/google_containers/redis:e2e # or just image: redis + resources: + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 6379 +--- +apiVersion: v1 +kind: Service +metadata: + name: redis-slave + labels: + app: redis + tier: backend + role: slave +spec: + ports: + # the port that this service should serve on + - port: 6379 + selector: + app: redis + tier: backend + role: slave +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-slave + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: slave + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 2 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: slave + # tier: backend + template: + metadata: + labels: + app: redis + role: slave + tier: backend + spec: + containers: + - name: slave + image: gcr.io/google_samples/gb-redisslave:v1 + resources: + requests: + cpu: 100m + memory: 100Mi + 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 +--- +apiVersion: v1 +kind: Service +metadata: + name: frontend + labels: + app: guestbook + tier: frontend +spec: + # if your cluster supports it, uncomment the following to automatically create + # an external load-balanced IP for the frontend service. + # type: LoadBalancer + ports: + # the port that this service should serve on + - port: 80 + selector: + app: guestbook + tier: frontend +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: frontend + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: guestbook + tier: frontend +spec: + # this replicas value is default + # modify it according to your case + replicas: 3 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # tier: frontend + template: + metadata: + labels: + app: guestbook + tier: frontend + spec: + containers: + - name: php-redis + image: gcr.io/google_samples/gb-frontend:v3 + resources: + requests: + cpu: 100m + memory: 100Mi + 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/guestbook/all-in-one/redis-slave.yaml b/guestbook/all-in-one/redis-slave.yaml new file mode 100644 index 0000000000..de2d08e138 --- /dev/null +++ b/guestbook/all-in-one/redis-slave.yaml @@ -0,0 +1,61 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis-slave + labels: + app: redis + role: slave + tier: backend +spec: + ports: + # the port that this service should serve on + - port: 6379 + selector: + app: redis + role: slave + tier: backend +--- +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-slave + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: slave + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 2 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: slave + # tier: backend + template: + metadata: + labels: + app: redis + role: slave + tier: backend + spec: + containers: + - name: slave + image: gcr.io/google_samples/gb-redisslave:v1 + resources: + requests: + cpu: 100m + memory: 100Mi + 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/guestbook/frontend-controller.yaml b/guestbook/frontend-controller.yaml new file mode 100644 index 0000000000..b9b8c40f19 --- /dev/null +++ b/guestbook/frontend-controller.yaml @@ -0,0 +1,41 @@ +apiVersion: v1 +kind: ReplicationController +metadata: + name: frontend + # these labels can be applied automatically + # from the labels in the pod template if not set + # labels: + # app: guestbook + # tier: frontend +spec: + # this replicas value is default + # modify it according to your case + replicas: 3 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # tier: frontend + template: + metadata: + labels: + app: guestbook + tier: frontend + spec: + containers: + - name: php-redis + image: gcr.io/google_samples/gb-frontend:v4 + resources: + requests: + cpu: 100m + memory: 100Mi + 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/guestbook/frontend-service.yaml b/guestbook/frontend-service.yaml new file mode 100644 index 0000000000..72ea61327a --- /dev/null +++ b/guestbook/frontend-service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: frontend + labels: + app: guestbook + tier: frontend +spec: + # if your cluster supports it, uncomment the following to automatically create + # an external load-balanced IP for the frontend service. + # type: LoadBalancer + ports: + # the port that this service should serve on + - port: 80 + selector: + app: guestbook + tier: frontend diff --git a/guestbook/php-redis/Dockerfile b/guestbook/php-redis/Dockerfile new file mode 100644 index 0000000000..b7f699c582 --- /dev/null +++ b/guestbook/php-redis/Dockerfile @@ -0,0 +1,24 @@ +# Copyright 2016 The Kubernetes Authors 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. + +FROM php:5-apache + +RUN apt-get update +RUN apt-get install -y php-pear +RUN pear channel-discover pear.nrk.io +RUN pear install nrk/Predis + +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/guestbook/php-redis/controllers.js b/guestbook/php-redis/controllers.js new file mode 100644 index 0000000000..1e4b550424 --- /dev/null +++ b/guestbook/php-redis/controllers.js @@ -0,0 +1,29 @@ +var redisApp = angular.module('redis', ['ui.bootstrap']); + +/** + * Constructor + */ +function RedisController() {} + +RedisController.prototype.onRedis = function() { + this.scope_.messages.push(this.scope_.msg); + this.scope_.msg = ""; + var value = this.scope_.messages.join(); + this.http_.get("guestbook.php?cmd=set&key=messages&value=" + value) + .success(angular.bind(this, function(data) { + this.scope_.redisResponse = "Updated."; + })); +}; + +redisApp.controller('RedisCtrl', function ($scope, $http, $location) { + $scope.controller = new RedisController(); + $scope.controller.scope_ = $scope; + $scope.controller.location_ = $location; + $scope.controller.http_ = $http; + + $scope.controller.http_.get("guestbook.php?cmd=get&key=messages") + .success(function(data) { + console.log(data); + $scope.messages = data.data.split(","); + }); +}); diff --git a/guestbook/php-redis/guestbook.php b/guestbook/php-redis/guestbook.php new file mode 100644 index 0000000000..ee0670ee69 --- /dev/null +++ b/guestbook/php-redis/guestbook.php @@ -0,0 +1,41 @@ + 'tcp', + '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' => $host, + 'port' => 6379, + ]); + + $value = $client->get($_GET['key']); + print('{"data": "' . $value . '"}'); + } +} else { + phpinfo(); +} ?> diff --git a/guestbook/php-redis/index.html b/guestbook/php-redis/index.html new file mode 100644 index 0000000000..4ffb4ed2ab --- /dev/null +++ b/guestbook/php-redis/index.html @@ -0,0 +1,25 @@ + + + Guestbook + + + + + + +
+

Guestbook

+
+
+
+ +
+
+
+
+ {{msg}} +
+
+
+ + diff --git a/guestbook/redis-master-controller.yaml b/guestbook/redis-master-controller.yaml new file mode 100644 index 0000000000..2b9b8ec7a4 --- /dev/null +++ b/guestbook/redis-master-controller.yaml @@ -0,0 +1,36 @@ +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-master + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: master + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 1 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: master + # tier: backend + template: + metadata: + labels: + app: redis + role: master + tier: backend + spec: + containers: + - name: master + image: gcr.io/google_containers/redis:e2e # or just image: redis + resources: + requests: + cpu: 100m + memory: 100Mi + ports: + - containerPort: 6379 diff --git a/guestbook/redis-master-service.yaml b/guestbook/redis-master-service.yaml new file mode 100644 index 0000000000..a9db703128 --- /dev/null +++ b/guestbook/redis-master-service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis-master + labels: + app: redis + role: master + tier: backend +spec: + ports: + # the port that this service should serve on + - port: 6379 + targetPort: 6379 + selector: + app: redis + role: master + tier: backend diff --git a/guestbook/redis-slave-controller.yaml b/guestbook/redis-slave-controller.yaml new file mode 100644 index 0000000000..1d54a82804 --- /dev/null +++ b/guestbook/redis-slave-controller.yaml @@ -0,0 +1,44 @@ +apiVersion: v1 +kind: ReplicationController +metadata: + name: redis-slave + # these labels can be applied automatically + # from the labels in the pod template if not set + labels: + app: redis + role: slave + tier: backend +spec: + # this replicas value is default + # modify it according to your case + replicas: 2 + # selector can be applied automatically + # from the labels in the pod template if not set + # selector: + # app: guestbook + # role: slave + # tier: backend + template: + metadata: + labels: + app: redis + role: slave + tier: backend + spec: + containers: + - name: slave + image: gcr.io/google_samples/gb-redisslave:v1 + resources: + requests: + cpu: 100m + memory: 100Mi + 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/guestbook/redis-slave-service.yaml b/guestbook/redis-slave-service.yaml new file mode 100644 index 0000000000..e1f0401714 --- /dev/null +++ b/guestbook/redis-slave-service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis-slave + labels: + app: redis + role: slave + tier: backend +spec: + ports: + # the port that this service should serve on + - port: 6379 + selector: + app: redis + role: slave + tier: backend diff --git a/guestbook/redis-slave/Dockerfile b/guestbook/redis-slave/Dockerfile new file mode 100644 index 0000000000..1d097b57ac --- /dev/null +++ b/guestbook/redis-slave/Dockerfile @@ -0,0 +1,21 @@ +# Copyright 2016 The Kubernetes Authors 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. + +FROM redis + +ADD run.sh /run.sh + +RUN chmod a+x /run.sh + +CMD /run.sh diff --git a/guestbook/redis-slave/run.sh b/guestbook/redis-slave/run.sh new file mode 100755 index 0000000000..9f79ccef17 --- /dev/null +++ b/guestbook/redis-slave/run.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# Copyright 2014 The Kubernetes Authors 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. + +if [[ ${GET_HOSTS_FROM:-dns} == "env" ]]; then + redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379 +else + redis-server --slaveof redis-master 6379 +fi