通过K8s部署wordpress:5-php7.2-fpm

目标

在默认的名称空间下创建wordpress:5-php7.2-fpm、nginx、mysql的pod。
由于nginx和wordpress(php-fpm)需要共享一个volume,所以这两个pod需要指定在同一node上部署(使用nodeSelector)。

一、给一个node打标签

目前我们测试环境有三个工作节点,分别是35、36、37:
1
kubectl get nodes
2
NAME STATUS ROLES AGE VERSION
3
31 Ready master 8d v1.18.2
4
32 Ready master 6d19h v1.18.2
5
33 Ready master 6d19h v1.18.2
6
35 Ready <none> 6d23h v1.18.2
7
36 Ready <none> 6d23h v1.18.2
8
37 Ready <none> 6d23h v1.18.2
Copied!
我选择为36节点打上label:
1
kubectl label nodes 36 nodetype=wordpress
2
node/36 labeled
Copied!
kubectl get nodes --show-labels 可以看到36节点最后面加入了nodetype=wordpress标签。

二、设置nginx

1)nginx的default.conf配置

1
server {
2
listen 80;
3
server_name localhost;
4
access_log /var/log/nginx/host.access.log main;
5
location / {
6
root /var/www/html;
7
index index.php index.html index.htm;
8
try_files $uri $uri/ /index.php?$args;
9
}
10
error_page 500 502 503 504 /50x.html;
11
location = /50x.html {
12
root /usr/share/nginx/html;
13
}
14
location ~ \.php$ {
15
fastcgi_pass wordpress.default.svc.cluster.local:9000;
16
fastcgi_index index.php;
17
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
18
include fastcgi_params;
19
}
20
}
Copied!

2)设置nginx的yaml文件

1
apiVersion: v1
2
kind: ConfigMap
3
metadata:
4
name: nginx-conf
5
namespace: default
6
data:
7
default.conf: |
8
server {
9
listen 80;
10
server_name localhost;
11
access_log /var/log/nginx/host.access.log main;
12
location / {
13
root /var/www/html;
14
index index.php index.html index.htm;
15
try_files $uri $uri/ /index.php?$args;
16
}
17
error_page 500 502 503 504 /50x.html;
18
location = /50x.html {
19
root /usr/share/nginx/html;
20
}
21
location ~ \.php$ {
22
fastcgi_pass wordpress.default.svc.cluster.local:9000;
23
fastcgi_index index.php;
24
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
25
include fastcgi_params;
26
}
27
}
28
---
29
apiVersion: apps/v1
30
kind: Deployment
31
metadata:
32
labels:
33
app: nginx
34
name: nginx
35
spec:
36
replicas: 1
37
selector:
38
matchLabels:
39
app: nginx
40
template:
41
metadata:
42
labels:
43
app: nginx
44
spec:
45
nodeSelector:
46
nodetype: wordpress #指定使用的node
47
containers:
48
- image: nginx:alpine
49
name: nginx
50
volumeMounts:
51
- name: ngxconfs
52
mountPath: /etc/nginx/conf.d/
53
readOnly: true
54
- name: wwwdata
55
mountPath: /var/www/html
56
volumes:
57
- name: ngxconfs
58
configMap:
59
name: nginx-conf
60
optional: false
61
- name: wwwdata
62
hostPath:
63
path: /root/wwwdata
64
65
---
66
apiVersion: v1
67
kind: Service
68
metadata:
69
labels:
70
app: nginx
71
name: nginx
72
namespace: default
73
spec:
74
ports:
75
- name: "80"
76
port: 80
77
protocol: TCP
78
targetPort: 80
79
selector:
80
app: nginx
81
type: NodePort #NodePort实现Pod外部通信
82
---
83
Copied!

二、设置mysql

创建mysql.yaml,内容如下:
1
apiVersion: apps/v1
2
kind: Deployment
3
metadata:
4
labels:
5
app: wpdb
6
name: wpdb
7
spec:
8
replicas: 1
9
selector:
10
matchLabels:
11
app: wpdb
12
template:
13
metadata:
14
labels:
15
app: wpdb
16
spec:
17
containers:
18
- image: mysql:5.7
19
name: wpdb
20
env:
21
- name: MYSQL_DATABASE
22
value: wpdb
23
- name: MYSQL_USER
24
value: wpuser
25
- name: MYSQL_PASSWORD
26
value: wppass
27
- name: MYSQL_RANDOM_ROOT_PASSWORD
28
value: '1'
29
---
30
apiVersion: v1
31
kind: Service
32
metadata:
33
labels:
34
app: wpdb
35
name: wpdb
36
namespace: default
37
spec:
38
ports:
39
- name: "3306"
40
port: 3306
41
protocol: TCP
42
targetPort: 3306
43
selector:
44
app: wpdb
45
type: ClusterIP
Copied!

三、设置wordpress(php-fpm镜像版本)

创建wordpress.yaml
1
apiVersion: v1
2
kind: ConfigMap
3
metadata:
4
name: fpm-conf
5
namespace: default
6
data:
7
www.conf: |
8
[www]
9
user = www-data
10
group = www-data
11
listen = 127.0.0.1:9000
12
pm = dynamic
13
pm.max_children = 5
14
pm.start_servers = 2
15
pm.min_spare_servers = 1
16
pm.max_spare_servers = 3
17
---
18
apiVersion: apps/v1
19
kind: Deployment
20
metadata:
21
labels:
22
app: wordpress
23
name: wordpress
24
spec:
25
replicas: 1
26
selector:
27
matchLabels:
28
app: wordpress
29
template:
30
metadata:
31
labels:
32
app: wordpress
33
spec:
34
nodeSelector:
35
nodetype: wordpress
36
containers:
37
- image: wordpress:5-php7.2-fpm
38
name: wordpress
39
volumeMounts:
40
- name: fpmconfs
41
mountPath: /usr/local/etc/php-fpm.d/
42
readOnly: true
43
- name: wwwdata
44
mountPath: /var/www/html
45
env:
46
- name: WORDPRESS_DB_NAME
47
value: wpdb
48
- name: WORDPRESS_DB_USER
49
value: wpuser
50
- name: WORDPRESS_DB_PASSWORD
51
value: wppass
52
- name: WORDPRESS_DB_HOST
53
value: "wpdb.default.svc.cluster.local."
54
volumes:
55
- name: fpmconfs
56
configMap:
57
name: fpm-conf
58
optional: fals
59
- name: wwwdata
60
hostPath:
61
path: /root/wwwdata
62
---
63
apiVersion: v1
64
kind: Service
65
metadata:
66
labels:
67
app: wordpress
68
name: wordpress
69
namespace: default
70
spec:
71
ports:
72
- name: "9000"
73
port: 9000
74
protocol: TCP
75
targetPort: 9000
76
selector:
77
app: wordpress
78
type: ClusterIP
79
Copied!

四、创建应用

1
$ kubectl apply -f mysql.yaml
2
deployment.apps/wpdb created
3
service/wpdb created
4
5
$ kubectl apply -f wordpress.yaml
6
configmap/fpm-conf unchanged
7
deployment.apps/wordpress created
8
service/wordpress created
9
10
$ kubectl apply -f nginx.yaml
11
configmap/nginx-conf created
12
deployment.apps/nginx created
13
service/nginx created
Copied!
查看pod的状态
1
$ kubectl get pod -o wide
2
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
3
nginx-7fc4b6d479-q5q6q 1/1 Running 0 2m6s 10.244.2.22 36 <none> <none>
4
wordpress-666b699b97-5lggt 0/1 CrashLoopBackOff 7 15m 10.244.2.20 36 <none> <none>
5
wpdb-698dcf9f99-pz58j 1/1 Running 0 2m53s 10.244.2.21 36 <none> <none>
6
Copied!
P.S. 在部署wordpress的容器时我曾经遇到CrashLoopBackOff错误,用kubectl logs 容器Name -p查看日志进行排障,显示为FPM的设置问题。
1
[27-May-2020 16:03:34] ERROR: unable to bind listening socket for address 'wordpress.default.svc.cluster.local.:9000': Cannot assign requested address (99)
2
[27-May-2020 16:03:34] ERROR: FPM initialization failed
Copied!
当时误把socket的监听地址写成容器地址了。
查看pod的端口:
1
$ kubectl get svc
2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
3
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
4
nginx NodePort 10.100.128.76 <none> 80:31456/TCP 72m
5
wordpress NodePort 10.100.230.206 <none> 80:31009/TCP 94s
6
wpdb ClusterIP 10.99.43.10 <none> 3306/TCP 73m
Copied!
遗留问题:wordpress.yaml配置问题,导致该pod和docker无法正常启动,留日后修改。问题文件gitee路径:https://gitee.com/maxidea/k8s-testing-files/blob/master/k8s/wordpress/wordpress.yaml
Last modified 1yr ago