文档章节

Kubernetes的deployment使用

openthings
 openthings
发布于 2018/03/19 16:01
字数 2450
阅读 817
收藏 0

Deployment管理Pods和ReplicaSets,提供声明式更新。和老的ReplicationController(命令式管理)对应,发展趋势是取代老的,所以后面也不会起文章单独讨论ReplicationController了。

但由Deployment-controller管理的Pods和ReplicaSets最好自始至终都由Deployment-controller管理,最好不要手动去管理,以免发生冲突。

创建Deployment

如下一个Deployment的配置(nginx-deployment.yaml),创建一个ReplicaSet包含3个nginx Pods

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
  • apiVersion这里为apps/v1,如果是1.9之前的版本为extensions/v1beta1
  • replicas:3 起3个replicated Pods
  • selector指明哪个pod被管理,这里我们指定了label(app:nginx)
  • template: spec 指明了运行一个容器nginx(以nginx:1.7.9为镜像)
  • 开放80端口给container,以使container之间能发送和接收流量

注意这里定义name 或 label 时不要和其他的重复,k8s不会检查这个,需要人工自己确认

要创建此部署,执行下面的命令(在这之前我们提前下好nginx相关的镜像,docker pull nginx:1.7.9)

[root@master yaml]# kubectl create -f nginx-deployment.yaml --record
deployment "nginx-deployment" created
  • --record会记录操作历史,以便于后面回滚操作

查看deployments

[root@master yaml]# kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         0         0            0           1s
  • NAME:在集群中的部署名称
  • DESIRED:显示配置里定义的副本数量,这是应该达到的副本数量
  • CURRENT:当前正在运行的副本数量
  • UP-TO-DATE:更新到当前所需状态的副本数量
  • AVAILABLE:可供使用的副本数量
  • AEG:显示app存活的时间

通过下面语句可查追踪部署情况

[root@master ~]# kubectl rollout status deployment/nginx-deployment
deployment "nginx-deployment" successfully rolled out
# 这是部署完成的状态

# 未完成的会显示当前部署哪一步了
[root@master ~]# kubectl rollout status deployment/nginx-deployment
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out

过一会我们再查看,就全

[root@master ~]# kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           51s

这里注意如果定义了.spec.minReadySeconds,那么必须经过定义的时间才会达到AVAILABLE 状态

通过下面的命令查看Deployment创建的ReplicaSet(rs)

[root@master ~]# kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-6c54bd5869   3         3         3         56m

注意ReplicaSet的名称格式为[DEPLOYMENT-NAME]-[POD-TEMPLATE-HASH-VALUE],后面的hash值是由Deployment自动创建的

查看Pods

[root@master ~]# kubectl get pods --show-labels
NAME                                READY     STATUS    RESTARTS   AGE       LABELS
nginx-deployment-6c54bd5869-9brqp   1/1       Running   0          58m       app=nginx,pod-template-hash=2710681425
nginx-deployment-6c54bd5869-dkmgh   1/1       Running   0          58m       app=nginx,pod-template-hash=2710681425
nginx-deployment-6c54bd5869-vzsht   1/1       Running   0          58m       app=nginx,pod-template-hash=2710681425

创建的ReplicaSet 会确保时刻有3个nginx Pods的副本在运行

 

更新Deployment

假设我们想把nginx从1.7.9更新到1.9.1,有以下3种方式

1. 直接set命令设置变更的部分

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

以上命令会自动回滚更改Pods,即停止一定量的老的,新建新的,直到来的终止完,新的启动完

通过describe即可查看所有的细节

[root@master yaml]# kubectl describe deployment/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Thu, 15 Mar 2018 02:51:06 -0400
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision=2
                        kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.9.1
    Port:         80/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-5964dfd755 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled up replica set nginx-deployment-5964dfd755 to 1
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled down replica set nginx-deployment-6c54bd5869 to 2
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled up replica set nginx-deployment-5964dfd755 to 2
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled down replica set nginx-deployment-6c54bd5869 to 1
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled up replica set nginx-deployment-5964dfd755 to 3
  Normal  ScalingReplicaSet  2m    deployment-controller  Scaled down replica set nginx-deployment-6c54bd5869 to 0
  • 可见image已经变了
  • 另外Events可查看滚动更新的过程
  • 另外上面的说的停止和新建的比例在这里体现RollingUpdateStrategy:  25% max unavailable, 25% max surge,25% max unavailable确保在更新时只有部分会关闭(这里是pod数的25%会关闭)。25% max surge确保创建新的pod也在一定比例上(这里默认也是25%)

2. 通过直接修改线上的配置也可直接修改

kubectl edit deployment/nginx-deployment

会打开一个编辑器,修改指定的部分即可,这里是.spec.template.spec.containers[0].image

3. 修改yaml文件,通过apply重新部署

[root@master yaml]# kubectl apply -f nginx-deployment.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment "nginx-deployment" configured

但这里有个警告: 也就是apply方式更新的资源应该是由kubectl create 加--save-config参数创建的 或 由apply创建的 (apply当资源不存在时会创建)

这时我们查看rs,会显示新起了一个rs并将副本扩到3个,旧的rs都缩减为0

[root@master yaml]# kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-5964dfd755   3         3         3         21m
nginx-deployment-6c54bd5869   0         0         0         1h

 

回滚Deployment

有时需要回滚的操作,比如更新错误,手误等一系列问题

比如上面的操作更新到1.9.1时,写错了,写成1.91了

[root@master yaml]# kubectl set image deployment/nginx-deployment nginx=nginx:1.91
deployment "nginx-deployment" image updated

追踪状态

[root@master yaml]# kubectl rollout status deployments nginx-deployment
Waiting for rollout to finish: 1 out of 3 new replicas have been updated...

可见卡住不动了, Ctrl+C终止,查看rs如下

[root@master yaml]# kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-5964dfd755   3         3         3         40m
nginx-deployment-5d5cfdbd5f   1         1         0         1m
nginx-deployment-6c54bd5869   0         0         0         2h

新的rs只启动了Pod但没有处于READY状态

查看Pods

[root@master yaml]# kubectl get pods
NAME                                READY     STATUS             RESTARTS   AGE
nginx-deployment-5964dfd755-8z8b7   1/1       Running            0          27m
nginx-deployment-5964dfd755-bnznj   1/1       Running            0          27m
nginx-deployment-5964dfd755-pt54q   1/1       Running            0          27m
nginx-deployment-5d5cfdbd5f-srdcc   0/1       ImagePullBackOff   0          2m

可发现ImagePullBackOff,实际就是镜像不存在

要修复这个,我们就需要rollback到前一个ok的版本

查看操作历史

[root@master yaml]# kubectl rollout history deployment/nginx-deployment
deployments "nginx-deployment"
REVISION    CHANGE-CAUSE
1           kubectl create -f docs/user-guide/nginx-deployment.yaml --record
2           kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
3           kubectl set image deployment/nginx-deployment nginx=nginx:1.91

要查看每个版本的详细情况,指定--revision

[root@master yaml]# kubectl rollout history deployment/nginx-deployment --revision=2
deployments "nginx-deployment" with revision #2
Pod Template:
  Labels:       app=nginx
        pod-template-hash=2710681425
  Annotations:  kubernetes.io/change-cause=kubectl edit deployment/nginx-deployment
  Containers:
   nginx:
    Image:      nginx:1.9.1
    Port:       80/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

接下来进行回滚的操作

不指定版本,默认回滚到上一个版本

[root@master yaml]# kubectl rollout undo deployment/nginx-deployment
deployment "nginx-deployment" rolled back

指定版本,通过--to-revision指定

[root@master yaml]# kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment "nginx-deployment" rolled back

查看

kubectl describe deployment/nginx-deployment

....略

Events:
  FirstSeen LastSeen    Count   From                    SubobjectPath   Type        Reason              Message
  --------- --------    -----   ----                    -------------   --------    ------              -------
  30m       30m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-2035384211 to 3
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-1564180365 to 1
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set nginx-deployment-2035384211 to 2
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-1564180365 to 2
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set nginx-deployment-2035384211 to 0
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-3066724191 to 2
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-3066724191 to 1
  29m       29m         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set nginx-deployment-1564180365 to 2
  2m        2m          1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set nginx-deployment-3066724191 to 0
  2m        2m          1       {deployment-controller }                Normal      DeploymentRollback  Rolled back deployment "nginx-deployment" to revision 2
  29m       2m          2       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set nginx-deployment-1564180365 to 3

可看到有DeploymentRollback  Reason的事件

 

扩展Deployment

可通过如下的命令进行扩展

[root@master yaml]# kubectl scale deployment nginx-deployment --replicas=5
deployment "nginx-deployment" scaled

查看Pods

[root@master yaml]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-5964dfd755-8z8b7   1/1       Running   0          41m
nginx-deployment-5964dfd755-bhm2s   1/1       Running   0          1m
nginx-deployment-5964dfd755-bnznj   1/1       Running   0          41m
nginx-deployment-5964dfd755-cftfj   1/1       Running   0          6s
nginx-deployment-5964dfd755-pt54q   1/1       Running   0          41m

可见已扩展到5个

使用autoscale还可设置自动水平扩展(hpa),可根据机器负载之类的信息自动扩展或缩减,这个后面细讲

$ kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
deployment "nginx-deployment" autoscaled

 

暂停和恢复Deployment

有时需要修改多个部分,而不是上面的只修改image,这样的话每次改完都自动部署,显然不好,通过pause即可暂停Deployment,更改完了,通过resume即可恢复部署

暂停

[root@master yaml]# kubectl rollout pause deployment/nginx-deployment
deployment "nginx-deployment" paused

修改

[root@master yaml]# kubectl set image deploy/nginx-deployment nginx=nginx:1.7.9
deployment "nginx-deployment" image updated

查看Pods

[root@master yaml]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-5964dfd755-8z8b7   1/1       Running   0          57m
nginx-deployment-5964dfd755-bhm2s   1/1       Running   0          17m
nginx-deployment-5964dfd755-bnznj   1/1       Running   0          57m
nginx-deployment-5964dfd755-cftfj   1/1       Running   0          16m
nginx-deployment-5964dfd755-pt54q   1/1       Running   0          57m

注意后面的AGE还是之前的Pod,这里就不会自动更新了

恢复

[root@master yaml]# kubectl rollout resume deploy/nginx-deployment
deployment "nginx-deployment" resumed

这时再查看Pods

[root@master yaml]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-6c54bd5869-htjxz   1/1       Running   0          25s
nginx-deployment-6c54bd5869-lj288   1/1       Running   0          30s
nginx-deployment-6c54bd5869-nt8lt   1/1       Running   0          30s
nginx-deployment-6c54bd5869-svqz6   1/1       Running   0          29s
nginx-deployment-6c54bd5869-zq8tl   1/1       Running   0          25s

可见已经更新部署了

内部Deployment部分大概就讲完了,下面把nginx服务暴露到外面

删除Deployment

[root@master yaml]# kubectl delete -f nginx-deployment.yaml 
deployment "nginx-deployment" deleted

部署service

服务的暴露需要Service,它是Pod的抽象代理(具体机制见这里)。见nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  sessionAffinity: ClientIP
  selector:
    app: nginx
  ports:
    - port: 80
      nodePort: 30080
  • kind:Service代表是一个服务
  • type:NodePort k8s将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过<NodeIP>:NodePort的方式Kubernetes集群外部的程序可以访问Service。
  • selector:哪个服务需要暴露
  • port:service暴露的端口
  • TargetPort:pod的端口
  • nodePort:对外暴露的端口,不设置会默认分配,范围:30000-32767
  • 转发逻辑是:
    <NodeIP>:<nodeport> => <ServiceVIP>:<port>=> <PodIP>:<targetport>

部署service服务:

[root@master yaml]# kubectl create -f nginx-service.yaml 
service "nginx-service" created

可看到启动了一个svc

NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc/kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        1d
svc/nginx-service   NodePort    10.101.86.235   <none>        80:30080/TCP   4m

浏览器测试

 

over!

本文转载自:https://my.oschina.net/u/914655/blog/1635492

openthings
粉丝 322
博文 1134
码字总数 685726
作品 1
东城
架构师
私信 提问
通过Rancher部署并扩容Kubernetes集群基础篇二

接上一篇通过Rancher部署并扩容Kubernetes集群基础篇一 7. 使用ConfigMap配置redis https://github.com/kubernetes/kubernetes.github.io/blob/master/docs/user-guide/configmap/redis/redi......

自由linux
2017/07/12
0
0
Kubernetes 入门:运行无状态服务

本文以 ANOYI-BLOG 这个博客系统为例,演示在 Kubernetes 集群中运行无状态服务,以及相关命令的用法介绍。 先决条件 确保 kubernetes 集群处于运行状态 【Kubernetes 环境搭建 - MacOS】 输...

Anoyi
2018/05/09
0
0
伸缩APP-运行多个APP实例

原文地址:https://kubernetes.io/docs/tutorials/kubernetes-basics/scale-intro/ 目标: 使用kubectl扩展应用 在之前的单元里我们创建了一个Deployment,然后紧接着通过Service公开曝光了出...

趁你还年轻233
2018/03/12
0
0
第一次部署Kubernetes

如果阅读过这篇《Kubernetes介绍》,那么你应该已经很好地理解了组成Kubernetes的基础组件。如果你和我一样的话,那么不真正动手用用Kubernetes,其实无法彻底地理解它的理念。这是本系列文章...

Docker
2018/10/17
0
0
Kubernetes部署Fabric,使用Helm Chart

Hyperledger Fabric Deployment Using Helm Chart Posted on 2018/08/29 by Henry Zhang Henry Zhang, Jiahao Chen, Hui Hu, Wenkai Yin This blog post is to share the personal technical......

openthings
2018/09/19
123
0

没有更多内容

加载失败,请刷新页面

加载更多

CSS--列表

一、列表标识项 list-style-type none:去掉标识项 disc:默认实心圆 circle:空心圆 squire:矩形 二、列表项图片 list-style-img: 取值:url(路径) 三、列表项位置 list-style-position:...

wytao1995
今天
4
0
linux 命令-文本比较comm、diff、patch

本文原创首发于公众号:编程三分钟 今天学了三个文本比较的命令分享给大家。 comm comm 命令比较相同的文本 $ cat charabc$ cat chardiffadc 比如,我有两个文件char和chardiff如上,...

编程三分钟
今天
7
0
QML教程

https://blog.csdn.net/qq_40194498/article/category/7580030 https://blog.csdn.net/LaineGates/article/details/50887765...

shzwork
今天
5
0
HA Cluster之5

对于使用heartbeat v2版的CRM配置的集群信息都是保存在一个名为cib.xml的配置文件中,存放在/var/lib/heartbeat/crm/下。CIB:Cluster Information Base,由于xml文件配置不是那么方便,所以...

lhdzw
今天
6
0
玩转Redis-Redis基础数据结构及核心命令

  《玩转Redis》系列文章主要讲述Redis的基础及中高级应用,文章基于Redis5.0.4+。本文主要讲述Redis的数据结构String,《玩转Redis-Redis基础数据结构及核心命令》相关操作命令为方便对比...

zxiaofan666
今天
11
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部