2016年12月13日星期二

OpenShift_013:手动弹性扩展应用

环境:OSE 3.0.1

1. [student@workstation ~]$ git clone http://workstation.pod0.example.com/scaling

2. [student@workstation scaling]$ cat scaling/index.php
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <br/> Server IP: <?php echo $_SERVER['SERVER_ADDR']; ?>
 </body>
</html>

3. [student@workstation ~]$ oc login -u developer -p openshift

4. [student@workstation ~]$ oc new-project scaling

5. 根据源代码生成 .json 资源文件
[student@workstation ~]$ oc -o json new-app http://workstation.pod0.example.com/scaling > $HOME/scaling.json
[student@workstation ~]$ cat scaling.json
{
    "kind": "List",
    "apiVersion": "v1",
    "metadata": {},
    "items": [
        {
            "kind": "ImageStream",
            "apiVersion": "v1",
            "metadata": {
                "name": "scaling",
                "creationTimestamp": null,
                "labels": {
                    "app": "scaling"
                }
            },
            "spec": {},
            "status": {
                "dockerImageRepository": ""
            }
        },
        {
            "kind": "BuildConfig",
            "apiVersion": "v1",
            "metadata": {
                "name": "scaling",
                "creationTimestamp": null,
                "labels": {
                    "app": "scaling"
                }
            },
            "spec": {
                "triggers": [
                    {
                        "type": "GitHub",
                        "github": {
                            "secret": "rYTfc9Uw7XSmDjFpcLa6"
                        }
                    },
                    {
                        "type": "Generic",
                        "generic": {
                            "secret": "AO6BiYKUx_9aFuEekQJ0"
                        }
                    },
                    {
                        "type": "ImageChange",
                        "imageChange": {}
                    }
                ],
                "source": {
                    "type": "Git",
                    "git": {
                        "uri": "http://workstation.pod0.example.com/scaling"
                    }
                },
                "strategy": {
                    "type": "Source",
                    "sourceStrategy": {
                        "from": {
                            "kind": "ImageStreamTag",
                            "namespace": "openshift",
                            "name": "php:latest"
                        }
                    }
                },
                "output": {
                    "to": {
                        "kind": "ImageStreamTag",
                        "name": "scaling:latest"
                    }
                },
                "resources": {}
            },
            "status": {
                "lastVersion": 0
            }
        },
        {
            "kind": "DeploymentConfig",
            "apiVersion": "v1",
            "metadata": {
                "name": "scaling",
                "creationTimestamp": null,
                "labels": {
                    "app": "scaling"
                }
            },
            "spec": {
                "strategy": {
                    "resources": {}
                },
                "triggers": [
                    {
                        "type": "ConfigChange"
                    },
                    {
                        "type": "ImageChange",
                        "imageChangeParams": {
                            "automatic": true,
                            "containerNames": [
                                "scaling"
                            ],
                            "from": {
                                "kind": "ImageStreamTag",
                                "name": "scaling:latest"
                            }
                        }
                    }
                ],
                "replicas": 1,
                "selector": {
                    "deploymentconfig": "scaling"
                },
                "template": {
                    "metadata": {
                        "creationTimestamp": null,
                        "labels": {
                            "deploymentconfig": "scaling"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "name": "scaling",
                                "image": "library/scaling:latest",
                                "ports": [
                                    {
                                        "containerPort": 8080,
                                        "protocol": "TCP"
                                    }
                                ],
                                "resources": {}
                            }
                        ]
                    }
                }
            },
            "status": {}
        },
        {
            "kind": "Service",
            "apiVersion": "v1",
            "metadata": {
                "name": "scaling",
                "creationTimestamp": null,
                "labels": {
                    "app": "scaling"
                }
            },
            "spec": {
                "ports": [
                    {
                        "name": "8080-tcp",
                        "protocol": "TCP",
                        "port": 8080,
                        "targetPort": 8080,
                        "nodePort": 0
                    }
                ],
                "selector": {
                    "deploymentconfig": "scaling"
                }
            },
            "status": {
                "loadBalancer": {}
            }
        }
    ]
}

修改 "replicas" : 3, 即扩展为 3 个 pod,保存。

6. [student@workstation ~]$ oc create -f scaling.json
imagestreams/scaling
buildconfigs/scaling
deploymentconfigs/scaling
services/scaling

7. [student@workstation ~]$ watch oc status
最终会显示如下信息:
service/scaling - 172.30.136.254:8080
  dc/scaling deploys istag/scaling:latest <-
    builds http://workstation.pod0.example.com/scaling with openshift/php:latest through bc/scaling
    #1 deployed about a minute ago - 3 pods

To see more, use 'oc describe <resource>/<name>'.
You can use 'oc get all' to see a list of other objects.

8. [student@workstation ~]$ watch oc get pods
NAME              READY     STATUS       RESTARTS   AGE
scaling-1-221rz   1/1       Running      0          41s
scaling-1-9w9yy   1/1       Running      0          29s
scaling-1-build   0/1       ExitCode:0   0          2m
scaling-1-xg2ka   1/1       Running      0          29s

9. [student@workstation ~]$ oc get services
NAME      LABELS        SELECTOR                   IP(S)            PORT(S)
scaling   app=scaling   deploymentconfig=scaling   172.30.136.254   8080/TCP

10. [student@workstation ~]$ oc expose service scaling --hostname=scaling.cloudapps0.example.com

11. [student@workstation ~]$ for i in {1..5}; do curl -s http://scaling.cloudapps0.example.com | grep IP; done
 <br/> Server IP: 10.1.0.51
 <br/> Server IP: 10.1.0.52
 <br/> Server IP: 10.1.0.50
 <br/> Server IP: 10.1.0.51
 <br/> Server IP: 10.1.0.52
可以看出,5 次访问会显示 3 个不同的 pod IP 地址,说明弹性扩展成功。

13. 扩展到 5 个 pod
[student@workstation ~]$ oc describe dc scaling | grep -a Replicas
Replicas:    3
Replicas:    3 current / 3 desired

[student@workstation ~]$ oc scale --replicas=5 dc scaling

[student@workstation ~]$ oc get pods
NAME              READY     STATUS       RESTARTS   AGE
scaling-1-221rz   1/1       Running      0          11m
scaling-1-9w9yy   1/1       Running      0          11m
scaling-1-build   0/1       ExitCode:0   0          12m
scaling-1-rt5d4   1/1       Running     0          1m
scaling-1-s0sxi   1/1       Running     0          1m
scaling-1-xg2ka   1/1       Running      0          11m

[student@workstation ~]$ for i in {1..5}; do curl -s http://scaling.cloudapps0.example.com | grep IP; done
 <br/> Server IP: 10.1.0.50
 <br/> Server IP: 10.1.0.51
 <br/> Server IP: 10.1.0.52
 <br/> Server IP: 10.1.0.53
 <br/> Server IP: 10.1.0.54
可以看出,5 次访问会显示 5 个不同的 pod IP 地址,说明弹性扩展成功。

14. router 是怎样直接路由到 pod 的?
[root@master ~]# oc login -u system:admin
[root@master ~]# oc project default
[root@master ~]# oc get pods
NAME                      READY     STATUS    RESTARTS   AGE
docker-registry-2-wj9t5   1/1       Running   1          14d
oserouter-1-4q0xf         1/1       Running   1          14d

[root@master ~]# oc rsh oserouter-1-4q0xf

[root@oserouter-1-4q0xf conf]# cat haproxy.config
......
##-------------- app level backends ----------------


backend be_http_bookstore-bookstore-http-route

  mode http
  balance leastconn
  timeout check 5000ms

    cookie OPENSHIFT_bookstore-bookstore-http-route_SERVERID insert indirect nocache httponly


  server 10.1.0.8:8080 10.1.0.8:8080 check inter 5000ms cookie 10.1.0.8:8080


backend be_tcp_bookstore-bookstore-https-route
  balance source
  hash-type consistent
  timeout check 5000ms

  server 10.1.0.8:8443 10.1.0.8:8443 check inter 5000ms


backend be_http_scaling-scaling

  mode http
  balance leastconn
  timeout check 5000ms

    cookie OPENSHIFT_scaling-scaling_SERVERID insert indirect nocache httponly


  server 10.1.0.50:8080 10.1.0.50:8080 check inter 5000ms cookie 10.1.0.50:8080

  server 10.1.0.51:8080 10.1.0.51:8080 check inter 5000ms cookie 10.1.0.51:8080

  server 10.1.0.52:8080 10.1.0.52:8080 check inter 5000ms cookie 10.1.0.52:8080

  server 10.1.0.53:8080 10.1.0.53:8080 check inter 5000ms cookie 10.1.0.53:8080

  server 10.1.0.54:8080 10.1.0.54:8080 check inter 5000ms cookie 10.1.0.54:8080

没有评论: