K8S php+nginx(双pod)
k8s php+nginx
·
本文内容纯属自己整理网上,如有错误请自行处理
首先项目目录下Dockerfile
文件将项目制作成镜像
#安装所需要的的拓展
ARG PHP_EXTENSIONS="apcu bcmath pdo_mysql redis imagick gd"
#一个第三方的php镜像
FROM thecodingmachine/php:8.0-v4-fpm as php_base
ENV TEMPLATE_PHP_INI=production
#把项目文件复制到 to html目录
COPY --chown=docker:docker . /var/www/html
#composer 安装依赖
RUN composer install --quiet --optimize-autoloader --ignore-platform-reqs --no-dev
#node镜像安装依赖(小声嘀咕现在不都分离了,怎么还要在后台装)
FROM node:14 as node_dependencies
# 设置主目录的意思
WORKDIR /var/www/html
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=false
#这里是阶段构建的意思从a搬到b加工一下
COPY --from=php_base /var/www/html /var/www/html
RUN npm set progress=false && \
npm config set depth 0 && \
npm install && \
npm run prod && \
rm -rf node_modules
FROM php_base
#阶段构建再从b搬到a
COPY --from=node_dependencies --chown=docker:docker /var/www/html /var/www/html
写完文件然后制作镜像,镜像tag 下面要用
docker build -t="laravel-php" .
-t 标签是自己定义 .代表着寻找当前目录下的Dockerfile
文件制作镜像
k8s ConfigMap
配置主要是配置nginx配置也没什么好说的都能看明白
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-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 php:9000; //这里的php 是指k8s里的name
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
k8s volume
存储卷为了共享两个pod的目录
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: code #给存储卷起个名
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: hostpath #额这里是我找了个默认的,因为教程上写我电脑上没有
k8s Deployment
POD配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: php
labels:
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: php
tier: backend
template:
metadata:
labels:
app: php
tier: backend
spec:
enableServiceLinks: false
containers:
- name: php
image: laravel-php
imagePullPolicy: Never
volumeMounts:
- name: code #用到定义的存储卷
mountPath: /code #目录映射 code 到存储卷 code
lifecycle:
postStart:
exec:
command: [ "/bin/sh", "-c", "cp -r /var/www/html/. /code" ]
volumes:
- name: code
persistentVolumeClaim:
claimName: code
为了看直观些php和nginx 我都分成单独的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
tier: backend
spec:
replicas: 1
selector:
matchLabels:
app: nginx
tier: backend
template:
metadata:
labels:
app: nginx
tier: backend
spec:
volumes:
- name: code
persistentVolumeClaim:
claimName: code
- name: config
configMap:
name: nginx-config
items:
- key: config
path: site.conf #读取了定义的configmap 到文件
containers:
- name: nginx
image: nginx:1.21.0
ports:
- containerPort: 80
volumeMounts:
- name: code
mountPath: /code #共享了php的目录
- name: config
mountPath: /etc/nginx/conf.d #把上面网站配置文件site.conf 挂载到nginx,这样nginx就能转发到php了
k8s最后的Service
因为我本机装不上ingress,所以先用NodePort替代下吧,正式是要用ingress 负载均衡的
apiVersion: v1
kind: Service
metadata:
name: php
labels:
tier: backend
spec:
selector:
app: php
tier: backend
ports:
- protocol: TCP
port: 9000
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
tier: backend
spec:
type: NodePort #暴露端口 这样就可以在127.0.0.1:xxxxx 暴露的端口访问到网站了
selector:
app: nginx
tier: backend
ports:
- protocol: TCP
port: 80
更多推荐
已为社区贡献4条内容
所有评论(0)