一、项目架构
前端、后端、数据库
1)前端
静态的资源:img css html js文件
js:axios、ajax
2)后端
提供数据:根据web前端发送的请求,从数据库中获取数据
请求都是无状态的,如何保持会话
每次请求都会携带一个token字符串,能够验证用户身份
3)数据库
存储数据与表格
二、流程
1)访问nginx服务,获得页面(下载静态资源)
在旧的版本中,都是使用浏览器直接访问java或者php服务,由这些服务临时渲染一个页面文件响应给客户端。
2)在浏览器中解析并且渲染页面
3)向服务器发送登录请求或者其他的请求
请求的位置:服务器主机ip或者域名在哪里,端口是哪个,服务名称映射
请求的参数:账号,密码
请求的方法:get,post
4)java服务器收到请求之后,对请求解析
例如登录,分发dispatch到login模块
5)从数据库中查询对应的用户信息
如果用户信息的账号和密码都是正确的,就返回字符串,允许用户登录,否则也返回异常信息字符串
三、部署
k8s中使用到的技术:deployment、service、volums
1、准备nfs服务器
1)新建虚拟机
2)设置计划任务,核对时间
[root@nginx ~]# yum -y install ntpdate
[root@nginx ~]# ntpdate time2.aliyun.com
26 Sep 09:45:40 ntpdate[1358]: adjust time server 203.107.6.88 offset 0.009069 sec
[root@nginx ~]# which ntpdate
/usr/sbin/ntpdate
[root@nginx ~]# crontab -e
* 5 * * * /usr/sbin/ntpdate time2.aliyun.com
3)安装nfs服务应用
[root@nginx ~]# yum -y install rpcbind
[root@nginx ~]# yum -y install nfs-utils
4)配置文件
[root@nginx ~]# echo "/root/pes *(rw,sync)" >> /etc/exports
5)准备pes资源
[root@nginx ~]# ls
pes
6)启动nfs服务
[root@nginx ~]# systemctl start rpcbind nfs-server
7)测试验证
[root@k8s-master ~]# showmount -e 10.0.0.99
Export list for 10.0.0.99:
/root/pes *
2、前端web实现
nginx:latest,使用nfs挂载
1)编写deployment清单
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
namespace: default
labels:
app: web-deployment
spec:
selector:
matchLabels:
app: web-deployment
replicas: 3
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: web-deployment
spec:
containers:
- name: nginx
image: docker.io/library/nginx:latest
imagePullPolicy: Never
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 80
volumeMounts:
- name: pesdist
mountPath: /usr/share/nginx/html
volumes:
- name: pesdist
nfs:
server: 10.0.0.99
path: /root/pes/web/src/dist
restartPolicy: Always
2)创建deployment项目
[root@k8s-master cc]# kubectl create -f web-deployment.yaml
deployment.apps/web-deployment created
[root@k8s-master cc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-zdfdf 1/1 Running 32 (54m ago) 13d
web-deployment-7fd7479d49-6x4nn 1/1 Running 0 8s
web-deployment-7fd7479d49-bxdp9 1/1 Running 0 8s
web-deployment-7fd7479d49-xhjwb 1/1 Running 0 8s
3)测试验证
[root@k8s-master cc]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-zdfdf 1/1 Running 32 (57m ago) 13d 172.16.85.200 k8s-node01 <none> <none>
web-deployment-7fd7479d49-6x4nn 1/1 Running 0 2m38s 172.16.58.251 k8s-node02 <none> <none>
web-deployment-7fd7479d49-bxdp9 1/1 Running 0 2m38s 172.16.85.201 k8s-node01 <none> <none>
web-deployment-7fd7479d49-xhjwb 1/1 Running 0 2m38s 172.16.58.252 k8s-node02 <none> <none>
[root@k8s-master cc]# curl 172.16.58.251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<script type="module" crossorigin src="/assets/index-C4kAShR5.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CSz7ARPP.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
3、后端java实现
自定义容器,使用nfs挂载
1)在pes目录中找到java的Dockerfile文件,创建镜像
[root@nginx ~]# cd pes/java/
[root@nginx java]# ls
Dockerfile jdk src start.sh
[root@nginx java]# docker build -t java:v1 .
[root@nginx java]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v1 5dc6610d8a6c 18 hours ago 592MB
2)镜像打包发送到两个节点
[root@nginx ~]# docker save -o java.tar java:v1
[root@nginx ~]# ls
java.tar
[root@nginx ~]# scp java.tar 10.0.0.77:/root
[root@nginx ~]# scp java.tar 10.0.0.88:/root
3)在节点中加载镜像包
[root@k8s-node01 ~]# ctr -n k8s.io images import java.tar --platform=linux/amd64
[root@k8s-node01 ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/java v1 5dc6610d8a6cc 599MB
[root@k8s-node02 ~]# ctr -n k8s.io images import java.tar --platform=linux/amd64
[root@k8s-node02 ~]# crictl images
IMAGE TAG IMAGE ID SIZE
docker.io/library/java v1 5dc6610d8a6cc 599MB
4)创建资源清单
apiVersion: apps/v1
kind: Deployment
metadata:
name: java-deployment
namespace: default
labels:
app: java-deployment
spec:
selector:
matchLabels:
app: java-deployment
replicas: 3
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: java-deployment
spec:
containers:
- name: java
image: docker.io/library/java:v1
imagePullPolicy: Never
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 100Mi
ports:
- containerPort: 8080
volumeMounts:
- name: pesjava
mountPath: /java/src
volumes:
- name: pesjava
nfs:
server: 10.0.0.99
path: /root/pes/java/src
restartPolicy: Always
5)创建deployment项目
[root@k8s-master cc]# kubectl create -f java-deployment.yaml
deployment.apps/java-deployment created
[root@k8s-master cc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-zdfdf 1/1 Running 33 (45m ago) 13d
java-deployment-7b9f747b8d-9cpsw 1/1 Running 0 7s
java-deployment-7b9f747b8d-j6l22 1/1 Running 0 7s
java-deployment-7b9f747b8d-t4cvb 1/1 Running 0 7s
web-deployment-7fd7479d49-6x4nn 1/1 Running 0 51m
web-deployment-7fd7479d49-bxdp9 1/1 Running 0 51m
web-deployment-7fd7479d49-xhjwb 1/1 Running 0 51m
6)测试验证(出现问题)
"OOMKilled" 是一种在容器化环境或某些操作系统中常见的状态,表示 "Out Of Memory Killed",即由于内存不足而被终止。
不断重启
[root@k8s-master cc]# kubectl get po -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-zdfdf 1/1 Running 33 (52m ago) 13d 172.16.85.200 k8s-node01 <none> <none>
java-deployment-7b9f747b8d-9cpsw 0/1 OOMKilled 3 (2m1s ago) 6m32s 172.16.85.198 k8s-node01 <none> <none>
java-deployment-7b9f747b8d-j6l22 0/1 OOMKilled 3 (118s ago) 6m32s 172.16.58.253 k8s-node02 <none> <none>
修改资源清单(解决问题)
limits:
cpu: 100m
memory: 300Mi
重新创建deployment
[root@k8s-master cc]# kubectl delete -f java-deployment.yaml
[root@k8s-master cc]# kubectl create -f java-deployment.yaml
[root@k8s-master cc]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-zdfdf 1/1 Running 33 (58m ago) 13d 172.16.85.200 k8s-node01 <none> <none>
java-deployment-6d9c668fc5-6ftrk 1/1 Running 0 109s 172.16.85.202 k8s-node01 <none> <none>
java-deployment-6d9c668fc5-crk2p 1/1 Running 0 109s 172.16.58.254 k8s-node02 <none> <none>
重新测试
[root@k8s-master cc]# curl 172.16.85.202:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@k8s-master cc]#
4、后端数据库实现
mysql:5.7.44,使用nfs挂载
1)检查是否存在mysql:5.7.44镜像
[root@k8s-node01 ~]# crictl images | grep mysql
docker.io/library/mysql 5.7.44 5107333e08a87 520MB
[root@k8s-node02 ~]# crictl images | grep mysql
docker.io/library/mysql 5.7.44 5107333e08a87 520MB
2)创建资源清单
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mysql-deployment
template:
metadata:
labels:
app: mysql-deployment
spec:
containers:
- name: mysql
image: docker.io/library/mysql:5.7.44
imagePullPolicy: Never
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 200Mi
ports:
- name: mysqlport
containerPort: 3306
volumeMounts:
- name: localtime
mountPath: /var/lib/mysql
volumes:
- name: localtime
nfs:
server: 10.0.0.99
path: /root/pes/mysql/data
restartPolicy: Always
# 如果创建时不使用volumes挂载,则需要将数据导入数据库中
[root@k8s-master cc]# mysql -h172.16.85.222 -uzhangmin -pzhangmin < ~/project_exam_system.sql
3)创建deployment项目(出现问题)
[root@k8s-master cc]# kubectl create -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-deployment-569c465d84-lrp2j 0/1 OOMKilled 2 (18s ago) 30s
修改资源清单(内存不够,扩大使用内存)
resources:
requests:
cpu: 100m
memory: 100Mi
limits:
cpu: 100m
memory: 1000Mi
重新创建deployment项目(出现问题)
"Permission denied"(权限被拒绝)
[root@k8s-master cc]# kubectl delete -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl create -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-deployment-6d67955c9d-lcvrn 0/1 Error 3 (75s ago) 3m14s 172.16.85.219 k8s-node01 <none> <none>
[root@k8s-master cc]# kubectl logs mysql-deployment-6d67955c9d-lcvrn
2024-09-26 11:15:07+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.44-1.el7 started.
find: '/var/lib/mysql/': Permission denied
find: '/var/lib/mysql': Permission denied
修改nfs的文件共享权限
[root@nginx assets]# vim /etc/exports
/root/pes/ *(rw,sync,no_root_squash)
[root@nginx ~]# systemctl restart rpcbind.service
[root@nginx ~]# systemctl restart nfs-server
再次创建deployment项目
[root@k8s-master cc]# kubectl delete -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl create -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test-66bb44bd88-zdfdf 1/1 Running 41 (39m ago) 14d 172.16.85.200 k8s-node01 <none> <none>
java-deployment-6d9c668fc5-6ftrk 1/1 Running 1 (84m ago) 7h42m 172.16.85.202 k8s-node01 <none> <none>
java-deployment-6d9c668fc5-crk2p 1/1 Running 0 7h42m 172.16.58.254 k8s-node02 <none> <none>
java-deployment-6d9c668fc5-hqxjw 1/1 Running 0 7h42m 172.16.58.255 k8s-node02 <none> <none>
mysql-deployment-6d67955c9d-c4kbr 1/1 Running 0 23s 172.16.85.222 k8s-node01 <none> <none>
web-deployment-7fd7479d49-6x4nn 1/1 Running 0 8h 172.16.58.251 k8s-node02 <none> <none>
web-deployment-7fd7479d49-bxdp9 1/1 Running 0 8h 172.16.85.201 k8s-node01 <none> <none>
web-deployment-7fd7479d49-xhjwb 1/1 Running 0 8h 172.16.58.252 k8s-node02 <none> <none>
4)测试验证
[root@k8s-master cc]# mysql -h172.16.85.224 -p'zhangmin' -uzhangmin
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.08 sec)
5、web-service准备
1)创建资源清单
apiVersion: v1
kind: Service
metadata:
name: webservice
spec:
selector:
app: web-deployment
ports:
- name: webport
port: 80
targetPort: 80
nodePort: 32000
protocol: TCP
type: NodePort
2)创建service项目
[root@k8s-master cc]# kubectl create -f web-service.yaml
service/webservice created
[root@k8s-master cc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
webservice NodePort 10.96.126.91 <none> 80:32000/TCP 19s
3)测试验证
找不到域名(设置域名)
此时,不能登录的原因是10.0.0.66的8080端口没有开发,也没有映射到java的pod上(也就是未连接后端)
[root@k8s-master cc]# netstat -lntup | grep 8080
6、java-service准备
1)创建资源清单
apiVersion: v1
kind: Service
metadata:
name: javaservice
spec:
selector:
app: java-deployment
type: NodePort
sessionAffinity: None
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
ports:
- name: javaport
protocol: TCP
port: 8080
targetPort: 8080
nodePort: 32100
2)创建service项目
[root@k8s-master cc]# kubectl create -f java-service.yaml
[root@k8s-master cc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
javaservice NodePort 10.96.83.133 <none> 8080:32100/TCP 12m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
webservice NodePort 10.96.126.91 <none> 80:32000/TCP 13h
3)测试验证
进行前后端连接
7、web与java连接
1)修改web连接java的配置文件
修改nfs服务器pes/web/assets中关于端口请求的文件,将8080端口修改为32100端口
[root@nginx assets]# pwd
/root/pes/web/src/dist/assets
[root@nginx assets]# for fn in $(ls *.js) ; do echo $fn ; done
AdminView-yX0Ltz_1.js
CategoryView-Ca4t3JNT.js
......
[root@nginx assets]# for fn in $(ls *.js) ; do echo $fn ; cat $fn | grep 8080 ; done
index-8SnX15u9.js
http://bu.yuanyu.zhangmin:8080
# 将8080改为32100
[root@nginx assets]# vim index-8SnX15u9.js
"http://bu.yuanyu.zhangmin:32100",
2)测试验证
测试 web服务发送登录请求会发送的32100端口上,就直接可以访问java服务了
前端访问的时候,发现500错误
# 检查java日志,发现连接不到数据库
[root@k8s-master cc]# kubectl logs java-deployment-6d9c668fc5-j9dmm
Caused by: java.net.NoRouteToHostException: No route to host
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
8、java与数据库连接
1)修改java连接数据库的配置文件
[root@nginx assets]# vim ~/pes/java/src/application.properties
spring.datasource.url=jdbc:mysql://pesmysql:3306/project_exam_system
# 浏览器访问后查看java日志
[root@k8s-master cc]# kubectl logs java-deployment-6d9c668fc5-28lvt
Caused by: java.net.UnknownHostException: pesmysql
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:801)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1532)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1384)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1305)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:130)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:62)
... 16 common frames omitted
2)mysql-service资源清单
apiVersion: v1
kind: Service
metadata:
name: pesmysql
spec:
selector:
app: mysql-deployment
type: ClusterIP
sessionAffinity: None
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
ports:
- name: mysqlport
protocol: TCP
port: 3306
targetPort: 3306
3)创建service项目
[root@k8s-master cc]# kubectl create -f mysql-service.yaml
[root@k8s-master cc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
javaservice NodePort 10.96.83.133 <none> 8080:32100/TCP 99m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
pesmysql ClusterIP 10.96.62.94 <none> 3306/TCP 20m
webservice NodePort 10.96.126.91 <none> 80:32000/TCP 14h
4)测试验证
[root@k8s-master cc]# kubectl exec -it cluster-test-66bb44bd88-zdfdf -- bash
(02:16 cluster-test-66bb44bd88-zdfdf:/) nslookup pesmysql
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: pesmysql.default.svc.cluster.local
Address: 10.96.62.94
5)查看数据库数据
MySQL [sys]> use project_exam_system
MySQL [project_exam_system]> show tables;
| user |
+-------------------------------+
MySQL [project_exam_system]> select * from user;
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
| id | userName | password | status | createTime | createOr | editTime | editOr | isDel |
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
| 1 | 21001189 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-22 14:04:44 | NULL | 0 |
| 2 | 21001190 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 01:45:05 | NULL | 0 |
9、最终测试
[root@k8s-master cc]# ls
java-deployment.yaml java-service.yaml mysql-deployment.yaml mysql-service.yaml web-deployment.yaml web-service.yaml
[root@k8s-master cc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
javaservice NodePort 10.96.44.204 <none> 8080:32100/TCP 5h24m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
pesmysql ClusterIP 10.96.129.65 <none> 3306/TCP 16m
webservice NodePort 10.96.144.91 <none> 80:32000/TCP 5h25m
[root@k8s-master cc]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test-66bb44bd88-w7hws 1/1 Running 4 (13m ago) 4h18m
java-deployment-b7857bbf-2zldq 1/1 Running 0 47m
java-deployment-b7857bbf-zj564 1/1 Running 0 47m
mysql-deployment-755cb9cbb6-m6qwn 1/1 Running 0 27m
web-deployment-7b85c4bc87-454jn 1/1 Running 0 55m
web-deployment-7b85c4bc87-qbc2m 1/1 Running 0 55m
四、优化1(pv部分)
1、web部分
1)web的pv创建
资源清单
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 7Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs-slow
nfs:
path: /root/pes/web/src/dist
server: 10.0.0.99
创建
[root@k8s-master cc]# kubectl create -f pv-nfs.yaml
persistentvolume/pv-nfs created
[root@k8s-master cc]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-nfs 7Gi RWO Recycle Available nfs-slow 84s
2)web的pvc创建
资源清单
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs
spec:
storageClassName: nfs-slow
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
创建
[root@k8s-master cc]# kubectl create -f pvc-nfs.yaml
persistentvolumeclaim/pvc-nfs created
[root@k8s-master cc]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-nfs Bound pv-nfs 7Gi RWO nfs-slow 9s
3)修改deployment资源清单
volumes:
- name: pesdist
persistentVolumeClaim:
claimName: pvc-nfs
重新创建
[root@k8s-master cc]# kubectl delete -f web-deployment.yaml
[root@k8s-master cc]# kubectl create -f web-deployment.yaml
4)测试
[root@k8s-master cc]# curl 172.16.85.236
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<script type="module" crossorigin src="/assets/index-C4kAShR5.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CSz7ARPP.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
2、java部分
1)java的pv创建
资源清单
apiVersion: v1
kind: PersistentVolume
metadata:
name: java-pv-nfs
spec:
capacity:
storage: 7Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs-slow
nfs:
path: /root/pes/java/src
server: 10.0.0.99
创建
[root@k8s-master cc]# kubectl create -f java-pv-nfs.yaml
2)java的pvc创建
资源清单
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: java-pvc-nfs
spec:
storageClassName: nfs-slow
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
创建
[root@k8s-master cc]# kubectl create -f java-pvc-nfs.yaml
3)修改deployment资源清单
volumes:
- name: pesjava
persistentVolumeClaim:
claimName: java-pvc-nfs
重新创建
[root@k8s-master cc]# kubectl delete -f java-deployment.yaml
[root@k8s-master cc]# kubectl create -f java-deployment.yaml
4)测试
[root@k8s-master cc]# curl 172.16.85.243:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@k8s-master cc]#
3、mysql部分
1)mysql的pv创建
资源清单
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-nfs
spec:
capacity:
storage: 7Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs-slow
nfs:
path: /root/pes/mysql/data
server: 10.0.0.99
创建
[root@k8s-master cc]# kubectl create -f mysql-pv-nfs.yaml
2)mysql的pvc创建
资源清单
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc-nfs
spec:
storageClassName: nfs-slow
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
创建
[root@k8s-master cc]# kubectl create -f mysql-pvc-nfs.yaml
3)修改deployment资源清单
volumes:
- name: localtime
persistentVolumeClaim:
claimName: mysql-pvc-nfs
重新创建
[root@k8s-master cc]# kubectl delete -f mysql-deployment.yaml
[root@k8s-master cc]# kubectl create -f mysql-deployment.yaml
4)测试
[root@k8s-master cc]# mysql -h172.16.85.242 -uzhangmin -pzhangmin
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
4、最终测试
[root@k8s-master cc]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
java-pv-nfs 7Gi RWO Recycle Bound default/java-pvc-nfs nfs-slow 60m
mysql-pv-nfs 7Gi RWO Recycle Bound default/mysql-pvc-nfs nfs-slow 40m
pv-nfs 7Gi RWO Recycle Bound default/pvc-nfs nfs-slow 74m
[root@k8s-master cc]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
java-pvc-nfs Bound java-pv-nfs 7Gi RWO nfs-slow 60m
mysql-pvc-nfs Bound mysql-pv-nfs 7Gi RWO nfs-slow 40m
pvc-nfs Bound pv-nfs 7Gi RWO nfs-slow 74m
五、优化2(ingress部分)
1、创建web的ingress
资源清单
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
namespace: default
spec:
ingressClassName: nginx
rules:
- host: wp-web.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: web-service
port:
number: 80
创建
[root@k8s-master cc]# kubectl create -f web-ingress.yaml
2、最终测试
[root@k8s-master cc]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
web-ingress nginx wp-web.com 80 20m