要求:使用StorageClass、configmap、secret、https、探针
规划
1、构建镜像
alvinos/nginx:discuz-v1
alvinos/php:discuz-v2
mysql:5.7
2、设置网络
ingress ---> svc ---> pod ---> mysql svc ---> mysql pod
3、编写配置清单
1、mysql
1、mysql命名空间
2、mysql存储
3、MySQL应用
4、备份
2、discuz
1、discuz命名空间
2、存储
3、应用(服务和svc)
4、访问测试
要求:一主多从,多个从节点可以任意添加
https://hub.docker.com/repositories
1.下载discuz包
[root@k8s-master1 ~]# git clone https://gitee.com/3dming/DiscuzL.git
[root@k8s-master1 ~]# tree DiscuzL/upload/ -L 1
DiscuzL/upload/
├── admin.php
├── api
├── api.php
├── archiver
├── config
├── connect.php
├── crossdomain.xml
├── data
├── favicon.ico
├── forum.php
├── group.php
├── home.php
├── index.php
├── install
├── m
├── member.php
├── misc.php
├── plugin.php
├── portal.php
├── robots.txt
├── search.php
├── source
├── static
├── template
├── uc_client
└── uc_server
2.解压 # 此包默认是zip格式,无需改名
[root@k8s-master1 ~]# unzip download
3.授权可写、压缩 # 打包后续备用
[root@k8s-master1 ~]# chmod -R o+x upload
[root@k8s-master1 ~]# tar zcf discuz.gz upload/*
4创建站点目录
[root@k8s-master1 ~]# mkdir /Project/discuz/{php,nginx} -p
[root@k8s-master1 ~]# cp -ar discuz.gz /Project/discuz/php
[root@k8s-master1 ~]# cp -ar discuz.gz /Project/discuz/nginx
# 编写Dockerfile
[root@k8s-master1 ~]# cd /Project/discuz/php
[root@k8s-master1 ~]# cat > Dockerfile <
FROM 18954354671/lnmp-php-wp:v3
ADD discuz.gz /usr/share/nginx/html
EXPOSE 9000
EOF
# 查看
[root@k8s-master1 php]# ll
total 10092
-rw-r--r-- 1 root root 10328804 Apr 12 22:16 discuz.gz
-rw-r--r-- 1 root root 80 Apr 12 23:55 Dockerfile
# 构建、上传镜像
[root@k8s-master1 php]# docker build -t 18954354671/lnmp-php-discuz:v2 .
[root@k8s-master1 php]# docker push 18954354671/lnmp-php-discuz:v2
# 编写Dockerfile
[root@k8s-master1 php]# cd /Project/discuz/nginx
[root@k8s-master1 nginx]# cat > Dockerfile <
FROM 18954354671/lnmp-nginx-wp:v3
ADD discuz.gz /usr/share/nginx/html
EXPOSE 80 443
CMD ["nginx","daemon off;"]
EOF
# 查看
[root@k8s-master1 nginx]# ll
total 10092
-rw-r--r-- 1 root root 10328804 Apr 12 22:35 discuz.gz
-rw-r--r-- 1 root root 112 Apr 12 22:37 Dockerfile
# 构建、上传镜像
[root@k8s-master1 nginx]# docker build -t 18954354671/lnmp-nginx-discuz:v2 .
[root@k8s-master1 nginx]# docker push 18954354671/lnmp-nginx-discuz:v2
[root@k8s-master1 ~]# mkdir -pv /Project/mysql/{master,slave}
[root@k8s-master1 ~]# cd /Project/mysql/master
# 编写Dockerfile
[root@k8s-master1 master]# vim Dockerfile
FROM mysql:5.7
ADD my.cnf /etc/mysql/my.cnf
# 配置mysql配置文件
[root@k8s-master1 master]# vim my.cnf
[mysql]
socket=/var/lib/mysql/mysql.sock
[mysqld]
user=mysql
port=3306
binlog_format=mixed
log_bin=mysql-bin
socket=/var/lib/mysql/mysql.sock
server_id=1
sync_binlog=1
log-slave-updates=on
# 构建master镜像
[root@k8s-master1 master]# docker build -t registry.cn-hangzhou.aliyuncs.com/slave/mysql/mysql-master:v1 .
# 登录仓库
[root@k8s-master1 master]# docker login --username=zzp247364 registry.cn-hangzhou.aliyuncs.com
# 推送
[root@k8s-master1 master]# docker push registry.cn-hangzhou.aliyuncs.com/slave/mysql/mysql-master:v1
[root@k8s-master1 ~]# cd /Project/mysql/slave
# 编写Dockerfile
[root@k8s-master1 slave]# vim Dockerfile
FROM mysql:5.7
ADD my.cnf /etc/mysql/my.cnf
# 配置mysql配置文件
[root@k8s-master1 slave]# vim my.cnf
[mysql]
socket=/var/lib/mysql/mysql.sock
[mysqld]
user=mysql
port=3306
binlog_format=mixed
log_bin=mysql-bin
socket=/var/lib/mysql/mysql.sock
server_id=2
read-only=1
# 构建salve镜像
[root@k8s-master1 slave]# docker push registry.cn-hangzhou.aliyuncs.com/slave/mysql/mysql-slave:v1
# 推送
[root@k8s-master1 slave]# docker push registry.cn-hangzhou.aliyuncs.com/slave/mysql/mysql-slave:v1
[root@k8s-master1 ~]# kubectl apply -f flannel.yaml
[root@k8s-master1 ~]# kubectl apply -f deploy.yaml
[root@k8s-master1 ~]# kubectl apply -f ingress-nginx.yaml
# 官网参考链接:https://github.com/helm/helm
1.安装helm # helm(helm相当于kubernetes中的yum)
[root@k8s-master1 ~]# wget https://get.helm.sh/helm-v3.5.3-linux-amd64.tar.gz
[root@k8s-master1 ~]# tar -xf helm-v3.3.4-linux-amd64.tar.gz
[root@k8s-master1 ~]# cd linux-amd64/
[root@k8s-master1 ~]# for i in m1 m2 m3;do scp helm root@$i:/usr/local/bin/; done
# 测试安装, 出现参数即为出轨
[root@k8s-master1 ~]# helm
The Kubernetes package manager
Common actions for Helm:
- helm search: search for charts
- helm pull: download a chart to your local directory to view
- helm install: upload the chart to Kubernetes
- helm list: list releases of charts
2.安装存储类
## 安装一个helm的存储库
[root@k8s-master1 ~]# helm repo add ckotzbauer https://ckotzbauer.github.io/helm-charts
"ckotzbauer" has been added to your repositories
[root@k8s-master1 ~]# helm repo list
NAME URL
ckotzbauer https://ckotzbauer.github.io/helm-charts
# 方式1> :部署nfs客户端及存储类
[root@k8s-master1 ~]# helm install nfs-client --set nfs.server=172.16.1.51 --set nfs.path=/nfs/v6 ckotzbauer/nfs-client-provisioner
NAME: nfs-client
LAST DEPLOYED: Fri Apr 9 09:33:23 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
## 查看部署结果
[root@k8s-master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nfs-client-nfs-client-provisioner-56dddf479f-h9qqb 1/1 Running 0 41s
[root@k8s-master1 ~]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client cluster.local/nfs-client-nfs-client-provisioner Delete Immediate true 61s
# 方式2> :直接使用配置清单(推荐)
accessModes: ReadWriteMany
### 下载包
[root@k8s-master1 ~]# helm pull ckotzbauer/nfs-client-provisioner
### 解压
[root@k8s-master1 ~]# tar -xf nfs-client-provisioner-1.0.2.tgz
### 修改values.yaml
[root@k8s-master1 ~]# cd nfs-client-provisioner/
# 所需修改项为如下:
[root@k8s-master1 /opt/nfs-client-provisioner]# vim values.yaml
nfs:
server: 192.168.12.11
path: /nfs/v6
reclaimPolicy: Retain
accessModes: ReadWriteMany
# 查看当前pvc
[root@k8s-master1 ~]# kubectl get pvc
No resources found in default namespace.
# 过滤查看pv
[root@k8s-master1 ~]# kubectl get pv | awk '{print $1}'
NAME
pvc-406c3f25-f857-491a-b8b9-a79f7b3262ac
pvc-7b1083e7-58e1-4984-b61c-f130d332a26f
pvc-d6ed3bd3-4a1a-4032-aec3-909d4a2aca3f
# 清空pv
[root@k8s-master1 ~]# kubectl get pv | awk '{print $1}' | xargs -I {} kubectl delete pv {}
Error from server (NotFound): persistentvolumes "NAME" not found
persistentvolume "pvc-406c3f25-f857-491a-b8b9-a79f7b3262ac" deleted
persistentvolume "pvc-7b1083e7-58e1-4984-b61c-f130d332a26f" deleted
persistentvolume "pvc-d6ed3bd3-4a1a-4032-aec3-909d4a2aca3f" deleted
# 已清空pv
[root@k8s-master1 ~]# kubectl get pv
No resources found
# 清空/nfs/v3/*
[root@k8s-master1 ~]# rm -rf /nfs/v3/*
[root@k8s-master1 discuz-store-mysql]# ll
total 32
-rw-r--r-- 1 root root 364 Apr 12 19:15 1-mysql-storage.yaml
-rw-r--r-- 1 root root 1927 Apr 13 20:22 2-mysql-deployment.yaml
-rw-r--r-- 1 root root 115 Apr 13 21:23 3-discuz-namespace.yaml
-rw-r--r-- 1 root root 440 Apr 12 23:02 4-discuz-storage.yaml
-rw-r--r-- 1 root root 531 Apr 13 22:32 5-discuz-configmap.yaml
-rw-r--r-- 1 root root 3527 Apr 13 23:42 6-discuz-deployment.yaml
cat > 1-mysql-storage.yaml apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: discuz-mysql
name: mysql-data
spec:
storageClassName: nfs-client
accessModes:
- "ReadWriteMany" # 给一个策略(读与写)
resources:
requests:
storage: "1Gi" # 数据库一般建议给500G
EOF
cat > 2-mysql-deployment.yaml <kind: Namespace
apiVersion: v1
metadata:
name: discuz-mysql
---
kind: Service
apiVersion: v1
metadata:
name: mysql-cluster-svc
namespace: discuz-mysql
spec:
ports:
- port: 3306
targetPort: 3306
name: mysql-tcp
type: ClusterIP
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: discuz
namespace: discuz-mysql
spec:
selector:
matchLabels:
app: discuz
deploy: mysql-cluster
template:
metadata:
labels:
app: discuz
deploy: mysql-cluster
spec:
containers:
- name: mysql
image: mysql:5.7
livenessProbe: # 存活检测定义
tcpSocket:
port: 3306 # tcpSocket连接端口
initialDelaySeconds: 30 # 数据库初始化启动时间,根据机器反应快慢定义
successThreshold: 1 # 探测数据库启动成功次数
failureThreshold: 3 # 探测数据库启动失败次数
timeoutSeconds: 1 # 本地连接探测超时时间,一秒足够长
periodSeconds: 2 # 执行探测频率,几秒一次,默认10秒
readinessProbe: # 就绪检测定义,同上
tcpSocket:
port: 3306
initialDelaySeconds: 30
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 1
periodSeconds: 2
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: "discuz"
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-data # 此处定死
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data # 挂载到上方定义的容器目录
EOF
cat > 3-discuz-namespace.yaml <kind: Namespace
apiVersion: v1
metadata:
name: discuz
EOF
cat > 4-deployment-storage.yaml <apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: discuz # 指定为discuz的命名空间
name: upload-data # 指定为discuz内的的pv名称
spec:
storageClassName: nfs-client
accessModes:
- "ReadWriteMany" # 给一个策略(读与写)
resources:
requests:
storage: "1Gi" # 数据库一般建议给500G
EOF
cat 5-discuz-configmap.yaml
kind: ConfigMap
apiVersion: v1
metadata:
namespace: discuz
name: discuz-configmap
data:
default.conf: |
server {
listen 80;
server_name linux.discuz.com;
root /opt/upload/;
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;
}
}
EOF
cat > 6-discuz-deployment.yaml <# discuz部署信息
kind: Deployment
apiVersion: apps/v1
metadata:
name: discuz
namespace: discuz
spec:
selector:
matchLabels:
app: discuz
deploy: discuz
template:
metadata:
labels:
app: discuz
deploy: discuz
spec:
containers:
- name: php
image: 18954354671/lnmp-php-discuz:v2 # 此时去准备构建discuz的镜像并上传,再往下写
livenessProbe: # 存活检测定义
tcpSocket: # tcpSocket连接端口
port: 9000
initialDelaySeconds: 30 # 数据库初始化启动时间,根据机器反应快慢定义
successThreshold: 1 # 探测数据库启动成功次数
failureThreshold: 3 # 探测数据库启动失败次数
timeoutSeconds: 1 # 本地连接探测超时时间,一秒足够长
periodSeconds: 2 # 执行探测频率,几秒一次,默认10秒
readinessProbe: # 就绪检测定义,同上
tcpSocket:
port: 9000
initialDelaySeconds: 30
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 1
periodSeconds: 2
- name: nginx
image: 18954354671/lnmp-nginx-discuz:v2
command: ["/bin/bash","-c","--"] # 此两行解决nginx拉取失败(且无错误日志)
args: ["while true;do sleep 30;done;"] # 此两行解决nginx拉取失败(且无错误日志)
livenessProbe: # 存活检测定义
tcpSocket:
port: 80 # tcpSocket连接端口
initialDelaySeconds: 30 # 数据库初始化启动时间,根据机器反应快慢定义
successThreshold: 1 # 探测数据库启动成功次数
failureThreshold: 3 # 探测数据库启动失败次数
timeoutSeconds: 1 # 本地连接探测超时时间,一秒足够长
periodSeconds: 2 # 执行探测频率,几秒一次,默认10秒
readinessProbe: # 就绪检测定义,同上
tcpSocket:
port: 80
initialDelaySeconds: 30
successThreshold: 1
failureThreshold: 3
timeoutSeconds: 1
periodSeconds: 2
volumeMounts:
- mountPath: /usr/share/nginx/html/upload
name: upload-data
- mountPath: /etc/nginx/conf.d
name: discuz-configmap
volumes: # 此时需要给Deployment创建pv(建议单独创建,不能随便修改且防止误删除:deployment-pv.yaml)
- name: upload-data # pv需指定为此discuz的命名空间
persistentVolumeClaim:
claimName: upload-data
- name: discuz-configmap
configMap:
name: discuz-configmap
items:
- key: default.conf
path: default.conf
---
# 部署discuz-Service
kind: Service
apiVersion: v1
metadata:
name: discuz-svc
namespace: discuz
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: discuz
deploy: discuz
clusterIP: None
---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: discuz
namespace: discuz
spec:
rules:
- host: linux.discuz.com
http:
paths:
- backend:
serviceName: discuz-svc
servicePort: 80
EOF
# 运行刚刚构建的nginx镜像,复制其nginx配置到配置清单
[root@k8s-master1 nginx]# docker run --rm -it 18954354671/lnmp-nginx-discuz:v2 bash
root@dcadb97a6799:/usr/share/nginx/html# cat /etc/nginx/conf.d/linux.wp.com.conf
server {
listen 80;
server_name linux.discuz.com;
root /opt/upload/;
location / {
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
}
kubectl apply -f 1-mysql-storage.yaml
kubectl apply -f 2-mysql-deployment.yaml
kubectl apply -f 3-discuz-namespace.yaml
kubectl apply -f 4-discuz-storage.yaml
kubectl apply -f 5-discuz-configmap.yaml
kubectl apply -f 6-discuz-deployment.yaml
[root@k8s-master1 ~]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
- --feature-gates=RemoveSelfLink=false
# 重新部署即可~
[root@k8s-master1 ~]# kubectl apply -f /etc/kubernetes/manifests/kube-apiserver.yaml