---
# System prepended metadata

title: 'PHP :heart: K8s'

---

# PHP :heart: K8s

## 準備環境

### Docker

[Docker 下載](https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe)
[官網教學](https://docs.docker.com/desktop/install/windows-install/)


### Docker compose

桌面版安裝預設應有 docker compose。[官網文件](https://docs.docker.com/compose/install/)
從命令提示字元執行下方指令，出現版本號代表完成
```cmd
docker compose version
```

Docker compose 用來在本機環境快速建立整個容器系統

## PHP 程式調整

有一些會有因為佈署對應的參數要設，開放出能透過環境變數設定的能力。
下方例子設定 使用環境變數的`ALL_CONTROLLER_1`的值，不存在的話為後方預設值。
方便從k8s佈署腳本指定不同環境要用的值
```php
define('WebSocketSendCtrlArray',array(
    1	=> getenv('ALL_CONTROLLER_1') ?: 'ws://ruby-csserver.local:9000',
);
```

## 範例

以一份設定檔描述一個由
* nginx for business
* nginx for client
* mysql
* redis
* php apps
* 各種 game client
 
組成的環境，啟動的先後順序可由`depends_on`調整。例如nginx啟動前一定要所有的game client端啟動好，nginx需要確認upstream活著才會放心交還給主人。

這份環境有些服務是需含有自己的程式，要經過建置。
描述檔裡面也有指定建置相關的檔案 `build:` 底下

docker-compose.yml:
```yaml
version: "3.8"
services:
  nginx-2b: 
    container_name: nginx-2b-svc.rtg
    image: rtg_nginx2b
    build:
      dockerfile: Dockerfiles/Dockerfile.entry
      args:
        - CONFIG=2b
    ports:
      - "8082:80"
    healthcheck:
      test: "curl localhost/fgm/ -H 'Host: cms.rbgame.com' || exit 1"
      start_period: 10s
    depends_on:
      - playback025
  nginx-2c:
    container_name: nginx-2c-svc.rtg
    image: rtg_nginx2c
    build:
      dockerfile: Dockerfiles/Dockerfile.entry
      args:
        - CONFIG=2c
    ports:
      - "8083:80"
    healthcheck:
      test: "curl localhost/client/ -H 'Host: www.rbgame.com' || exit 1"
      start_period: 10s
    depends_on:
      - playback025
  rubyapi:
    container_name: rubyapi-svc.rtg
    image: rtg_rubyapi
    build:
      dockerfile: Dockerfiles/Dockerfile.api
    extra_hosts:
      - rubygameserver.local:192.168.31.211
    environment: 
      - SERVER_ID_SETTING=0
      - RUBY_SERVER=http://www.rbgame.com:8083
      - RUBY_SOCKET=ws://www.rbgame.com:8083
      - DB_WRITE_HOSTNAME=db
      - DB_READ_HOSTNAME=db
      - ALL_CONTROLLER_1=ws://rubygameserver.local:9000
    ports: 
      - "9000:9000"
    healthcheck:
      test: service nginx status || service nginx restart
      start_period: 1m
    depends_on:
      - "rubygame025"
      - "redis"
      - "db"
  rubyctrl:
    container_name: rubyctrl-svc.rtg
    image: rtg_rubyctrl
    build:
      dockerfile: Dockerfiles/Dockerfile.ctrl
    environment: 
      - SERVER_ID_SETTING=0
      - RUBY_SERVER=http://www.rbgame.com:8083
      - RUBY_SOCKET=ws://www.rbgame.com:8083
      - DB_MAJOR_USERNAME=root
      - DB_MAJOR_PASSWORD=rubygame789
    healthcheck:
      test: service nginx status || service nginx restart
      start_period: 1m
    volumes:
      - ./QA_tool:/opt/RubyGame/tool
      - ./data/log/rubyctrl:/var/log/RubyGame
    depends_on:
      - "rubygame025"
      - "redis"
      - "db"
  rubyweb:
    container_name: rubyweb-svc.rtg
    image: rtg_rubyweb
    build:
      dockerfile: Dockerfiles/Dockerfile.web
    environment:
      - SERVER_ID_SETTING=0
      - RUBY_SERVER=http://www.rbgame.com:8083
      - RUBY_SOCKET=ws://www.rbgame.com:8083
      - ALL_CONTROLLER_1=ws://rubygameserver.local:9000
    healthcheck:
      test: service nginx status || service nginx restart
      start_period: 1m
    depends_on:
      - "rubygame025"
      - "redis"
      - "db"
  rubygs1:
    container_name: rubygs1-svc.rtg
    image: rtg_rubygs1
    build:
      dockerfile: Dockerfiles/Dockerfile.gs
    environment: 
      - SERVER_ID_SETTING=1
    healthcheck:
      test: service nginx status || service nginx restart
      start_period: 1m
  rubygs2:
    container_name: rubygs2-svc.rtg
    image: rtg_rubygs1
    environment: 
      - SERVER_ID_SETTING=2
    healthcheck:
      test: service nginx status || service nginx restart
      start_period: 1m
    depends_on:
      - rubygs1
  redis:
    image: redis
  db:
    image: mysql/mysql-server:8.0
    restart: always
    ports:
      - "13306:3306"
    volumes:
      - "./data/db/mysql:/var/lib/mysql:Z"
      - "./sql:/docker-entrypoint-initdb.d"
    environment:
      - MYSQL_ROOT_PASSWORD=rubygame789
      - TZ=Asia/Taipei
    command:
      - "--character-set-server=utf8mb4"
      - "--collation-server=utf8mb4_unicode_ci"
      # - "--collation-connection=utf8mb4_unicode_ci"
      - "--default-authentication-plugin=mysql_native_password"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - "8084:80"
    environment:
      - PMA_HOST=db
  rubygame001:
    container_name: rubygame001-svc.rtg
    image: rubygame001
    depends_on:
      - db
  rubygame002:
    container_name: rubygame002-svc.rtg
    image: rubygame002
    depends_on:
      - db
```

#### 建置 php 的 image設定

Dockerfile.api
```dockerfile
FROM php:7.2-fpm AS system_env

RUN apt-get update

RUN apt-get install -y \
  libbz2-dev \
  libc-client-dev \
  libfreetype6-dev \
  libgmp-dev \
  libicu-dev \
  libjpeg62-turbo-dev \
  libkrb5-dev \
  libonig-dev \
  libpng-dev \
  libpng-dev \
  libpng-dev libxpm-dev \
  libwebp-dev \
  libxml2-dev \
  nginx \
  zlib1g-dev 
RUN pecl install redis

RUN docker-php-ext-install \
  bcmath \
  bz2 \
  gd \
  gettext \
  gmp \
  intl \
  mbstring \
  mysqli \
  pdo_mysql \
  sockets \
  xmlrpc 
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
  docker-php-ext-install imap
RUN docker-php-ext-configure gd \
  --with-freetype-dir \
  --with-gd \
  --with-jpeg-dir \
  --with-png-dir \
  --with-webp-dir \
  --with-xpm-dir \
  --with-zlib-dir &&\
  docker-php-ext-install gd
RUN docker-php-ext-enable bcmath redis

WORKDIR /opt/RubyGame
COPY all_config     all_config
COPY ShareBaseFile  ShareBaseFile
COPY entrypoint.sh  /etc/entrypoint.sh
COPY php.ini        /usr/local/etc/php/php.ini
COPY data/nginx/default.conf /etc/nginx/conf.d/default.conf

WORKDIR /var/log
RUN mkdir -p \
  RubyGame/DB \
  RubyGame/ForeignAPI \
  RubyGame/GM \
  RubyGame/LobbyAPI \
  RubyGame/ServerAPI \
  RubyGame/Web \
  RubyGame/ClientLog && \
  chown -R www-data:www-data RubyGame

FROM alpine/git AS clone

ARG GIT_UESRNAME=n1d<
ARG GIT_PASSWORD=p45$w0rd
ARG GIT_HOST=12.34.56.78
ARG CACHEBUST=1
RUN git clone http://${GIT_UESRNAME}:${GIT_PASSWORD}@${GIT_HOST}/rtg_handover/Dev_Server_API.git
RUN git clone http://${GIT_UESRNAME}:${GIT_PASSWORD}@${GIT_HOST}/rtg_handover/Dev_Foreign_API.git
RUN rm -rf Dev_Server_API/.git Dev_Foreign_API/.git

FROM system_env AS final

WORKDIR /opt/RubyGame
COPY --from=clone /git/Dev_Server_API   ServerAPI
COPY --from=clone /git/Dev_Foreign_API  ForeignAPI

EXPOSE 80 443 9000
ENTRYPOINT ["/etc/entrypoint.sh"]

```