2016年12月12日星期一

OpenShift_011:从源代码到应用服务(通过 oc 命令)

环境:OSE 3.0.1

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

2. [student@workstation ~]$ cat php-helloworld/index.php
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
 </body>
</html>

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

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

5. [student@workstation ~]$ oc new-app http://workstation.pod0.example.com/php-helloworld
imagestreams/php-helloworld
buildconfigs/php-helloworld
deploymentconfigs/php-helloworld
services/php-helloworld
A build was created - you can run `oc start-build php-helloworld` to start it.
Service "php-helloworld" created at 172.30.32.253 with port mappings 8080.
Run 'oc status' to view your app.

说明:openshift 会根据根目录下的文件来选择 builder image。
(1)ruby:Rakefile, Gemfile, config.ru
(2)jee:pom.xml
(3)nodejs:app.json, package.json
(4)php:index.php, composer.json
(5)python:requirements.txt, config.py
(6)perl:index.pl, cpanfile

6. [student@workstation ~]$ watch oc status
(1)开始信息显示如下:
service/php-helloworld - 172.30.221.84:8080
  dc/php-helloworld deploys istag/php-helloworld:latest <-
    builds http://workstation.pod0.example.com/php-helloworld with openshift/php:latest through bc/php-helloworld
      not built yet
    #1 deployment waiting on image or update

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

说明:准备使用 openshift/php:latest 作为 builder image 根据 bc/php-helloworld 构建 istag/php-helloworld:latest。
此时,还没有生成 building pod,还没开始构建,部署也没开始,在等待应用镜像的生成或更新。

(2)过了一会,信息显示如下:
service/php-helloworld - 172.30.221.84:8080
  dc/php-helloworld deploys istag/php-helloworld:latest <-
    builds http://workstation.pod0.example.com/php-helloworld with openshift/php:latest through bc/php-helloworld
    build 1 pending/running for 2 seconds
    #1 deployment waiting on image or update

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

说明:准备使用 openshift/php:latest 作为 builder image 根据 bc/php-helloworld 构建 istag/php-helloworld:latest。
此时,building pod 生成,开始构建,但部署还没开始,在等待应用镜像的生成或更新。

(3)再过一会,信息显示如下:
service/php-helloworld - 172.30.221.84:8080
  dc/php-helloworld deploys istag/php-helloworld:latest <-
    builds http://workstation.pod0.example.com/php-helloworld with openshift/php:latest through bc/php-helloworld
    build 1 running for 2 seconds
    #1 deployment pending/running for 13 seconds - 1 pod

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

说明:准备使用 openshift/php:latest 作为 builder image 根据 bc/php-helloworld 构建 istag/php-helloworld:latest。
此时,building pod 已经生成并且构建应用镜像完毕,开始部署。

(4)最后,信息显示如下:
service/php-helloworld - 172.30.221.84:8080
  dc/php-helloworld deploys istag/php-helloworld:latest <-
    builds http://workstation.pod0.example.com/php-helloworld with openshift/php:latest through bc/php-helloworld
    #1 deployed 3 minutes ago - 1 pod

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

说明:准备使用 openshift/php:latest 作为 builder image 根据 bc/php-helloworld 构建 istag/php-helloworld:latest。
此时,应用镜像部署完毕。

7. [student@workstation ~]$ watch oc get pods
(1)一开始只显示 1 个 pod,即 build pod,状态从 Pending 到 Running
NAME                     READY     STATUS    RESTARTS   AGE
php-helloworld-1-build   0/1       Pending/   0          1s
...
php-helloworld-1-build   1/1       Running   0          1s
(2)接着显示 2 个 pod,build pod (已成功退出) 和 deploy pod(状态从 Pending 到 Running )
NAME                      READY     STATUS       RESTARTS   AGE
php-helloworld-1-build    0/1        ExitCode:0   0          18s
php-helloworld-1-deploy   0/1       Pending      0          3s
(3)接着会显示 3 个 pod,build pod (已成功退出) 和 deploy pod(正在运行)和 应用运行 pod
NAME                      READY     STATUS       RESTARTS   AGE
php-helloworld-1-build    0/1        ExitCode:0     0          42s
php-helloworld-1-deploy   1/1       Running      0          27s
php-helloworld-1-nktrf    1/1       Running         0          21s
(4)最后会显示 2 个 pod,build pod (已成功退出) 和 应用运行 pod
NAME                     READY     STATUS       RESTARTS   AGE
php-helloworld-1-build   0/1        ExitCode:0     0           1m
php-helloworld-1-zwfmi   1/1       Running      0          55s

8. 在 master 或 node 节点上测试 service
由于 service 是在内网中,因此在客户机上是不能访问 service 的 IP 的,要在 master 或 node 节点上访问
[root@master ~]# curl http://172.30.221.84:8080
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <p>Hello World</p>
 </body>

说明:测试 service

9. 查看 build 日志
[student@workstation ~]$ oc get builds
[student@workstation ~]$ oc build-logs php-helloworld-1
I1211 23:35:15.968884       1 builder.go:42] $BUILD env var is {"kind":"Build","apiVersion":"v1","metadata":{"name":"php-helloworld-1","namespace":"php-helloworld","selfLink":"/oapi/v1/namespaces/php-helloworld/builds/php-helloworld-1","uid":"5e53f933-c024-11e6-a71b-52540000000a","resourceVersion":"20099","creationTimestamp":"2016-12-12T04:35:11Z","labels":{"app":"php-helloworld","buildconfig":"php-helloworld"}},"spec":{"serviceAccount":"builder","source":{"type":"Git","git":{"uri":"http://workstation.pod0.example.com/php-helloworld"}},"strategy":{"type":"Source","sourceStrategy":{"from":{"kind":"DockerImage","name":"workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest"}}},"output":{"to":{"kind":"DockerImage","name":"172.30.215.148:5000/php-helloworld/php-helloworld:latest"},"pushSecret":{"name":"builder-dockercfg-hstvd"}},"resources":{}},"status":{"phase":"Pending","config":{"kind":"BuildConfig","namespace":"php-helloworld","name":"php-helloworld"}}}
I1211 23:35:15.972230       1 cfg.go:50] Problem accessing /root/.dockercfg: stat /root/.dockercfg: no such file or directory
I1211 23:35:15.976707       1 docker.go:199] Image workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest available locally
I1211 23:35:15.976816       1 sti.go:93] Creating a new S2I builder with build config: "Builder Name:\t\tApache 2.4 with PHP 5.5\nBuilder Image:\t\tworkstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest\nSource:\t\t\thttp://workstation.pod0.example.com/php-helloworld\nOutput Image Tag:\t172.30.215.148:5000/php-helloworld/php-helloworld:latest\nEnvironment:\t\tOPENSHIFT_BUILD_SOURCE=http://workstation.pod0.example.com/php-helloworld,OPENSHIFT_BUILD_NAME=php-helloworld-1,OPENSHIFT_BUILD_NAMESPACE=php-helloworld\nIncremental Build:\tdisabled\nRemove Old Build:\tdisabled\nForce Pull:\t\tdisabled\nQuiet:\t\t\tdisabled\nLayered Build:\t\tdisabled\nDocker Endpoint:\tunix:///var/run/docker.sock\n"
I1211 23:35:15.980585       1 docker.go:199] Image workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest available locally
I1211 23:35:15.984882       1 sti.go:99] Starting S2I build from php-helloworld/php-helloworld-1 BuildConfig ...
I1211 23:35:15.984928       1 sti.go:113] Building 172.30.215.148:5000/php-helloworld/php-helloworld:latest
I1211 23:35:15.996304       1 clone.go:27] Cloning into /tmp/sti265768138/upload/src
I1211 23:35:16.334322       1 docker.go:199] Image workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest available locally
I1211 23:35:16.334347       1 docker.go:305] Image contains io.openshift.s2i.scripts-url set to 'image:///usr/local/sti'
I1211 23:35:16.334370       1 download.go:57] Using image internal scripts from: image:///usr/local/sti/assemble
I1211 23:35:16.334378       1 download.go:57] Using image internal scripts from: image:///usr/local/sti/run
I1211 23:35:16.336868       1 docker.go:199] Image workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest available locally
I1211 23:35:16.336888       1 docker.go:305] Image contains io.openshift.s2i.scripts-url set to 'image:///usr/local/sti'
I1211 23:35:16.336907       1 download.go:57] Using image internal scripts from: image:///usr/local/sti/save-artifacts
I1211 23:35:16.336924       1 sti.go:186] Using assemble from image:///usr/local/sti
I1211 23:35:16.336933       1 sti.go:186] Using run from image:///usr/local/sti
I1211 23:35:16.336937       1 sti.go:186] Using save-artifacts from image:///usr/local/sti
I1211 23:35:16.336947       1 sti.go:121] Clean build will be performed
I1211 23:35:16.336950       1 sti.go:124] Performing source build from http://workstation.pod0.example.com/php-helloworld
I1211 23:35:16.336954       1 sti.go:134] Building 172.30.215.148:5000/php-helloworld/php-helloworld:latest
I1211 23:35:16.336959       1 sti.go:331] Using image name workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest
I1211 23:35:16.336972       1 sti.go:335] No .sti/environment provided (no environment file found in application sources)
I1211 23:35:16.337822       1 tar.go:133] Adding to tar: /tmp/sti265768138/upload/src/index.php as src/index.php
I1211 23:35:16.340402       1 docker.go:305] Image contains io.openshift.s2i.scripts-url set to 'image:///usr/local/sti'
I1211 23:35:16.340424       1 docker.go:367] Base directory for STI scripts is '/usr/local/sti'. Untarring destination is '/tmp'.
I1211 23:35:16.340438       1 docker.go:393] Creating container using config: {Hostname: Domainname: User: Memory:0 MemorySwap:0 CPUShares:0 CPUSet: AttachStdin:false AttachStdout:true AttachStderr:false PortSpecs:[] ExposedPorts:map[] Tty:false OpenStdin:true StdinOnce:true Env:[OPENSHIFT_BUILD_NAMESPACE=php-helloworld OPENSHIFT_BUILD_SOURCE=http://workstation.pod0.example.com/php-helloworld OPENSHIFT_BUILD_NAME=php-helloworld-1] Cmd:[/bin/sh -c tar -C /tmp -xf - && /usr/local/sti/assemble] DNS:[] Image:workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest Volumes:map[] VolumesFrom: WorkingDir: MacAddress: Entrypoint:[] NetworkDisabled:false SecurityOpts:[] OnBuild:[] Labels:map[]}
I1211 23:35:16.697905       1 docker.go:400] Attaching to container
I1211 23:35:16.700510       1 docker.go:457] Starting container
I1211 23:35:16.897439       1 docker.go:467] Waiting for container
I1211 23:35:16.947620       1 sti.go:393] ---> Installing application source ...
I1211 23:35:17.092675       1 docker.go:473] Container exited
I1211 23:35:17.092705       1 docker.go:479] Invoking postExecution function
I1211 23:35:17.092728       1 sti.go:220] No .sti/environment provided (no environment file found in application sources)
I1211 23:35:17.092765       1 docker.go:513] Committing container with config: {Hostname: Domainname: User: Memory:0 MemorySwap:0 CPUShares:0 CPUSet: AttachStdin:false AttachStdout:false AttachStderr:false PortSpecs:[] ExposedPorts:map[] Tty:false OpenStdin:false StdinOnce:false Env:[OPENSHIFT_BUILD_NAME=php-helloworld-1 OPENSHIFT_BUILD_NAMESPACE=php-helloworld OPENSHIFT_BUILD_SOURCE=http://workstation.pod0.example.com/php-helloworld] Cmd:[/usr/local/sti/run] DNS:[] Image: Volumes:map[] VolumesFrom: WorkingDir: MacAddress: Entrypoint:[] NetworkDisabled:false SecurityOpts:[] OnBuild:[] Labels:map[io.openshift.s2i.build.image:workstation.pod0.example.com:5000/openshift3/php-55-rhel7:latest io.openshift.s2i.build.commit.author:root <root@workstation.pod0.example.com> io.openshift.s2i.build.commit.date:Mon Nov 28 12:14:49 2016 +0800 io.openshift.s2i.build.commit.id:45ce7bd io.openshift.s2i.build.commit.ref:master io.openshift.s2i.build.commit.message:Initial commit io.openshift.s2i.build.source-location:http://workstation.pod0.example.com/php-helloworld io.k8s.display-name:172.30.215.148:5000/php-helloworld/php-helloworld:latest]}
I1211 23:35:19.413031       1 sti.go:251] Successfully built 172.30.215.148:5000/php-helloworld/php-helloworld:latest
I1211 23:35:19.456430       1 cleanup.go:23] Removing temporary directory /tmp/sti265768138
I1211 23:35:19.456454       1 fs.go:99] Removing directory '/tmp/sti265768138'
I1211 23:35:19.457993       1 cfg.go:46] PUSH_DOCKERCFG_PATH=/var/run/secrets/openshift.io/push/.dockercfg
I1211 23:35:19.458242       1 cfg.go:64] Using serviceaccount user for Docker authentication
I1211 23:35:19.458255       1 sti.go:146] Using provided push secret for pushing 172.30.215.148:5000/php-helloworld/php-helloworld:latest image
I1211 23:35:19.458261       1 sti.go:149] Pushing 172.30.215.148:5000/php-helloworld/php-helloworld:latest image ...
I1211 23:35:26.382736       1 sti.go:153] Successfully pushed 172.30.215.148:5000/php-helloworld/php-helloworld:latest

10. 创建 route
[student@workstation ~]$ oc create -f route.json
route.json 内容如下:
{
  "apiVersion": "v1",
  "kind": "Route",
  "metadata": {
    "name": "php-helloworld"
  },
  "spec": {
    "host": "php-helloworld.cloudapps0.example.com",
    "to": {
      "kind": "Service",
      "name": "php-helloworld"
    }
  }
}

[student@workstation ~]$ oc describe route php-helloworld
Name:            php-helloworld
Created:        50 seconds ago
Labels:            <none>
Annotations:        openshift.io/host.generated=false
Host:            php-helloworld.cloudapps0.example.com
Path:            <none>
Service:        php-helloworld
TLS Termination:    <none>

[student@workstation ~]$ curl http://php-helloworld.cloudapps0.example.com

没有评论: