# Knativeで始めるServerlessPHP

- URL: https://tech.fusic.co.jp/posts/2019-09-22-knativephp/
- 公開日: 2019-09-22
- 著者: shiro seike / せいけ しろう / 清家 史郎
- タグ: Knative, Serverless
- 掲載: Fusic Tech Blog（株式会社Fusic）

> Serverlessしてますか？ 今日はKnativeを利用したServerless PHPをしてみます。

---

Serverlessしてますか？

今日はKnativeを利用したServerless PHPをしてみます。  

## そもそもKnativeとは

公式サイトの言葉を借りると、`最新のサーバーレス ワークロードをビルド、デプロイ、管理できる Kubernetes ベースのプラットフォーム`です。

以下のコンポーネントを提供します。
- Build
- Serving
- Eventing

KubernetesベースのpodsをゼロスケールでDeploy出来て、イベントベースでスケールさせることが出来ます。

まずは[Hello World - PHP](https://knative.dev/docs/serving/samples/hello-world/helloworld-php/)をGKE上にて実行してみようと思います。

## PHPファイル

まずは普通にPHPファイル(index.php)を作成

```php
<?php
$target = getenv('TARGET', true) ?: 'World';
echo sprintf("Hello %s!", $target);
```

## Dockerfile

Knativeで利用するPHPコンテナ用のDockerfileを作成します

```Dockerfile
# Use the official PHP 7.3 image.
# https://hub.docker.com/_/php
FROM php:7.3-apache

# Copy local code to the container image.
COPY index.php /var/www/html/

# Use the PORT environment variable in Apache configuration files.
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf

# Configure PHP for development.
# Switch to the production php.ini for production operations.
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
# https://hub.docker.com/_/php#configuration
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
```

## DockerHub

コンテナをビルド後、Knativeが利用出来るリポジトリにコンテナをPushします。  
今回はDocker Hubを利用します。

```bash
$ docker build -t seike460/helloworld-php .
$ docker push seike460/helloworld-php
```

## GKE上にクラスターを作成

Knative用のクラスターを作成します。  
`--machine-type` オプションで適切なマシンタイプを設定しましょう。

```bash
seike460@cloudshell:~ (knative-php)$ gcloud container clusters create seike460 --zone=asia-northeast2
```

## Knativeの設定

### istioインストール

```bash
seike460@cloudshell:~ (knative-php)$ kubectl apply -f https://github.com/knative/serving/releases/download/v0.9.0/serving-v1-crds.yaml
seike460@cloudshell:~ (knative-php)$ kubectl apply -f https://github.com/knative/serving/releases/download/v0.9.0/serving-istio.yaml
```

### istio を有効化

default ネームスペースでistio を有効化します

```bash
seike460@cloudshell:~ (knative-php)$ kubectl label namespace default istio-injection=enabled
```

### Knativeインストール

```bash
seike460@cloudshell:~ (knative-php)$ kubectl apply -f https://github.com/knative/serving/releases/download/v0.9.0/serving.yaml
seike460@cloudshell:~ (knative-php)$ kubectl apply -f https://github.com/knative/eventing/releases/download/v0.9.0/release.yaml
seike460@cloudshell:~ (knative-php)$ kubectl apply -f https://github.com/knative/serving/releases/download/v0.9.0/monitoring.yaml
```

## service.yamlを作成

service用のyamlを作成して適用します

```service.yaml
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: helloworld-php
  namespace: default
spec:
  template:
    spec:
      containers:
        - image: docker.io/seike460/helloworld-php
          env:
            - name: TARGET
              value: "PHP Sample v1"
```

Deployします。

```bash
seike460@cloudshell:~ (knative-php)$ kubectl apply -f service.yaml
```

## 動作確認

### 公開IPを取得

istioのingressgatewayが公開しているIPを取得します。

```bash
seike460@cloudshell:~ (knative-php)$ kubectl get svc istio-ingressgateway --namespace istio-system
```

### Serviceに紐付けられたドメインを取得

VirtualHostで公開している為、公開されているドメインを取得します。

```bash
seike460@cloudshell:~ (knative-php)$ kubectl get ksvc helloworld-php
NAME             DOMAIN                               LATESTCREATED          LATESTREADY            READY   REASON
helloworld-php   helloworld-php.default.example.com   helloworld-php-z8k6r   helloworld-php-z8k6r   True
```

### 接続

curlでHostヘッダーつける、もしくは「/etc/hosts」に設定を書いてアクセスします。

```bash
$ curl -H "Host: helloworld-php.default.example.com" http://xxx.xxx.xxx.xxx
$ Hello PHP Sample v1!
```

GKE上のKnativeでHello Worldすることが出来ました。  
Serverlessの利用環境として注目されるKnativeですがもっと本格的に触ってみようと思います
