linux12k8s -->10搭建wordpess+ discuz
文章目錄
一、搭建wordpress
1、構(gòu)建鏡像
2、構(gòu)思架構(gòu),同時(shí)編寫配置清單
1、創(chuàng)建名稱空間
2、部署
3、測試網(wǎng)絡(luò)連接
3、部署
1、搭建wordpress
# 1、編寫wordpress文件
[root@k8s-m-01 ~]# vim wordpress.yaml
# 數(shù)據(jù)庫服務(wù)部署
# 數(shù)據(jù)庫名稱空間創(chuàng)建
apiVersion: v1
kind: Namespace
metadata:
name: mysql
---
# 數(shù)據(jù)庫控制器創(chuàng)建
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: wordpress
livenessProbe: # 存活性檢查
exec:
command:
- "/bin/bash"
- "-c"
- "cat /etc/mysql/my.cnf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe: # 就緒性檢查
tcpSocket:
port: 3306
initialDelaySeconds: 20
periodSeconds: 1
successThreshold: 3
failureThreshold: 1
timeoutSeconds: 1
---
# 給數(shù)據(jù)庫配置Service
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
targetPort: 3306
type: NodePort
# 數(shù)據(jù)庫部署完畢
---
# 創(chuàng)建項(xiàng)目的名稱空間
apiVersion: v1
kind: Namespace
metadata:
namespace: wordpress
name: wordpress
---
# 創(chuàng)建項(xiàng)目的控制器
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: php
image: alvinos/php:wordpress-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/bash"
- "-c"
- "ps -ef | grep php"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 1
readinessProbe:
tcpSocket:
port: 9000
initialDelaySeconds: 20
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
- name: nginx
image: alvinos/nginx:wordpress-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/bash"
- "-c"
- "cat /etc/nginx/nginx.conf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 1
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 10
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
# 控制器部署完畢
---
# 部署控制器Service
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
app: wordpress
ports:
- port: 80
targetPort: 80
name: http
nodePort: 30080
- port: 443
targetPort: 443
name: https
type: NodePort
# 2、查看svc
[root@k8s-m-01 blog]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress NodePort 10.99.127.193 <none> 80:30080/TCP,443:30404/TCP 5m11s
# 3、IP訪問
192.168.15.111:30080


二、健康檢查搭建discuz
1、創(chuàng)建目錄
[root@k8s-m-01 ~]# mkdir -p discuz
[root@k8s-m-01 ~]# cd discuz/
[root@k8s-m-01 discuz]# mkdir php
[root@k8s-m-01 discuz]# mkdir nginx
2、構(gòu)建nginx
# 1、上次代碼包
[root@k8s-m-01 nginx]# wget http://www.mmin.xyz:81/package/blog/Discuz_X3.4_SC_UTF8_20210320_%281%29.zip
# 2、解壓后把upload改名成discuz
[root@k8s-m-01 nginx]# mv upload discuz
# 3、準(zhǔn)備nginx文件
[root@k8s-m-01 nginx]# vim nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_comp_level 1;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
gzip_buffers 32 4k;
gzip_http_version 1.0;
include /etc/nginx/conf.d/*.conf;
}
[root@k8s-m-01 nginx]# vim default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 4、構(gòu)建Dockerfile
[root@k8s-m-01 nginx]# vim Dockerfile
FROM nginx
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666
ADD nginx.conf /etc/nginx/nginx.conf
ADD default.conf /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html
ADD discuz /usr/share/nginx/html
RUN chown -R www.www /usr/share/nginx/html
WORKDIR /usr/share/nginx/html
EXPOSE 80 443
CMD ["nginx","-g","daemon off;"]
# 5、查看
[root@k8s-m-01 nginx]# ll
total 20
-rw-r--r-- 1 root root 389 Aug 9 09:05 default.conf
drwxr-xr-x 13 root root 4096 Aug 9 10:19 discuz
-rw-r--r-- 1 root root 353 Aug 9 09:13 Dockerfile
-rw-r--r-- 1 root root 1043 Aug 9 09:05 nginx.conf
# 6、上次到自己倉庫(也可以不需要)
[root@k8s-m-01 nginx]# docker build -t registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:nginx-v2 .
[root@k8s-m-01 nginx]# docker push registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:nginx-v2
3、構(gòu)建php
# 1、把discuz包也復(fù)制到php中
[rook8s-m-01 php]# cp -r ../nginx/discuz .
# 2、上傳php.tar.gz
[rook8s-m-01 php]# wget http://www.mmin.xyz:81/package/lnmp/php.tar.gz
# 3、編寫Dockerfile
[root@k8s-m-01 php]# vim Dockerfile
FROM centos:7
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666
ADD php.tar.gz /tmp
RUN yum -y localinstall /tmp/*.rpm
RUN sed -i 's#apache#www#g' /etc/php-fpm.d/www.conf
RUN sed -i 's#127.0.0.1:9000#9000#g' /etc/php-fpm.d/www.conf
RUN sed -i 's#;request_terminate_timeout#request_terminate_timeout#g' /etc/php-fpm.d/www.conf
EXPOSE 9000
WORKDIR /usr/share/nginx/html
ADD discuz /usr/share/nginx/html
RUN chown -R www.www /usr/share/nginx/html
CMD php-fpm -F
# 4、查看
[root@k8s-m-01 php]# ll
total 19436
drwxr-xr-x 13 root root 4096 Aug 9 10:19 discuz
-rw-r--r-- 1 root root 477 Aug 9 09:24 Dockerfile
-rw-r--r-- 1 root root 19889622 Jul 25 01:01 php.tar.gz
# 5、上次到自己倉庫(也可以不需要)
[root@k8s-m-01 nginx]# docker build -t registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:php-v2 .
[root@k8s-m-01 nginx]# docker push registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:php-v2
3、編寫yaml文件
# 1、編寫mysql.yaml
[root@k8s-m-01 discuz]# vim mysql.yaml
kind: Namespace
apiVersion: v1
metadata:
name: mysql
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: mysql
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: discuz
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/mysql/my.cnf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: mysql
spec:
ports:
- port: 3306
targetPort: 3306
protocol: TCP
name: mysql
selector:
app: mysql
# 2、編寫web.yaml
[root@k8s-m-01 discuz]# vim web.yaml
kind: Namespace
apiVersion: v1
metadata:
name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: web
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: php
image: registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:php-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/php-fpm.d/www.conf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 9000
initialDelaySeconds: 10
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/aliyun_mm/discuz:nginx-v2
imagePullPolicy: Always
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /etc/nginx/nginx.conf"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: web
namespace: web
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: web
type: NodePort
4、生成aml文件
# 1、生成yaml文件
[root@k8s-m-01 discuz]# kubectl apply -f web.yaml
[root@k8s-m-01 discuz]# kubectl apply -f mysql.yaml
# 2、查看
[root@k8s-m-01 discuz]# kubectl get pod -n web
NAME READY STATUS RESTARTS AGE
web-7f897d448c-9hr26 2/2 Running 0 16m
[root@k8s-m-01 discuz]# kubectl get pod -n mysql
NAME READY STATUS RESTARTS AGE
mysql-6f9b947c9f-vs5rv 1/1 Running 0 30m
[root@k8s-m-01 discuz]# kubectl get svc -n web web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web NodePort 10.98.114.209 <none> 80:32263/TCP 30m
# 3、IP訪問
192.168.15.111.32263
5、故障排查
# 1、報(bào)css錯(cuò)誤,連接不上css
[root@k8s-m-01 discuz]# kubectl cp -n web web-7f897d448c-b67pk:/usr/share/nginx/html . -c php
# 2、把discuz包重新生成并上傳
與前面類似,略過
# 3、刪除web的pod,讓其自己生成
[root@k8s-m-01 discuz]# kubectl delete pod -n web web-7f897d448c-9hr26


三、數(shù)據(jù)卷搭建Discuz
連接:https://gitee.com/3dming/DiscuzL/attach_files
要求:ingress —> headless service —> pod
1、要有健康檢查
2、要求有https
3、要求有存儲卷,數(shù)據(jù)持久化,防止容器停止或宕機(jī)數(shù)據(jù)隨之丟失
(hostpath:類似于docker -v參數(shù),將存儲卷掛載在本地【pod部署的節(jié)點(diǎn)上】)
# 1、準(zhǔn)備軟件包
[root@k8s-m-01 discuz]# wget http://www.mmin.xyz:81/package/blog/Discuz_X3.4_SC_UTF8_20210320_%281%29.zip
[root@k8s-m-01 discuz]# ll
總用量 12044
-rw-r--r-- 1 root root 12330468 4月 7 2021 Discuz_X3.4_SC_UTF8_20210320.zip
# 2、解壓discuz
[root@k8s-m-01 discuz]# unzip Discuz_X3.4_SC_UTF8_20210320.zip
[root@k8s-m-01 discuz]# ll
總用量 12172
-rw-r--r-- 1 root root 12330468 4月 7 2021 Discuz_X3.4_SC_UTF8_20210320.zip
-rw-r--r-- 1 root root 17886 3月 20 10:36 LICENSE
-rw-r--r-- 1 root root 31040 1月 19 17:18 qqqun.png
drwxr-xr-x 2 root root 124 3月 22 19:44 readme
-rw-r--r-- 1 root root 71107 1月 19 17:20 readme.html
drwxr-xr-x 13 root root 4096 3月 22 19:44 upload
drwxr-xr-x 4 root root 94 3月 22 19:44 utility
# 3、給upload打包以便后邊存儲卷hostpath使用
[root@k8s-m-01 discuz]# tar -czf discuz.tar.gz upload/
[root@k8s-m-01 discuz]# ll
總用量 22260
-rw-r--r-- 1 root root 10329409 4月 4 01:42 discuz.tar.gz
# 4、給每個(gè)節(jié)點(diǎn)都推一份upload壓縮包并授權(quán)777upload
[root@k8s-m-01 discuz]# chmod o+w -R upload/
[root@k8s-m-01 discuz]# for i in n2 n1;do ssh root@$i "mkdir -pv /opt/discuz" && scp discuz.tar.gz root@$i:/opt/discuz/; ssh root@$i "cd /opt/discuz && tar -xf discuz.tar.gz -C /opt/discuz && chmod -R o+w /opt/discuz/upload"; done
# 5、編寫配置清單思路梳理
1.部署MySQL集群
命名空間
service提供負(fù)載均衡
使用控制器部署MySQL實(shí)例
2.部署discuz應(yīng)用
創(chuàng)建命名空間
創(chuàng)建service提供負(fù)載均衡(headless service)
創(chuàng)建ingress,用于域名轉(zhuǎn)發(fā)
3.服務(wù)之間的互連
discuz連接MySQL===》mysql.mysql.svc.cluster.local
==============================================================================
# 6、創(chuàng)建證書
[root@k8s-m-01 discuz]# openssl genrsa -out tls.key 2048
Generating RSA private key, 2048 bit long modulus
.+++
........................................................................+++
e is 65537 (0x10001)
[root@k8s-m-01 discuz]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=ShangHai/L=ShangHai/O=Ingress/CN=www.discuz.cluster.local.com #注意域名要與配置清單定義相同
# 7、部署證書
[root@k8s-m-01 discuz]# kubectl create namespace discuz #部署證書之前要先創(chuàng)命名空間
namespace/discuz created
[root@k8s-m-01 discuz]# kubectl -n discuz create secret tls discuz-secret --cert=tls.crt --key=tls.key #注意證書的secretna
me要與配置清單定義相同(discuz-secret)
secret/discuz-secret created
[root@k8s-m-01 discuz]# ll #查看生成證書
-rw-r--r-- 1 root root 1334 4月 4 03:50 tls.crt
-rw-r--r-- 1 root root 1675 4月 4 03:49 tls.key
# 8、部署配置清單
[root@k8s-m-01 discuz]# vim discuz.yaml
apiVersion: v1 #定義MySQL命名空間
kind: Namespace
metadata:
name: mysql
---
apiVersion: v1 #定義MySQLservice
kind: Service
metadata:
name: mysql-svc
namespace: mysql
spec:
ports:
- port: 3306
targetPort: 3306
name: mysql
protocol: TCP
selector:
app: mysql
deploy: discuz
---
apiVersion: apps/v1 #定義MySQL控制器
kind: Deployment
metadata:
name: mysql-deployment
namespace: mysql
spec:
selector:
matchLabels:
app: mysql
deploy: discuz
template:
metadata:
labels:
app: mysql
deploy: discuz
spec:
nodeName: gdx3 #指定調(diào)度到哪個(gè)節(jié)點(diǎn)上(kubectl get nodes 查看nodename)
containers:
- name: mysql
image: mysql:5.7
livenessProbe: #存活性檢查
tcpSocket:
port: 3306
readinessProbe: #就緒性檢查
tcpSocket:
port: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
- name: MYSQL_DATABASE
value: "discuz"
volumeMounts: #容器存儲卷===》相當(dāng)于掛載
- mountPath: /var/lib/mysql
name: mysql-data
volumes: #宿主機(jī)掛載目錄
- name: mysql-data
hostPath:
path: /opt/discuz/mysql
---
apiVersion: v1 #discuz命名空間
kind: Namespace
metadata:
name: discuz
---
apiVersion: v1 #discuzservice
kind: Service
metadata:
name: discuz-svc
namespace: discuz
spec:
clusterIP: None #使用無頭service,因?yàn)橄路接昧薸ngress域名解析
ports:
- port: 80
targetPort: 80
name: http
selector:
app: discuz
deploy: discuz
---
apiVersion: apps/v1 #discuz控制器
kind: Deployment
metadata:
name: discuz-deployment
namespace: discuz
spec:
selector:
matchLabels:
app: discuz
deploy: discuz
template:
metadata:
labels:
app: discuz
deploy: discuz
spec:
nodeName: gdx3 #因?yàn)闆]有nfs共享目錄,此處指定一臺節(jié)點(diǎn)
containers:
- name: php
image: elaina0808/lnmp-php:v6
livenessProbe: #存活性檢查
tcpSocket:
port: 9000
readinessProbe: #就緒性檢查
tcpSocket:
port: 9000
volumeMounts: #存儲卷掛載
- mountPath: /usr/share/nginx/html
name: discuz-data
- name: nginx
image: elaina0808/lnmp-nginx:v9
livenessProbe: #存活性檢查
httpGet:
port: 80
path: /
readinessProbe: #就緒性檢查
httpGet:
port: 80
path: /
volumeMounts: #存儲卷掛載
- mountPath: /usr/share/nginx/html
name: discuz-data
volumes: #存儲卷掛載
- name: discuz-data
hostPath:
path: /opt/discuz/upload
---
apiVersion: extensions/v1beta1 #定義ingress域名解析
kind: Ingress
metadata:
name: discuz-ingress
namespace: discuz
spec:
tls: #使用https加密
- hosts:
- www.discuz.cluster.local.com
secretName: discuz-secret
rules:
- host: www.discuz.cluster.local.com
http:
paths:
- backend:
serviceName: discuz-svc
servicePort: 80
# 9.生成容器
[root@k8s-m-01 discuz]# kubectl get pods -n discuz
NAME READY STATUS RESTARTS AGE
discuz-deployment-cbbbfc54b-l22wq 2/2 Running 0 58m
# 10、查看nginx php容器是否正常運(yùn)行
[root@k8s-m-01 discuz]# kubectl get pods -n discuz
NAME READY STATUS RESTARTS AGE
discuz-deployment-cbbbfc54b-l22wq 2/2 Running 0 58m
# 11、查看數(shù)據(jù)庫容器是否正常啟動(dòng)
[root@k8s-m-01 discuz]# kubectl get pods -n mysql
NAME READY STATUS RESTARTS AGE
mysql-deployment-c687787fc-l7n5s 1/1 Running 0 118m
# 12、查看ingress是否正常
[root@k8s-m-01 discuz]# kubectl get ingress -n discuz
NAME CLASS HOSTS ADDRESS PORTS AGE
discuz-ingress <none> www.discuz.cluster.local.com 192.168.12.12 80, 443 121m
# 13、查看端口號
[root@k8s-m-01 discuz]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.60.88 <none> 80:32708/TCP,443:32731/TCP 36h
ingress-nginx-controller-admission ClusterIP 10.106.141.57 <none> 443/TCP 36h
#14、配置主機(jī)host文件并訪問
192.168.12.11 www.discuz.cluster.local.com


浙公網(wǎng)安備 33010602011771號