Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Kubernetes で構築する PHP 開発環境 / php-development-env...
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
shin1x1
December 01, 2019
Programming
5.3k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kubernetes で構築する PHP 開発環境 / php-development-environment-on-kubernetes
2019/12/01 PHP カンファレンス
shin1x1
December 01, 2019
More Decks by shin1x1
See All by shin1x1
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
700
抽象化という思考のツール - 開発現場での活用 - / Abstraction-as-a-Tool-for-Thinking-in-dev
shin1x1
0
170
抽象化という思考のツール - 理解と活用 - / Abstraction-as-a-Tool-for-Thinking
shin1x1
1
1.5k
php-fpm がリクエスト処理する仕組みを追う / Tracing-How-php-fpm-Handles-Requests
shin1x1
6
4.2k
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
3
2.5k
PHPコードの実行モデルを理解する / Understanding-the-PHP-Execution-Model
shin1x1
2
3.4k
制約の力 - 状態を限定する -
shin1x1
6
5.9k
Apple Silicon Mac 時代の PHP 開発環境構築 2021 / php-dev-env-on-m1-mac-era
shin1x1
2
4.9k
Docker イメージのマルチアーキテクチャビルド / docker-muti-arch-build
shin1x1
1
580
Other Decks in Programming
See All in Programming
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
1
970
iOSDCのペンライトを自動制御したい!
akkeylab
0
280
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2k
Are APIs Still Relevant in the AI Era?
soyuka
0
380
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
130
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
270
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
4.5k
一人だけ、Kiroが静止する日
hideg
0
140
Security issues being discussed on Web Platforms
petamoriken
0
1.4k
設計次第でAIコードの読む量は減らせる / designing-for-code-reading
minodriven
30
13k
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
3
4.3k
How STYLIGHT went responsive
nonsquared
100
6.3k
WCS-LA-2024
lcolladotor
0
840
The Curse of the Amulet
leimatthew05
3
15k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Producing Creativity
orderedlist
PRO
348
41k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Done Done
chrislema
187
17k
Unsuck your backbone
ammeep
672
58k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
Transcript
Kubernetes で構築する PHP 開発環境 2019/12/01 PHP カンファレンス 2019 @shin1x1
Agenda Kubernetes 基礎知識(開発環境用) PHP 開発環境の構築 CI 環境の構築(CircleCI ) PHP 開発環境に
Kubernetes を選ぶか? 2
Kubernetes 基礎知識 (開発環境用) 3
本発表について 開発環境に限定した内容 Mac 等の開発 PC 環境 本番環境に k8s を利用する話ではない Kubernetes
のみで構築 Helm や Kustomise といったツールは利用しな い サンプルコード https://github.com/shin1x1/laravel-k8s-dev- sample 4
Kubernetes (k8s) コンテナアプリケーションをいい感じに 動作させる OSS ホストとは独立した環境 デファクトとなりつつあるので、ノウハウや 周辺ツールが豊富 YAML! YAML!
YAML! https://kubernetes.io 5
kubernetes 実行環境 実行環境が必要。開発 PC にインストール。 Docker Desktop for Mac (
旧: Docker for Mac) 手軽にやるなら、これ minikube Kind などなど 6
Kubernetes Objects 本セッションで利用するもの Pod Con gMap Service 7
Pod 単一 or 複数のコンテナをまとめたグループ アプリケーションの最小単位 同じ Pod のコンテナは、localhost で通信できる ex)
php Pod nginx コンテナ + php-fpm コンテナ 8
Pod apiVersion: v1 kind: Pod metadata: name: sample-php # <---
Pod name spec: containers: - image: nginx:1.14 # <--- docker image name: nginx ports: - containerPort: 80 # expose port (snip) # docker image - image: shin1x1/php-dev:7.4.0-fpm-buster-pgsql name: php-fpm 9
Con gMap 設定情報やファイルなどを格納 Pod では、これを Volume としてマウントする ex) php-fpm Con
gMap php-fpm.d/www.conf php.ini 10
Con gMap 2 つのファイルをデータとして保持している apiVersion: v1 kind: ConfigMap metadata: name:
sample-php-fpm data: php.ini: | ; timezone date.timezone = Asia/Tokyo (snip) www.conf: | [www] pm.status_path = /phpfpm_status 11
PHP の Pod で Volume としてアタッチして、 volumes: - name: php-fpm-config
configMap: name: sample-php-fpm volumeMounts でファイルシステムにマウントする volumeMounts: - mountPath: /usr/local/etc/php/conf.d/zzmyphp.ini subPath: php.ini name: php-fpm-config - mountPath: /usr/local/etc/php-fpm.d/zzmyphp-fpm.conf subPath: php-fpm.conf name: php-fpm-config 12
Service Pod で動くアプリケーションをサービスとして公開 type: LoadBalancer を利用 k8s クラスタ内では、サービス名で通信できる ホストからは、localhost で通信できる
Docker Desktop for Mac の場合 13
Service k8s クラスタ内では、http://sample-php:8000/ 、 ホストからは http://localhost:8000/ で通信 apiVersion: v1 kind:
Service metadata: name: sample-php spec: type: LoadBalancer ports: - name: "8000" port: 8000 # expose port targetPort: 80 # pod port selector: app: sample role: web 14
PHP 開発環境の構築 15
16
PHP 開発環境のポイント 設定ファイル ホストのソースコードをマウントして実行 PhpStorm 使いたい! Xdebug によるリモートデバッグ phpunit 等の
CLI 実行 17
設定ファイル データベースなどの連携する Service 名を指定 .env (Laravel) DB_HOST=sample-db phpunit.xml <php> <env
name="DB_HOST" value="sample-db-test"/> </php> 18
ホストディレクトリのマウント Pod の Volume で hostPath を指定 ホストのパスを直書きする必要がある volumes: -
name: host hostPath: # マウントするホストのパスを指定 path: /Users/hoge/path/to/projects 19
ホストディレクトリのマウント sed で置換 :thinking_face: $ sed "s#%HOST_PATH%#${PWD}#" \ k8s/php.yaml.base >
k8s/php.yaml 20
Xdebug によるリモートデバッグ xdebug.remote_host でホストを指定 Docker Desktop なら docker.for.mac.localhost xdebug.remote_enable =
On xdebug.remote_autostart = On xdebug.remote_connect_back = Off xdebug.remote_host = docker.for.mac.localhost docker-compose の環境構築と理屈は同じ 21
phpunit 等の CLI 実行 kubectl exec で Pod 内でコマンドを実行 -c
でコンテナを指定 $ kubectl exec sample-php -c php-fpm \ -- ./vendor/bin/phpunit PhpStorm 連携は無いので、シェルで実行 (これは地味に辛いところ) 22
Make le でまとめる make install で構築 install: k8s-apply composer cp
-a .env.example .env kubectl exec sample-php -c php-fpm -- ./artisan key:generate kubectl exec sample-php -c php-fpm -- ./artisan migrate kubectl exec sample-php -c php-fpm -- ./artisan db:seed .PHONY: install k8s-apply: sed "s#%HOST_PATH%#${PWD}#" k8s/php.yaml.base > k8s/php.yaml kubectl apply -f k8s/ kubectl wait all -l app=sample --for condition=Ready --timeout composer: docker run --rm -v `pwd`:/opt -w /opt --entrypoint '' composer 23
demo 24
CI 環境の構築 - CircleCI - 25
CI 環境の構築 - CircleCI - 2019/12 現在、k8s 実行環境の提供は無い 各自で環境構築する必要がある Orbs
にいくつか k8s 関連はあるが、 クライアントツール Kind で環境構築 k8s in Docker https://kind.sigs.k8s.io 26
Kind のインストール .circleci/con g.yaml curl -Lo ./kind https://github.com/kubernetes-sigs/kind /releases/download/v0.5.1/kind-$(uname)-amd64 chmod
+x ./kind perl -pi -e "s#%HOST_PATH%#`pwd`#g" \ .circleci/kind-config.yaml ./kind create cluster \ --config .circleci/kind-config.yaml --wait 5m 27
Kind 設定ファイル ホストファイルをマウントするために設定が必要 下記の %HOST_PATH% をホストパスに置換する kind: Cluster apiVersion: kind.sigs.k8s.io/v1alpha3
nodes: - role: control-plane extraMounts: - containerPath: %HOST_PATH% hostPath: %HOST_PATH% readOnly: false 28
CI 環境の構築 - CircleCI kubectl は、Orbs にあるので、これでインストール orbs: kube-orb: circleci/
[email protected]
jobs: build: machine: true steps: - checkout - kube-orb/install-kubectl 29
CI 環境の構築 - CircleCI k8s 環境ができれば、あとは開発環境と同じ make install で環境構築して、 実行テスト等も実行できる
30
実行イメージ k8s 環境構築に 2 分ほどかかるのがネック CircleCI が環境を用意してくれることを期待 GKE や EKS
などを使う手も 31
PHP 開発環境に Kubernetes を選ぶか? 32
docker-compose と比べると 構築するのに手間がかかる docker-compose.yaml の方がかなり簡単 k8s 単体では、docker build もできない 学習コストも高い
PhpStorm 連携が弱い(無い) PhpStorm から Pod 内でコマンド実行できれば 33
開発環境に k8s 導入したいです! 本番環境が k8s で無いなら、導入する必要は無い 本番環境が k8s でも、よく考えて docker-compose
じゃダメ? 全員の PC で k8s 動かすの? CI も k8s にするんだよ? 34
現在取り組んでいるプロジェクト Helm / helm le を活用 本番、検証、開発、CI 環境を 1 ソースで構築
環境ごとの違いを吸収 つまり複雑 本番環境用のものを拡張して、開発環境を構築 開発環境だけなら、もっとシンプルにできそう でも楽しい! 35
どうしても使いたい!使ってる! ぜひ情報交換させて下さいm(_ _)m 36
PHP 開発環境に k8s を選ぶか? 学習コストの割に得られるものがまだ少ない 開発環境には焦って使わなくても良い docker-compose で十分 以下のような場合ならあり k8s
を学習したい docker-compose では複雑な構成 k8s 大好き ※2019/12/01 現在 37
Q? @shin1x1 38