第151天學習打卡(Kubernetes 集群YAML文件詳解 Pod Controller)


kubectl子命令使用分類

1.(基礎命令)

image-20210608152333066

 

(2)部署和集群管理命令

image-20210608152520301

(3)故障調試命令

image-20210608152630764

(4)其他命令

image-20210608152722919

 

 [root@master ~]# kubectl get cs #查看當前狀態
 Warning: v1 ComponentStatus is deprecated in v1.19+
 NAME                 STATUS     MESSAGE                                                                                       ERROR
 scheduler           Unhealthy   Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused  
 controller-manager   Unhealthy   Get "http://127.0.0.1:10252/healthz": dial tcp 127.0.0.1:10252: connect: connection refused  
 etcd-0               Healthy     {"health":"true"}                                                         #狀態檢查發現是unhealth,解決辦法:注釋掉下面兩個文件夾的--port=0                    
 [root@master ~]# vim /etc/kubernetes/manifests/kube-controller-manager.yaml
 [root@master ~]# vim /etc/kubernetes/manifests/kube-scheduler.yaml
 #再次檢查,變成health了。
 [root@master ~]# kubectl get cs
 Warning: v1 ComponentStatus is deprecated in v1.19+
 NAME                 STATUS   MESSAGE             ERROR
 controller-manager   Healthy   ok                  
 scheduler           Healthy   ok                  
 etcd-0               Healthy   {"health":"true"}  
 [root@master ~]#
 

 

 

image-20210608154458367

 

image-20210608154648933

 

Kubernetes 集群YAML文件詳解

1.YAML文件概述

k8s集群中對資源管理和資源對象編排部署都可以通過聲明樣式(YAML)文件來解決,也就是說可以把需要對資源對象操作編輯到YAML格式文件中,我們把這種文件叫做資源清單文件,通過kubectl 命令直接使用資源清單文件就可以實現對大量的資源對象進行編排部署了。

2.YAML文件書寫格式

(1)YAML介紹

YAML:仍是一種標記語言。為了強調這種語言以數據作為中心,而不是以標記語言為重點。

YAML是一個可讀性高,用來表達數據序列的格式。

(2)YAML基本語法

使用空格作為縮進

縮進的空格數目不重要,只要相同層級的元素左側對齊即可。

語法格式:

  • 通過縮進表示層級關系

  • 不能使用Tab進行縮進,只能使用空格

  • 一般開頭縮進兩個空格

  • 字符后縮進一個空格,比如冒號,逗號等后面縮進一個空格。

  • 使用---表示新的yaml文件開始

  • 使用#代表注釋

image-20210608162736831

yaml文件組成部分

(1)控制器定義

image-20210608162901154

image-20210608162949911

如何快速編寫yaml文件

第一種使用kubectl create命令生成yaml文件

 [root@master ~]# kubectl create deployment web --image=nginx -o yaml --dry-run
 W0608 16:42:14.284905   10172 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
 apiVersion: apps/v1
 kind: Deployment
 metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
 spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
       - image: nginx
        name: nginx
        resources: {}
 status: {}
 [root@master ~]# kubectl create deployment web --image=nginx -o yaml --dry-run > my1.yaml
 W0608 16:42:56.667661   10462 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
 [root@master ~]# ls
 10-flannel.conflist admin.conf   deploy.yaml.1           kube-flannel.yml recommended.yaml
 10-kubeadm.conf     deploy.yaml ingress-nginx-rule.yaml my1.yaml
 [root@master ~]# cat my1.yaml
 

 

第二種方式 當資源已經部署了,從部署資源中,拿出來已經生成好的yaml文件對它做修改。使用kubectl get 命令導出yaml文件。

 [root@master ~]# kubectl get deploy 
 NAME     READY   UP-TO-DATE   AVAILABLE   AGE
 nginx    1/1     1            1           45h
 tomcat   1/1     1            1           44h
 [root@master ~]# kubectl get deployment nginx -o=yaml --export > my2.yaml
 Error: unknown flag: --export
 See 'kubectl get --help' for usage.
 #原因是 --export在所使用的版本中已經被去除了,將--export刪除即可,改成:
 [root@master ~]# kubectl get deployment nginx -o=yaml > my2.yaml
 [root@master ~]# ls
 10-flannel.conflist admin.conf   deploy.yaml.1           kube-flannel.yml my2.yaml
 10-kubeadm.conf     deploy.yaml ingress-nginx-rule.yaml my1.yaml         recommended.yaml
 [root@master ~]#
 

kubernetes核心技術-Pod

1.Pod概述

Pod 是 k8s 系統中可以創建和管理的最小單元,是資源對象模型中由用戶創建或部署的最小資源對象模型,也是在 k8s 上運行容器化應用的資源對象,其他的資源對象都是用來支撐或者擴展 Pod 對象功能的,比如控制器對象是用來管控 Pod 對象的,Service 或者 Ingress 資源對象是用來暴露 Pod 引用對象的,PersistentVolume 資源對象是用來為 Pod提供存儲等等,k8s 不會直接處理容器,而是 Pod,Pod 是由一個或多個 container 組成Pod 是 Kubernetes 的最重要概念,每一個 Pod 都有一個特殊的被稱為”根容器“的 Pause 容器。Pause 容器對應的鏡 像屬於 Kubernetes 平台的一部分,除了 Pause 容器,每個 Pod還包含一個或多個緊密相關的用戶業務容器

(1)最小部署的單元

(2)包含多個容器(一組容器的集合)

(3)一個pod中容器共享網絡命令空間

(4)pod是短暫的

image-20210608170827654

 

2.Pod存在的意義

(1)創建容器使用docker(單進程),一個docker對應一個容器,一個容器有一個進程,一個容器運行一個應用程序。

(2)Pod是多進程設計,運行多個應用程序

  • 一個Pod有多個容器,一個容器里面運行一個應用程序

(3)Pod存在為了親密性應用

  • 兩個應用之間進行交互

  • 網絡之間調用

  • 兩個應用需要頻繁調用

 

3.Pod實現機制

(1)共享網絡:通過Pause容器,把其他業務容器加入到pause容器里面,讓所有業務容器在同一個名稱空間中,可以實現網絡共享。

 

image-20210608183639759

(2)共享存儲:引入數據卷概念Volumn,把其他業務容器加入到Pause容器里面,讓所有容器在同一個名稱空間中,可以實現網絡共享。

image-20210608184012605

image-20210608184424958

image-20210608185713870

4.Pod鏡像拉取策略

 

image-20210608190442840

 

5.Pod資源限制示例

image-20210608190718382

 

image-20210608191005528

 

6.Pod重啟機制

image-20210608191247801

7.Pod健康檢查

image-20210608191807886

 

 [root@master ~]# touch /tmp/a
 [root@master ~]# cat /tmp/a
 [root@master ~]# echo $?
 0
 [root@master ~]# rm /tmp/a
 rm: remove regular empty file ‘/tmp/a’? y
 [root@master ~]# echo $?     #查看狀態碼
 0
 [root@master ~]# cat /tmp/a
 cat: /tmp/a: No such file or directory
 [root@master ~]# echo $?
 1
 

 

image-20210608193307417

8.Pod調度

影響調用的屬性

  • Pod資源限制對Pod調用產生影響

image-20210608193559987

根據request找到足夠node節點進行調度

  • 節點選擇器標簽影響Pod調度

首先對節點創建標簽

 #例如:
 kubectl label node node1 env_role=prod
 
 
 
 [root@master ~]# kubectl label node node01 env_role=dev
 node/node01 labeled
 [root@master ~]# kubectl get nodes node01 --show-labels
 NAME     STATUS   ROLES   AGE     VERSION   LABELS
 node01   Ready   <none>   2d22h   v1.20.7   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env_role=dev,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
 

 

image-20210608193753067

image-20210608194017208

9.節點親和性

image-20210608195010100

image-20210608195308053

 

支持常用操作符:

 In  NotIn   Exists Gt Lt DoesNotExists
 

 

10.影響Pod調度

污點和污點容忍

(1)基本介紹:nodeSelector和nodeAffinity: Pod調度到某些節點上,Pod屬性,調度時候實現

Taint污點:節點不做普通分配調度,是節點屬性

(2)場景

  • 專用節點

  • 配置特定硬件節點

  • 基於Taint驅逐

(3)具體演示

 #查看節點污點情況
 [root@master ~]# kubectl describe node master | grep Taint
 Taints:             node-role.kubernetes.io/master:NoSchedule
 [root@master ~]#
 

污點值有三個

  • NoSchedule:一定不被調度

  • PreferNoSchdule:盡量不被調度

  • NoExecute:不會被調度,並且還會驅逐Node已有Pod

(4)為節點添加污點

 kubectl taint node [node] key=value:污點三個值
 [root@master ~]# kubectl describe node master | grep Taint
 Taints:             node-role.kubernetes.io/master:NoSchedule
 [root@master ~]# kubectl describe node node01 | grep Taint
 Taints:             <none>
 [root@master ~]# kubectl describe node node02 | grep Taint
 Taints:             <none>
 [root@master ~]# kubectl describe node node03 | grep Taint
 Taints:             <none>
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d
 [root@master ~]# kubectl create deployment web --image=nginx
 deployment.apps/web created
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS             RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running             3         2d
 tomcat-7d987c7694-8sjkd   1/1     Running             2         2d
 web-96d5df5c8-6hq2l       0/1     ContainerCreating   0         14s
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d
 web-96d5df5c8-6hq2l       1/1     Running   0         26s
 #查看被分配到的節點
 [root@master ~]# kubectl get pods -o wide #查看被分配到的節點
 NAME                     READY   STATUS   RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d    10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d    10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-6hq2l       1/1     Running   0         50s   10.244.1.11   node01   <none>           <none>副本
 [root@master ~]# kubectl scale deployment web --replicas=5 #創建
 deployment.apps/web scaled
 [root@master ~]# kubectl get pods -o wide
 NAME                     READY   STATUS             RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running             3         2d      10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running             2         2d      10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-6hq2l       1/1     Running             0         4m19s   10.244.1.11   node01   <none>           <none>
 web-96d5df5c8-bktth       0/1     ContainerCreating   0         13s     <none>       node01   <none>           <none>
 web-96d5df5c8-cshfg       0/1     ContainerCreating   0         13s     <none>       node02   <none>           <none>
 web-96d5df5c8-nbtdr       0/1     ContainerCreating   0         13s     <none>       node01   <none>           <none>
 web-96d5df5c8-nlblr       0/1     ContainerCreating   0         13s     <none>       node02   <none>           <none>
 
 [root@master ~]# kubectl get pods -o wide
 NAME                     READY   STATUS   RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d      10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d      10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-6hq2l       1/1     Running   0         6m20s   10.244.1.11   node01   <none>           <none>
 web-96d5df5c8-bktth       1/1     Running   0         2m14s   10.244.1.13   node01   <none>           <none>
 web-96d5df5c8-cshfg       1/1     Running   0         2m14s   10.244.2.11   node02   <none>           <none>
 web-96d5df5c8-nbtdr       1/1     Running   0         2m14s   10.244.1.12   node01   <none>           <none>
 web-96d5df5c8-nlblr       1/1     Running   0         2m14s   10.244.2.12   node02   <none>           <none>
 
 [root@master ~]# kubectl delete deployment web #刪除web
 deployment.apps "web" deleted
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d
 
 # 創建污點 env_role是創建的名字
 [root@master ~]# kubectl taint node node01 env_role=yes:NoSchedule #為node01加上污點
 node/node01 tainted
 [root@master ~]# kubectl create deployment web --image=nginx
 deployment.apps/web created
 [root@master ~]# kubectl get pods -o wide
 NAME                     READY   STATUS   RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d1h   10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d     10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-4sj5m       1/1     Running   0         25s    10.244.2.13   node02   <none>           <none>
 [root@master ~]# kubectl scale deployment web --replicas=5
 deployment.apps/web scaled
 [root@master ~]# kubectl get pods -o wide #因為node01設置了污點所以創建的五個web調度不到
 NAME                     READY   STATUS             RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running             3         2d1h   10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running             2         2d     10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-4sj5m       1/1     Running             0         94s    10.244.2.13   node02   <none>           <none>
 web-96d5df5c8-mdqrb       0/1     ContainerCreating   0         23s   <none>       node02   <none>           <none>
 web-96d5df5c8-nxppp       0/1     ContainerCreating   0         23s   <none>       node03   <none>           <none>
 web-96d5df5c8-tk2cd       0/1     ContainerCreating   0         23s   <none>       node02   <none>           <none>
 web-96d5df5c8-wlnn7       0/1     ContainerCreating   0         23s   <none>       node02   <none>           <none>
 [root@master ~]# kubectl get pods -o wide
 NAME                     READY   STATUS   RESTARTS   AGE     IP           NODE     NOMINATED NODE   READINESS GATES
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d1h    10.244.2.6   node02   <none>           <none>
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d      10.244.1.6   node01   <none>           <none>
 web-96d5df5c8-4sj5m       1/1     Running   0         4m3s    10.244.2.13   node02   <none>           <none>
 web-96d5df5c8-mdqrb       1/1     Running   0         2m52s   10.244.2.15   node02   <none>           <none>
 web-96d5df5c8-nxppp       1/1     Running   0         2m52s   10.244.3.11   node03   <none>           <none>
 web-96d5df5c8-tk2cd       1/1     Running   0         2m52s   10.244.2.16   node02   <none>           <none>
 web-96d5df5c8-wlnn7       1/1     Running   0         2m52s   10.244.2.14   node02   <none>           <none>
 
 #刪除污點 env_role是創建的名字
 
 [root@master ~]# kubectl describe node node01 | grep Taint
 Taints:             env_role=yes:NoSchedule
 [root@master ~]# kubectl taint node node01 env_role:NoSchedule- #刪除污點
 node/node01 untainted
 [root@master ~]# kubectl describe node node01 | grep Taint
 Taints:             <none>
 

11.污點容忍

 

image-20210608204603383

Controller

1.什么是Controller

  • 確保預期的pod副本數量

  • 無狀態應用部署

  • 有狀態應用部署

  • 在集群上管理和運行容器的對象

確保所有的node運行同一個pod

一次性任務和定時任務

2.Pod和Controller關系

  • Pod是通過Controller實現應用的運維,比如伸縮,滾動升級等

  • Pod和Controller之間通過label標簽建立關系 selector

image-20210608205708725

3.deployment應用場景

  • 部署無狀態應用

  • 管理Pod和ReplicaSet

  • 部署,滾動升級等功能

應用場景:web服務,微服務

4.使用deployment部署應用(yaml)

 第一步#生成yaml文件
 [root@master ~]# kubectl create deployment web --image=nginx --dry-run -o yaml
 W0608 21:03:20.882677   24381 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
 apiVersion: apps/v1
 kind: Deployment
 metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
 spec:
  replicas: 1
  selector: #控制器里面的selector標簽
    matchLabels:
      app: web
  strategy: {}
  template: #template模板里面是pod
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
       - image: nginx
        name: nginx
        resources: {}
 status: {}
 第二步#導出yaml文件
 [root@master ~]# kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml
 [root@master ~]# ls
 10-flannel.conflist admin.conf   deploy.yaml.1           kube-flannel.yml my2.yaml         web.yaml
 10-kubeadm.conf     deploy.yaml ingress-nginx-rule.yaml my1.yaml         recommended.yaml
 #刪除yaml文件
 [root@master ~]# rm my1.yaml
 rm: remove regular file ‘my1.yaml’? y
 [root@master ~]# rm my2.yaml
 rm: remove regular file ‘my2.yaml’? y
 [root@master ~]# cat web.yaml
 apiVersion: apps/v1
 kind: Deployment
 metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
 spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
       - image: nginx
        name: nginx
        resources: {}
 status: {}
 [root@master ~]#
 

 

image-20210608211132489

 第三步#使用yaml文件部署應用
 [root@master ~]# kubectl apply -f web.yaml
 Warning: resource deployments/web is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
 deployment.apps/web configured
 [root@master ~]# ls
 10-flannel.conflist admin.conf   deploy.yaml.1           kube-flannel.yml web.yaml
 10-kubeadm.conf     deploy.yaml ingress-nginx-rule.yaml recommended.yaml
 [root@master ~]# kubectl get pods #查看狀態
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d1h
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d1h
 web-96d5df5c8-nxppp       1/1     Running   0         37m
 [root@master ~]#
 
 第四步#對外發布(暴露對外端口號)
 [root@master ~]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web1 -o yaml > web1.yaml
 [root@master ~]# ls
 10-flannel.conflist admin.conf   deploy.yaml.1           kube-flannel.yml web1.yaml
 10-kubeadm.conf     deploy.yaml ingress-nginx-rule.yaml recommended.yaml web.yaml
 [root@master ~]# cat web1.yaml #查看這個文件
 [root@master ~]# kubectl apply -f web1.yaml #應用這個文件
 Warning: resource services/web1 is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
 service/web1 configured
 [root@master ~]# kubectl get pods,svc #查看對外端口號
 NAME                         READY   STATUS   RESTARTS   AGE
 pod/nginx-6799fc88d8-kqfmm    1/1     Running   3         2d1h
 pod/tomcat-7d987c7694-8sjkd   1/1     Running   2         2d1h
 pod/web-96d5df5c8-nxppp       1/1     Running   0         54m
 
 NAME                 TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)         AGE
 service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP         3d
 service/nginx       NodePort    10.98.160.67   <none>        80:32169/TCP     2d1h
 service/tomcat       NodePort    10.105.92.64   <none>        8080:30513/TCP   2d1h
 service/web1         NodePort    10.111.154.60   <none>        80:32594/TCP     2m15s
 [root@master ~]#
 
 

注意:master或者node的阿里雲Ip加上這個端口號都可以訪問到nginx界面,32594這個端口號要在阿里雲安全組進行配置

image-20210608213344903

5.應用升級回滾和彈性伸縮

 [root@master ~]# vim web.yaml

image-20210608214929910

image-20210608215642806

 [root@master ~]# cat web.yaml
 apiVersion: apps/v1
 kind: Deployment
 metadata:
  creationTimestamp: null
  labels:
    app: web
  name: web
 spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web
    spec:
      containers:
       - image: nginx:1.14
        name: nginx
        resources: {}
 status: {}
 [root@master ~]# kubectl apply -f web.yaml
 deployment.apps/web configured
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d1h
 web-88c6cbf44-2mz4b       1/1     Running   0         85s
 web-88c6cbf44-8gnkb       1/1     Running   0         5m22s
 
 #把nginx1.14版本升級到1.15版本
 [root@master ~]# kubectl set image deployment web nginx=nginx:1.15
 deployment.apps/web image updated
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS       RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running       3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running       2         2d1h
 web-586db47859-px6r4      1/1     Running       0         62s#這個1.14版本的沒有停住,繼續啟用
 web-586db47859-zgw49      1/1     Running       0         36s#這個1.14版本的沒有停住。繼續啟用
 web-88c6cbf44-2mz4b       0/1     Terminating   0         4m27s# 這個過程在下載1.15的鏡像
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d1h
 web-586db47859-px6r4      1/1     Running   0         101s#1.15替換掉了1.14
 web-586db47859-zgw49      1/1     Running   0         75s#1.15替換掉了1.14
 
 #查看升級的狀態
 [root@master ~]# kubectl rollout status deployment web
 deployment "web" successfully rolled out

image-20210608215712265

 

 #查看升級版本
 [root@master ~]# kubectl rollout history deployment web
 deployment.apps/web
 REVISION CHANGE-CAUSE #這里有3個版本
 1         <none>
 2         <none>
 3         <none>
 
 #回滾到上一個版本
 [root@master ~]# kubectl rollout undo deployment web
 deployment.apps/web rolled back
 [root@master ~]# kubectl rollout status deployment web
 Waiting for deployment "web" rollout to finish: 1 out of 2 new replicas have been updated...
 Waiting for deployment "web" rollout to finish: 1 out of 2 new replicas have been updated...
 Waiting for deployment "web" rollout to finish: 1 out of 2 new replicas have been updated...
 Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
 Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
 deployment "web" successfully rolled out
 [root@master ~]# kubectl rollout undo --help
 Rollback to a previous rollout.
 
 Examples:
   # Rollback to the previous deployment
  kubectl rollout undo deployment/abc
   
   # Rollback to daemonset revision 3
  kubectl rollout undo daemonset/abc --to-revision=3
   
   # Rollback to the previous deployment with dry-run
  kubectl rollout undo --dry-run=server deployment/abc
 
 Options:
       --allow-missing-template-keys=true: If true, ignore any errors in templates when a field or map key is missing in
 the template. Only applies to golang and jsonpath output formats.
       --dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be
 sent, without sending it. If server strategy, submit server-side request without persisting the resource.
   -f, --filename=[]: Filename, directory, or URL to files identifying the resource to get from a server.
   -k, --kustomize='': Process the kustomization directory. This flag can't be used together with -f or -R.
   -o, --output='': Output format. One of:
 json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
   -R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage
 related manifests organized within the same directory.
       --template='': Template string or path to template file to use when -o=go-template, -o=go-template-file. The
 template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
       --to-revision=0: The revision to rollback to. Default to 0 (last revision).
 
 Usage:
  kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags] [options]
 
 Use "kubectl options" for a list of global command-line options (applies to all commands).
 
 #回滾到指定的版本
 [root@master ~]# kubectl rollout undo deployment web --to-revision=3
 deployment.apps/web rolled back
 [root@master ~]# kubectl rollout status deployment web
 Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
 Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
 deployment "web" successfully rolled out
 

彈性伸縮

 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d2h
 web-586db47859-gr8wj      1/1     Running   0         3m28s
 web-586db47859-hnp7f      1/1     Running   0         3m44s
 [root@master ~]# kubectl scale deployment web --replicas=10
 deployment.apps/web scaled
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS             RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running             3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running             2         2d2h
 web-586db47859-22g6n      0/1     ContainerCreating   0         19s
 web-586db47859-5j8x7      0/1     ContainerCreating   0         19s
 web-586db47859-9vn5m      0/1     ContainerCreating   0         19s
 web-586db47859-gr8wj      1/1     Running             0         4m36s
 web-586db47859-gsgn8      0/1     ContainerCreating   0         19s
 web-586db47859-hnp7f      1/1     Running             0         4m52s
 web-586db47859-n4wbm      0/1     ContainerCreating   0         19s
 web-586db47859-s228l      0/1     ContainerCreating   0         19s
 web-586db47859-tmp54      0/1     ContainerCreating   0         19s
 web-586db47859-wkj8s      0/1     ContainerCreating   0         19s
 [root@master ~]# kubectl get pods
 NAME                     READY   STATUS   RESTARTS   AGE
 nginx-6799fc88d8-kqfmm    1/1     Running   3         2d2h
 tomcat-7d987c7694-8sjkd   1/1     Running   2         2d2h
 web-586db47859-22g6n      1/1     Running   0         93s
 web-586db47859-5j8x7      1/1     Running   0         93s
 web-586db47859-9vn5m      1/1     Running   0         93s
 web-586db47859-gr8wj      1/1     Running   0         5m50s
 web-586db47859-gsgn8      1/1     Running   0         93s
 web-586db47859-hnp7f      1/1     Running   0         6m6s
 web-586db47859-n4wbm      1/1     Running   0         93s
 web-586db47859-s228l      1/1     Running   0         93s
 web-586db47859-tmp54      1/1     Running   0         93s
 web-586db47859-wkj8s      1/1     Running   0         93s
 

B站學習網址:k8s教程由淺入深-尚硅谷嗶哩嗶哩bilibili


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM