LoginSignup
1
2

More than 5 years have passed since last update.

OpenShift Origin Try-It-Out の実施と mongoDB のデプロイ

Last updated at Posted at 2016-08-04

はじめに

経緯

前回 『GMOクラウドALTUS Basic with Docker上に OpenShift Origin をインストールする』にて OpenShift Origin を Docker コンテナで起動するところまでを記載しました。

今回は、その環境を使って OpenShift Origin の Try It Out の実施と MongoDB のデプロイ方法について記載します。

コンセプト

  • OpenShift Origin をなるべく簡単に試す。

概要

  • OpenShift Origin 環境を準備します
  • OpenShift Origin の Try It Out を試します
  • MongoDB を Openshift Origin にデプロイします

OpenShift Origin 環境の準備

Docker 上で OpenShift Origin コンテナが実行される環境を利用します。
実行環境の準備がお済みでない方は、前回の記事 で環境を構築していますので参考までにどうぞ。

利用環境

項目 内容
サーバー GMOクラウド ALTUS Basicシリーズ
利用テンプレート Ubuntu 14.04 64bit with Docker_20160428
仮想サーバーの種類 Custom Server
CPU 2vCPU
Memory 4GB
HDD 20GB
Docker v1.11.0
VMのローカルIP(例) 10.0.0.1
OpenShift Origin v1.3.0-alpha.2-250-g61eba05
kubernetes v1.3.0-alpha.3-599-g2746284

overview

※172.30.1.1, 172.30.2.2 については Docker の内部ネットワークで割り当てられたローカルIPアドレスの例となります。

ログイン・ログアウト、プロジェクトの操作

前回の記事では WebUI の起動まで確認を行いましたが、Try It Out に記載のあるとおり CLI にて操作していきます。

ログインからプロジェクトの作成まで

1.OpenShift を操作するために Docker 上で起動している OpenShift Origin のコンテナに接続します。

# docker exec -it origin bash
※origin はコンテナ名になりますので適宜利用環境により変更してください。

2.oc コマンドで OpenShift Origin にログインします。

# oc login
Username : test
Password : test

Authentication required for https://10.0.0.1:8443 (openshift)
Username: test
Password:
Login successful.

You don't have any projects. You can try to create a new project, by running

    oc new-project <projectname>

【補足】複数プロジェクトが存在していた場合、ログアウト後に再度ログインすると、下記のように最後に利用していたプロジェクトが選択されます。

Login successful.

You have access to the following projects and can switch between them with 'oc project <projectname>':

  * mongo-db
    test

Using project "mongo-db".

3.新しいプロジェクトを作成します。

# oc new-project test

Now using project "test" on server "https://10.0.0.1:8443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

to build a new example application in Ruby.

【補足】既にプロジェクトが存在する場合は、下記のようにエラーになります。異なるユーザーでも同じプロジェクト名で作成しようとするとエラーになるので注意が必要です。
Error from server: project "test" already exists

その他操作コマンド

  • ログアウト
    # oc logout

  • プロジェクト一覧の取得
    # oc get project

NAME       DISPLAY NAME   STATUS
mongo-db                     Active
test                         Active
delete-test                  Active
  • プロジェクトの切替え
    # oc project mongo-db
Now using project "mongo-db" on server "https://10.0.0.1:8443".
  • プロジェクトの削除
    # oc delete project delete-test
project "delete-test" deleted

Try It Out 実施

OpenShift Origin にてプロジェクト作成後、Try It Out に記載の方法でアプリケーションをデプロイしていきます。
※8080, 8443 ポートに制限がかかっていないことをご確認ください。

下記の操作は docker exec -it origin bash で OpenShift Origin コンテナ(コマンドの origin はコンテナ名)に接続して実行しています。

用語

用語 略語 概要
Pod pod デプロイする際の最小単位。1つ以上のコンテナを含みます。
Image Stream is OpenShit Origin で利用されるコンテナ本体。Docker Image のようなもの。
Replication Controller rc Pod の同時起動数制御役
Service svc Pod への通信分散役
Deployment Config dc Replication Controller の制御や Pod の起動役

デプロイ後のイメージ

service_and_etc

Try It Out

1.Try It Out で利用されている Node.jsベースのサンプルアプリケーションをデプロイします。

# oc new-app openshift/deployment-example

--> Found Docker image 1c839d8 (12 months old) from Docker Hub for "openshift/deployment-example"

    * An image stream will be created as "deployment-example:latest" that will track this image
    * This image will be deployed in deployment config "deployment-example"
    * Port 8080/tcp will be load balanced by service "deployment-example"
      * Other containers can access this service through the hostname "deployment-example"
    * WARNING: Image "openshift/deployment-example" runs as the 'root' user which may not be permitted by your cluster administrator

--> Creating resources with label app=deployment-example ...
    imagestream "deployment-example" created
    deploymentconfig "deployment-example" created
    service "deployment-example" created
--> Success
    WARNING: No Docker registry has been configured with the server. Automatic builds and deployments may not function.
    Run 'oc status' to view your app.

【補足】下記は、Image Stream 以外を削除した後に再度 oc new-app コマンドを実行した結果となります。 Image Stream や Deployment Config, Service などが既に登録されている場合、Error および Failed と出力されますが最終的には、問題なくデプロイされているようでした。
※コンテナの種類や状態によっては上手くいかないものもあるかもしれませんのでご留意ください。
※OpenShift Origin 上に構築した環境の削除方法は後述しています。

# oc new-app openshift/deployment-example

--> Found Docker image 1c839d8 (12 months old) from Docker Hub for "openshift/deployment-example"

    * An image stream will be created as "deployment-example:latest" that will track this image
    * This image will be deployed in deployment config "deployment-example"
    * Port 8080/tcp will be load balanced by service "deployment-example"
      * Other containers can access this service through the hostname "deployment-example"
    * WARNING: Image "openshift/deployment-example" runs as the 'root' user which may not be permitted by your cluster administrator

--> Creating resources with label app=deployment-example ...
    error: imagestreams "deployment-example" already exists
    deploymentconfig "deployment-example" created
    service "deployment-example" created
--> Failed

寄り道

Openshift Origin の WebUI にログインすると Pod や Service などが作成されたことを確認できます。

openshift-login-test-project

2.上記で作成したアプリケーションの状態やIPアドレスを確認します。

# oc status

In project test on server https://10.0.0.1:8443

svc/deployment-example - 172.30.1.1:8080
  dc/deployment-example deploys istag/deployment-example:latest
    deployment #1 deployed 55 seconds ago - 1 pod

1 warning identified, use 'oc status -v' to see details.

3.接続確認します。(IPアドレスは deployment-example に割り当てられた例となります)

ローカルIPとなるため、このままでは直接 Web ブラウザで接続確認できません。
ポートフォーワードして Web ブラウザから確認するか OpenShift Origin コンテナから curlコマンドで確認する事ができます。

  • ポートフォワードする場合は、下記を設定後、 http://localhost:8888 にアクセスしてください。
    ※設定は TeraTermPro の例になります。

[ローカルポート] : 8888(例)
[リモート側ホスト] : deployment-example(Node.js) のローカルIP 172.30.1.1(例)
[ポート] : 8080
openshift-origin-try-it-out-port-forward

openshift-origin-deployment-example

  • curl コマンドによる確認
    curl コマンドで確認する場合は OpenShift Origin のコンテナにて下記コマンドを実行します。 ※IPアドレスは、oc statusで確認した deployment-example(Node.js) のローカル IP アドレスの例となります。
    # curl http://172.30.1.1:8080
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Deployment Demonstration</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    HTML{height:100%;}
    BODY{font-family:Helvetica,Arial;display:flex;display:-webkit-flex;align-items:center;justify-content:center;-webkit-align-items:center;-webkit-box-align:center;-webkit-justify-content:center;height:100%;}
    .box{background:#006e9c;color:white;text-align:center;border-radius:10px;display:inline-block;}
    H1{font-size:10em;line-height:1.5em;margin:0 0.5em;}
    H2{margin-top:0;}
  </style>
</head>
<body>
<div class="box"><h1>v1</h1><h2></h2></div>
</body>
</html>[root@openshift-test3 origin]#

4.その他確認コマンド

  • Pod の確認
    # oc get pods
NAME                         READY     STATUS    RESTARTS   AGE
deployment-example-1-ugzlm   1/1       Running   0          6m
  • Service の確認
    # oc get services
NAME                 CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
deployment-example   172.30.1.1   <none>        8080/TCP   7m
  • Replication Controller の確認
    # oc get rc
NAME                   DESIRED   CURRENT   AGE
deployment-example-1   1         1         7m
  • Deployment Config の確認
    # oc get dc
NAME                 REVISION   DESIRED   CURRENT   TRIGGERED BY
deployment-example   1          1         1         config,image(deployment-example:latest)
  • Image Stream の確認
    # oc get is
NAME                 DOCKER REPO   TAGS      UPDATED
deployment-example                 latest    8 minutes ago

Try It Out の確認は以上となります。

MongoDB のデプロイ(公式の情報はこちら)

前提条件

  • Docker OpenShift Origin コンテナに接続
  • oc login 済み

MongoDB デプロイ開始

事前準備として、上述のプロジェクト操作にて、任意のプロジェクトを作成して切り替えます。

1.プロジェクトの作成、切り替え

# oc new-project mongo-db
# oc project mongo-db

2.oc new-app コマンドで MongoDB をデプロイします。

下記内容は適宜変更して利用してください。

<USER_NAME>
<USER_PASS>
<DB_NAME>
<ADMIN_PASS>

# oc new-app -e \    MONGODB_USER='<USER_NAME>',MONGODB_PASSWORD='<USER_PASS>',MONGODB_DATABASE='<DB_NAME>',MONGODB_ADMIN_PASSWORD='<ADMIN_PASS>' \
    centos/mongodb-26-centos7
--> Found Docker image 00c0f2d (43 hours old) from Docker Hub for "centos/mongodb-26-centos7"

    MongoDB 2.6
    -----------
    MongoDB is a scalable, high-performance, open source NoSQL database.

    Tags: database, mongodb, rh-mongodb26

    * An image stream will be created as "mongodb-26-centos7:latest" that will track this image
    * This image will be deployed in deployment config "mongodb-26-centos7"
    * Port 27017/tcp will be load balanced by service "mongodb-26-centos7"
      * Other containers can access this service through the hostname "mongodb-26-centos7"
    * This image declares volumes and will default to use non-persistent, host-local storage.
      You can add persistent volumes later by running 'volume dc/mongodb-26-centos7 --add ...'

--> Creating resources with label app=mongodb-26-centos7 ...
    imagestream "mongodb-26-centos7" created
    deploymentconfig "mongodb-26-centos7" created
    service "mongodb-26-centos7" created
--> Success
    WARNING: No Docker registry has been configured with the server. Automatic builds and deployments may not function.
    Run 'oc status' to view your app.

3.pod 名を確認します。

# oc get pods

NAME                         READY     STATUS    RESTARTS   AGE
mongodb-26-centos7-1-vee52   1/1       Running   0          1m

# oc get services

NAME                 CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
mongodb-26-centos7   172.30.2.2   <none>        27017/TCP   1m

4.oc rsh コマンドで、対象の pod にリモートシェル接続します。

# oc rsh mongodb-26-centos7-1-vee52

sh-4.2$

5.mongo コマンドで DB に接続します。

サービスデプロイ時に下記が環境環境変数に登録されているので、その一部を利用してDBにアクセスします。

MONGODB_USER='<USER_NAME>'
MONGODB_PASSWORD='<USER_PASS>'
MONGODB_DATABASE='<DB_NAME>'
MONGODB_ADMIN_PASSWORD='<ADMIN_PASS>'

$ mongo $MONGODB_DATABASE -u $MONGODB_USER -p $MONGODB_PASSWORD

MongoDB shell version: 2.6.9
connecting to: <DB_NAME>
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user

db.stats() で問題なくデプロイできていることを確認し、exit で DB から抜けます。
なお、MongoDB の確認は、MongoDBのCRUD を参考にさせて頂きました。

MongoDB のデプロイについては、以上となります。

Pod や Service の削除方法について

Try It Out で利用した deployment-example の削除を例に削除方法について記載しています。

後片付け開始

1.Deployment Config を削除します。

# oc delete dc deployment-example

deploymentconfig "deployment-example" deleted

2.Deployment Config 削除後、Deployment Config, Replication Controller, Pod も一緒に削除されていることを確認します。

もし削除できていないものがあった場合は oc delete で対照のオブジェクトを削除してください。

# oc get dc
# oc get rc
# oc get pods

3.Service を確認します。

# oc get services

NAME                 CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
deployment-example   172.30.1.1   <none>        8080/TCP   7m

4.Service を削除します。

# oc delete services deployment-example

service "deployment-example" deleted

5.Service が削除されたことを確認します。

# oc get services

6.Image Stream を削除します。

# oc delete is deployment-example

imagestream "deployment-example" deleted

7.Image Stream が削除されたことを確認します。

# oc get is

【注意事項】
Pod は、いくら消しても rc(Replication Controller) の Pod 起動数を 0 にするか dc(Deployment Config) を消さなければ新規生成し続けられます。

おわりに

ひとこと

ざっくりな内容でしたが、最後までお付き合い頂きありがとうございます。 OpenShift Origin については、一旦終了予定です。

OpenShift Origin の中でも構成や内容は簡単なことを選んでいますが、 事前知識がなかったので難しかったなぁというのが正直な感想です。

本格的な利用を考える場合には、 Kubernetes の Service や Pod といった OpenShift Origin の中でも中核を担っている概念について、知っておく必要があると感じました。

今後も気になる技術を試した際には、記事の投稿にもトライしていきたいと思います。

開発・運用者視点で考えてみたこと。

  • 開発者視点
    コンテナの起動は早いし、多重化も簡単なので、アプリケーションのデプロイ自動化環境まで作れたら、取り入れたいと思えるポイントになるのではないかと感じました。

  • 運用者視点
    Docker と Kubernetes と OpenShift の知識が必要になるため、学習コストが少し高いように思われましたが Service ごとに状態を確認したり、WebUI からコンソール画面使えたりするのは便利なので、ちょっとした環境で利用する分には良いと思いました。

    しかし、問題切り分けまで求められる環境では運用するにあたって切り分けに苦労しそうなので、安定稼動を目指す環境で自分が運用するのであれば少し敬遠したくなる構成だと感じました。

参考

Pods and Services(公式:英語)
OpenShift Origin(公式:英語)
Try It Out(公式:英語)
OpenShift v3 Technical Overview(SlideShare)
OpenShift v3 と source-to-image (s2i)(Qiita)
MongoDBのCRUD(Qiita)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2