LoginSignup
6
1

More than 5 years have passed since last update.

kubernetesでapache-phpのpodを作成する

Posted at

kubernetesを使ってapache-phpが動くimageを使ってpodを作成し、
アクセスできることを確認してみたいと思います

今回はdockerで試します。
こんなdockerの設定になっています。
https://github.com/shiotaro/gke-sample

コンテナクラスタを作成

下記のURLから作成します
https://console.cloud.google.com/kubernetes/list

名前やゾーンを変更しても良いですが、そのままでも良いです

cloud shellを開く

ソースコードをクローンします。
Cloud ソース レポジトリを利用しました。

TUTORIALDIR=~/src/hogehoge
cd $TUTORIALDIR
gcloud source repos clone gke-quickstart --project=hogehoge
cd $TUTORIALDIR/gke-quickstart/containerengine/php-hello-world

作成したコンテナクラスタのgcloud認証情報を取得する

gcloud container clusters get-credentials cluster-1 --zone us-central1-a

アプリケーションのイメージを作成してpushする

docker build -t gcr.io/hogehoge/hello-php:v1 $PWD

※ここで出来上がったimageはContainer Registryに上げています。

gcloud docker -- push gcr.io/hogehoge/hello-php:v1

コンテナ上のアプリケーションを実行する

kubectl run hello-php --image=gcr.io/hogehoge/hello-php:v1 --port=80

ここでpodを確認すると表示されます

$ kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
hello-php-265407199-2prxr   1/1       Running   0          58s

コンテナを公開する

kubectl expose deployment hello-php --type="LoadBalancer"

外部IPアドレスを見つける

下記のコマンドで外部IPを見つけることができます(EXTERNAL-IP)

$ kubectl get service hello-php --watch
NAME         CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
hello-php    10.3.250.245   xxx.xxx.xxx.xxx  80:32335/TCP   1m

http://xxx.xxx.xxx.xxx でアクセスするとphpのバージョンが表示されることを確認しました。

[番外]podへのアクセス

あまり行わないのですが、直接podへsshすることも可能です

podの名前を調べます

$ kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
hello-php-265407199-2prxr   1/1       Running   0          8m

podの名前を指定してアクセスします

$ kubectl exec -it hello-php-265407199-2prxr /bin/bash
root@hello-php-265407199-2prxr:/var/www/html# 

まとめ

以上、ざっとですがkubernetesを使って管理する方法を試してみました。
次はReplication Controllerを利用して、podを管理する方法を試して見たいと思います。
多分これがkubernetesの肝。

6
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1