2022-07-16 10:21:54 +08:00
|
|
|
|
# 实践大作业
|
2022-07-16 10:20:46 +08:00
|
|
|
|
|
2022-07-16 10:21:54 +08:00
|
|
|
|
## 服务端
|
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
### 简介
|
|
|
|
|
|
2022-07-16 10:21:54 +08:00
|
|
|
|
服务端采用Golang编写,提供PutPet、GetPet、DeletePet三个接口。
|
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
gRPC默认服务端口为`5000`
|
|
|
|
|
|
|
|
|
|
### 使用方式
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cd PCGBigHomeworkServer
|
|
|
|
|
go run main.go
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-16 10:21:54 +08:00
|
|
|
|
## 客户端
|
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
### 简介
|
|
|
|
|
|
2022-07-17 00:31:39 +08:00
|
|
|
|
客户端采用Golang编写,可以进行宠物数据的写入、查询和删除操作。
|
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
采用Gin框架和Layui框架实现了一个简~~单~~陋的前端页面和一套接口。
|
|
|
|
|
|
2022-07-17 16:48:18 +08:00
|
|
|
|
服务默认端口为`80`,gRPC服务端默认为`localhost:5000`。可以通过指定环境变量`GRPC_SERVER`更改服务端地址(端口仍固定为`5000`)。
|
2022-07-17 16:22:37 +08:00
|
|
|
|
|
|
|
|
|
### 使用方式
|
|
|
|
|
|
2022-07-17 16:48:18 +08:00
|
|
|
|
> Tips: 请先启动服务端,再启动客户端。
|
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
```bash
|
|
|
|
|
cd PCGBigHomeworkClient
|
|
|
|
|
go run main.go
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-17 16:42:38 +08:00
|
|
|
|
### 运行截图
|
|
|
|
|
|
2022-07-17 17:00:55 +08:00
|
|
|
|
![homepage](https://www.gitlink.org.cn/repo/TPCGJYProject/120L021230_chenbogeng/raw/branch/master/Picture/homepage.png)
|
2022-07-17 16:42:38 +08:00
|
|
|
|
|
2022-07-17 17:00:55 +08:00
|
|
|
|
![getpage](https://www.gitlink.org.cn/repo/TPCGJYProject/120L021230_chenbogeng/raw/branch/master/Picture/getpage.png)
|
2022-07-17 16:42:38 +08:00
|
|
|
|
|
2022-07-17 17:00:55 +08:00
|
|
|
|
![putpage](https://www.gitlink.org.cn/repo/TPCGJYProject/120L021230_chenbogeng/raw/branch/master/Picture/putpage.png)
|
2022-07-17 16:42:38 +08:00
|
|
|
|
|
2022-07-17 17:00:55 +08:00
|
|
|
|
![deletepage](https://www.gitlink.org.cn/repo/TPCGJYProject/120L021230_chenbogeng/raw/branch/master/Picture/deletepage.png)
|
2022-07-17 16:42:38 +08:00
|
|
|
|
|
2022-07-17 16:22:37 +08:00
|
|
|
|
## 镜像 & Minikube配置文件
|
|
|
|
|
|
|
|
|
|
### 简介
|
|
|
|
|
|
|
|
|
|
#### 镜像
|
|
|
|
|
|
|
|
|
|
镜像已打包完成,`Dockerfile`已分别放置在`PCGBigHomeworkClient`、`PCGBigHomeworkServer`下。
|
|
|
|
|
|
|
|
|
|
镜像使用`腾讯云容器镜像服务`个人版托管,地址为:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# 服务器端
|
|
|
|
|
ccr.ccs.tencentyun.com/pcgbigwork/petserver:v3
|
|
|
|
|
# 客户端
|
2022-07-18 00:53:31 +08:00
|
|
|
|
ccr.ccs.tencentyun.com/pcgbigwork/petclient:v5
|
2022-07-17 16:22:37 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Minikube配置文件
|
|
|
|
|
|
|
|
|
|
Minikube配置文件放在了repo根目录下,文件名为`k8s.yml`
|
|
|
|
|
|
|
|
|
|
起了两个service,一个用于客户端负载均衡,一个用于固定服务端地址。
|
|
|
|
|
|
|
|
|
|
服务端的service类型为ClusterIP。客户端的service类型为NodePort,暴露在节点上的端口为`30080`。
|
|
|
|
|
|
|
|
|
|
> 由于要求使用minikube,minikube是模拟集群,我们的机器并不是节点,节点是minikube通过一个docker容器模拟出来的,所以并不能直接在机器上通过`localhost:30080`访问到服务(即便设置为了NodePort类型)。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
客户端默认起了3个,服务端默认只起1个。
|
|
|
|
|
|
2022-07-17 16:48:18 +08:00
|
|
|
|
在客户端的配置中,使用了configMap(L1 ~ L6)+环境变量(L43 ~ L48),规定了K8S中的gRPC服务器地址。
|
2022-07-17 16:22:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 使用方式
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
kubectl apply -f k8s.yml
|
|
|
|
|
```
|
|
|
|
|
|
2022-07-17 16:42:38 +08:00
|
|
|
|
### 验证工作正常
|
|
|
|
|
```bash
|
|
|
|
|
ubuntu@VM-0-10-ubuntu:~$ kubectl get pod
|
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
|
client-66554d7ffd-4znrb 1/1 Running 0 175m
|
|
|
|
|
client-66554d7ffd-59kcq 1/1 Running 0 175m
|
|
|
|
|
client-66554d7ffd-kgf5j 1/1 Running 0 176m
|
|
|
|
|
server-5fb78c447-mtcxq 1/1 Running 0 5h5m
|
|
|
|
|
|
2022-07-17 16:48:18 +08:00
|
|
|
|
ubuntu@VM-0-10-ubuntu:~$ kubectl get svc
|
|
|
|
|
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
|
|
|
|
client NodePort 10.106.194.160 <none> 80:30080/TCP 5h23m
|
|
|
|
|
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h31m
|
|
|
|
|
server ClusterIP 10.99.94.147 <none> 5000/TCP 5h23m
|
|
|
|
|
|
2022-07-17 16:42:38 +08:00
|
|
|
|
ubuntu@VM-0-10-ubuntu:~$ docker ps
|
|
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
|
afbc3c8c2c1c gcr.io/k8s-minikube/kicbase:v0.0.32 "/usr/local/bin/entr…" 5 hours ago Up 5 hours 127.0.0.1:49162->22/tcp, 127.0.0.1:49161->2376/tcp, 127.0.0.1:49160->5000/tcp, 127.0.0.1:49159->8443/tcp, 127.0.0.1:49158->32443/tcp minikube
|
|
|
|
|
# 这里印证了上方提到的“节点是minikube通过一个docker容器模拟出来的”这一观点。因而只有先进入该容器节点,才能通过指定的节点的30080端口访问客户端服务。
|
|
|
|
|
|
|
|
|
|
ubuntu@VM-0-10-ubuntu:~$ docker exec -it minikube bash
|
|
|
|
|
root@minikube:/# curl -d 'type=1&name=WangWang' -X POST localhost:30080/put
|
2022-07-17 22:44:47 +08:00
|
|
|
|
{"code":0,"message":"success","pet":{"pet_type":1,"pet_id":"1","name":"WangWang","created_at":{"year":2022,"month":7,"day":17,"hours":8,"minutes":32,"seconds":14,"nanos":566029499,"TimeOffset":null}}}
|
|
|
|
|
root@minikube:/# curl localhost:30080/get?id=1
|
|
|
|
|
{"code":0,"message":"success","pet":{"pet_type":1,"pet_id":"1","name":"WangWang","created_at":{"year":2022,"month":7,"day":17,"hours":8,"minutes":32,"seconds":14,"nanos":566029499,"TimeOffset":null}}}
|
|
|
|
|
root@minikube:/# curl -d 'id=1' -X POST localhost:30080/delete
|
2022-07-17 16:42:38 +08:00
|
|
|
|
{"code":0,"message":"success"}
|
|
|
|
|
|
|
|
|
|
# 各接口服务正常,与服务端之间的连接正常。
|
|
|
|
|
```
|
|
|
|
|
|