📌

Google Cloud に OpenShift をデプロイする

に公開

クラウドエースの岸本です。
この記事では Google Cloud の Compute Engine を利用して OpenShift Container Platform 4.17 をデプロイする手順を解説します。
公式のドキュメントを参考にしながら、構築していきます。

前提条件

  • Google Cloud の Cloud Shell を利用すること
  • 作成者が Google Cloud Project のオーナーロールであること
  • DNS レコードの設定ができるドメインを取得していること
  • Red Hat Hybrid Cloud Console にアクセスできること(こちらからアカウント作成可能です)

手順

以下の手順で構築していきます。

  1. Google Cloud API の有効化
  2. DNS レコードの設定
  3. Quota の確認
  4. Service Account の作成とロールの付与
  5. Compute Engine の作成
  6. OpenShift のインストール
  7. OpenShift の初期設定

1. Google Cloud API の有効化

今回使用する API を有効化します。
こちらから有効化する API 一覧を確認できます。

# api の有効化
gcloud services enable compute.googleapis.com \
    cloudresourcemanager.googleapis.com \
    dns.googleapis.com \
    iamcredentials.googleapis.com \
    iam.googleapis.com \
    serviceusage.googleapis.com \
    servicemanagement.googleapis.com \
    storage-api.googleapis.com \
    storage-component.googleapis.com

2. DNS レコードの設定

今回使用するドメインを Cloud DNS のレコードに設定します。
このドメインは、OpenShift にアクセスするために使用します。
例として、example.com というドメインからopenshift.example.com というサブドメインを切り出して使用することを想定します。
まず、DNS ゾーンを作成します。

# ゾーンの作成
gcloud dns managed-zones create openshift-example-com \
    --dns-name=openshift.example.com \
    --description="openshift.example.com zone" \
    --project=YOUR_PROJECT_ID

次に、openshift.example.com の NS レコードを取得し、openshift.example.comのドメインを管理しているサービスに登録します。
詳しい手順はこちらを参照してください。

# レコードの確認
gcloud dns record-sets list --zone=openshift-example-com \
    --project=YOUR_PROJECT_ID

# 想定される出力
NAME: openhift.example.com.
TYPE: NS
TTL: 21600
DATA: ns-cloud-b1.googledomains.com.,ns-cloud-b2.googledomains.com.,ns-cloud-b3.googledomains.com.,ns-cloud-b4.googledomains.com.

NAME: openhift.example.com.
TYPE: SOA
TTL: 21600
DATA: ns-cloud-b1.googledomains.com. cloud-dns-hostmaster.google.com. 1 21600 3600 259200 300

3. Quota の確認

Quota とは、日本語で「割り当て」という意味です。
Google Cloud の各サービスには、リソースの使用量などの急増を防ぐために、Quota という制限が設けられています。
この割り当てを超えてリソースを作成しようとすると、エラーが発生し、作成できません。
使用中の Google Cloud Project のリソース割り当ては割り当てとシステム上限から確認できます。

以下の「Quota の確認」から、OpenShift のデプロイに必要な Quota を確認します。

Quota の確認

以下の表は公式ドキュメントのこちらを参照しました。

Service Component Location Total resources required Resources removed after bootstrap
Service account IAM Global 6 1
Firewall rules Compute Global 11 1
Forwarding rules Compute Global 2 0
In-use global IP addresses Compute Global 4 1
Health checks Compute Global 3 0
Images Compute Global 1 0
Networks Compute Global 2 0
Static IP addresses Compute Region 4 1
Routers Compute Global 1 0
Routes Compute Global 2 0
Subnetworks Compute Global 2 0
Target pools Compute Global 3 0
CPUs Compute Region 28 4
Persistent disk SSD (GB) Compute Region 896 128

リソースが足りない場合は、この手順に従いリクエストを送信してください。

4. Service Account の作成とロールの付与

OpenShift Container Platform には、Google APIs へのアクセス権限が必要です。
そのため、Service Account を作成し、必要な権限を付与します。

# Service Account の作成
gcloud iam service-accounts create openshift-sa \
    --display-name="OpenShift Service Account" \
    --project=YOUR_PROJECT_ID

次に、Service Account に必要な権限を付与します。
今回は、必要な権限が全て含まれている roles/owner ロールを付与します。
必要なロールと権限はこちらから確認できます。

# Service Account にロールを付与
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
    --member=serviceAccount:openshift-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com \
    --role=roles/owner

サービスアカウントのキーを作成します。

# サービスアカウントのキーを作成
gcloud iam service-accounts keys create ./sa-key.json \
  --iam-account openshift-sa@PROJECT_ID.iam.gserviceaccount.com

# 確認
ls

# 想定される出力
sa-key.json

5. Compute Engine の作成

踏み台サーバーとして、Red Hat Enterprise Linux 9 (以下、RHEL 9) のインスタンスを作成します。
はじめにインスタンスを配置するためのネットワークを作成します。

# ネットワークの作成
gcloud compute networks create openshift-network \
  --subnet-mode=custom

# サブネットの作成
gcloud compute networks subnets create openshift-subnet \
  --network=openshift-network \
  --range=10.0.0.0/24 \
  --region=us-central1 \
  --project=YOUR_PROJECT_ID

次に、Firewall ルールを作成します。
作成するルールは、SSH でのアクセスを許可するルールです。
ALLOW_IP_ADDRESS には、アクセスを許可する IP アドレスを指定してください。

# Firewall ルールの作成
gcloud compute firewall-rules create openshift-allow-ssh \
  --network=openshift-network \
  --allow=tcp:22 \
  --source-ranges=ALLOW_IP_ADDRESS \
  --project=YOUR_PROJECT_ID

Compute Engine を作成します。

# Compute Engine の作成
gcloud compute instances create rhel-9 \
  --image-project=rhel-cloud \
  --image-family=rhel-9 \
  --zone=us-central1-a \
  --machine-type=e2-small \
  --scopes=cloud-platform \
  --network-interface=network=openshift-network,subnet=openshift-subnet

サービスアカウントのキーを Compute Engine にアップロードします。

# サービスアカウントのキーを Compute Engine にアップロード
gcloud compute scp ./sa-key.json rhel-9:~ --zone=us-central1-a

Compute Engine に SSH で接続します。

# Compute Engine に SSH 接続
gcloud compute ssh rhel-9 --zone=us-central1-a \

6. OpenShift のインストール

踏み台として作成したインスタンスに OpenShift Container Platform をインストールします。
ここで必要な情報は、以下 2 点です。

  • OpenShift インストーラー
  • Pull Secret

Pull Secret とは、OpenShift のコンポーネント用イメージを取得するためのサービスやレジストリへのアクセス認証情報です。
各ユーザーごとに Pull Secret が生成されます。
Red Hat Hybrid Cloud Console にアクセスし、Pull Secret を取得してください。

Pull Secret の取得方法
  1. Red Hat Hybrid Cloud Consoleにアクセスします。
  2. ログインします。
  3. 以下の画面に推移し、赤枠をクリックします。
    Red Hat Hybrid Cloud Console
  4. 以下の画面のクラスタータイプをスクロールし、「Google Cloud」 を選択します。
    Red Hat Hybrid Cloud Console
  5. 今回は自動でクラスターを作成するため、左側の「Automated」を選択します。
    Red Hat Hybrid Cloud Console
  6. 以下の画面から Pull Secret を取得します。
    Red Hat Hybrid Cloud Console

OpenShift インストーラーをダウンロードします。
ここでは、RHEL 9 用のインストーラーをダウンロードします。

# インストーラーのダウンロード
curl -O https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/stable/openshift-install-rhel9-amd64.tar.gz
tar xvf openshift-install-rhel9-amd64.tar.gz

マスターノードにアクセスするための SSH キーを作成します。

# SSH キーの作成
ssh-keygen -t ed25519 -f ~/.ssh/openshift-master -N "" <<<y

インストール設定ファイルを作成します。
対話形式で設定を入力していきます。
pull secret は、Red Hat Hybrid Cloud Console から取得したものをぺーストしてください。

# 環境変数の設定
export GOOGLE_CLOUD_KEYFILE_JSON=sa-key.json

# インストーラーの実行
./openshift-install-fips create install-config

? SSH Public Key /home/User/.ssh/openshift-master.pub
? Platform gcp
INFO Credentials loaded from environment variable "GOOGLE_CLOUD_KEYFILE_JSON", file "/home/User/sa-key.json"
? Project ID YOUR_PROJECT_ID
? Region us-central1
? Base Domain openshift.example.com
? Cluster Name YOUR_CLUSTER_NAME
? Pull Secret [? for help] **************************************************************************************************************************************
INFO Install-Config created in: .

# 確認(今回作成されるインストール設定ファイル)
cat install-config.yaml

インストールを開始します。
インストールされた後に install-config.yaml が削除されるため、バックアップを取ります。

# バックアップ
cp install-config.yaml install-config.yaml.bak

# インストールの開始
./openshift-install-fips create cluster

インストールが完了すると、以下のようなログが表示されます。
後ほど、ログインするための情報などが出力されます。

INFO Credentials loaded from environment variable "GOOGLE_CLOUD_KEYFILE_JSON", file "sa-key.json"
INFO Consuming Install Config from target directory
・
・
INFO Waiting up to 45m0s (until 11:28AM UTC) for bootstrapping to complete...
INFO Destroying the bootstrap resources...
WARNING Destroying GCP Bootstrap Resources
INFO Removing firewall rules created by cluster-api-provider-gcp
INFO Waiting up to 5m0s for bootstrap machine deletion openshift-cluster-api-guests/xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
INFO Shutting down local Cluster API controllers...
INFO Stopped controller: Cluster API
WARNING process cluster-api-provider-gcp exited with error: signal: killed
INFO Stopped controller: gcp infrastructure provider
INFO Shutting down local Cluster API control plane...
INFO Local Cluster API system has completed operations
INFO no post-destroy requirements for the gcp provider
INFO Finished destroying bootstrap resources
INFO Waiting up to 40m0s (until 11:34AM UTC) for the cluster at https://api.openshift.open.example.com:6443 to initialize...
・
・
INFO Checking to see if there is a route at openshift-console/console...
INFO Install complete!
INFO To access the cluster as the system:admin user when using 'oc', run 'export KUBECONFIG=/home/User/auth/kubeconfig'
INFO Access the OpenShift web-console here: https://console-openshift-console.apps.openshift.open.example.com
INFO Login to the console with user: "kubeadmin", and password: "xxxx"
INFO Time elapsed: 36m8s

7. OpenShift の初期設定

ログインの方法は、Web コンソールまたは CLI の 2 つがあります。
それぞれ紹介します。

Web コンソール

ログに表示された URL にアクセスして、Web コンソールにログインします。
ログの中で URL が以下のように表示されています。

INFO Access the OpenShift web-console here: https://console-openshift-console.apps.openshift.open.example.com

ログインに必要なパスワードは、ログに表示されています。

# ログからパスワードの確認
INFO Login to the console with user: "kubeadmin", and password: "xxxxx"

以下のコマンドでもパスワードを確認できます。

# パスワードの確認
cat ./auth/kubeadmin-password

URL にアクセスして「kubeadmin」とパスワードを入力してログインします。
以下の画面が表示されたらログイン成功です。
OpenShift Console

途中で接続が切れた場合

以下のコマンドでログを確認できます。

# ログの確認
cat .openshift_install.log

または、Red Hat Hybrid Cloud Console から直接コンソールにアクセスできます。

CLI

OpenShift は、oc コマンドを使用します。
はじめに oc コマンドをインストールします。

# oc コマンドのインストール
curl -O https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/stable/openshift-client-linux.tar.gz
tar xvf openshift-client-linux.tar.gz

# 想定される出力
README.md
oc
kubectl

# パスの通し
sudo mv oc /usr/local/bin

# バージョンの確認
oc version

# 想定される出力
Client Version: 4.17.7
Kustomize Version: v5.0.4

ログインには以下の 2 つの作業が必要です。

  • CLI でログインする URL
  • 認証情報の設定

CLI でログインする URL は、ログに表示されています。
以下のようなログが表示されます。

INFO Waiting up to 20m0s (until xx:xxAM UTC) for the Kubernetes API at https://api.openshift.open.example.com:6443...

今回必要な情報は、https://api.openshift.open.example.com:6443 です。

次に、認証情報の設定を行います。
認証情報の設定もログに表示されています。

# ログの確認
INFO To access the cluster as the system:admin user when using 'oc', run 'export KUBECONFIG=/home/User/auth/kubeconfig'

具体的には、export KUBECONFIG=/home/User/auth/kubeconfig が必要です。

以上の情報を使用して、ログインします。

# 認証情報の設定
export KUBECONFIG=/home/User/auth/kubeconfig

# パスワードの確認
cat /home/User/auth/kubeadmin-password

# ログイン
oc login -u kubeadmin https://api.openshift.open.example.com:6443

# 想定される出力
Console URL: https://api.openshift.open.example.com:6443
Authentication required for https://api.openshift.open.example.com:6443 (openshift)
Username: kubeadmin
Password:
Login successful.

You have access to 70 projects, the list has been suppressed. You can list all projects with 'oc projects'

Using project "default".

以上で CLI でのログインが完了しました。

まとめ

以上で、Google Cloud 上に OpenShift Container Platform 4.17 をデプロイする手順を紹介しました。
これからは、OpenShift の機能を活用して、アプリケーションの開発などの環境を構築したいと思います。
ぜひこの手順を参考に、OpenShift のデプロイを行ってみてください。

Discussion