Openshift-getting-started

提供:Dev Guides
移動先:案内検索

OpenShift-はじめに

OpenShiftは、GUIまたはCLIによってアプリケーションを作成およびデプロイするための2種類の中央値で構成されています。 この章では、CLIを使用して新しいアプリケーションを作成します。 OCクライアントを使用してOpenShift環境と通信します。

新しいアプリケーションを作成する

OpenShiftには、新しいアプリケーションを作成する3つの方法があります。

  • ソースコードから
  • 画像から
  • テンプレートから

ソースコードから

ソースコードからアプリケーションを作成しようとすると、OpenShiftは、アプリケーションビルドフローを定義するレポジトリ内に存在するDockerファイルを探します。 oc new-appを使用してアプリケーションを作成します。

レポジトリを使用する際に最初に留意すべきことは、OpenShiftがコードをプルしてビルドするレポジトリのオリジンを指す必要があるということです。

レポジトリがOCクライアントがインストールされているDockerマシンで複製され、ユーザーが同じディレクトリ内にある場合、次のコマンドを使用して作成できます。

$ oc new-app . <Hear. Denotes current working directory>

以下は、特定のブランチのリモートリポジトリからビルドしようとする例です。

$ oc new-app https://github.com/openshift/Testing-deployment.git#test1

ここで、test1はOpenShiftで新しいアプリケーションを作成しようとしているブランチです。

リポジトリでDockerファイルを指定する場合、以下に示すようにビルド戦略を定義する必要があります。

$ oc new-app OpenShift/OpenShift-test~https://github.com/openshift/Testingdeployment.git

画像から

イメージを使用してアプリケーションを構築している間、イメージはローカルDockerサーバー、社内ホストDockerリポジトリ、またはDockerハブに存在します。 ユーザーが確認する必要があるのは、ハブから問題なく画像をプルするアクセス権があることだけです。

OpenShiftには、Dockerイメージまたはソースストリームのどちらであるかを使用するソースを決定する機能があります。 ただし、ユーザーが希望する場合は、イメージストリームかDockerイメージかを明示的に定義できます。

$ oc new-app - - docker-image tomcat

画像ストリームを使用する-

$ oc new-app tomcat:v1

テンプレートから

テンプレートは、新しいアプリケーションの作成に使用できます。 既存のテンプレートまたは新しいテンプレートを作成できます。

次のyamlファイルは、基本的に展開に使用できるテンプレートです。

apiVersion: v1
kind: Template
metadata:
   name: <Name of template>
   annotations:
      description: <Description of Tag>
      iconClass: "icon-redis"
      tags: <Tages of image>
objects:
   - apiVersion: v1
   kind: Pod
   metadata:
      name: <Object Specification>
spec:
   containers:
      image: <Image Name>
      name: master
      ports:
      - containerPort: <Container port number>
         protocol: <Protocol>
labels:
   redis: <Communication Type>

Webアプリケーションの開発と展開

OpenShiftでの新しいアプリケーションの開発

OpenShiftで新しいアプリケーションを作成するには、新しいアプリケーションコードを記述し、OpenShift OCビルドコマンドを使用してビルドする必要があります。 説明したように、新しいイメージを作成する方法は複数あります。 ここでは、テンプレートを使用してアプリケーションを構築します。 このテンプレートは、oc new-appコマンドで実行すると、新しいアプリケーションを構築します。

次のテンプレートが作成されます-2つのフロントエンドアプリケーションと1つのデータベース。 それとともに、2つの新しいサービスを作成し、それらのアプリケーションをOpenShiftクラスターにデプロイします。 アプリケーションをビルドおよびデプロイする際、最初にOpenShiftでネームスペースを作成し、そのネームスペースの下にアプリケーションをデプロイする必要があります。

新しい名前空間を作成

$ oc new-project openshift-test --display-name = "OpenShift 3 Sample" --
description = "This is an example project to demonstrate OpenShift v3"

テンプレート

{
   "kind": "Template",
   "apiVersion": "v1",
   "metadata": {
      "name": "openshift-helloworld-sample",
      "creationTimestamp": null,
         "annotations": {
         "description": "This example shows how to create a simple openshift
         application in openshift origin v3",
         "iconClass": "icon-openshift",
         "tags": "instant-app,openshift,mysql"
      }
   }
},

オブジェクト定義

テンプレートの秘密の定義

"objects": [
{
   "kind": "Secret",
   "apiVersion": "v1",
   "metadata": {"name": "dbsecret"},
   "stringData" : {
      "mysql-user" : "${MYSQL_USER}",
      "mysql-password" : "${MYSQL_PASSWORD}"
   }
},

テンプレート内のサービス定義

{
   "kind": "Service",
   "apiVersion": "v1",
   "metadata": {
      "name": "frontend",
      "creationTimestamp": null
   },
   "spec": {
      "ports": [
         {
            "name": "web",
            "protocol": "TCP",
            "port": 5432,
            "targetPort": 8080,
            "nodePort": 0
         }
      ],
      "selector": {"name": "frontend"},
      "type": "ClusterIP",
      "sessionAffinity": "None"
   },
   "status": {
      "loadBalancer": {}
   }
},

テンプレートでのルート定義

{
   "kind": "Route",
   "apiVersion": "v1",
   "metadata": {
      "name": "route-edge",
      "creationTimestamp": null,
      "annotations": {
         "template.openshift.io/expose-uri": "http://{.spec.host}{.spec.path}"
      }
   },
   "spec": {
      "host": "www.example.com",
      "to": {
         "kind": "Service",
         "name": "frontend"
      },
      "tls": {
         "termination": "edge"
      }
   },
   "status": {}
},
{
   "kind": "ImageStream",
   "apiVersion": "v1",
   "metadata": {
      "name": "origin-openshift-sample",
      "creationTimestamp": null
   },
   "spec": {},
   "status": {
      "dockerImageRepository": ""
   }
},
{
   "kind": "ImageStream",
   "apiVersion": "v1",
   "metadata": {
      "name": "openshift-22-ubuntu7",
      "creationTimestamp": null
   },
   "spec": {
      "dockerImageRepository": "ubuntu/openshift-22-ubuntu7"
   },
   "status": {
      "dockerImageRepository": ""
   }
},

テンプレートで構成定義を作成

{
   "kind": "BuildConfig",
   "apiVersion": "v1",
   "metadata": {
      "name": "openshift-sample-build",
      "creationTimestamp": null,
      "labels": {name": "openshift-sample-build"}
   },
   "spec": {
      "triggers": [
         { "type": "GitHub",
            "github": {
            "secret": "secret101" }
         },
         {
            "type": "Generic",
            "generic": {
               "secret": "secret101",
               "allowEnv": true }
         },
         {
            "type": "ImageChange",
            "imageChange": {}
         },
         { "type": "ConfigChange”}
      ],
      "source": {
         "type": "Git",
         "git": {
            "uri": https://github.com/openshift/openshift-hello-world.git }
      },
      "strategy": {
         "type": "Docker",
         "dockerStrategy": {
            "from": {
               "kind": "ImageStreamTag",
               "name": "openshift-22-ubuntu7:latest”
            },
            "env": [
               {
                  "name": "EXAMPLE",
                  "value": "sample-app"
               }
            ]
         }
      },
      "output": {
         "to": {
            "kind": "ImageStreamTag",
            "name": "origin-openshift-sample:latest"
         }
      },
      "postCommit": {
         "args": ["bundle", "exec", "rake", "test"]
      },
      "status": {
         "lastVersion": 0
      }
   }
},

テンプレートでの展開設定

"status": {
   "lastVersion": 0
}
{
   "kind": "DeploymentConfig",
   "apiVersion": "v1",
   "metadata": {
      "name": "frontend",
      "creationTimestamp": null
   }
},
"spec": {
   "strategy": {
      "type": "Rolling",
      "rollingParams": {
         "updatePeriodSeconds": 1,
         "intervalSeconds": 1,
         "timeoutSeconds": 120,
         "pre": {
            "failurePolicy": "Abort",
            "execNewPod": {
               "command": [
                  "/bin/true"
               ],
               "env": [
                  {
                     "name": "CUSTOM_VAR1",
                     "value": "custom_value1"
                  }
               ]
            }
         }
      }
   }
}
"triggers": [
   {
      "type": "ImageChange",
      "imageChangeParams": {
         "automatic": true,
         "containerNames": [
            "openshift-helloworld"
         ],
         "from": {
            "kind": "ImageStreamTag",
            "name": "origin-openshift-sample:latest"
         }
      }
   },
   {
      "type": "ConfigChange"
   }
],
"replicas": 2,
"selector": {
   "name": "frontend"
},
"template": {
   "metadata": {
      "creationTimestamp": null,
      "labels": {
         "name": "frontend"
      }
   },
   "spec": {
      "containers": [
         {
            "name": "openshift-helloworld",
            "image": "origin-openshift-sample",
            "ports": [
               {
                  "containerPort": 8080,
                  "protocol": "TCP”
               }
            ],
            "env": [
               {
                  "name": "MYSQL_USER",
                  "valueFrom": {
                     "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-user"
                     }
                  }
               },
               {
                  "name": "MYSQL_PASSWORD",
                  "valueFrom": {
                     "secretKeyRef" : {
                        "name" : "dbsecret",
                        "key" : "mysql-password"
                     }
                  }
               },
               {
                  "name": "MYSQL_DATABASE",
                  "value": "${MYSQL_DATABASE}"
               }
            ],
            "resources": {},
            "terminationMessagePath": "/dev/termination-log",
            "imagePullPolicy": "IfNotPresent",
            "securityContext": {
               "capabilities": {},
               "privileged": false
            }
         }
      ],
      "restartPolicy": "Always",
      "dnsPolicy": "ClusterFirst"
   },
   "status": {}
},

テンプレート内のサービス定義

{
   "kind": "Service",
   "apiVersion": "v1",
   "metadata": {
      "name": "database",
      "creationTimestamp": null
   },
   "spec": {
   "ports": [
      {
         "name": "db",
         "protocol": "TCP",
         "port": 5434,
         "targetPort": 3306,
         "nodePort": 0
      }
   ],
   "selector": {
      "name": "database
   },
   "type": "ClusterIP",
   "sessionAffinity": "None" },
   "status": {
      "loadBalancer": {}
   }
},
  • テンプレートでの展開構成定義 *
{
   "kind": "DeploymentConfig",
   "apiVersion": "v1",
   "metadata": {
      "name": "database",
      "creationTimestamp": null
   },
   "spec": {
      "strategy": {
         "type": "Recreate",
         "resources": {}
      },
      "triggers": [
         {
            "type": "ConfigChange"
         }
      ],
      "replicas": 1,
      "selector": {"name": "database"},
      "template": {
         "metadata": {
            "creationTimestamp": null,
            "labels": {"name": "database"}
         },
         "template": {
            "metadata": {
               "creationTimestamp": null,
               "labels": {
                  "name": "database"
               }
            },
            "spec": {
               "containers": [
                  {
                     "name": "openshift-helloworld-database",
                     "image": "ubuntu/mysql-57-ubuntu7:latest",
                     "ports": [
                        {
                           "containerPort": 3306,
                           "protocol": "TCP"
                        }
                     ],
                     "env": [
                        {
                           "name": "MYSQL_USER",
                           "valueFrom": {
                              "secretKeyRef" : {
                                 "name" : "dbsecret",
                                 "key" : "mysql-user"
                              }
                           }
                        },
                        {
                           "name": "MYSQL_PASSWORD",
                           "valueFrom": {
                              "secretKeyRef" : {
                                 "name" : "dbsecret",
                                 "key" : "mysql-password"
                              }
                           }
                        },
                        {
                           "name": "MYSQL_DATABASE",
                           "value": "${MYSQL_DATABASE}"
                        }
                     ],
                     "resources": {},
                     "volumeMounts": [
                        {
                           "name": "openshift-helloworld-data",
                           "mountPath": "/var/lib/mysql/data"
                        }
                     ],
                     "terminationMessagePath": "/dev/termination-log",
                     "imagePullPolicy": "Always",
                     "securityContext": {
                        "capabilities": {},
                        "privileged": false
                     }
                  }
               ],
               "volumes": [
                  {
                     "name": "openshift-helloworld-data",
                     "emptyDir": {"medium": ""}
                  }
               ],
               "restartPolicy": "Always",
               "dnsPolicy": "ClusterFirst”
            }
         }
      },
      "status": {}
   },
   "parameters": [
      {
         "name": "MYSQL_USER",
         "description": "database username",
         "generate": "expression",
         "from": "user[A-Z0-9]{3}",
         "required": true
      },
      {
         "name": "MYSQL_PASSWORD",
         "description": "database password",
         "generate": "expression",
         "from": "[a-zA-Z0-9]{8}",
         "required": true
      },
      {
         "name": "MYSQL_DATABASE",
         "description": "database name",
         "value": "root",
         "required": true
      }
   ],
   "labels": {
      "template": "application-template-dockerbuild"
   }
}

上記のテンプレートファイルは、一度にコンパイルする必要があります。 最初にすべてのコンテンツを1つのファイルにコピーし、完了したらyamlファイルとして名前を付ける必要があります。

アプリケーションを作成するには、次のコマンドを実行する必要があります。

$ oc new-app application-template-stibuild.json
--> Deploying template openshift-helloworld-sample for "application-template-stibuild.json"

   openshift-helloworld-sample
   ---------
   This example shows how to create a simple ruby application in openshift origin v3
  * With parameters:
 *MYSQL_USER = userPJJ # generated
     * MYSQL_PASSWORD = cJHNK3se # generated
      * MYSQL_DATABASE = root

--> Creating resources with label app = ruby-helloworld-sample ...
   service "frontend" created
   route "route-edge" created
   imagestream "origin-ruby-sample" created
   imagestream "ruby-22-centos7" created
   buildconfig "ruby-sample-build" created
   deploymentconfig "frontend" created
   service "database" created
   deploymentconfig "database" created

--> Success
   Build scheduled, use 'oc logs -f bc/ruby-sample-build' to track its progress.
   Run 'oc status' to view your app.

ビルドを監視したい場合、それを使用して行うことができます-

$ oc get builds

NAME                        TYPE      FROM          STATUS     STARTED         DURATION
openshift-sample-build-1    Source   Git@bd94cbb    Running    7 seconds ago   7s

以下を使用して、OpenShiftにデプロイされたアプリケーションを確認できます-

$ oc get pods
NAME                            READY   STATUS      RESTARTS   AGE
database-1-le4wx                1/1     Running     0          1m
frontend-1-e572n                1/1     Running     0          27s
frontend-1-votq4                1/1     Running     0          31s
opeshift-sample-build-1-build   0/1     Completed   0          1m

サービス定義に従ってアプリケーションサービスが作成されているかどうかを確認できます。

$ oc get services
NAME        CLUSTER-IP      EXTERNAL-IP     PORT(S)      SELECTOR          AGE
database    172.30.80.39    <none>         5434/TCP     name=database      1m
frontend    172.30.17.4     <none>         5432/TCP     name=frontend      1m