Implementation of guest book case _docker based on Kubernetes and Docke

Source: Internet
Author: User
Tags curl redis docker ps

First, introduce

This case is based on Kubernetes and Docker, which includes
1, Web front-end
2, Redis Master
3, Redis Slave
Where the Web front-end interacts with the JavaScript Redis API and Redis Master

Kubernetes System Architecture

Second, the configuration

0. Prerequisites
Kubernetes Cluster

1, start Redis Master
use replication controller to ensure that only one pod is running (when a node is down, RC will start Redis Master in another healthy node), but there may be data loss.

[Root@centos1 example]# kubectl create-f redis-master-controller.json replicationcontrollers/redis-master
[ROOT@CENTOS1 example]# kubectl get RC
CONTROLLER   CONTAINER (s)  IMAGE (s)  SELECTOR      Replicas
redis-master  Master     redis   name=redis-master  1

Verify that Master is running successfully, as shown in the following pod running on the centos2/192.168.1.112 machine

[Root@centos1 example]# kubectl get pods
POD         IP      CONTAINER (s)  IMAGE (s)  HOST          LABELS       STATUS  CREATED   message
redis-master-svar7  172.17.0.9               centos2/192.168.1.112  name= Redis-master  Running  seconds  
                 master     redis                         Running    

SSH to centos2/192.168.1.112 view docker status

[Root@centos2 yum.repos.d]# Docker PS
CONTAINER ID    IMAGE                 COMMAND        CREATED       STATUS       PORTS        NAMES
91689ce56668    redis:latest              "/entrypoint.sh redi  3 minutes ago up    3 minutes              K8s_ master.52732b08_redis-master-svar7_default_5b6d5485-1894-11e5-b3ad-000c293c8c19_97e79b7b  
38C3180813C3    gcr.io/google_containers/pause:0.8.0  "/pause"        3 minutes ago up    3 minutes              K8s_ pod.49eee8c2_redis-master-svar7_default_5b6d5485-1894-11e5-b3ad-000c293c8c19_298e038f   

Note:after kubectl create executes, if the mirror does not exist, the Docker pull is executed, and pods on the Kubertnetes UI will display the pending status on the network.

2. Start Master Service
a kubernetes service will load-balance one or more container, which is achieved through the labels metadata defined in the redis-master above, and it is noteworthy that There is only one master in Redis, but we still create a service for it because we can use a elastic IP routing to a specific master.
The service found in the Kubernetes cluster is realized by the environment variable in the container, and it realizes the container load balance based on the pod label.
The pod created in the first step contains a label "Name=redis-master", and the service Selector field determines the service's forwarding of traffic to which Pod,port and targetport information defined What port the proxy runs on.

[Root@centos1 example]# kubectl create-f redis-master-service.json 
services/redis-master
[root@centos1 example]# Kubectl Get Services
name      LABELS                  SELECTOR      IP (s)      PORT (s)
Redis-master  NAME =redis-master             name=redis-master  10.254.154.90  6379/tcp

After the success of the above operation, all pods can find Redis master running in Port 6379, from salve to master traffic will have the following two steps:
1, a redis slave will be connected to the port of Redis master Service
2, traffic will be from the service node port to Targetport
If Targetport is not specified, the default and port are the same

3, start replicated slave pod
Although Redis Master is a separate Pod,redis slaves is a replicated pod, In Kubernetes, a replication controller is responsible for managing multiple instances of a replicated pod, and RC automatically pulls down replica (which can be simply tested by killing the Docker process)

[Root@centos1 example]# kubectl create-f Redis-slave-controller.json replicationcontrollers/redis-slave [ ROOT@CENTOS1 example]# kubectl get RC CONTROLLER CONTAINER (s) IMAGE (s) SELECTOR replicas Redis-master ma Ster Redis name=redis-master 1 redis-slave slave kubernetes/redis-slave:v2 name=redis-slave 2 [root  @centos1 example]# kubectl Get pods POD IP CONTAINER IMAGE (s) HOST LABELS STATUS   CREATED message redis-master-svar7 172.17.0.9 centos2/192.168.1.112 name=redis-master Running   Minutes Master Redis Running minutes redis-slave-31tkb      172.17.0.10 centos2/192.168.1.112 name=redis-slave Running seconds slave                       Kubernetes/redis-slave:v2 Running seconds Redis-slave-uk8nu 172.17.0.11 centos2/192.168.1.112 name=redIs-slave Running seconds slave kubernetes/redis-slave:v2 Running  
 Econds

You can see a master pod and two slave pod

4, start slave service
like master, we want a proxy service to connect to Redis slave, in addition to service discovery, the slave service provides a transparent proxy for web App client.
The selector of this service is name=redis-slave, we can conveniently use Kubectl get services-l "label=value" command to locate these services

[Root@centos1 example]# kubectl create-f redis-slave-service.json 
services/redis-slave
[root@centos1 example]# Kubectl Get Services
name      LABELS                  SELECTOR      IP (s)      PORT (s)
Redis-master  NAME =redis-master             name=redis-master  10.254.154.90  6379/tcp
redis-slave   name=redis-slave             name=redis-slave  10.254.159.145  6379/tcp

5. Create Frontend pod
This is a simple PHP service to interact with master service (write request) or slave service (read request)

[Root@centos1 example]# kubectl create-f frontend-controller.json 
replicationcontrollers/frontend
[ ROOT@CENTOS1 example]# kubectl get RC
CONTROLLER   CONTAINER (s)  IMAGE (s)                  SELECTOR
Replicas Frontend    php-redis   kubernetes/example-guestbook-php-redis:v2  name=frontend    3
Redis-master  Master     redis                    name=redis-master  1
redis-slave  slave     kubernetes/ Redis-slave:v2          Name=redis-slave  2

After successful operation, view current pod running status

[Root@centos1 example]# kubectl get pods POD IP CONTAINER (s) IMAGE (s) HOST LABELS STATUS CREATED message frontend-fr5z1 172.17.0.13 centos2/192.168.1.112 name=f                        Rontend Running 2 minutes Php-redis Kubernetes/example-guestbook-php-redis:v2     Running 2 minutes frontend-gjx3t 172.17.0.14 centos2/192.168.1.112 name=frontend Running 2 minutes Php-redis kubernetes/example-guestbook-php-redis:v2 Ru Nning 2 minutes frontend-v608r 172.17.0.12 centos2/192.168.1.112 name=frontend Run  Ning 2 minutes Php-redis kubernetes/example-guestbook-php-redis:v2 Running 2 minutes redis-master-svar7 172.17.0.9 centos2/192.168.1.112 name=redis-master Runnin  
        G minutes          Master Redis Running minutes redis-slave-31tkb 172.17.0.10 centos2/192.168.1.112 name=redis-slave Running minutes slave ku                               Bernetes/redis-slave:v2 Running minutes Redis-slave-uk8nu 172.17.0.11 centos2/192.168.1.112 name=redis-slave Running minutes slave Kubernetes/redi  
 S-slave:v2 Running minutes

You can see a Redis master, two Redis slave and three frontend pods

6. Create Guestbook Service
As with other service, you can create a service management frontend pods

[Root@centos1 example]# kubectl create-f frontend-service.json 
services/frontend
[root@centos1 example]# Kubectl Get services
NAME      LABELS                  SELECTOR      IP (s)      PORT (s)
frontend    name=frontend               name=frontend    10.254.154.111  80/tcp
redis-master  name=redis-master             name= Redis-master  10.254.154.90  6379/tcp
redis-slave   name=redis-slave             name=redis-slave  10.254.159.145  6379/tcp

We can access pods via Frontend service (10.254.154.111), but this IP is obviously not accessible externally, and the next section explains how to access the external network guestbook

7. External Network access Guestbook
Kubernetes supports two types of access to expose a service to an external IP address nodeports and loadbalancers, and to view the firewall and find the service's corresponding port, as follows

[Root@centos1 example]# kubectl get pods,services POD IP CONTAINER (s) IMAGE (s) HOST LABELS STATUS CREATED message frontend-fr5z1 172.17.0.13 centos2/192.168.1.                        112 name=frontend Running minutes Php-redis kubernetes/example-guestbook-php-redis:v2   Running minutes frontend-gjx3t 172.17.0.14 centos2/192.168.1.112                        Name=frontend Running minutes Php-redis kubernetes/example-guestbook-php-redis:v2 Running minutes frontend-v608r 172.17.0.12 centos2/192.168.1.112 N                        Ame=frontend Running minutes Php-redis kubernetes/example-guestbook-php-redis:v2 Running minutes redis-master-svar7 172.17.0.9 centos2/192.168.1.112 na Me=redis-master Running  About an hour master Redis Running about a hour red   
                  Is-slave-31tkb 172.17.0.10 centos2/192.168.1.112 Name=redis-slave Running Slave kubernetes/redis-slave:v2 Running minutes Redis-slave-uk   
                  8nu 172.17.0.11 centos2/192.168.1.112 name=redis-slave Running minutes                  Slave kubernetes/redis-slave:v2 Running minutes NAME LABELS
SELECTOR IP (s) PORT (s) frontend name=frontend name=frontend 10.254.154.111 80/tcp Kubernetes component=apiserver,provider=kubernetes <none> 10.254.0.2 443/tcp kubernetes-ro component=ap Iserver,provider=kubernetes <none> 10.254.0.1 80/tcp redis-master name=redis-master Name=redi S-master 10.254.154.90 6379/tcp redis-slave name=redis-slave name=redis-slave 10.254.159.145 6379/tcp [root@centos1 example]# ku               Bectl Get services NAME LABELS SELECTOR IP (s) PORT (s) frontend name=frontend Name=frontend 10.254.154.111 80/tcp kubernetes component=apiserver,provider=kubernetes <none> 10.25 4.0.2 443/tcp kubernetes-ro component=apiserver,provider=kubernetes <none> 10.254.0.1 80/tcp ter name=redis-master name=redis-master 10.254.154.90 6379/tcp redis-slave name=redis-slave N
 Ame=redis-slave 10.254.159.145 6379/tcp

Found that Redis-master was on the 10.254.154.90, logged on to the Centos2, executed Iptables-save, and found that there was such a rule

Copy Code code as follows:
-a kube-portals-host-d 10.254.154.90/32-p tcp-m comment--comment "default/redis-master:"-M TCP--dport 6379-j DNAT- -to-destination 192.168.1.112:49038

Note that the Centos2 49038 port is mapped to 6379 in master container, and of course we are able to perform a native access Redis

[Root@centos2 yum.repos.d]# redis-cli-p 49038
127.0.0.1:49038> set a b
OK
127.0.0.1:49038> get a
"B"

8, the use of Curl simple test

Submitting data

Copy Code code as follows:
Curl "Localhost:8000/index.php?cmd=set&key=messages&value=jay_sais_hi"

Querying data
Copy Code code as follows:
Curl "Localhost:8000/index.php?cmd=get&key=messages"

The 6. json files used in this case
1, Redis-master-controller.json

{
  "kind": "Replicationcontroller", "
  apiversion": "V1beta3",
  "metadata": {
   "name": "Redis-master",
   "Labels": {
     "name": "Redis-master"
   }
  },
  "spec": {
   "Replicas": 1,
   "selector": {
     ' Name ': ' Redis-master '
   },
   ' template ': {' metadata ': {' labels ': {'
        name ': ' Redis-master '
      }
     ,
     ' spec ': {'
      containers ': [
        {
         ' name ': ' Master ',
         ' image ': ' Redis ',
         ' Ports ': [
           {
            ' containerport ': 6379
           }
         ]
}}}

2, Redis-master-service.json

{
  "kind": "Service",
  "apiversion": "V1beta3",
  "metadata": {
   "name": "Redis-master",
   "labels": {
     ' Name ': ' Redis-master '
   }
  },
  ' spec ': {
   ' ports ': [
    {
     ' port ': 6379,
     ' targetport ': 6379
    }
   ],
   ' selector ': {
     ' name ': ' Redis-master '
}}}

3, Redis-slave-controller.json

{
  "kind": "Replicationcontroller", "
  apiversion": "V1beta3",
  "metadata": {
   "name": "Redis-slave",
   "Labels": {
     "name": "Redis-slave"
   }
  },
  "spec": {
   "Replicas": 2,
   "selector": {
     " Name ': ' Redis-slave '
   },
   ' template ': {' metadata ': {' labels ': {'
        name ': ' Redis-slave '
      }
     ,
     "spec": {"
      containers": [
        {
         "name": "Slave",
         "image": "Kubernetes/ Redis-slave:v2 ',
         ' ports ': [
           {
            ' containerport ': 6379
           }
         ]
   }}
  }
}

4, Redis-slave-service.json

{"
  kind": "Service",
  "apiversion": "V1beta3",
  "metadata": {
   "name": "Redis-slave",
   " Labels ': {
     ' name ': ' Redis-slave '
   }
  },
  ' spec ': {
   ' ports ': [
    {
     ' port ': 6379
    }
   ],
   ' selector ': {
     ' name ': ' Redis-slave '
   }
  }
}

5, Frontend-controller.json

{
  "kind": "Replicationcontroller", "
  apiversion": "V1beta3",
  "metadata": {
   "name": "Frontend",
   "Labels": {
     "name": "Frontend"
   }
  },
  "spec": {
   "Replicas": 3,
   "selector": {
     "name": " Frontend "
   },
   " template ": {" metadata ": {" labels ": {"
        name ":" Frontend "
      }
     },
     "Spec": {"
      containers": [
        {
         "name": "Php-redis", "
         image": "Kubernetes/ Example-guestbook-php-redis:v2 ",
         " ports ": [
           {
            " containerport ":)
           }
      ]
     }
   }
  }
}

6, Frontend-service.json

{
  "kind": "Service",
  "apiversion": "V1beta3",
  "metadata": {
   "name": "Frontend",
   "labels": {
     ' Name ': ' Frontend '
   }
  },
  ' spec ': {
   ' ports ': [
    {
     ' port ': '
   ] ',
   ' selector ': {
     ' name ': ' Frontend '}}}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.