第1章 OpenShift CLI (oc)

1.1. CLI の使用方法

1.1.1. CLI について

OpenShift Container Platform のコマンドラインインターフェイス (CLI) を使用すると、ターミナルからアプリケーションを作成し、OpenShift Container Platform プロジェクトを管理できます。CLI の使用は、以下の場合に適しています。

  • プロジェクトのソースコードを直接使用している。
  • OpenShift Container Platform の操作をスクリプト化する。
  • 帯域幅リソースの制限下にあり、Web コンソールを使用できない。

1.1.2. CLI のインストール

OpenShift CLI(oc) をインストールするには、バイナリーをダウンロードするか、RPM を使用します。

1.1.2.1. バイナリーのダウンロードによる CLI のインストール

コマンドラインインターフェイスを使用して OpenShift Container Platform と対話するために CLI (oc) をインストールすることができます。oc は Linux、Windows、または macOS にインストールできます。

重要

以前のバージョンの oc をインストールしている場合、これを使用して OpenShift Container Platform 4.5 のすべてのコマンドを実行することはできません。新規バージョンの oc をダウンロードし、インストールします。

1.1.2.1.1. Linux への CLI のインストール

以下の手順を使用して、OpenShift CLI (oc) バイナリーを Linux にインストールできます。

手順

  1. Red Hat OpenShift Cluster Manager サイトの Infrastructure Provider ページに移動します。
  2. インフラストラクチャープロバイダーを選択し、(該当する場合は) インストールタイプを選択します。
  3. Command-line interface セクションで、ドロップダウンメニューの Linux を選択し、Download command-line tools をクリックします。
  4. アーカイブを展開します。

    $ tar xvzf <file>
  5. oc バイナリーを、PATH にあるディレクトリーに配置します。

    PATH を確認するには、以下のコマンドを実行します。

    $ echo $PATH

CLI のインストール後は、oc コマンドを使用して利用できます。

$ oc <command>
1.1.2.1.2. Windows での CLI のインストール

以下の手順を使用して、OpenShift CLI (oc) バイナリーを Windows にインストールできます。

手順

  1. Red Hat OpenShift Cluster Manager サイトの Infrastructure Provider ページに移動します。
  2. インフラストラクチャープロバイダーを選択し、(該当する場合は) インストールタイプを選択します。
  3. Command-line interface セクションで、ドロップダウンメニューの Windows を選択し、Download command-line tools をクリックします。
  4. ZIP プログラムでアーカイブを解凍します。
  5. oc バイナリーを、PATH にあるディレクトリーに移動します。

    PATH を確認するには、コマンドプロンプトを開いて以下のコマンドを実行します。

    C:\> path

CLI のインストール後は、oc コマンドを使用して利用できます。

C:\> oc <command>
1.1.2.1.3. macOS への CLI のインストール

以下の手順を使用して、OpenShift CLI (oc) バイナリーを macOS にインストールできます。

手順

  1. Red Hat OpenShift Cluster Manager サイトの Infrastructure Provider ページに移動します。
  2. インフラストラクチャープロバイダーを選択し、(該当する場合は) インストールタイプを選択します。
  3. Command-line interface セクションで、ドロップダウンメニューの MacOS を選択し、Download command-line tools をクリックします。
  4. アーカイブを展開し、解凍します。
  5. oc バイナリーをパスにあるディレクトリーに移動します。

    PATH を確認するには、ターミナルを開き、以下のコマンドを実行します。

    $ echo $PATH

CLI のインストール後は、oc コマンドを使用して利用できます。

$ oc <command>

1.1.2.2. RPM を使用した CLI のインストール

Red Hat Enterprise Linux (RHEL) の場合、Red Hat アカウントに有効な OpenShift Container Platform サブスクリプションがある場合は、OpenShift CLI (oc) を RPM としてインストールできます。

前提条件

  • root または sudo の権限が必要です。

手順

  1. Red Hat Subscription Manager に登録します。

    # subscription-manager register
  2. 最新のサブスクリプションデータをプルします。

    # subscription-manager refresh
  3. 利用可能なサブスクリプションを一覧表示します。

    # subscription-manager list --available --matches '*OpenShift*'
  4. 直前のコマンドの出力で、OpenShift Container Platform サブスクリプションのプール ID を見つけ、これを登録されたシステムにアタッチします。

    # subscription-manager attach --pool=<pool_id>
  5. OpenShift Container Platform 4.5 で必要なリポジトリーを有効にします。

    • Red Hat Enterprise Linux 8 の場合:

      # subscription-manager repos --enable="rhocp-4.5-for-rhel-8-x86_64-rpms"
    • Red Hat Enterprise Linux 7 の場合:

      # subscription-manager repos --enable="rhel-7-server-ose-4.5-rpms"
  6. openshift-clients パッケージをインストールします。

    # yum install openshift-clients

CLI のインストール後は、oc コマンドを使用して利用できます。

$ oc <command>

1.1.3. CLI へのログイン

oc CLI にログインしてクラスターにアクセスし、これを管理できます。

前提条件

  • OpenShift Container Platform クラスターへのアクセスがあること。
  • CLI をインストールしていること。
注記

HTTP プロキシーサーバー上でのみアクセスできるクラスターにアクセスするには、HTTP_PROXYHTTPS_PROXY および NO_PROXY 変数を設定できます。これらの環境変数は、クラスターとのすべての通信が HTTP プロキシーを経由するように oc CLI で使用されます。

手順

  • oc login コマンドを使用して CLI にログインし、プロンプトが出されたら必要な情報を入力します。

    $ oc login

    出力例

    Server [https://localhost:8443]: https://openshift.example.com:6443 1
    The server uses a certificate signed by an unknown authority.
    You can bypass the certificate check, but any data you send to the server could be intercepted by others.
    Use insecure connections? (y/n): y 2
    
    Authentication required for https://openshift.example.com:6443 (openshift)
    Username: user1 3
    Password: 4
    Login successful.
    
    You don't have any projects. You can try to create a new project, by running
    
        oc new-project <projectname>
    
    Welcome! See 'oc help' to get started.

    1
    OpenShift Container Platform サーバー URL を入力します。
    2
    非セキュアな接続を使用するかどうかを入力します。
    3
    ログインに使用するユーザー名を入力します。
    4
    ユーザーのパスワードを入力します。

これで、プロジェクトを作成でき、クラスターを管理するための他のコマンドを実行することができます。

1.1.4. CLI の使用

以下のセクションで、CLI を使用して一般的なタスクを実行する方法を確認します。

1.1.4.1. プロジェクトの作成

新規プロジェクトを作成するには、oc new-project コマンドを使用します。

$ oc new-project my-project

出力例

Now using project "my-project" on server "https://openshift.example.com:6443".

1.1.4.2. 新しいアプリケーションの作成

新規アプリケーションを作成するには、oc new-app コマンドを使用します。

$ oc new-app https://github.com/sclorg/cakephp-ex

出力例

--> Found image 40de956 (9 days old) in imagestream "openshift/php" under tag "7.2" for "php"

...

    Run 'oc status' to view your app.

1.1.4.3. Pod の表示

現在のプロジェクトの Pod を表示するには、oc get pods コマンドを使用します。

$ oc get pods -o wide

出力例

NAME                  READY   STATUS      RESTARTS   AGE     IP            NODE                           NOMINATED NODE
cakephp-ex-1-build    0/1     Completed   0          5m45s   10.131.0.10   ip-10-0-141-74.ec2.internal    <none>
cakephp-ex-1-deploy   0/1     Completed   0          3m44s   10.129.2.9    ip-10-0-147-65.ec2.internal    <none>
cakephp-ex-1-ktz97    1/1     Running     0          3m33s   10.128.2.11   ip-10-0-168-105.ec2.internal   <none>

1.1.4.4. Pod ログの表示

特定の Pod のログを表示するには、oc logs コマンドを使用します。

$ oc logs cakephp-ex-1-deploy

出力例

--> Scaling cakephp-ex-1 to 1
--> Success

1.1.4.5. 現在のプロジェクトの表示

現在のプロジェクトを表示するには、oc project コマンドを使用します。

$ oc project

出力例

Using project "my-project" on server "https://openshift.example.com:6443".

1.1.4.6. 現在のプロジェクトのステータスの表示

サービス、デプロイメント、およびビルド設定などの現在のプロジェクトについての情報を表示するには、oc status コマンドを使用します。

$ oc status

出力例

In project my-project on server https://openshift.example.com:6443

svc/cakephp-ex - 172.30.236.80 ports 8080, 8443
  dc/cakephp-ex deploys istag/cakephp-ex:latest <-
    bc/cakephp-ex source builds https://github.com/sclorg/cakephp-ex on openshift/php:7.2
    deployment #1 deployed 2 minutes ago - 1 pod

3 infos identified, use 'oc status --suggest' to see details.

1.1.4.7. サポートされる API のリソースの一覧表示

サーバー上でサポートされる API リソースの一覧を表示するには、oc api-resources コマンドを使用します。

$ oc api-resources

出力例

NAME                                  SHORTNAMES       APIGROUP                              NAMESPACED   KIND
bindings                                                                                     true         Binding
componentstatuses                     cs                                                     false        ComponentStatus
configmaps                            cm                                                     true         ConfigMap
...

1.1.5. ヘルプの表示

CLI コマンドおよび OpenShift Container Platform リソースに関するヘルプを以下の方法で表示することができます。

  • 利用可能なすべての CLI コマンドの一覧および説明を表示するには、oc help を使用します。

    例: CLI についての一般的なヘルプの表示

    $ oc help

    出力例

    OpenShift Client
    
    This client helps you develop, build, deploy, and run your applications on any OpenShift or Kubernetes compatible
    platform. It also includes the administrative commands for managing a cluster under the 'adm' subcommand.
    
    Usage:
      oc [flags]
    
    Basic Commands:
      login           Log in to a server
      new-project     Request a new project
      new-app         Create a new application
    
    ...

  • 特定の CLI コマンドについてのヘルプを表示するには、--help フラグを使用します。

    例: oc create コマンドについてのヘルプの表示

    $ oc create --help

    出力例

    Create a resource by filename or stdin
    
    JSON and YAML formats are accepted.
    
    Usage:
      oc create -f FILENAME [flags]
    
    ...

  • 特定リソースについての説明およびフィールドを表示するには、oc explain コマンドを使用します。

    例: Pod リソースのドキュメントの表示

    $ oc explain pods

    出力例

    KIND:     Pod
    VERSION:  v1
    
    DESCRIPTION:
         Pod is a collection of containers that can run on a host. This resource is
         created by clients and scheduled onto hosts.
    
    FIELDS:
       apiVersion	<string>
         APIVersion defines the versioned schema of this representation of an
         object. Servers should convert recognized schemas to the latest internal
         value, and may reject unrecognized values. More info:
         https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
    
    ...

1.1.6. CLI からのログアウト

CLI からログアウトし、現在のセッションを終了することができます。

  • oc logout コマンドを使用します。

    $ oc logout

    出力例

    Logged "user1" out on "https://openshift.example.com"

これにより、サーバーから保存された認証トークンが削除され、設定ファイルから除去されます。