mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #4133 from ddysher/0-fix-0
Use replication controller for redis master.
This commit is contained in:
commit
5d2ea04bc2
@ -28,5 +28,3 @@ Definition of columns:
|
|||||||
- **Commercial**: A commercial offering with its own support arrangements.
|
- **Commercial**: A commercial offering with its own support arrangements.
|
||||||
- **Community**: Actively supported by community contributions. May not work with more recent releases of kubernetes.
|
- **Community**: Actively supported by community contributions. May not work with more recent releases of kubernetes.
|
||||||
- **Inactive**: No active maintainer. Not recommended for first-time K8s users, and may be deleted soon.
|
- **Inactive**: No active maintainer. Not recommended for first-time K8s users, and may be deleted soon.
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ The example combines a web frontend, a redis master for storage and a replicated
|
|||||||
|
|
||||||
### Step Zero: Prerequisites
|
### Step Zero: Prerequisites
|
||||||
|
|
||||||
This example assumes that you have forked the repository and [turned up a Kubernetes cluster](https://github.com/GoogleCloudPlatform/kubernetes#contents):
|
This example assumes that you have forked the repository and [turned up a Kubernetes cluster](../../docs/getting-started-guides):
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cd kubernetes
|
$ cd kubernetes
|
||||||
@ -15,35 +15,37 @@ $ hack/dev-build-and-up.sh
|
|||||||
|
|
||||||
### Step One: Turn up the redis master.
|
### 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.
|
Use the file `examples/guestbook-go/redis-master-controller.json` to create a replication controller which manages a single pod. The pod runs a redis key-value server in a container. Using a replication controller is the preferred way to launch long-running pods, even for 1 replica, so the pod will benefit from self-healing mechanism in kubernetes.
|
||||||
|
|
||||||
Create the redis pod in your Kubernetes cluster using the `kubectl` CLI:
|
Create the redis master replication controller in your Kubernetes cluster using the `kubectl` CLI:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-master-pod.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-master-controller.json
|
||||||
```
|
```
|
||||||
|
|
||||||
Once that's up you can list the pods in the cluster, to verify that the master is running:
|
Once that's up you can list the replication controllers in the cluster:
|
||||||
|
```shell
|
||||||
|
$ cluster/kubectl.sh get rc
|
||||||
|
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
|
||||||
|
redis-master-controller redis-master gurpartap/redis name=redis,role=master 1
|
||||||
|
```
|
||||||
|
|
||||||
|
List pods in cluster to verify the master is running. 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).
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh get pods
|
$ cluster/kubectl.sh get pods
|
||||||
```
|
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS
|
||||||
|
redis-master-pod-hh2gd 10.244.3.7 redis-master gurpartap/redis kubernetes-minion-4.c.lucid-walker-725.internal/104.154.49.134 name=redis,role=master Running
|
||||||
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.thockin-dev.internal/86.75.30.9 name=redis,role=master Waiting
|
|
||||||
```
|
```
|
||||||
|
|
||||||
If you ssh to that machine, you can run `docker ps` to see the actual pod:
|
If you ssh to that machine, you can run `docker ps` to see the actual pod:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
me@workstation$ gcloud compute ssh --zone us-central1-b kubernetes-minion-3
|
me@workstation$ gcloud compute ssh --zone us-central1-b kubernetes-minion-4
|
||||||
|
|
||||||
me@kubernetes-minion-3:~$ sudo docker ps
|
me@kubernetes-minion-3:~$ sudo docker ps
|
||||||
CONTAINER ID IMAGE COMMAND CREATED STATUS
|
CONTAINER ID IMAGE COMMAND CREATED STATUS
|
||||||
e443647cd064 gurpartap/redis:latest redis-server /etc/r 2 minutes ago Up 2 minutes
|
d5c458dabe50 gurpartap/redis:latest "/usr/local/bin/redi 5 minutes ago Up 5 minutes
|
||||||
```
|
```
|
||||||
|
|
||||||
(Note that initial `docker pull` may take a few minutes, depending on network conditions.)
|
(Note that initial `docker pull` may take a few minutes, depending on network conditions.)
|
||||||
@ -51,35 +53,30 @@ e443647cd064 gurpartap/redis:latest redis-server /etc/r 2 minutes ago Up 2
|
|||||||
### Step Two: Turn up the master service.
|
### 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.
|
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`
|
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 in the `kubectl` cli:
|
||||||
|
|
||||||
To create the service with the `kubectl` cli:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-master-service.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-master-service.json
|
||||||
|
|
||||||
$ cluster/kubectl.sh get services
|
$ cluster/kubectl.sh get services
|
||||||
ID LABELS SELECTOR PORT
|
NAME LABELS SELECTOR IP PORT
|
||||||
redis-master name=redis,role=master 6379
|
redis-master <none> name=redis,role=master 10.0.186.234 6379
|
||||||
```
|
```
|
||||||
|
|
||||||
This will cause all new pods to see the redis master apparently running on $REDIS_MASTER_SERVICE_HOST at port 6379.
|
This will cause all new pods to see the redis master apparently running on $REDIS_MASTER_SERVICE_HOST at port 6379. Once created, the service proxy on each node is configured to set up a proxy on the specified port (in this case port 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.
|
### 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.
|
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`
|
Use the file `examples/guestbook-go/redis-slave-controller.json` to create the replication controller:
|
||||||
|
|
||||||
to create the replication controller by running:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-slave-controller.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-slave-controller.json
|
||||||
|
|
||||||
$ cluster/kubectl.sh get replicationControllers
|
$ cluster/kubectl.sh get rc
|
||||||
ID IMAGE(S) SELECTOR REPLICAS
|
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
|
||||||
redis-slave-controller gurpartap/redis name=redis,role=slave 2
|
redis-master-controller redis-master gurpartap/redis name=redis,role=master 1
|
||||||
|
redis-slave-controller redis-slave 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:
|
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:
|
||||||
@ -92,10 +89,10 @@ Once that's up you can list the pods in the cluster, to verify that the master a
|
|||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh get pods
|
$ cluster/kubectl.sh get pods
|
||||||
ID IMAGE(S) HOST LABELS STATUS
|
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS
|
||||||
redis-master-pod gurpartap/redis kubernetes-minion-1.c.thockin-dev.internal/86.75.30.9 name=redis,role=master Running
|
redis-master-pod-hh2gd 10.244.3.7 redis-master gurpartap/redis kubernetes-minion-4.c.lucid-walker-725.internal/104.154.49.134 name=redis,role=master Running
|
||||||
1472fd26-54d6-11e4-90fd-42010af00690 gurpartap/redis kubernetes-minion-4.c.thockin-dev.internal/12.34.56.78 name=redis,replicationController=redis-slave-controller,role=slave Running
|
redis-slave-controller-i7hvs 10.244.2.7 redis-slave gurpartap/redis kubernetes-minion-3.c.lucid-walker-725.internal/104.154.52.39 name=redis,role=slave Running
|
||||||
1473363e-54d6-11e4-90fd-42010af00690 gurpartap/redis kubernetes-minion-3.c.thockin-dev.internal/9.3.19.76 name=redis,replicationController=redis-slave-controller,role=slave Running
|
redis-slave-controller-nyxxv 10.244.1.6 redis-slave gurpartap/redis kubernetes-minion-2.c.lucid-walker-725.internal/130.211.144.5 name=redis,role=slave Running
|
||||||
```
|
```
|
||||||
|
|
||||||
You will see a single redis master pod and two redis slave pods.
|
You will see a single redis master pod and two redis slave pods.
|
||||||
@ -112,40 +109,38 @@ Now that you have created the service specification, create it in your cluster w
|
|||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-slave-service.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/redis-slave-service.json
|
||||||
|
|
||||||
$ cluster/kubectl.sh get services
|
$ cluster/kubectl.sh get services
|
||||||
ID LABELS SELECTOR PORT
|
NAME LABELS SELECTOR IP PORT
|
||||||
redis-master name=redis,role=master 6379
|
redis-master <none> name=redis,role=master 10.0.186.234 6379
|
||||||
redis-slave name=redis,role=slave name=redis,role=slave 6379
|
redis-slave name=redis,role=slave name=redis,role=slave 10.0.22.180 6379
|
||||||
```
|
```
|
||||||
|
|
||||||
### Step Five: Create the guestbook pod.
|
### 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.
|
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`:
|
The pod is described in the file `examples/guestbook-go/guestbook-controller.json`. Using this file, you can turn up your guestbook with:
|
||||||
|
|
||||||
Using this file, you can turn up your guestbook with:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/guestbook-controller.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/guestbook-controller.json
|
||||||
|
|
||||||
$ cluster/kubectl.sh get replicationControllers
|
$ cluster/kubectl.sh get replicationControllers
|
||||||
ID IMAGE(S) SELECTOR REPLICAS
|
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
|
||||||
redis-slave-controller gurpartap/redis name=redis,role=slave 2
|
guestbook-controller guestbook kubernetes/guestbook name=guestbook 3
|
||||||
guestbook-controller kubernetes/guestbook name=guestbook 3
|
redis-master-controller redis-master gurpartap/redis name=redis,role=master 1
|
||||||
|
redis-slave-controller redis-slave gurpartap/redis name=redis,role=slave 2
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
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
|
```shell
|
||||||
$ cluster/kubectl.sh get pods
|
$ cluster/kubectl.sh get pods
|
||||||
ID IMAGE(S) HOST LABELS STATUS
|
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS
|
||||||
redis-master-pod gurpartap/redis kubernetes-minion-1.c.thockin-dev.internal/86.75.30.9 name=redis,role=master Running
|
guestbook-controller-182tv 10.244.2.8 guestbook kubernetes/guestbook kubernetes-minion-3.c.lucid-walker-725.internal/104.154.52.39 name=guestbook Running
|
||||||
1472fd26-54d6-11e4-90fd-42010af00690 gurpartap/redis kubernetes-minion-4.c.thockin-dev.internal/12.34.56.78 name=redis,replicationController=redis-slave-controller,role=slave Running
|
guestbook-controller-jzjpe 10.244.0.7 guestbook kubernetes/guestbook kubernetes-minion-1.c.lucid-walker-725.internal/104.154.37.86 name=guestbook Running
|
||||||
1473363e-54d6-11e4-90fd-42010af00690 gurpartap/redis kubernetes-minion-3.c.thockin-dev.internal/9.3.19.76 name=redis,replicationController=redis-slave-controller,role=slave Running
|
guestbook-controller-zwk1b 10.244.3.8 guestbook kubernetes/guestbook kubernetes-minion-4.c.lucid-walker-725.internal/104.154.49.134 name=guestbook Running
|
||||||
fc58aa01-54d6-11e4-90fd-42010af00690 kubernetes/guestbook kubernetes-minion-1.c.thockin-dev.internal/1.18.19.78 name=guestbook,replicationController=guestbook-controller Running
|
redis-master-pod-hh2gd 10.244.3.7 redis-master gurpartap/redis kubernetes-minion-4.c.lucid-walker-725.internal/104.154.49.134 name=redis,role=master Running
|
||||||
fc592fbb-54d6-11e4-90fd-42010af00690 kubernetes/guestbook kubernetes-minion-1.c.thockin-dev.internal/12.9.20.9 name=guestbook,replicationController=guestbook-controller Running
|
redis-slave-controller-i7hvs 10.244.2.7 redis-slave gurpartap/redis kubernetes-minion-3.c.lucid-walker-725.internal/104.154.52.39 name=redis,role=slave Running
|
||||||
fc59569e-54d6-11e4-90fd-42010af00690 kubernetes/guestbook kubernetes-minion-2.c.thockin-dev.internal/1.11.20.13 name=guestbook,replicationController=guestbook-controller Running
|
redis-slave-controller-nyxxv 10.244.1.6 redis-slave gurpartap/redis kubernetes-minion-2.c.lucid-walker-725.internal/130.211.144.5 name=redis,role=slave Running
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You will see a single redis master pod, two redis slaves, and three guestbook pods.
|
You will see a single redis master pod, two redis slaves, and three guestbook pods.
|
||||||
@ -158,10 +153,10 @@ Just like the others, you want a service to group your guestbook pods. The serv
|
|||||||
$ cluster/kubectl.sh create -f examples/guestbook-go/guestbook-service.json
|
$ cluster/kubectl.sh create -f examples/guestbook-go/guestbook-service.json
|
||||||
|
|
||||||
$ cluster/kubectl.sh get services
|
$ cluster/kubectl.sh get services
|
||||||
ID LABELS SELECTOR IP PORT
|
NAME LABELS SELECTOR IP PORT
|
||||||
redis-master name=redis,role=master 10.0.0.1 6379
|
guestbook <none> name=guestbook 10.0.12.110 3000
|
||||||
redis-slave name=redis,role=slave name=redis,role=slave 10.0.0.2 6379
|
redis-master <none> name=redis,role=master 10.0.186.234 6379
|
||||||
guestbook name=guestbook 10.0.0.3 3000
|
redis-slave name=redis,role=slave name=redis,role=slave 10.0.22.180 6379
|
||||||
```
|
```
|
||||||
|
|
||||||
To play with the service itself, find the external IP of the load balancer from the [Google Cloud Console][cloud-console] or the `gcloud` tool, and visit `http://<ip>:3000`.
|
To play with the service itself, find the external IP of the load balancer from the [Google Cloud Console][cloud-console] or the `gcloud` tool, and visit `http://<ip>:3000`.
|
||||||
|
24
examples/guestbook-go/redis-master-controller.json
Normal file
24
examples/guestbook-go/redis-master-controller.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"apiVersion": "v1beta1",
|
||||||
|
"kind": "ReplicationController",
|
||||||
|
"id": "redis-master-controller",
|
||||||
|
"desiredState": {
|
||||||
|
"replicas": 1,
|
||||||
|
"replicaSelector": { "name": "redis", "role": "master" },
|
||||||
|
"podTemplate": {
|
||||||
|
"desiredState": {
|
||||||
|
"manifest": {
|
||||||
|
"version": "v1beta1",
|
||||||
|
"id": "redis-master-controller",
|
||||||
|
"containers": [{
|
||||||
|
"name": "redis-master",
|
||||||
|
"image": "gurpartap/redis",
|
||||||
|
"ports": [{ "name": "redis-server", "containerPort": 6379 }]
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"labels": { "name": "redis", "role": "master" }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"labels": { "name": "redis", "role": "master" }
|
||||||
|
}
|
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"kind": "Pod",
|
|
||||||
"id": "redis-master-pod",
|
|
||||||
"desiredState": {
|
|
||||||
"manifest": {
|
|
||||||
"version": "v1beta1",
|
|
||||||
"id": "redis-master-pod",
|
|
||||||
"containers": [{
|
|
||||||
"name": "redis-master",
|
|
||||||
"image": "gurpartap/redis",
|
|
||||||
"ports": [{ "name": "redis-server", "containerPort": 6379 }]
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"labels": { "name": "redis", "role": "master" }
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user