目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@pool1 data]# tree
├── v1
│   ├── data
│   │   ├── index.html
│   │   └── index.php
└── yaml
├── dockerfile
├── namespace.yaml
├── nginx-cm.yaml
├── nginx-deploy.yaml
├── nginx-svc.yaml
├── php-deploy.yaml
└── php-svc.yaml

构建php镜像

1
2
3
4
5
6
7
8
9
10
11
12
[root@pool1 yaml]# vi dockerfile
FROM centos:7
MAINTAINER SeMaik
RUN yum makecache
RUN yum -y install php-fpm php php-gd php-mysql php-mbstring php-xml php-mcrypt php-imap php-odbc php-pear php-xmlrpc
RUN sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /etc/php-fpm.d/www.conf
RUN sed -i 's/listen.allowed_clients = 127.0.0.1/;listen.allowed_clients = 127.0.0.1/' /etc/php-fpm.d/www.conf

EXPOSE 9000
CMD ["/sbin/php-fpm"]

# docker build -t php:v5.4.16 .

部署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
[root@pool1 yaml]# vi php-deploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-deploy
spec:
replicas: 1
selector:
matchLabels:
app: php-latest
template:
metadata:
labels:
app: php-latest
spec:
containers:
- name: php-latest
image: php:v5.4.16
imagePullPolicy: Never
ports:
- containerPort: 9000
volumeMounts:
- name: html
mountPath: /var/www/html
volumes:
- name: html
hostPath:
path: /data/v1/data/
1
2
3
4
5
6
7
8
9
10
11
[root@pool1 yaml]# vi php-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: php
spec:
selector:
app: php-latest
ports:
- port: 9000
targetPort: 9000
1
2
3
4
5
6
7
8
9
10
11
12
[root@pool1 yaml]# kubectl apply -f php-deploy.yaml

[root@pool1 yaml]# kubectl apply -f php-svc.yaml

[root@pool1 yaml]# kubectl get pods -A -o wide| grep php
default php-deploy-6d5574759c-6rpjz 1/1 Running 0 24h 240.185.1.40 pool2 <none> <none>

[root@pool1 yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
php ClusterIP 10.96.131.137 <none> 9000/TCP 29h

部署nginx

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
[root@pool1 yaml]# vi nginx-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginxconf
data:
nginx.conf: |
#a
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


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

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/local/nginx/html;
index index.html index.htm;
}

#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/local/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;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# 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
[root@pool1 yaml]# vi nginx-deploy.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
spec:
replicas: 1
selector:
matchLabels:
app: nginx-v1.11.1
template:
metadata:
labels:
app: nginx-v1.11.1
spec:
containers:
- name: nginx-latest
image: zx/nginx:v1.11.1
imagePullPolicy: Never
ports:
- containerPort: 80
volumeMounts:
- name: nginxhtml
mountPath: /usr/local/nginx/html/
- name: nginxconf
mountPath: /usr/local/nginx/conf/nginx.conf
subPath: nginx.conf
volumes:
- name: nginxhtml
hostPath:
path: /data/v1/data/
- name: nginxconf
configMap:
name: nginxconf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@pool1 yaml]# vi nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-php
spec:
type: NodePort
selector:
app: nginx-v1.11.1
ports:
- port: 80
targetPort: 80
nodePort: 30800

1
2
3
4
5
6
7
8
9
10
11
12
[root@pool1 yaml]# kubectl apply -f nginx-deploy.yaml

[root@pool1 yaml]# kubectl apply -f nginx-php.yaml

[root@pool1 yaml]# kubectl get pods -A -o wide| grep nginx
default nginx-deploy-757bc559b8-mfbwq 1/1 Running 1 24h 240.185.2.30 pool3 <none> <none>

[root@pool1 yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
nginx-php NodePort 10.98.154.219 <none> 80:30800/TCP 24h
php ClusterIP 10.96.131.137 <none> 9000/TCP 29h

验证

访问172.16.1.155:30800

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@pool1 yaml]# curl -I 172.16.1.155:30800
HTTP/1.1 200 OK
Server: nginx/1.11.1
Date: Wed, 12 May 2021 07:49:57 GMT
Content-Type: text/html
Content-Length: 3951
Last-Modified: Sat, 08 May 2021 07:53:21 GMT
Connection: keep-alive
ETag: "60964371-f6f"
Accept-Ranges: bytes

[root@pool1 yaml]# curl -I 172.16.1.155:30800/index.php
HTTP/1.1 200 OK
Server: nginx/1.11.1
Date: Wed, 12 May 2021 07:50:06 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.16