美文网首页
Go微服务架构实战 中篇:3. 扩缩容、自愈和故障转移、滚动更新

Go微服务架构实战 中篇:3. 扩缩容、自愈和故障转移、滚动更新

作者: stackfuture | 来源:发表于2022-01-06 12:28 被阅读0次

    Go微服务架构实战-【公粽号:堆栈future】

    Go微服务架构实战目录

    1. 微服务架构上篇

    1. grpc技术介绍

    2. grpc+protobuf+网关实战

    3. etcd技术介绍

    4. 基于etcd的服务发现与注册

    5. 基于etcd的分布式锁实战

    2. 微服务架构中篇

    1. k8s架构介绍

    2. 基于k8s的容器化部署

    基于k8s的Deployment工作负载

    主要是利用Deployment资源对象实现的,包括一下功能:

    1. 多副本集
    2. 扩缩容
    3. 自愈和故障转移
    4. 滚动更新
    5. 回退能力

    接下来详细了解下操作过程

    1. 多副本集

    deploy.yaml中只需修改一下replicas,让它的值变成我们想要的副本数量。

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">apiVersion: apps/v1 kind: Deployment metadata: name: k8sdemo-deploy labels: app: k8sdemo spec: replicas: 3 //修改这里 selector: matchLabels: app: k8sdemo template: metadata: labels: app: k8sdemo spec: containers: - name: k8sdemo image: k8s-grpc-demo:latest imagePullPolicy: Never ports: - containerPort: 50007 </pre>

    然后kubectl apply -f deploy.yaml应用一下就可以了,看下创建结果:

    image.gif

    从结果看出三个pod成功起来了。

    2. 扩缩容

    比较简单,一行命令搞定:

    kubectl scale --replicas=1 deployment/k8sdemo-deploy回车之后打印如下:

    image.gif

    然后看下pod数量kubectl get pod发现只有一个pod了,这里就不展示了,非常简单。

    3. 自愈和故障转移

    因为我只有一台机器,没办法验证,所以只列出步骤,下去大家自己可以尝试。

    • 停某一台机器
    • 删除某个pod

    大家可以去试试,看看是否停掉机器之后,pod会在别的机器上再起来,保持Deployment的副本集数量不变。或者在某台机器上删除某个pod之后,看看这个pod是否重新建立(归功于Deployment的能力)。

    tips:当你要删除pod的时候发现一直删除不掉,pod会自动起来,不要着急莫慌,你应该先删除Deployment,这样pod就会自动删除的。

    4. 滚动更新

    首先你基于你的代码做了部分更新,比如增加了日志等功能,然后在本地构建镜像,比如k8s-grpc-demo:v2,然后你用一行命令就可以搞定滚动更新:

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl set image deployment/k8sdemo-deploy k8s-grpc-demo=k8s-grpc-demo:v2 --record </pre>

    查看下更新状态

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl rollout status deployment/k8sdemo-deploy </pre>

    图片

    5. 回退能力

    1. 查看历史版本记录

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl rollout history deployment/k8sdemo-deploy </pre>

    图片
    1. 查看某个历史详情

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl rollout history deployment/k8sdemo-deploy --revision=2 </pre>

    图片
    1. 回滚(回到上次)

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl rollout undo deployment/k8sdemo-deploy </pre>

    1. 回滚(回到指定版本)

    <pre data-tool="mdnice编辑器" style="box-sizing: border-box !important; margin: 10px 0px; padding: 0px; outline: 0px; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; overflow: auto; display: block; color: rgb(33, 37, 41); max-width: 100%; overflow-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.8px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0.8px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">kubectl rollout undo deploymen/k8sdemo-deploy --to-revision=2 </pre>

    6. 小结

    其实k8s使用不是很复杂,你可以学完k8s之后搭建自己的服务,但是如果想深入定制k8s,需要有一定golang知识和容器化知识,所以想要进阶的可以在深入学习下源码理解下原理。

    下篇文章继续给大家讲解下k8s的service的服务注册和发现能力和ingress的入口网关能力,以及它们的网络架构知识。喜欢这篇文章的童鞋可以关注,转发和分享哈。

    github地址:https://github.com/guojiangli/k8s-grpc-demo

    【公粽号:堆栈future】

    相关文章

      网友评论

          本文标题:Go微服务架构实战 中篇:3. 扩缩容、自愈和故障转移、滚动更新

          本文链接:https://www.haomeiwen.com/subject/xsolcrtx.html