准备基于docker的php-fpm、nginx镜像

准备基础php-fpm镜像

1、准备基础php-fpm镜像,准备Dockerfile,打包镜像docker build --rm -t private-php-fpm:develop .

FROM php:7.3-fpm-alpine3.12

MAINTAINER wly <1228022817@qq.com>

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk update \
    && apk add --no-cache \
           bzip2 zlib libzip libxml2 gmp libpng \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
           bzip2-dev zlib-dev libzip-dev libxml2-dev gmp-dev libpng-dev \
    && apk add -U tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && apk del tzdata

RUN docker-php-ext-install calendar bz2 zip soap iconv exif gmp bcmath sockets mysqli pdo_mysql opcache \
    && docker-php-ext-enable calendar bz2 zip soap iconv exif gmp bcmath sockets mysqli pdo_mysql opcache \
    && docker-php-ext-install -j$(nproc) gd
#pecl安装扩展
RUN pecl install redis protobuf \
    && docker-php-ext-enable redis protobuf
# 使用默认的生产环境配置文件(官方推荐)
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
#下载源码方式安装扩展
RUN cd /tmp \
    && wget https://pecl.php.net/get/msgpack-2.1.1.tgz \
    && tar zxvf msgpack-2.1.1.tgz \
    && mv msgpack-2.1.1 msgpack \
    && docker-php-ext-install /tmp/msgpack \
    && docker-php-ext-enable  msgpack \
    && rm -rf msgpack-2.1.1.tgz msgpack
    #拷贝代码,我这边php代码在Dockerfile同级的code文件夹中
COPY code/ /var/www/html/
#文件权限设置,根据实际情况授予对应权限
RUN chmod 755 /var/www/html/*

准备nginx镜像

2、准备nginx镜像(如果code文件夹中包含静态h5或者其他资源需要通过nginx访问的,请执行该步骤)生成Dockerfile-nginx,构建镜像docker build --rm -f Dockerfile-nginx -t private-nginx:develop .

FROM nginx:alpine

MAINTAINER wly <1228022817@qq.com>
#拷贝代码,并修改时区和alpine镜像源
COPY open.iuiweb.com/ /var/www/html/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk update \
    && apk add -U tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && apk del tzdata \
    && chmod 755 /var/www/html/*

k8s部署准备

准备yaml编排文件

1、准备nginx的配置映射卷
kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-config
  namespace: default
data:
  nginx.conf: |
  events {
        worker_connections  1024;
    }


    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$http_host" "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '"$upstream_status" "$upstream_response_time" "$request_time"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  0;

        #gzip  on;

        #include /etc/nginx/conf.d/*.conf;
    server {
            listen 80 default_server;
            server_name _;
            #add_header Connection "close";
            location / {
              root /var/www/html;
              index index.php;
              try_files $uri $uri/ /index.html;
            }
            location ~ \.php$ {
              include /etc/nginx/fastcgi.conf;
              fastcgi_param REQUEST_METHOD $request_method;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              fastcgi_pass 127.0.0.1:9000;
              #proxy_set_header Connection "close";
            }
          }
    }

2、准备工作负载的deploymet.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-fpm-nginx-cluster
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-fpm-nginx-cluster
  template:
    metadata:
      labels:
        app: php-fpm-nginx-cluster
    spec:
      containers:
      - image: private-php-fpm:develop
        imagePullPolicy: Always
        name: private-php-fpm
        ports:
        - containerPort: 9000
          name: fpm-tcp
          protocol: TCP
      - image: private-nginx:develop
        imagePullPolicy: Always
        name: nginx
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: vol1-nginx-confi
          readOnly: true
          subPath: nginx.conf
      initContainers:
      - image: busybox
        command:
        - sh
        - -c
        - echo 10000 > /proc/sys/net/core/somaxconn && echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
          && echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout && echo 5 > /proc/sys/net/ipv4/tcp_syn_retries
          && echo 1200 > /proc/sys/net/ipv4/tcp_keepalive_time && echo 1024 65000
          > /proc/sys/net/ipv4/ip_local_port_range && echo 8192 > proc/sys/net/ipv4/tcp_max_syn_backlog
          && echo 5000 > /proc/sys/net/ipv4/tcp_max_tw_buckets
        imagePullPolicy: Always
        name: setsysctl
        securityContext:
          privileged: true
      restartPolicy: Always
      volumes:
      - configMap:
          defaultMode: 256
          items:
          - key: nginx.conf
            path: nginx.conf
          name: nginx-config
          optional: false
        name: vol1-nginx-config

3、

部署

通过dashnoard或者rancher面板,亦或者通过k8s命令

kubectl apply -f xxxx.yaml -n namespace

修改ingress-nginx的名称为 nginx-configuration的配置映射,添加键值对:键(key):keep-alive 值(value):0

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐