k8s nginx+php

作者: 王宣成 | 来源:发表于2021-12-16 11:12 被阅读0次

工作负载

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-php
  labels:
    name: my-php
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-php
  template:
    metadata:
      labels:
        app: my-php
    spec:
      containers:
      - name: my-php
        image: php:7.2-fpm
        volumeMounts:
        - mountPath: /var/www/html/
          name: nginx-data
        ports:
        - containerPort: 9000
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/html
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      app: my-nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.8
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/html
      - name: nginx-conf
        hostPath:
         path: /root/k8s/conf

服务

apiVersion: v1
kind: Service
metadata:
  name: my-php
spec:
  ports:
  - name: my-php
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: my-php
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
spec:
  type: NodePort
  ports:
  - name: my-nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30003
  selector:
    app: my-nginx

nginx配置 default.conf

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;#index.php文件位置 与php服务中/var/www/html同步
        index  index.html index.htm index.php;
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        #try_files $uri =404;
        fastcgi_pass   my-php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
        include        fastcgi_params;
        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
             set $real_script_name $1;
             set $path_info $2;
        }
        fastcgi_param SCRIPT_NAME $real_script_name;
        fastcgi_param PATH_INFO $path_info;

    }
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

nginx-example-ingess.yaml

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute # 类型为路由
metadata:
  name: example-nginx # 定义名称
  namespace: default
spec:
  entryPoints:
    - web
  routes:
  - match: Host(`example.xxx.com`)  # 转发到域名,同时可以映射路径 && PathPrefix(`/notls`)
    kind: Rule
    services:
    - name: my-nginx  # 前面创建的 service,要绑定对应上
      port: 80
# 发布路由配置
kubectl apply -f nginx-example-ingess.yaml

修改 Host 为自己解析域名,新增nginx配置文件

cp default.conf nginxtest.conf 
#  server_name  example.xxx.com;

访问
http://ip:30003
http://example.xxx.com

相关文章

网友评论

    本文标题:k8s nginx+php

    本文链接:https://www.haomeiwen.com/subject/uktvfrtx.html