当前位置: 首页 > article >正文

游戏、网关等服务借助Docker容器化并使用Kubernetes部署、更新等

本文首发在这里
请先看完

  • 实现负责消息转发、推送的网关服务
  • 负责网络、定时、坐下、站起、重连等,支持多类游戏的无锁房间

Docker容器化

Build and Push
docker build -t panshiqu/game_server:latest -t panshiqu/game_server:1 -t panshiqu/game_server:1.0 --build-arg SERVER=game_server .
docker build -t panshiqu/gate_server:latest -t panshiqu/gate_server:1 -t panshiqu/gate_server:1.0 --build-arg SERVER=gate_server .

docker push panshiqu/game_server:latest
docker push panshiqu/game_server:1
docker push panshiqu/game_server:1.0
docker push panshiqu/gate_server:latest
docker push panshiqu/gate_server:1
docker push panshiqu/gate_server:1.0

# 记得调整Import,这样才能编出两个不同的版本
docker build -t panshiqu/game_server:client --target client-final .
docker build -t panshiqu/gate_server:client --target client-final .
docker push panshiqu/game_server:client
docker push panshiqu/gate_server:client
验证
# 创建网络使用别名发现服务
docker network create server

docker run --network server --network-alias dice --name game_server -p 60001:60001 --rm panshiqu/game_server:1.0
docker run --network server --name gate_server -p 60006:60006 -e JWT_KEY=ZGVmYXVsdF9rZXk= --rm panshiqu/gate_server:1.0

cd ~/go/src/github.com/panshiqu/server/game_server/game/dice/client
go run main.go

docker run --network server --rm -it panshiqu/game_server:client ./dice_client -ip dice

Kubernetes容器编排

部署
kubectl apply -f https://raw.githubusercontent.com/panshiqu/server/main/k8s/dice.yaml

kubectl create secret generic jwt --from-literal=key=ZGVmYXVsdF9rZXk=

kubectl apply -f https://raw.githubusercontent.com/panshiqu/server/main/k8s/gate.yaml

minikube service gate --url # Mac DockerDesktop minikube print the port
cd ~/go/src/github.com/panshiqu/server/game_server/game/dice/client
go run main.go -port 60006

kubectl run dice -it --rm --restart Never --image panshiqu/gate_server:client
./dice_client -ip $GATE_SERVICE_HOST
EFK (Elasticsearch + Fluentd + Kibana)

方案取自以下官方文档

  • 日志架构
  • Fluentd
  • 快速开始系列:其中Elasticsearch和Kibana
kubectl create -f https://download.elastic.co/downloads/eck/2.14.0/crds.yaml

kubectl apply -f https://download.elastic.co/downloads/eck/2.14.0/operator.yaml

cat <<EOF | kubectl apply -f -
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
  name: quickstart
spec:
  version: 8.15.0
  nodeSets:
  - name: default
    count: 1
    config:
      node.store.allow_mmap: false
EOF
{% raw %}
kubectl get secret quickstart-es-elastic-user -o go-template='{{.data.elastic | base64decode}}'
{% endraw %}
cat <<EOF | kubectl apply -f -
apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
  name: quickstart
spec:
  version: 8.15.0
  count: 1
  elasticsearchRef:
    name: quickstart
EOF

kubectl port-forward service/quickstart-kb-http 5601

# 复制修改自:https://github.com/fluent/fluentd-kubernetes-daemonset/blob/master/fluentd-daemonset-elasticsearch-rbac.yaml
curl -s -O https://raw.githubusercontent.com/panshiqu/server/main/k8s/fluentd-daemonset-elasticsearch-rbac.yaml
kubectl apply -f fluentd-daemonset-elasticsearch-rbac.yaml # 请修改密码 FLUENT_ELASTICSEARCH_PASSWORD

# Open https://localhost:5601
更新

游戏优雅停服细节说明

  • 请先查看资源配置中对preStop的注释
  • 首次收到SIGTERM信号,先标记停服后等待所有房间主动解散,没有房间则无需等待
  • 此时服务发现不再分配本服务但允许定向连接,可以进入旧房间但不允许创建新房间
  • 可对本服所有客户端定时持续广播房间将由系统解散,请主动解散后创建新房间(待实现)
  • 再次收到SIGTERM信号,触发解散现有房间
# 可选借助此命令将本地打包的镜像载入minikube
# minikube image load panshiqu/game_server:1.3

# 可选在修改镜像前后通过调整partition来执行金丝雀发布,用以先更新并验证单个服务,值=replicas-1
# kubectl patch statefulsets dice -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":1}}}}'

kubectl set image statefulsets dice game=panshiqu/game_server:1.3

kubectl rollout status statefulsets dice

# 验证测试不通过可以回滚
# kubectl rollout undo statefulsets dice

# kubectl patch statefulsets dice -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":0}}}}'

网关得益于设计成重启几乎没有代价,客户端只会感到可能因为网络不好而触发了重连

kubectl set image deployments gate gate=panshiqu/gate_server:1.3

部署AB服,客户端老转新服将没有明确的时间限制

# 默认启用A服
kubectl apply -f https://raw.githubusercontent.com/panshiqu/server/main/k8s/dice-ab.yaml

kubectl set image statefulsets dice-b game=panshiqu/game_server:1.3

# 扩容开B服
kubectl scale statefulsets dice-b --replicas=2

# 启用B服
kubectl patch service dice -p '{"spec":{"selector":{"group":"b"}}}'

# 缩容停A服
kubectl scale statefulsets dice-a --replicas=0

http://www.kler.cn/news/311373.html

相关文章:

  • MySQL 中的 EXPLAIN 命令:洞察查询性能的利器
  • MySQL 中的索引覆盖扫描:加速查询的秘密武器
  • 【Linux】Ubuntu 22.04 shell实现MySQL5.7 tar 一键安装
  • 独立站技能树之建站33项自检清单 1.0丨出海笔记
  • STM32 HAL freertos零基础(十一)中断管理
  • Linux技术04-IPVS
  • 游戏如何对抗定制挂
  • Linux线程基础
  • Java-测试-Mockito 入门篇
  • FTP、SFTP安装,整合Springboot教程
  • 基于剪切板的高速翻译工具
  • 【Qt | QAction】Qt 的 QAction 类介绍
  • 电脑键盘功能基础知识汇总
  • Leetcode面试经典150题-130.被围绕的区域
  • MySql-单表以及多表查询详解
  • paddle 分类网络
  • 【Linux】【Vim】Vim 基础
  • Doris相关记录
  • 【计算机基础题目】二叉树的前序中序后续遍历之间相互转换 详细例子
  • 我的demo保卫萝卜中的技术要点
  • O1-preview:智能预测与预取驱动的性能优化处理器设计OPEN AI
  • Semaphore UI --Ansible webui
  • 心觉:成功学就像一把刀,有什么作用关键在于使用者(二)
  • 进入C++
  • Spring WebFlux实践与源码解析
  • leetcode41. 缺失的第一个正数,原地哈希表
  • Vue2篇
  • 无线感知会议系列【2】【智能无感感知 特征,算法,数据集】
  • 【AI大模型】LLM主流开源大模型介绍
  • 【neo4j】neo4j和Cypher 查询语言相关知识点