Docker & Kubenetes 系列四:叢集,擴容,升級,回滾
阿新 • • 發佈:2020-05-20
> 本篇將會講解應用部署到Kubenetes叢集,叢集副本集檢視,叢集自愈能力演示,叢集擴容,滾動升級,以及回滾。
本篇是Docker&Kubenetes系列的第四篇,在前面的篇幅中,我們向Kubenetes中部署了單個應用例項。如果單個節點故障了,那麼服務就不可用了,這在實際環境中是不能接受的。在實際的正式環境中,我們不僅要避免單點,還要根據負載變化動態的調整節點數量。為了實現這個目的,我們可以藉助於Kubenetes的Deployment,Deployment可以建立指定數量的Pod,並有自愈能力,還能完成升級更新及回滾操作。
## 向Kubenetes中部署一個3節點的叢集
和之前介紹的一樣部署叢集也由過yml配置檔案開始的,本例我們依然使用前面篇幅中建立的docker映象來部署叢集。Deployment配置檔案內容如下,這個配置檔案是從Kubenetes官網複製的,手寫yml檔案太容易出錯了,yml格式挺煩的。
```
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-first-demo-dep
labels:
app: my-first-demo
spec:
replicas: 3
selector:
matchLabels:
app: my-first-demo
template:
metadata:
labels:
app: my-first-demo
spec:
containers:
- name: my-first-demo
image: hellodm/my-first-demo:v1.0
ports:
- containerPort: 80
```
配置檔案中,`replicas=3`表示要啟動3個副本,使用的映象是`hellodm/my-first-demo:v1.0`。配置完了之後,我們來啟動叢集,命令如下:
```
$ kubectl create -f dep.yml
deployment.apps/my-first-demo-dep created
```
我們來看檢視副本集情況:
```
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
my-first-demo-dep-5647fd55f 3 3 3 3m15s
```
在檢視一下pod:
```
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-first-demo-dep-5647fd55f-4wzxs 1/1 Running 0 22s
my-first-demo-dep-5647fd55f-c9lwx 1/1 Running 0 22s
my-first-demo-dep-5647fd55f-nnwt6 1/1 Running 0 22s
```
暴露叢集,使得我們可以在訪問叢集內部的應用。
```
$ kubectl apply -f svc.yml
service/my-first-demo-svc created
```
訪問:
```
$ curl 'http://localhost:30000/'
Hello world!