LoginSignup
5
1

More than 3 years have passed since last update.

apacheとphpの環境をkubernetesで構築する

Posted at

Docker for Macでkubernetesを起動

dockerアプリからpreferencesを開き、kubernetesの「Enable Kubernetes」にチェックを入れて、kubernetesを有効にする。

kubectl config get-contexts

を実行して、CURRENTが”docker-for-desktop”になっていることを確認

仮に”docker-for-desktop”になっていない場合には、下記コマンドでCURRENTを変更

kubectl config use-context docker-for-desktop

下記コマンドにて起動しているMaster Nodeを確認

$ kubectl get nodes
NAME                 STATUS    ROLES     AGE       VERSION
docker-for-desktop   Ready     master    203d      v1.10.11

dockerを準備

ディレクトリ構成

workspace .
├── app
    ├── phpinfo.php
    └── index.php
└── docker
    ├── apache-php
    │   ├── vhost.conf
    │   └── Dockerfile
    └── templates
        └── main.yaml
phpinfo.php
<?php
  phpinfo();
?>
index.php
<?php
  echo "現在の時間は".date("Y/m/d H:i:s")."です。";
?>
vhost.conf
<VirtualHost *:80>
    DocumentRoot /var/www/html
    ServerName localhost
    ServerAdmin root@example.com
    LogFormat "[%V] %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" custom
    ErrorLog /etc/httpd/logs/error_log
    CustomLog /etc/httpd/logs/access_log custom
    <Directory "/var/www/html">
        AllowOverride All
        Options Indexes FollowSymLinks SymLinksIfOwnerMatch
    </Directory>
</VirtualHost>
FROM php:7.2.6-apache

ADD vhost.conf /etc/apache2/sites-available/vhost.conf

下記コマンドでdocke imageを作成

$ docker build -t k8sdemo/php-apache:1.0.0 docker/apache-php

クラスタ上にコンテナを起動するためのyamlを準備

main.yaml
apiVersion: v1
kind: Service
metadata:
  name: ssdemo-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: ssdemo-apache-php
---
apiVersion: extensions/v1beta1
kind: Deployment # リソースの種類
metadata:
  name: ssdemo-deployment # Deploymentの名前
spec:
  replicas: 2 # 起動するpod数
  template:
    metadata:
      labels:
        app: ssdemo-apache-php # podの名前
    spec:
      containers:
      - name: ssdemo-apache-php # コンテナ名
        image: k8sdemo/php-apache:1.0.4 # コンテナイメージ
        imagePullPolicy: IfNotPresent # ローカルのdocker imageを使用
        ports:
        - containerPort: 80 # コンテナのポート
        volumeMounts:
        - mountPath: /var/www/html/
          name: demo-volume
      volumes:
      - name: demo-volume
        hostPath:
          # directory location on host
          path: /Users/ytada/Documents/k8sdemo/app
          # this field is optional
          type: Directory

各yamlをクラスタにデプロイ

下記コマンドよりクラスタにデプロイ

$ kubectl apply -f main.yaml

クラスタの状況を確認

$ kubectl get all

サービスを確認

http://localhost/
http://localhost/phpinfo.php
5
1
0

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
5
1