k8s部署php项目示例
# 编写打包镜像的文件
Dockerfile
FROM php:7.2.34-fpm-alpine
WORKDIR /app
ENV TZ "Asia/Shanghai"
RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g" /etc/apk/repositories \
&& apk add --no-cache autoconf g++ libtool make curl-dev libxml2-dev linux-headers \
&& docker-php-ext-install -j 2 zip \
&& apk add --no-cache freetype-dev libjpeg-turbo-dev libpng-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j 2 gd \
&& docker-php-ext-install -j$(nproc) bcmath \
&& docker-php-ext-install -j 2 mysqli \
&& docker-php-ext-install pdo_mysql
RUN apk add nginx \
&& mkdir /run/nginx
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
copy . .
# 启动脚本
COPY docker/start.sh /start.sh
RUN chmod +x /start.sh
RUN chown -R www-data:www-data /app
EXPOSE 80
CMD ["/start.sh"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
nginx配置文件
server {
listen 80;
server_name localhost;
root /app/public;
client_max_body_size 50m;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header 'Access-Control-Allow-Headers' 'X-CSRF-Token';
index index.html index.htm index.php;
charset utf-8;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
root /app/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#location ~ /\.(?!well-known).* {
# deny all;
# }
# location ~* \.(sh)$ {
# deny all;
# }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
start.sh
nginx
php-fpm
1
2
2
执行打包镜像
docker build -t test/php:v1 .
1
# 编写部署文件
configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-files
data:
database.php: |
<?php
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '192.168.51.49',
// 数据库名
'database' => 'vmq_new',
// 用户名
'username' => 'root',
// 密码
'password' => '2025W#@qGhsd',
// 端口
'hostport' => '13306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 自动读取主库数据
'read_master' => false,
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型
'resultset_type' => 'array',
// 自动写入时间戳字段
'auto_timestamp' => false,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析
'sql_explain' => false,
// Builder类
'builder' => '',
// Query类
'query' => '\\think\\db\\Query',
// 是否需要断线重连
'break_reconnect' => false,
// 断线标识字符串
'break_match_str' => [],
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
deplyment
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-deployment
labels:
app: php-app
spec:
replicas: 3
selector:
matchLabels:
app: php-app
template:
metadata:
labels:
app: php-app
spec:
containers:
- name: php
image: docker.cnb.cool/zzppjj/docker-images/php_k8s:v6
ports:
- containerPort: 80
volumeMounts:
- name: app-config
mountPath: /app/config/database.php
subPath: database.php
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
volumes:
- name: app-config
configMap:
name: app-config-files
items:
- key: database.php
path: database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
svc
apiVersion: v1
kind: Service
metadata:
name: php-service
spec:
selector:
app: php-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: php-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: / # Nginx Ingress 专用注解
spec:
ingressClassName: nginx # 指定 Ingress 控制器类型
rules:
- host: yzhifu.xsnd.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: php-service
port:
number: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
将上面yaml文件依次启动
访问域名yzhifu.xsnd.com即可


上次更新: 2025/02/11, 13:17:47