Go微服務架構實戰(zhàn)-【公粽號:堆棧future】
1. 微服務架構上篇
1. grpc技術介紹
2. grpc+protobuf+網(wǎng)關實戰(zhàn)
3. etcd技術介紹
4. 基于etcd的服務發(fā)現(xiàn)與注冊
5. 基于etcd的分布式鎖實戰(zhàn)
2. 微服務架構中篇
基于k8s的Deployment工作負載
主要是利用Deployment資源對象實現(xiàn)的,包括一下功能:
- 多副本集
- 擴縮容
- 自愈和故障轉移
- 滾動更新
- 回退能力
接下來詳細了解下操作過程
1. 多副本集
deploy.yaml中只需修改一下replicas,讓它的值變成我們想要的副本數(shù)量。
<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應用一下就可以了,看下創(chuàng)建結果:

從結果看出三個pod成功起來了。
2. 擴縮容
比較簡單,一行命令搞定:
kubectl scale --replicas=1 deployment/k8sdemo-deploy回車之后打印如下:

然后看下pod數(shù)量kubectl get pod發(fā)現(xiàn)只有一個pod了,這里就不展示了,非常簡單。
3. 自愈和故障轉移
因為我只有一臺機器,沒辦法驗證,所以只列出步驟,下去大家自己可以嘗試。
- 停某一臺機器
- 刪除某個pod
大家可以去試試,看看是否停掉機器之后,pod會在別的機器上再起來,保持Deployment的副本集數(shù)量不變?;蛘咴谀撑_機器上刪除某個pod之后,看看這個pod是否重新建立(歸功于Deployment的能力)。
tips:當你要刪除pod的時候發(fā)現(xiàn)一直刪除不掉,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>
查看下更新狀態(tài)
<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. 回退能力
- 查看歷史版本記錄
<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>
- 查看某個歷史詳情
<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>
- 回滾(回到上次)
<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>
- 回滾(回到指定版本)
<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知識和容器化知識,所以想要進階的可以在深入學習下源碼理解下原理。
下篇文章繼續(xù)給大家講解下k8s的service的服務注冊和發(fā)現(xiàn)能力和ingress的入口網(wǎng)關能力,以及它們的網(wǎng)絡架構知識。喜歡這篇文章的童鞋可以關注,轉發(fā)和分享哈。
github地址:https://github.com/guojiangli/k8s-grpc-demo
【公粽號:堆棧future】