Red Hat Training
A Red Hat training course is available for OpenShift Container Platform
第6章 プロジェクトレベルのタスク
6.1. プロジェクトのバックアップ
関連するすべてのデータのバックアップの作成には、すべての重要な情報をエクスポートし、新規プロジェクトに復元することが関係します。
OpenShift Container Platform のプロジェクトのバックアップおよび復元ツールについては、現在 Red Hat で開発中です。詳細は、以下のバグを参照してください。
手順
バックアップ予定の関連データをすべて一覧表示します。
$ oc get all NAME TYPE FROM LATEST bc/ruby-ex Source Git 1 NAME TYPE FROM STATUS STARTED DURATION builds/ruby-ex-1 Source Git@c457001 Complete 2 minutes ago 35s NAME DOCKER REPO TAGS UPDATED is/guestbook 10.111.255.221:5000/myproject/guestbook latest 2 minutes ago is/hello-openshift 10.111.255.221:5000/myproject/hello-openshift latest 2 minutes ago is/ruby-22-centos7 10.111.255.221:5000/myproject/ruby-22-centos7 latest 2 minutes ago is/ruby-ex 10.111.255.221:5000/myproject/ruby-ex latest 2 minutes ago NAME REVISION DESIRED CURRENT TRIGGERED BY dc/guestbook 1 1 1 config,image(guestbook:latest) dc/hello-openshift 1 1 1 config,image(hello-openshift:latest) dc/ruby-ex 1 1 1 config,image(ruby-ex:latest) NAME DESIRED CURRENT READY AGE rc/guestbook-1 1 1 1 2m rc/hello-openshift-1 1 1 1 2m rc/ruby-ex-1 1 1 1 2m NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/guestbook 10.111.105.84 <none> 3000/TCP 2m svc/hello-openshift 10.111.230.24 <none> 8080/TCP,8888/TCP 2m svc/ruby-ex 10.111.232.117 <none> 8080/TCP 2m NAME READY STATUS RESTARTS AGE po/guestbook-1-c010g 1/1 Running 0 2m po/hello-openshift-1-4zw2q 1/1 Running 0 2m po/ruby-ex-1-build 0/1 Completed 0 2m po/ruby-ex-1-rxc74 1/1 Running 0 2m
プロジェクトオブジェクトを
.yamlまたは.jsonファイルにエクスポートします。プロジェクトオブジェクトを
project.yamlファイルにエクスポートするには、以下を実行します。$ oc export all -o yaml > project.yaml
プロジェクトオブジェクトを
project.jsonファイルにエクスポートするには、以下を実行します。$ oc export all -o json > project.json
プロジェクトの
role bindings、secrets、service accounts、およびpersistent volume claimsをエクスポートします。$ for object in rolebindings serviceaccounts secrets imagestreamtags podpreset cms egressnetworkpolicies rolebindingrestrictions limitranges resourcequotas pvcs templates cronjobs statefulsets hpas deployments replicasets poddisruptionbudget endpoints do oc export $object -o yaml > $object.yaml done
一部のエクスポートされたオブジェクトはプロジェクト内の特定のメタデータまたは固有の ID への参照に依存する場合があります。これは、再作成されるオブジェクトのユーザビリティーにおける制限になります。
imagestreamsの使用時に、deploymentconfigのimageパラメーターは、復元される環境に存在しない内部レジストリー内のイメージの特定のshaチェックサムをポイントする場合があります。たとえば、サンプル "ruby-ex" をoc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.gitとして実行すると、イメージをホストするための内部レジストリーを使用するimagestreamruby-exが作成されます。$ oc get dc ruby-ex -o jsonpath="{.spec.template.spec.containers[].image}" 10.111.255.221:5000/myproject/ruby-ex@sha256:880c720b23c8d15a53b01db52f7abdcbb2280e03f686a5c8edfef1a2a7b21ceeoc exportでのエクスポートと同じ方法で、deploymentconfigをインポートすると、イメージが存在しない場合には失敗します。このようなエクスポートを作成するには、
openshift-ansible-contribリポジトリーのproject_export.shを使用します。これにより、複数の異なるファイルに、すべてのプロジェクトオブジェクトが作成されます。このスクリプトの実行先ホストに、このプロジェクト名が指定されたディレクトリーと、そのプロジェクトに含まれるオブジェクトタイプごとのjsonファイルが、このスクリプトにより作成されます。注記以下で参照される
openshift-ansible-contribリポジトリーのコードは Red Hat で明示的にサポートされている訳ではありませんが、リファレンスアーキテクチャーチームはコードが定義通りに動作し、安全であることを確認するためにテストを実行しています。スクリプトは Linux で実行され、これには
jqおよびocコマンドがインストールされている必要があります。また、対象のプロジェクトにある全オブジェクトを読み取ることのできるユーザーで、OpenShift Container Platform 環境のシステムにログインしておく必要があります。$ mkdir ~/git $ cd ~/git $ git clone https://github.com/openshift/openshift-ansible-contrib.git $ cd openshift-ansible-contrib/reference-architecture/day2ops/scripts $ ./project_export.sh <projectname>
例:
$ ./project_export.sh myproject Exporting namespace to project-demo/ns.json Exporting rolebindings to project-demo/rolebindings.json Exporting serviceaccounts to project-demo/serviceaccounts.json Exporting secrets to project-demo/secrets.json Exporting deploymentconfigs to project-demo/dc_*.json Patching DC... Exporting buildconfigs to project-demo/bcs.json Exporting builds to project-demo/builds.json Exporting imagestreams to project-demo/iss.json Exporting imagestreamtags to project-demo/imagestreamtags.json Exporting replicationcontrollers to project-demo/rcs.json Exporting services to project-demo/svc_*.json Exporting pods to project-demo/pods.json Exporting podpreset to project-demo/podpreset.json Exporting configmaps to project-demo/cms.json Exporting egressnetworkpolicies to project-demo/egressnetworkpolicies.json Exporting rolebindingrestrictions to project-demo/rolebindingrestrictions.json Exporting limitranges to project-demo/limitranges.json Exporting resourcequotas to project-demo/resourcequotas.json Exporting pvcs to project-demo/pvcs.json Exporting routes to project-demo/routes.json Exporting templates to project-demo/templates.json Exporting cronjobs to project-demo/cronjobs.json Exporting statefulsets to project-demo/statefulsets.json Exporting hpas to project-demo/hpas.json Exporting deployments to project-demo/deployments.json Exporting replicasets to project-demo/replicasets.json Exporting poddisruptionbudget to project-demo/poddisruptionbudget.json
これが実行されたら、ファイルを確認し、コンテンツが適切にエクスポートされていることを確認します。
$ cd <projectname> $ ls -1 bcs.json builds.json cms.json cronjobs.json dc_ruby-ex.json dc_ruby-ex_patched.json deployments.json egressnetworkpolicies.json endpoint_external-mysql-service.json hpas.json imagestreamtags.json iss.json limitranges.json ns.json poddisruptionbudget.json podpreset.json pods.json pvcs.json rcs.json replicasets.json resourcequotas.json rolebindingrestrictions.json rolebindings.json routes.json secrets.json serviceaccounts.json statefulsets.json svc_external-mysql-service.json svc_ruby-ex.json templates.json $ less bcs.json ...
注記元のオブジェクトが存在しない場合、エクスポート時に空のファイルが作成されます。
imagestreamsを使用している場合、スクリプトはdeploymentconfigをイメージshaの代わりにイメージ参照を使用するように変更し、_patchedの追加情報を使用してエクスポートされたもの以外のjsonファイルを作成します。$ diff dc_hello-openshift.json dc_hello-openshift_patched.json 45c45 < "image": "docker.io/openshift/hello-openshift@sha256:42b59c869471a1b5fdacadf778667cecbaa79e002b7235f8091540ae612f0e14", --- > "image": "hello-openshift:latest",
現時点でこのスクリプトは複数のコンテナー Pod をサポートしていないため、注意して使用してください。
6.2. プロジェクトの復元
プロジェクトを復元するために、oc create -f pods.json を実行して新規プロジェクトを作成してから、エクスポートされたファイルを復元します。ただし、プロジェクトをゼロから復元するには、一部のプロジェクトが他のプロジェクトに依存していることから特定の順序を指定する必要があります。たとえば、pods を作成する前に configmaps を作成する必要があります。
手順
プロジェクトが単一ファイルとしてエクスポートされている場合は、以下のコマンドを実行してこのファイルをインポートします。
$ oc new-project <projectname> $ oc create -f project.yaml $ oc create -f secret.yaml $ oc create -f serviceaccount.yaml $ oc create -f pvc.yaml $ oc create -f rolebindings.yaml
警告Pod およびデフォルトサービスアカウントなどの一部のリソースは作成できない場合があります。
project_export.shスクリプトを使用してプロジェクトをエクスポートした場合、ファイルはprojectnameディレクトリーにあります。それらは、project_import.shスクリプトを再度実行してインポートできます。このスクリプトは適切な順序でoc createプロセスを実行します。$ mkdir ~/git $ cd ~/git $ git clone https://github.com/openshift/openshift-ansible-contrib.git $ cd openshift-ansible-contrib/reference-architecture/day2ops/scripts $ ./project_import.sh <projectname_path>
例:
$ ls ~/backup/myproject bcs.json dc_guestbook_patched.json dc_ruby-ex_patched.json pvcs.json secrets.json builds.json dc_hello-openshift.json iss.json rcs.json serviceaccounts.json cms.json dc_hello-openshift_patched.json ns.json rolebindings.json svcs.json dc_guestbook.json dc_ruby-ex.json pods.json routes.json templates.json $ ./project_import.sh ~/backup/myproject namespace "myproject" created rolebinding "admin" created rolebinding "system:deployers" created rolebinding "system:image-builders" created rolebinding "system:image-pullers" created secret "builder-dockercfg-mqhs6" created secret "default-dockercfg-51xb9" created secret "deployer-dockercfg-6kvz7" created Error from server (AlreadyExists): error when creating "myproject//serviceaccounts.json": serviceaccounts "builder" already exists Error from server (AlreadyExists): error when creating "myproject//serviceaccounts.json": serviceaccounts "default" already exists Error from server (AlreadyExists): error when creating "myproject//serviceaccounts.json": serviceaccounts "deployer" already exists error: no objects passed to create service "guestbook" created service "hello-openshift" created service "ruby-ex" created imagestream "guestbook" created imagestream "hello-openshift" created imagestream "ruby-22-centos7" created imagestream "ruby-ex" created error: no objects passed to create error: no objects passed to create buildconfig "ruby-ex" created build "ruby-ex-1" created deploymentconfig "guestbook" created deploymentconfig "hello-openshift" created deploymentconfig "ruby-ex" created replicationcontroller "ruby-ex-1" created Error from server (AlreadyExists): error when creating "myproject//rcs.json": replicationcontrollers "guestbook-1" already exists Error from server (AlreadyExists): error when creating "myproject//rcs.json": replicationcontrollers "hello-openshift-1" already exists pod "guestbook-1-c010g" created pod "hello-openshift-1-4zw2q" created pod "ruby-ex-1-rxc74" created Error from server (AlreadyExists): error when creating "myproject//pods.json": object is being deleted: pods "ruby-ex-1-build" already exists error: no objects passed to create
注記serviceaccountsおよびシークレットなどの一部のオブジェクトがプロジェクト作成時に自動的に作成されるためにAlreadyExistsエラーが表示される場合があります。buildconfigsを使用するかどうかを確認します。$ oc get bc NAME TYPE FROM LATEST ruby-ex Source Git 1 $ oc get pods NAME READY STATUS RESTARTS AGE guestbook-1-plnnq 1/1 Running 0 26s hello-openshift-1-g4g0j 1/1 Running 0 26s
buildconfigsを使用する場合、ビルドは自動的にトリガーされず、アプリケーションは実行されません。buildconfigsを使用する場合、ビルドをトリガーするにはoc start-buildコマンドを実行します。$ for bc in $(oc get bc -o jsonpath="{.items[*].metadata.name}") do oc start-build ${bc} donePod はビルドの完了後にデプロイされます。
プロジェクトが復元されたことを確認するには、以下を実行します。
$ oc get all NAME TYPE FROM LATEST bc/ruby-ex Source Git 2 NAME TYPE FROM STATUS STARTED DURATION builds/ruby-ex-1 Source Git Error (BuildPodDeleted) About a minute ago builds/ruby-ex-2 Source Git@c457001 Complete 55 seconds ago 12s NAME DOCKER REPO TAGS UPDATED is/guestbook 10.111.255.221:5000/myproject/guestbook latest About a minute ago is/hello-openshift 10.111.255.221:5000/myproject/hello-openshift latest About a minute ago is/ruby-22-centos7 10.111.255.221:5000/myproject/ruby-22-centos7 latest About a minute ago is/ruby-ex 10.111.255.221:5000/myproject/ruby-ex latest 43 seconds ago NAME REVISION DESIRED CURRENT TRIGGERED BY dc/guestbook 1 1 1 config,image(guestbook:latest) dc/hello-openshift 1 1 1 config,image(hello-openshift:latest) dc/ruby-ex 1 1 1 config,image(ruby-ex:latest) NAME DESIRED CURRENT READY AGE rc/guestbook-1 1 1 1 1m rc/hello-openshift-1 1 1 1 1m rc/ruby-ex-1 1 1 1 43s NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/guestbook 10.111.126.115 <none> 3000/TCP 1m svc/hello-openshift 10.111.23.21 <none> 8080/TCP,8888/TCP 1m svc/ruby-ex 10.111.162.157 <none> 8080/TCP 1m NAME READY STATUS RESTARTS AGE po/guestbook-1-plnnq 1/1 Running 0 1m po/hello-openshift-1-g4g0j 1/1 Running 0 1m po/ruby-ex-1-h99np 1/1 Running 0 42s po/ruby-ex-2-build 0/1 Completed 0 55s
注記サービスおよび Pod IP アドレスは作成時に動的に割り当てられるので異なっています。
6.3. Persistent Volume Claim (永続ボリューム要求) のバックアップ
コンテナー内の永続データをサーバーと同期できます。
OpenShift Container Platform 環境をホストする一部のプロバイダーでは、バックアップおよび復元目的でサードパーティーのスナップショットサービスを起動する機能がある場合があります。ただし、OpenShift Container Platform ではこれらのサービスを起動する機能を提供していないため、本書ではこれらの手順については説明しません。
特定のアプリケーションに関する正しいバックアップ手順は、製品のドキュメントを参照してください。たとえば、mysql データディレクトリー自体をコピーしても使用可能なバックアップは作成されません。その代わりに、OpenShift Container Platform がホストするプラットフォームで提供されるスナップショットソリューションを使用するなど、関連のアプリケーション特有のバックアップ手順を実行してから、データを同期してください。
手順
プロジェクトおよび Pod を表示します。
$ oc get pods NAME READY STATUS RESTARTS AGE demo-1-build 0/1 Completed 0 2h demo-2-fxx6d 1/1 Running 0 1h
永続ボリュームで使用されているボリュームを検索できるように必要な Pod の情報を取得します。
$ oc describe pod demo-2-fxx6d Name: demo-2-fxx6d Namespace: test Security Policy: restricted Node: ip-10-20-6-20.ec2.internal/10.20.6.20 Start Time: Tue, 05 Dec 2017 12:54:34 -0500 Labels: app=demo deployment=demo-2 deploymentconfig=demo Status: Running IP: 172.16.12.5 Controllers: ReplicationController/demo-2 Containers: demo: Container ID: docker://201f3e55b373641eb36945d723e1e212ecab847311109b5cee1fd0109424217a Image: docker-registry.default.svc:5000/test/demo@sha256:0a9f2487a0d95d51511e49d20dc9ff6f350436f935968b0c83fcb98a7a8c381a Image ID: docker-pullable://docker-registry.default.svc:5000/test/demo@sha256:0a9f2487a0d95d51511e49d20dc9ff6f350436f935968b0c83fcb98a7a8c381a Port: 8080/TCP State: Running Started: Tue, 05 Dec 2017 12:54:52 -0500 Ready: True Restart Count: 0 Volume Mounts: */opt/app-root/src/uploaded from persistent-volume (rw)* /var/run/secrets/kubernetes.io/serviceaccount from default-token-8mmrk (ro) Environment Variables: <none> ...omitted...この出力は永続データが
/opt/app-root/src/uploadedディレクトリーにあることを示しています。データをローカルにコピーします。
$ oc rsync demo-2-fxx6d:/opt/app-root/src/uploaded ./demo-app receiving incremental file list uploaded/ uploaded/ocp_sop.txt uploaded/lost+found/ sent 38 bytes received 190 bytes 152.00 bytes/sec total size is 32 speedup is 0.14
ocp_sop.txtファイルはローカルシステムにダウンロードされ、バックアップソフトウェアまたは別のバックアップメカニズムでバックアップされます。注記また、
pvcを使用せずに Pod を起動する場合には、直前の手順を使用することもできますが、pvcが必要かどうかを後で確認する必要があります。データを保存してから復元プロセスを使用し、新規ストレージを生成することができます。
6.4. Persistent Volume Claim (永続ボリューム要求、PVC) の復元
バックアップした Persistent Volume Claim (永続ボリューム要求、PVC) データを復元することができます。ファイルを削除してからそのファイルを予想される場所に戻すか、または Persistent Volume Claim (永続ボリューム要求) を移行することができます。ストレージを移行する必要がある場合や、バックエンドストレージがすでに存在しないなどの障害発生時には移行する必要がある場合があります。
特定のアプリケーションの適切な復元手順については、それぞれの製品ドキュメントを参照してください。
6.4.1. ファイルの既存 PVC への復元
手順
ファイルを削除します。
$ oc rsh demo-2-fxx6d sh-4.2$ ls */opt/app-root/src/uploaded/* lost+found ocp_sop.txt sh-4.2$ *rm -rf /opt/app-root/src/uploaded/ocp_sop.txt* sh-4.2$ *ls /opt/app-root/src/uploaded/* lost+found
PVC にあったファイルの rsync バックアップが含まれるサーバーのファイルを置き換えます。
$ oc rsync uploaded demo-2-fxx6d:/opt/app-root/src/
oc rshを使用してファイルが Pod に戻されていることを確認し、Pod に接続してディレクトリーのコンテンツを表示します。$ oc rsh demo-2-fxx6d sh-4.2$ *ls /opt/app-root/src/uploaded/* lost+found ocp_sop.txt
6.4.2. データの新規 PVC への復元
以下の手順では、新規 pvc が作成されていることを前提としています。
手順
現在定義されている
claim-nameを上書きします。$ oc volume dc/demo --add --name=persistent-volume \ --type=persistentVolumeClaim --claim-name=filestore \ --mount-path=/opt/app-root/src/uploaded --overwrite
Pod が新規 PVC を使用していることを確認します。
$ oc describe dc/demo Name: demo Namespace: test Created: 3 hours ago Labels: app=demo Annotations: openshift.io/generated-by=OpenShiftNewApp Latest Version: 3 Selector: app=demo,deploymentconfig=demo Replicas: 1 Triggers: Config, Image(demo@latest, auto=true) Strategy: Rolling Template: Labels: app=demo deploymentconfig=demo Annotations: openshift.io/container.demo.image.entrypoint=["container-entrypoint","/bin/sh","-c","$STI_SCRIPTS_PATH/usage"] openshift.io/generated-by=OpenShiftNewApp Containers: demo: Image: docker-registry.default.svc:5000/test/demo@sha256:0a9f2487a0d95d51511e49d20dc9ff6f350436f935968b0c83fcb98a7a8c381a Port: 8080/TCP Volume Mounts: /opt/app-root/src/uploaded from persistent-volume (rw) Environment Variables: <none> Volumes: persistent-volume: Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace) *ClaimName: filestore* ReadOnly: false ...omitted...デプロイメント設定では新規の
pvcを使用しているため、oc rsyncを実行してファイルを新規のpvcに配置します。$ oc rsync uploaded demo-3-2b8gs:/opt/app-root/src/ sending incremental file list uploaded/ uploaded/ocp_sop.txt uploaded/lost+found/ sent 181 bytes received 39 bytes 146.67 bytes/sec total size is 32 speedup is 0.15
oc rshを使用してファイルが Pod に戻されていることを確認し、Pod に接続してディレクトリーのコンテンツを表示します。$ oc rsh demo-3-2b8gs sh-4.2$ ls /opt/app-root/src/uploaded/ lost+found ocp_sop.txt
6.5. イメージおよびコンテナーのプルーニング
収集されたデータおよびオブジェクトの古いバージョンのプルーニングについての詳細は、「Pruning Resources (リソースのプルーニング)」のトピックを参照してください。