k8s-实践(十)

试着相信梦吧!或许,当你为之开始努力,便无所谓不可能了。 ——祯旎

k8s 应用

创建deployment的时候:

# 对于版本回滚时,我们对于 rollout 指定每次版本回滚,我们对于镜像的指定可以一目了然。
$ kubectl apply -f zeus-piglet-deployment.yml --record

获取更新记录:

默认的部署方式不会记录CHANGE-CAUSE,需要在部署的时候添加 --record 选项~~

spec.revisionHistoryLimit被用来设置保留的历史记录数量,默认为2,设置为0时将不能回滚~~

$ kubectl rollout history deployment zeus-piglet
deployments "zeus-piglet"
REVISION  CHANGE-CAUSE
1         kubectl.exe apply --filename=zeus-piglet-deployment.yml --record=true
2         kubectl.exe set image deploy/zeus-piglet zeus-piglet=registry.d.peiqi.com/piglet-test/zeus-piglet:v0.0.3-20180725-183117-ba900fd
3         kubectl.exe set image deploy/zeus-piglet zeus-piglet=registry.d.peiqi.com/piglet-test/zeus-piglet:v0.0.3-20180725-190513-ba900fd

k8s 更新

滚动更新的三种方式:

$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

$ kubectl edit 命令修改Deployment的配置:    
.spec.template.spec.containers[0].image从nginx:1.8到nginx:1.9.1。

## 手动修改deployment Pod定义部分,然后执行:
$  kubectl apply -f  deployment.yaml

滚动更新状态查看

$ kubectl rollout status deployment/zeus-piglet

滚动更新操作命令

 kubectl rollout  子命令:
 $ kubectl rollout  [ history | pause | resume | status | undo ]
   ##分别为历史更新记录,暂停、恢复、回滚,下面基本上都是说这几个命令是如何使用的。

镜像的更新

$ kubectl set image deploy/zeus-piglet zeus-piglet=registry.cn-beijing.aliyuncs.com/peiqi/zeus-piglet:v0.0.3-$time-$gitversion
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: zeus-piglet
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate

k8s 镜像回滚

回滚上一个版本

$ kubectl rollout undo deployment nginx-deployment

回滚指定版本

$ kubectl rollout undo deployment nginx-deployment --to-revision=2

查看特定版本信息

$ kubectl rollout history deployment zeus-piglet --revision=53
deployments "zeus-piglet" with revision #53
Pod Template:
  Labels:       pod-template-hash=2530399188
        run=zeus-piglet
  Annotations:  kubernetes.io/change-cause=kubectl.exe apply --filename=zeus-piglet-deploy.yml --record=true
  Containers:
   zeus-piglet:
    Image:      registry.cn-beijing.aliyuncs.com/peiqi/zeus-piglet:v0.0.3-608331d
    Port:       28080/TCP
    Limits:
      cpu:      400m
      memory:   540Mi
    Requests:
      cpu:      400m
      memory:   540Mi
    Readiness:  tcp-socket :28080 delay=40s timeout=1s period=10s #success=1 #failure=3
    Environment:        
    Mounts:     
  Volumes:      

回滚某个版本,不使用版本号,默认是上个版本

$ kubectl rollout undo deployment zeus-piglet
 deployment "zeus-piglet" rolled back

回滚后查看该deploy的事件信息:

$ kubectl describe deployment zeus-piglet
Name:                   zeus-piglet
Namespace:              loan
CreationTimestamp:      Thu, 07 Jun 2018 18:04:12 +0800
Labels:                 run=zeus-piglet
Annotations:            deployment.kubernetes.io/revision=53
                        kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{"deployment.kubernetes.io/revision":"53","kubernetes.io/change-cause"...
                        kubernetes.io/change-cause=kubectl.exe apply --filename=zeus-piglet-deploy.yml --record=true
Selector:               run=zeus-piglet
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  run=zeus-piglet
  Containers:
   zeus-piglet:
    Image:  registry.cn-beijing.aliyuncs.com/peiqi/zeus-piglet:v0.0.3-608331d
    Port:   28080/TCP
    Limits:
      cpu:     400m
      memory:  540Mi
    Requests:
      cpu:        400m
      memory:     540Mi
    Readiness:    tcp-socket :28080 delay=40s timeout=1s period=10s #success=1 #failure=3
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  
NewReplicaSet:   zeus-piglet-69747ff5dd (3/3 replicas created)
Events:          

使用 --to-version 参数指定回滚到的部署版本号:

$ kubectl rollout undo deployment zeus-piglet --to-revision=1

暂停指定版本的更新

# 使用命令kubectl rollout pause  命令先暂停Deployment的更新操作:
$  kubectl rollout pause deployment zeus-piglet

在此期间我们可以任意更改配置中的信息,对于配置的修改,不会引起deployment的更新操作。

修改完后,最后恢复Deployment部署操作

$ kubectl rollout resume deployment zeus-piglet 
   deployment "zeus-piglet" resumed

k8s 伸缩

自动伸缩需要部署Heapstermetrics-server

部署自动扩容

$ kubectl scale deployment nginx-deployment --replicas=10

自动扩容,资源的固定大小

        resources:
          requests:
            memory: "64Mi"
            cpu: "25m"
          limits:
            memory: "128Mi"
            cpu: "50m"

自动扩容

$ kubectl autoscale deployment nginx-deployment --min=1 --max=10 --cpu-percent=20

文章作者: Asxing
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Asxing !
评论
  目录