bunchan blog

bunchan blog

kubernetesに入門した

このエントリーをはてなブックマークに追加

まず手始めにnginxとlaravelが動くやつを作ってみた

準備

  1. gcpのコンソールでクラスターを作っとく
  2. gcloudコマンドを使えるようにする https://cloud.google.com/sdk/docs/quickstarts?hl=ja

Dockerfileの準備

Dockerfile(php)

FROM php:7.3-fpm-stretch

RUN pecl install xdebug && \
    docker-php-ext-enable xdebug && \
    apt-get update && apt-get install -y git libzip-dev libxml2-dev && \
    docker-php-ext-configure zip --with-libzip && \
    docker-php-ext-install pdo_mysql mbstring zip xml && \
    apt-get -y install libgmp-dev && \
    apt-get -y install libpng-dev libjpeg-dev libfreetype6-dev && \
    docker-php-ext-install gmp && \
    docker-php-ext-configure gd --with-png-dir=/usr/include --with-jpeg-dir=/usr/include && \
    docker-php-ext-install  gd &&\
    docker-php-ext-install pdo_mysql opcache && \
    docker-php-ext-install zip && \
    pecl install redis-5.0.2 && \
    docker-php-ext-enable redis && \
    curl -sS https://getcomposer.org/installer | php && \
    mv composer.phar /usr/local/bin/composer

WORKDIR /var/www/app/
ADD ./laravel/ /var/www/app/
RUN composer install

Dockerfile(nginx)

FROM nginx

ADD ./default.conf /etc/nginx/conf.d/default.conf

nginxのconf

server {
    listen       0.0.0.0:80;
    server_name  localhost;
    charset      utf-8;

    root /var/www/app/public;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: laravel-deployment
  labels:
    app: laravel
spec:
  replicas: 1
  template:
    metadata:
      name: laravel
      labels:
        app: laravel
    spec:
      containers:
        - name: nginx
          image: gcr.io/your-project/nginx-laravel
          ports:
            - containerPort: 80
          imagePullPolicy: IfNotPresent
        - name: php
          image: gcr.io/your-project/php-laravel
          ports:
            - containerPort: 9000
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
  selector:
    matchLabels:
      app: laravel

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: laravel-service
spec:
  selector:
    app: laravel
  ports:
    - port: 80
  type: LoadBalancer

これらをbuild

docker build -t nginx-laravel .
docker build -t php-laravel .

イメージのデプロイとpush

https://cloud.google.com/container-registry/docs/pushing-and-pulling?hl=ja

認証

gcloud auth configure-docker

buildしたイメージにタグ付け🔖

docker tag laravel-php gcr.io/your-project/laravel-php
docker tag laravel-php gcr.io/your-project/laravle-nginx

push👆

docker tag nginx-laravle gcr.io/[PROJECT-ID]/laravel-php
docker tag php-laravle gcr.io/[PROJECT-ID]/laravle-nginx

kubectlからクラスタにアクセスできるようにする https://cloud.google.com/kubernetes-engine/docs/quickstart?hl=ja

gcloud container clusters get-credentials cluster-name

デプロイ

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

うごいた😁

続き

ConfigMapによる環境変数の管理