K8S php+nginx(单pod)

本文内容全由网上整理,如有错误请自行处理

pod是将php+nginx都放到一个pod,docker镜像请参考上一篇

ConfigMap配置文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-php-config
  labels:
    tier: backend
data:
  config : |
    server {
      index index.php index.html;
      error_log  /var/log/nginx/error.log;
      access_log /var/log/nginx/access.log;
      root /code/public;
      add_header X-Frame-Options "SAMEORIGIN";
      add_header X-XSS-Protection "1; mode=block";
      add_header X-Content-Type-Options "nosniff";
      location / {
          try_files $uri $uri/ /index.php?$query_string;
      }
      location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    }

Deployment pod配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-php-demo
  labels:
    app: nginx-php-demo
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-php-demo
      tier: backend
  template:
    metadata:
      labels:
        app: nginx-php-demo
        tier: backend
    spec:
      enableServiceLinks: false
      volumes:
        - name: nginx-php-vol #直接这里弄个跟随pod的存储卷,不需要单独建存储卷了
          emptyDir: {}
        - name: nginx-cnfig-vol
          configMap:
            name: nginx-php-config
            items:
             - key: config
               path: site.conf
      containers:
        - name: php-demo
          image: laravel-php
          imagePullPolicy: Never
          volumeMounts:
            - name: nginx-php-vol
              mountPath: /code
          lifecycle:
            postStart:
              exec:
                command: [ "/bin/sh", "-c", "cp -r /var/www/html/. /code" ]
        - name: nginx
          image: nginx:1.21.0
          ports:
            - containerPort: 80
          volumeMounts:
            - name: nginx-php-vol
              mountPath: /code
            - name: nginx-cnfig-vol
              mountPath: /etc/nginx/conf.d


Service 配置文件

apiVersion: v1
kind: Service
metadata:
  name: php-nginx-service
  labels:
    app: php-nginx-service
    tier: backend
spec:
  type: NodePort
  selector:
    app: nginx-php-demo
    tier: backend
  ports:
    - protocol: TCP
      port: 80

相比双pod 简洁了不少,这也是绝大多数推荐的方式,不符合单一原则,但是谁让phpfpm+nginx本身就不合规矩呢

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值