業務でmysql8をGKEで扱いそうになってるから、何か練習できる材料ないかなぁって探してた。
GKEでwordpressとmysqlやってみる。
だいたいのやり方はこのへんで調べてみた。
KubernetesでWordPress構築をやってみた。運用費は月額4000円ほど。 #GoogleCloud - Qiita
GCP GKE(Kubernetes) WordPressセットアップ│システムガーディアン株式会社
sys-guard.com
なるほど、できそう。
- cloud sqlじゃなくてmysql8のコンテナでやる
- wordpressは最新で6.2
- ディスクはvmで使う普通のものを割り当ててマウント
ついでにmac13にgcloud入れてgke扱えるかもやってみる。
少しナメてかかってたけど、やってみたらwordpressの設定でつまづいた。
gitでダウンロードしたyamlは書き足さなアカンかった。
クラスタもPodも何回か作り直したから、ログにあるPodの物理名はいろいろ混じってる。
mac13にgcloudのセットアップ#
ローカルのmac13からやってみる。
dockerはもともとlinuxの技術だったはず。
macはコンテナとか扱えないはずなんやけど、docker desktopとかrancher desktopとかあるらしい。
自分は元々linuxサーバでコンテナ取扱いするから、macにdocker入れる必要なくて、ubuntuのvmの中で環境作ってる。
GKE取扱いするのはmac13でもいけるのでgcloudとkubectlをインストールする。
gcloudの入れ方は普通にgoogleのドキュメントにある。
gcloud CLI をインストールする | Google Cloud SDK Documentation
ダウンロード#
まずは材料のダウンロードとtar展開。
1
2
3
4
5
6
7
8
9
10
|
nari@gvisMac13 gke-wordpress % tar xzf ./google-cloud-cli-426.0.0-darwin-x86_64.tar.gz
nari@gvisMac13 gke-wordpress % ll
total 269184
-rw-r--r--@ 1 nari staff 119M 4 23 06:57:15 2023 google-cloud-cli-426.0.0-darwin-x86_64.tar.gz
drwxr-xr-x 21 nari staff 672B 4 23 06:58:34 2023 google-cloud-sdk/
nari@gvisMac13 gke-wordpress % du -shc *
131M google-cloud-cli-426.0.0-darwin-x86_64.tar.gz
541M google-cloud-sdk
672M total
nari@gvisMac13 gke-wordpress %
|
インストール#
gcloudをインストールしてみる。
そやけど、ログ長いなぁ。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
nari@gvisMac13 gke-wordpress % ./google-cloud-sdk/install.sh
Welcome to the Google Cloud CLI!
To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. This data is
handled in accordance with our privacy policy
<https://cloud.google.com/terms/cloud-privacy-notice>. You may choose to opt in this
collection now (by choosing 'Y' at the below prompt), or at any time in the
future by running the following command:
gcloud config set disable_usage_reporting false
Do you want to help improve the Google Cloud CLI (y/N)? y
Your current Google Cloud CLI version is: 426.0.0
The latest available version is: 427.0.0
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Components │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────────┬──────────┤
│ Status │ Name │ ID │ Size │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────────┼──────────┤
│ Update Available │ BigQuery Command Line Tool │ bq │ 1.6 MiB │
│ Update Available │ Cloud Storage Command Line Tool │ gsutil │ 15.5 MiB │
│ Update Available │ Google Cloud CLI Core Libraries │ core │ 20.2 MiB │
│ Not Installed │ App Engine Go Extensions │ app-engine-go │ 4.4 MiB │
│ Not Installed │ Appctl │ appctl │ 18.5 MiB │
│ Not Installed │ Artifact Registry Go Module Package Helper │ package-go-module │ < 1 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool │ cbt │ 10.3 MiB │
│ Not Installed │ Cloud Bigtable Emulator │ bigtable │ 6.8 MiB │
│ Not Installed │ Cloud Datastore Emulator │ cloud-datastore-emulator │ 35.1 MiB │
│ Not Installed │ Cloud Firestore Emulator │ cloud-firestore-emulator │ 41.8 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator │ pubsub-emulator │ 66.4 MiB │
│ Not Installed │ Cloud Run Proxy │ cloud-run-proxy │ 7.7 MiB │
│ Not Installed │ Cloud SQL Proxy │ cloud_sql_proxy │ 7.6 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr │ 2.2 MiB │
│ Not Installed │ Kustomize │ kustomize │ 7.6 MiB │
│ Not Installed │ Log Streaming │ log-streaming │ 12.3 MiB │
│ Not Installed │ Minikube │ minikube │ 33.2 MiB │
│ Not Installed │ Nomos CLI │ nomos │ 24.6 MiB │
│ Not Installed │ On-Demand Scanning API extraction helper │ local-extract │ 14.0 MiB │
│ Not Installed │ Skaffold │ skaffold │ 23.8 MiB │
│ Not Installed │ Terraform Tools │ terraform-tools │ 62.1 MiB │
│ Not Installed │ anthos-auth │ anthos-auth │ 20.2 MiB │
│ Not Installed │ config-connector │ config-connector │ 57.1 MiB │
│ Not Installed │ enterprise-certificate-proxy │ enterprise-certificate-proxy │ 6.7 MiB │
│ Not Installed │ gcloud Alpha Commands │ alpha │ < 1 MiB │
│ Not Installed │ gcloud Beta Commands │ beta │ < 1 MiB │
│ Not Installed │ gcloud app Java Extensions │ app-engine-java │ 64.6 MiB │
│ Not Installed │ gcloud app PHP Extensions │ app-engine-php │ 21.9 MiB │
│ Not Installed │ gcloud app Python Extensions │ app-engine-python │ 8.4 MiB │
│ Not Installed │ gcloud app Python Extensions (Extra Libraries) │ app-engine-python-extras │ 26.4 MiB │
│ Not Installed │ gke-gcloud-auth-plugin │ gke-gcloud-auth-plugin │ 7.5 MiB │
│ Not Installed │ kpt │ kpt │ 14.1 MiB │
│ Not Installed │ kubectl │ kubectl │ < 1 MiB │
│ Not Installed │ kubectl-oidc │ kubectl-oidc │ 20.2 MiB │
│ Not Installed │ pkg │ pkg │ │
│ Installed │ Google Cloud CRC32C Hash Tool │ gcloud-crc32c │ 1.2 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────────┴──────────┘
To install or remove components at your current SDK version [426.0.0], run:
$ gcloud components install COMPONENT_ID
$ gcloud components remove COMPONENT_ID
To update your SDK installation to the latest version [427.0.0], run:
$ gcloud components update
Modify profile to update your $PATH and enable shell command completion?
Do you want to continue (Y/n)?
The Google Cloud SDK installer will now prompt you to update an rc file to bring the Google Cloud
CLIs into your environment.
Enter a path to an rc file to update, or leave blank to use [/Users/nari/.zshrc]:
Backing up [/Users/nari/.zshrc] to [/Users/nari/.zshrc.backup].
[/Users/nari/.zshrc] has been updated.
==> Start a new shell for the changes to take effect.
Google Cloud CLI works best with Python 3.7 and certain modules.
Download and run Python 3.7 installer? (Y/n)? Y
Running Python 3.7 installer, you may be prompted for sudo password...
Password:
installer: Package name is Python
installer: Installing at base path /
installer: The install was successful.
Setting up virtual environment
Creating virtualenv...
Installing modules...
|████████████████████████████████| 89 kB 5.9 MB/s
|████████████████████████████████| 23.5 MB 46.1 MB/s
|████████████████████████████████| 2.5 MB 52.0 MB/s
|████████████████████████████████| 55 kB 13.8 MB/s
|████████████████████████████████| 155 kB 38.6 MB/s
|████████████████████████████████| 178 kB 65.8 MB/s
|████████████████████████████████| 118 kB 69.7 MB/s
Running setup.py install for crcmod ... done
Updates are available for some Google Cloud CLI components. To install them,
please run:
$ gcloud components update
Virtual env enabled.
For more information on how to get started, please visit:
https://cloud.google.com/sdk/docs/quickstarts
nari@gvisMac13 gke-wordpress %
|
やってる間はずっとCPUの利用率高い。
パス通して使えるようにする#
gcloudのパスを通して使えるように。
zshrcにPATH追記しようとすると.zshrcにもう入ってた。
1
2
3
4
5
6
7
8
9
10
|
nari@gvisMac13 gke-wordpress % which gcloud
gcloud not found
nari@gvisMac13 gke-wordpress % cd
nari@gvisMac13 ~ % tail -5 ~/.zshrc
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/nari/Desktop/gke-wordpress/google-cloud-sdk/path.zsh.inc' ]; then . '/Users/nari/Desktop/gke-wordpress/google-cloud-sdk/path.zsh.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/nari/Desktop/gke-wordpress/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/nari/Desktop/gke-wordpress/google-cloud-sdk/completion.zsh.inc'; fi
nari@gvisMac13 ~ %
|
パス通す。
1
2
3
4
|
nari@gvisMac13 ~ % source .zshrc
nari@gvisMac13 ~ % which gcloud
/Users/nari/Desktop/gke-wordpress/google-cloud-sdk/bin/gcloud
nari@gvisMac13 ~ %
|
kubectl入れる#
mac13ではgcloud使ってスンナリ入る。
前に、gceでubuntu22のvm作って、gcloudからkubectl入れようとしたらうまく行かへん場面があった。
そのときはリポジトリ追加してから、apt-getでkubectl入れた。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
nari@gvisMac13 ~ % gcloud components install kubectl
Your current Google Cloud CLI version is: 426.0.0
Installing components from version: 426.0.0
┌─────────────────────────────────────────────┐
│ These components will be installed. │
├────────────────────────┬─────────┬──────────┤
│ Name │ Version │ Size │
├────────────────────────┼─────────┼──────────┤
│ gke-gcloud-auth-plugin │ 0.5.2 │ 7.5 MiB │
│ kubectl │ 1.24.12 │ 97.1 MiB │
│ kubectl │ 1.24.12 │ < 1 MiB │
└────────────────────────┴─────────┴──────────┘
For the latest full release notes, please visit:
https://cloud.google.com/sdk/release_notes
Do you want to continue (Y/n)? Y
╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gke-gcloud-auth-plugin ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gke-gcloud-auth-plugin ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: kubectl ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: kubectl ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation ═╣
╚════════════════════════════════════════════════════════════╝
Performing post processing steps...done.
Update done!
nari@gvisMac13 ~ %
|
gcpのプロジェクト接続初期化#
gcloud initする
昔に途中で1回やめたことあったから、もっかい初期化する。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
nari@gvisMac13 ~ % gcloud init
Welcome! This command will take you through the configuration of gcloud.
Settings from your current configuration [default] are:
core:
account: gavannitsales@gmail.com
disable_usage_reporting: 'False'
Pick configuration to use:
[1] Re-initialize this configuration [default] with new settings
[2] Create a new configuration
Please enter your numeric choice: 1
Your current configuration has been set to: [default]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
Choose the account you would like to use to perform operations for this configuration:
[1] gavannitsales@gmail.com
[2] Log in with a new account
Please enter your numeric choice: 1
You are logged in as: [gavannitsales@gmail.com].
Pick cloud project to use:
[1] fourth-elixir-383118
[2] Enter a project ID
[3] Create a new project
Please enter numeric choice or text value (must exactly match list item): 1
Your current project has been set to: [fourth-elixir-383118].
Do you want to configure a default Compute Region and Zone? (Y/n)? Y
Which Google Compute Engine zone would you like to use as project default?
If you do not specify a zone via a command line flag while working with Compute Engine resources,
the default is assumed.
[1] us-east1-b
[2] us-east1-c
[3] us-east1-d
[4] us-east4-c
[5] us-east4-b
[6] us-east4-a
[7] us-central1-c
[8] us-central1-a
[9] us-central1-f
[10] us-central1-b
[11] us-west1-b
[12] us-west1-c
[13] us-west1-a
[14] europe-west4-a
[15] europe-west4-b
[16] europe-west4-c
[17] europe-west1-b
[18] europe-west1-d
[19] europe-west1-c
[20] europe-west3-c
[21] europe-west3-a
[22] europe-west3-b
[23] europe-west2-c
[24] europe-west2-b
[25] europe-west2-a
[26] asia-east1-b
[27] asia-east1-a
[28] asia-east1-c
[29] asia-southeast1-b
[30] asia-southeast1-a
[31] asia-southeast1-c
[32] asia-northeast1-b
[33] asia-northeast1-c
[34] asia-northeast1-a
[35] asia-south1-c
[36] asia-south1-b
[37] asia-south1-a
[38] australia-southeast1-b
[39] australia-southeast1-c
[40] australia-southeast1-a
[41] southamerica-east1-b
[42] southamerica-east1-c
[43] southamerica-east1-a
[44] asia-east2-a
[45] asia-east2-b
[46] asia-east2-c
[47] asia-northeast2-a
[48] asia-northeast2-b
[49] asia-northeast2-c
[50] asia-northeast3-a
Did not print [63] options.
Too many options [113]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list item): 7
Your project default Compute Engine zone has been set to [us-central1-c].
You can change it by running [gcloud config set compute/zone NAME].
Your project default Compute Engine region has been set to [us-central1].
You can change it by running [gcloud config set compute/region NAME].
Created a default .boto configuration file at [/Users/nari/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!
* Commands that require authentication will use gavannitsales@gmail.com by default
* Commands will reference project `fourth-elixir-383118` by default
* Compute Engine commands will use region `us-central1` by default
* Compute Engine commands will use zone `us-central1-c` by default
Run `gcloud help config` to learn how to change individual settings
This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.
Some things to try next:
* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.
nari@gvisMac13 ~ %
|
gcloudの状態確認#
プロジェクトの概要をコマンドラインで取得する。
gcloudで使うプロジェクトIDを確認しとく。
1
2
3
4
5
6
7
|
nari@gvisMac13 ~ % gcloud projects describe fourth-elixir-383118
createTime: '2023-04-08T18:24:56.947Z'
lifecycleState: ACTIVE
name: gvis-7502
projectId: fourth-elixir-383118
projectNumber: 'xxxxxxxxxxxxx'
nari@gvisMac13 ~ %
|
無料枠300ドルまで使い切れるかなぁ。
いろいろやってみよ。
gcloudのプロジェクト指定#
gcloudコマンドラインで使うプロジェクトとリージョンのデフォルトを設定する。
1
2
3
4
|
nari@gvisMac13 ~ % gcloud config set project fourth-elixir-383118
Updated property [core/project].
nari@gvisMac13 ~ % gcloud config set compute/zone us-central1-a
Updated property [compute/zone].
|
gcloudの更新#
gcloudはときどきアップデートあるから更新しとく。
ログめっちゃ長いので一部カット。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
nari@gvisMac13 wordpress-persistent-disks % gcloud components update
Beginning update. This process may take several minutes.
Your current Google Cloud CLI version is: 426.0.0
You will be upgraded to version: 427.0.0
┌─────────────────────────────────────────────────────────┐
│ These components will be updated. │
├─────────────────────────────────┬────────────┬──────────┤
│ Name │ Version │ Size │
├─────────────────────────────────┼────────────┼──────────┤
│ BigQuery Command Line Tool │ 2.0.91 │ 1.6 MiB │
│ Cloud Storage Command Line Tool │ 5.23 │ 15.5 MiB │
│ Google Cloud CLI Core Libraries │ 2023.04.17 │ 20.2 MiB │
│ anthoscli │ 0.2.36 │ 69.2 MiB │
│ gcloud cli dependencies │ 2023.04.17 │ 11.3 MiB │
└─────────────────────────────────┴────────────┴──────────┘
:(中略)
Do you want to continue (Y/n)? Y
╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: BigQuery Command Line Tool ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Cloud Storage Command Line Tool ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Google Cloud CLI Core Libraries ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: anthoscli ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: gcloud cli dependencies ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Cloud CLI Core Libraries ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: anthoscli ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation ═╣
╚════════════════════════════════════════════════════════════╝
Performing post processing steps...done.
Update done!
To revert your CLI to the previously installed version, you may run:
$ gcloud components update --version 426.0.0
nari@gvisMac13 wordpress-persistent-disks %
|
wordpress動かすためのマニフェスト#
gkeで使うアプリケーションマニフェストがgithubにあるので使わせてもらった。
https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/tree/main/wordpress-persistent-disk …
github.com
1
2
3
4
5
6
7
8
9
10
|
nari@gvisMac13 ~ % cd /Users/nari/Desktop/gke-wordpress
nari@gvisMac13 ~ % git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Cloning into 'kubernetes-engine-samples'...
remote: Enumerating objects: 3409, done.
remote: Counting objects: 100% (55/55), done.
remote: Compressing objects: 100% (41/41), done.com
remote: Total 3409 (delta 19), reused 41 (delta 13), pack-reused 3354
Receiving objects: 100% (3409/3409), 2.30 MiB | 30.64 MiB/s, done.
Resolving deltas: 100% (1923/1923), done.
nari@gvisMac13 ~ %
|
git cloneしたファイルはこんな感じ。
いろいろある中、wordpress-persistent-disksってフォルダにあるものを使う。
1
2
3
4
5
6
7
8
9
10
11
|
nari@gvisMac13 wordpress-persistent-disks % ll
total 64
-rw-r--r-- 1 nari staff 440B 4 23 08:07:23 2023 README.md
-rw-r--r-- 1 nari staff 872B 4 23 08:07:23 2023 mysql-service.yaml
-rw-r--r-- 1 nari staff 945B 4 23 08:07:23 2023 mysql-volumeclaim.yaml
-rw-r--r-- 1 nari staff 1.5K 4 23 08:07:23 2023 mysql.yaml
-rw-r--r-- 1 nari staff 942B 4 23 08:07:23 2023 wordpress-service.yaml
-rw-r--r-- 1 nari staff 965B 4 23 08:07:23 2023 wordpress-volumeclaim.yaml
-rw-r--r-- 1 nari staff 1.6K 4 23 08:07:23 2023 wordpress.yaml
-rw-r--r-- 1 nari staff 2.2K 4 23 08:07:23 2023 wordpress_cloudsql.yaml.template
nari@gvisMac13 wordpress-persistent-disks %
|
gkeでやってみる#
クラスタ作ってpodを2つ動かす。
クラスタ作る#
kubernetes engineクラスタマスタを作成。
5分ほど待つ。
1
2
3
4
5
6
7
8
9
10
11
|
nari@gvisMac13 wordpress-persistent-disks % gcloud container clusters create blog --num-nodes=3
Default change: VPC-native is the default mode during cluster creation for versions greater than 1.21.0-gke.1500. To create advanced routes based clusters, please pass the `--no-enable-ip-alias` flag
Default change: During creation of nodepools or autoscaling configuration changes for cluster versions greater than 1.24.1-gke.800 a default location policy is applied. For Spot and PVM it defaults to ANY, and for all other VM kinds a BALANCED policy is used. To change the default values use the `--location-policy` flag.
Note: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
Creating cluster blog in us-central1-c... Cluster is being health-checked (master is healthy)...done.
Created [https://container.googleapis.com/v1/projects/fourth-elixir-383118/zones/us-central1-c/clusters/blog].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-c/blog?project=fourth-elixir-383118
kubeconfig entry generated for blog.
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
blog us-central1-c 1.25.7-gke.1000 34.71.133.57 e2-medium 1.25.7-gke.1000 3 RUNNING
nari@gvisMac13 wordpress-persistent-disks %
|
コンソール見たらこうなってた。

クラスタできてる。
ノードはgceのVM一覧で確認するとできてる。

やり直したいときは、クラスタを削除するとcompute engineに作られたノード(vm)もガサっと削除してくれる。
永続化ディスクを作る#
mysqlとwordpress用にそれぞれ永続ディスクを作る。
200GB未満でもたぶん動くけど、「GCPでは性能劣化するかもしれんで」って警告出るからいったん200GBでいっとく。
GKEのPod起動させたらマウントされるようにしてるんやけど、フォーマット勝手にやってくれるのはなんでなんやろなぁ。
まぁ、やってくれるからよしとしましょ。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
nari@gvisMac13 wordpress-persistent-disks % gcloud compute disks create --size 200GB mysql-disk
Created [https://www.googleapis.com/compute/v1/projects/fourth-elixir-383118/zones/us-central1-a/disks/mysql-disk].
NAME ZONE SIZE_GB TYPE STATUS
mysql-disk us-central1-a 200 pd-standard READY
New disks are unformatted. You must format and mount a disk before it
can be used. You can find instructions on how to do this at:
https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting
nari@gvisMac13 wordpress-persistent-disks % gcloud compute disks create --size 200GB wordpress-disk
Created [https://www.googleapis.com/compute/v1/projects/fourth-elixir-383118/zones/us-central1-a/disks/wordpress-disk].
NAME ZONE SIZE_GB TYPE STATUS
wordpress-disk us-central1-a 200 pd-standard READY
New disks are unformatted. You must format and mount a disk before it
can be used. You can find instructions on how to do this at:
https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting
nari@gvisMac13 wordpress-persistent-disks %
|
シークレットを作る#
gkeでデータベース扱うときには、シークレットってのを作るらしい。
今は使ってみたいから、理解はまた別のときに。
このシークレットでパスワードを指定しといてpodから参照させる。
実際は--from-literal=の次にパスワード書いとく。
ブラウザでクラスタの詳細見たらシークレットができる。
1
2
3
|
nari@gvisMac13 wordpress-persistent-disks % kubectl create secret generic mysql --from-literal=password=xxxxxxx
secret/mysql created
nari@gvisMac13 wordpress-persistent-disks %
|
mysqlのpod動かす#
mysql.yamlマニフェストを使って、ポート3306で動作する単一インスタンスのMySQLをデプロイする。
書き足したのは、環境変数のあたりと永続化ディスクのマウント。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:8
name: mysql
args:
- "--default-authentication-plugin=mysql_native_password"
env:
- name: MYSQL_DATABASE ## 書き足した
value: gvis
- name: MYSQL_USER ## 書き足した
value: gvis-user
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: password
- name: MYSQL_PASSWORD ## 書き足した
valueFrom:
secretKeyRef:
name: mysql
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
gcePersistentDisk:
# さっき作ったディスクを指定する
pdName: mysql-disk
fsType: ext4
|
マニフェストをデプロイする。
1
2
3
|
nari@gvisMac13 wordpress-persistent-disks % kubectl create -f mysql.yaml
deployment.apps/mysql created
nari@gvisMac13 wordpress-persistent-disks %
|
ポッドが実行されてるか確認する。
1
2
3
4
|
nari@gvisMac13 wordpress-persistent-disks % kubectl get pod -l app=mysql
NAME READY STATUS RESTARTS AGE
mysql-6c58c8cff8-hpf52 1/1 Running 0 64s
nari@gvisMac13 wordpress-persistent-disks %
|
ブラウザで見てもpod動いてる。

podは起動するだけじゃダメで、サービス作らなアカン。
ここはgitの内容丸パクリ。
1
2
3
4
5
6
7
8
9
10
11
12
|
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
type: ClusterIP
ports:
- port: 3306
selector:
app: mysql
|
デプロイしてみる。
1
2
3
|
nari@gvisMac13 wordpress-persistent-disks % kubectl create -f mysql-service.yaml
service/mysql created
nari@gvisMac13 wordpress-persistent-disks %
|
デプロイ結果確認する。
1
2
3
4
5
|
nari@gvisMac13 wordpress-persistent-disks % kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.92.0.1 <none> 443/TCP 16m
mysql ClusterIP 10.92.7.208 <none> 3306/TCP 8s
nari@gvisMac13 wordpress-persistent-disks %
|
ブラウザで見ると下のほうにサービスできてるのが見える。

最初はここまでの確認だけじゃダメで、mysql接続確認を細かくやった。
アカン理由をつきとめて、最終的にマニフェストに環境変数を書き足した。
wordpressのpod動かす#
wordpressもやってみよか。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql:3306
- name: WORDPRESS_DB_NAME ## 書き足した
value: gvis
- name: WORDPRESS_DB_USER ## 書き足した
value: gvis-user
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
# さっき作ったディスクを指定する
gcePersistentDisk:
pdName: wordpress-disk
fsType: ext4
|
デプロイして確認してみる。
1
2
3
4
5
6
|
nari@gvisMac13 wordpress-persistent-disks % kubectl create -f wordpress.yaml
deployment.apps/wordpress created
nari@gvisMac13 wordpress-persistent-disks % kubectl get pod -l app=wordpress
NAME READY STATUS RESTARTS AGE
wordpress-7dd579cbbf-vdtc9 0/1 ContainerCreating 0 13s
nari@gvisMac13 wordpress-persistent-disks %
|
wordpressサービスを公開してみっか。
ロードバランサ使うらしい。
マニフェスト丸パクリでええみたい。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
apiVersion: v1
kind: Service
metadata:
labels:
app: wordpress
name: wordpress
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
app: wordpress
|
さぁデプロイ。
直後はexternal-ipがpendingやけど、30秒もせんうちにすぐにアサインされる。
1
2
3
4
5
6
7
8
9
|
nari@gvisMac13 wordpress-persistent-disks % kubectl create -f wordpress-service.yaml
service/wordpress created
nari@gvisMac13 wordpress-persistent-disks % kubectl get svc -l app=wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.8.14.253 <pending> 80:30330/TCP 26s
nari@gvisMac13 wordpress-persistent-disks % kubectl get svc -l app=wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.8.14.253 34.31.20.8 80:30330/TCP 67s
nari@gvisMac13 wordpress-persistent-disks %
|
初回にここまでアッサリ来たけど、なんかやーな感じがしてた。
ブラウザで開いてみたら、やっぱり・・・。

gcpのログエクスプローラで見ると、mysqlの初期化して接続可能状態には来てるみたいやった。
ログに赤いのが見える。エラーじゃなくて警告でも赤表記なんやなぁ。

wordpressからmysqlつながらん#
サンプルでダウンロードしたマニフェストは、ストレートに動かんかったものもあった。
結論言うと、pod動かすときにenvの箇所で環境変数越しにDBの情報を書き足さなアカンかった。
gke/gcloud/kubectlとかの問題やなくて、単純にwordpress/mysqlのコンテナ利用の際の環境変数指定が欠けとった。
問題を特定するのに、mysqlとwordpressのPodにコマンドラインで入って、確認をやってった。
コマンドラインの違いはあるけど、やる主旨はdockerのコンテナとあんまり変わらん。
mysql側の確認#
最初にうまくいかんかったとき、mysqlのPodからコマンド実行して確認作業やってみた。
こういう作業が実務で力になってく。
dockerのコンテナに対してdocker execする要領で、GKEのPodに対してkubectl execってのが使える。
コマンドラインで接続してみると、secret作ったときのパスワード指定で接続はできてる。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
nari@gvisMac13 wordpress-persistent-disks % kubectl exec -it mysql-6c58c8cff8-ss9wk -- bash
bash-4.4#
bash-4.4# mysql -h localhost -u root -pXXXXXX
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
mysql>
|
データベース側はログインできて大丈夫そうやけど、wordpress用のデータベースができてなアカンのとちゃうか?
接続用ユーザと使うデータベースの指定できてへんっていうことに気づいた。
envのあたりを調べてこう書き足した。

wordpress側の確認#
そもそも疎通ができるんかっていう確認。
あるんかないんか知らんけど、ファイアウォールとかでポート塞がれてたりせえへんかの基礎確認。
wordpressコンテナに入って確認してみた。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
nari@gvisMac13 wordpress-persistent-disks % kubectl get po
NAME READY STATUS RESTARTS AGE
mysql-6c58c8cff8-ss9wk 1/1 Running 0 128m
wordpress-7dd579cbbf-9qvjt 1/1 Running 0 9s
nari@gvisMac13 wordpress-persistent-disks % kubectl exec -it wordpress-7dd579cbbf-9qvjt -- bash
root@wordpress-7dd579cbbf-9qvjt:/var/www/html#
root@wordpress-7dd579cbbf-9qvjt:/var/www/html# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
10.88.2.9 wordpress-7dd579cbbf-9qvjt
root@wordpress-7dd579cbbf-9qvjt:/var/www/html# ping 10.92.4.183
bash: ping: command not found
root@wordpress-7dd579cbbf-9qvjt:/var/www/html#
|
hostsはwordpressのコンテナの内部IPのこと書いてあるけど、mysqlのことは書いてへんし、pingとかも入ってへん。
ほな入れて確認したろ。
1
2
3
4
5
6
7
8
9
|
root@wordpress-7dd579cbbf-9qvjt:~# apt-get update
:(中略)
root@wordpress-7dd579cbbf-9qvjt:~# apt-get install iputils-ping net-tools
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
:(中略)
root@wordpress-7dd579cbbf-9qvjt:~# ping mysql
PING mysql.default.svc.cluster.local (10.92.4.183) 56(84) bytes of data.
|
ping疎通確認戻らへん。
アカンのかな。
ping通らへんでも、mysql-clientをaptしてコマンドラインでつながったらええ。
1
2
3
4
5
6
7
8
9
10
|
root@wordpress-7dd579cbbf-9qvjt:/var/www/html# apt-get install mysql-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package mysql-client is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package 'mysql-client' has no installation candidate
root@wordpress-7dd579cbbf-9qvjt:/var/www/html#
|
mysql-client入らんかぁ。じゃまくさいなぁ。
ほなmariadbの入れて接続確認してみっか。
やってみたら、サーバ側のバージョンが8.0.33って見えてた。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
root@wordpress-7dd579cbbf-9qvjt:~# apt-get update
:(中略)
root@wordpress-7dd579cbbf-9qvjt:~# apt-get install mariadb-client
Reading package lists... Done
Building dependency tree... Done
:(中略)
root@wordpress-7dd579cbbf-9qvjt:~# mysql -h mysql -u root -pXXXXXX
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.002 sec)
MySQL [(none)]> \q
Bye
|
Podからの接続はできるやん。
やっぱマニフェストに書き足さなアカンなぁ。
envのあたりを調べてこう書き足した。

サイトの確認#
最後はちゃんと開いた。日本語選んで認証情報作ってのあたりは、他で解説してるやろうから、適当にやって少し放置。

普段はすぐにクラスタと永続化領域を削除するけど、24時間置いといたら費用がだいたいわかる。
自分の本番環境とは見え方が少し違うなぁ。
無料枠使ってるからあんまり正確やないかもしれんけど、請求画面見てみた。
日付指定で見ると、試行錯誤で3172円使ってるなぁ。
レポート選んで右端で期間を選べばだいたいの費用がわかる。

日付で絞ってみた。
5月1日〜3日に毎日1回ずつクラスタ作り直して試行錯誤してた。
1
2
|
2023/05/01~2023/05/03(合計費用)
クレジットに -¥639 を含む
|
4日に1から作り直した。
クラスタ作成した日の費用はこんなもん。
1
2
|
2023/05/04~2023/05/04(合計費用)
クレジットに -¥933 を含む
|
翌日にPV作成とwordpressのサイト作成操作して初期画面表示できるようにした。
1
2
|
2023/05/05~2023/05/05(合計費用)
クレジットに -¥949 を含む
|
さらに翌々日はページの表示を3回だけ。
1
2
|
2023/05/06~2023/05/06(合計費用)
クレジットに -¥652 を含む
|
7日にはクラスタ削除して課金が0円になってた。
1〜3日と、4日、5日、6日のを足し算すると、639+933+949+652=3173円で、だいたいあってるか。1円ズレてるのは誤差っちゅうことで。
たぶん初期費用2000円弱、ランニング700円弱+ネットワーク費用ってことか?
課金内訳を見ると、GKE(kubernetes)以外にGCP(VM)とnetworkに課金がある。
これも足し算すると、1919+1049+204=3172円。

外部のブラウザからのリクエストに応じてページ表示させると、network課金がドカーンって増えるのかも。
けっこう高めやし、個人利用では使わんな。
作成練習なんで、いったんここまで。