k8s多容器实现nginx+php-fpm

背景

生产中总使用nginx+php的架构,在dockerhub上搜索都是nginx+php-fpm集合在一起的镜像,为了方便在k8s上方便的使用lnmp架构,并方便升级nginx或管理php插件,特总结此篇方便后期使用

实现思路

为了方便单独管理nginx和php,我通过多容器的思路来实现

configMap
configMap
volume
volume
Pod
nginx
php-fpm
nginx.conf
www.conf
volume发布目录

实现步骤

lnmp-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: lnmp-conf
  namespace: default
data:
  default.conf: |
    server {
      listen  80 default_server;
      #server_name _;
      root   /usr/share/nginx/html;
     
      location / {
       index index.html index.htm index.php;
       autoindex off;
      }
      location ~ \.php(.*)$ {
       root   /var/www/html;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include  fastcgi_params;
      }
    }

  www.conf: |
    [global]
 
    [www]
    user = www-data
    group = www-data
    listen = 0.0.0.0:9000
     
    pm = dynamic
    pm.max_children = 5
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3


lnmp-pod.yaml

发布目录在生产中,请根据实际情况使用其他类型volume,在这里使用了emptyDir

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: lnmp
  name: lnmp
spec:
  containers:
  - image: nginx:1.20
    name: nginx
    ports:
    - containerPort: 80
    volumeMounts:
    - name: web-data
      mountPath: /usr/share/nginx/html
    - name: default-conf
      mountPath: /etc/nginx/conf.d/default.conf
      subPath: default.conf
  - image: php:7.4-fpm
    name: php-fpm
    ports:
    - containerPort: 9000
    volumeMounts:
    - name: web-data
      mountPath: /var/www/html
  restartPolicy: Always
  volumes:
  - name: web-data
    emptyDir: {}
  - name: default-conf
    configMap:
      name: lnmp-conf
      items:
      - key: default.conf
        path: default.conf

测试步骤

1、准备php测试页面index.php


 phpinfo();
?>

2、通过kubectl cp把测试页面拷贝至Pod

kubectl cp index.php  lnmp:/usr/share/nginx/html/ -c nginx

kubectl cp示例:
kubectl cp /tmp/foo :/tmp/bar -c

3、通过k8s的service或者直接通过curl测试

在这里插入图片描述

PHP插件管理

通过Dockerfile定制自己需要的PHP插件
有的php镜像封装了docker-php-ext-install可以使用此命令直接安装插件

ls /usr/local/bin/install-php-extensions
docker-php-ext-install pdo pdo_mysql
#命令执行完成之后自动配置php.ini启用相关插件,无需手动在配置,仅需重启容器即可

1、准备sources.list的Ubuntu阿里源

deb http://mirrors.aliyun.com/debian bullseye main
deb http://mirrors.aliyun.com/debian-security bullseye-security main
deb http://mirrors.aliyun.com/debian bullseye-updates main

2、编写Dockerfile

FROM php:7.4-fpm
MAINTAINER PlatoWG
ADD sources.list /etc/apt/
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && sync 
RUN install-php-extensions mysqli pdo_mysql gd exif  zip  #根据自己业务的需要安装php插件
RUN pecl install redis   && docker-php-ext-enable redis #Redis插件需要自己安装

你可能感兴趣的:(k8s,Linux,docker,nginx+php-fpm)