はじめに
久しぶりにGCPを触る機会があったので、GCP環境上にOpenShiftをIPIインストールしてみました。備忘を兼ねて手順をまとめてみます。
前提条件
- ローカル端末にgcloudをインストールしていること
- GCP Projectを払い出していること
- 該当Projectにてowner権限をもつIAMユーザーを持っていること
- ドメインを取得していること
- 今回はAWS Route53で取得したドメインを使用します。
手順①:GCP設定
まずはOpenShiftをデプロイするためにいくつかGCP側の設定を行います。
基本的にこちらのドキュメントに書いてある準備を行えばOKです。
まずローカル環境でowner権限を持つユーザーアカウントで認証します。
gcloud auth login
Projectをセットします。
gcloud projects list
gcloud config set project <Project名>
OpenShiftのインストールに必要なAPIサービスを有効化します。
有効化するAPI一覧はこちらにあります。
gcloud services enable compute.googleapis.com \
cloudapis.googleapis.com \
cloudresourcemanager.googleapis.com \
dns.googleapis.com \
iamcredentials.googleapis.com \
iam.googleapis.com \
servicemanagement.googleapis.com \
serviceusage.googleapis.com \
storage-api.googleapis.com \
storage-component.googleapis.com
次に、OpenShiftで利用するクラスターのベースドメインをCloudDNSに登録します。
ここでは、AWS Route53で取得したXXX.comというドメインに対し、gcp-ocp.XXX.comというホストゾーンを登録しています。
gcloud dns managed-zones create gcp-ocp --dns-name gcp-ocp.XXX.com --description "openshift base domain"
登録されたホストゾーンのNSレコードを確認します。
gcloud dns managed-zones describe gcp-ocp
---omit---
name: gcp-ocp
nameServers:
- ns-cloud-e1.googledomains.com.
- ns-cloud-e2.googledomains.com.
- ns-cloud-e3.googledomains.com.
- ns-cloud-e4.googledomains.com.
visibility: public
表示されたnameServers情報をもとに、Route53のXXX.comホストゾーンにレコードを登録し、サブドメインの委任をおこなってください。
次にOpenShiftデプロイ用のService Accountを作成します。
gcloud iam service-accounts create ocp-deploy --display-name "ocp-deploy"
Project IDとService Account名を変数に格納します。
GCP_PROJECT=$(gcloud config get project)
GCP_SA=$(gcloud iam service-accounts list | grep ocp-deploy | awk '{print $2}')
Service AccountにRoleを紐付けます。
こちらにデプロイに必要なロールの一覧がありますが今回は簡略化のためowner権限を付与します。
gcloud projects add-iam-policy-binding $GCP_PROJECT \
--member serviceAccount:$GCP_SA \
--role 'roles/owner'
作成したService Accountのkeyを作成します。ここではcredentials.json
というファイルに書き出します。
gcloud iam service-accounts keys create ./credentials.json \
--iam-account $GCP_SA
次にOpenShiftのインストーラーを実行するためのインスタンスを作成します。
ここではローカル端末の環境によってつかう資材やコマンドの中身が変わることを防ぐことを目的にしていますが、複数人でクラスターを管理する上で踏み台サーバを構築する方がメンテナンス性が高くなりおすすめです。
まずローカル端末上でbastionへのSSHアクセス用のSSH認証鍵を作成します。
ssh-keygen -t ed25519 -f ./ssh-key -C ""
作成した公開鍵を変数に格納します。
SSH_KEYS=$(cat ssh-key.pub)
以下のコマンドでGCEインスタンスをデプロイします。
以下の設定を加えてますが、その他の項目(VPCやサブネット、静的IPなど)は、自分の環境に合わせてオプションを追加したりコマンドを実行してください。
コマンドオプションの一覧はこちらで確認できます。
- VM名:bastion
- ゾーン:asia-northeast1-a
- マシンタイプ:e2-small
- OSイメージ:RHEL 8
- SSHキー:作成した公開鍵
- 公開鍵を設定する際、その前に
<ユーザー名>:
を追加すると、このユーザー名でSSHするよう設定できます。ここではuser1
を設定しています。
- 公開鍵を設定する際、その前に
gcloud compute instances create bastion \
--image-project=rhel-cloud \
--image-family=rhel-8 \
--zone=asia-northeast1-a \
--machine-type=e2-small \
--metadata=ssh-keys="user1:$SSH_KEYS"
すると以下のようにインスタンスの情報が表示されるので、EXTERNAL_IP
を確認しておきます。
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
bastion asia-northeast1-a e2-small 10.146.0.5 34.146.XXX.XXX RUNNING
これで踏み台サーバの構築が完了しました。
ログインする前に、先ほど作成したサービスアカウントのキーを踏み台サーバに転送しておきます。
scp -i ./ssh-key credentials.json user1@34.146.XXX.XXX:~/
表示されたインスタンスのEXTERNAL_IP
にてSSHログインします。
ssh -i ssh-key user1@34.146.XXX.XXX
手順②:OpenShiftインストール
GCEインスタンスにログインできたら、まずopenshift-installをダウンロードし、解凍します。
curl -O https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable/openshift-install-linux.tar.gz
tar xvf openshift-install-linux.tar.gz
次にmasterノードにSSHする際に使うキーを生成します。ここではパスワードなしで作成しています。
$ ssh-keygen -t ed25519 -f ~/.ssh/openshift-master -N "" <<<y
インストール時に使用するGCPの環境変数に、先ほど転送したサービスアカウントのキーを設定しておきます。
export GOOGLE_CLOUD_KEYFILE_JSON=~/credentials.json
ドキュメント上ではgcloudでサービスアカウントログインを実行しておけばOKとありますが、実際に行うとinstall-configの作成時にエラーになります。
上記手順にて環境変数に認証情報のjsonファイルパスを指定することでgcloudログインしなくてもinstall-configの作成が可能になります。
最後に、OpenShiftのインストール時に使うpull-secretを取得します。後ほどコマンド実行時に中身をペーストするので、ここではCopy pull secret
を選択しましょう。
- Red Hat OpenShift Cluster Management
https://console.redhat.com/openshift/install/gcp/installer-provisioned
ではinstall-configを作成します。ここではクラスター名をtest-cluster
としています。
./openshift-install create install-config
? SSH Public Key /home/user1/.ssh/openshift-master.pub
? Platform gcp
INFO Credentials loaded from environment variable "GOOGLE_CLOUD_KEYFILE_JSON", file "/home/user1/credentials.json"
? Project ID XXXXXX (XXXXXX)
? Region asia-northeast1
? Base Domain gcp-ocp.XXX.com
? Cluster Name test-cluster
? Pull Secret :********
INFO Install-Config created in: .
install-configができたら、クラスターの構築コマンドを実行します。
./openshift-install create cluster
30-40分ほどで作成が完了します。
以下のようなログが出たら作成完了です。
INFO Credentials loaded from environment variable "GOOGLE_CLOUD_KEYFILE_JSON", file "/home/user1/credentials.json"
INFO Consuming Install Config from target directory
INFO Creating infrastructure resources...
INFO Waiting up to 20m0s (until 1:16AM) for the Kubernetes API at https://api.test-cluster.gcp-ocp.XXX.com:6443...
INFO API v1.23.5+b463d71 up
INFO Waiting up to 30m0s (until 1:30AM) for bootstrapping to complete...
INFO Destroying the bootstrap resources...
INFO Waiting up to 40m0s (until 1:50AM) for the cluster at https://api.test-cluster.gcp-ocp.XXX.com:6443 to initialize...
INFO Waiting up to 10m0s (until 1:29AM) for the openshift-console route to be created...
INFO Install complete!
INFO To access the cluster as the system:admin user when using 'oc', run 'export KUBECONFIG=/home/user1/auth/kubeconfig'
INFO Access the OpenShift web-console here: https://console-openshift-console.apps.test-cluster.gcp-ocp.XXX.com
INFO Login to the console with user: "kubeadmin", and password: "XXXXXXXXXXXXXXXX"
INFO Time elapsed: 26m22s
ログの中にログインに必要な情報が含まれているので、参考にしながらログインを実施します。
手順③:OpenShiftにログイン
ここでは踏み台サーバからのCLIログインと、ローカル端末からのコンソールログインの手順をまとめます。
CLIログイン
ログインの前にocコマンドのインストールを実施します。以下の手順でocコマンドを準備します。
curl -O https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable/openshift-client-linux.tar.gz
tar xvf openshift-client-linux.tar.gz
sudo mv oc /usr/local/bin
oc version
ログインしてみます。ログイン時にはログに表示されているクラスターのAPI URL
とuser
とpassword
を使います。
oc login -u kubeadmin https://api.test-cluster.gcp-ocp.XXX.com
Authentication required for https://api.test-cluster.gcp-ocp.XXX.com:6443 (openshift)
Username: kubeadmin
Password:<passwordを入力>
Login successful.
これでCLIログインができました。
コンソールログイン
次にローカル端末上からWebコンソールにログインします。
ログに出ているweb-console
のURLにアクセスしましょう。
-
https://console-openshift-console.apps.test-cluster.gcp-ocp.XXX.com
するとログイン画面が表示されるので、ログの最後にあるuser(kubeadmin)
とpassword
を使います。
これで無事にコンソールにアクセスできました。
以上がGCP上にOpenShiftをIPIインストールする手順になります。
AWSと比べて所々手順の異なる部分があるので、注意しながら実施いただければと思います。