- A+
Deployment管理Pods和ReplicaSets,提供声明式更新。和老的ReplicationController(命令式管理)对应,发展趋势是取代老的,所以后面也不会起文章单独讨论ReplicationController了。
但由Deployment-controller管理的Pods和ReplicaSets最好自始至终都由Deployment-controller管理,最好不要手动去管理,以免发生冲突。
创建Deployment
如下一个Deployment的配置(tomcat-deployment.yaml),创建一个ReplicaSet包含3个tomcat Pods
apiVersion: apps/v1 kind: Deployment metadata: name: tomcat-deployment labels: app: tomcat spec: replicas: 1 selector: matchLabels: app: tomcat template: metadata: labels: app: tomcat spec: containers: - name: tomcat image: 10.209.3.81/library/os7tom7jdk8:v1.0 command: [/root/run.sh] resources: {} imagePullPolicy: IfNotPresent ports: - containerPort: 8080
- apiVersion这里为apps/v1,如果是1.9之前的版本为extensions/v1beta1
- replicas:3 起3个replicated Pods
- selector指明哪个pod被管理,这里我们指定了label(app:nginx)
- template: spec 指明了运行一个容器tomcat(以自己制作的镜像为例)
- 开放8080端口给container,以使container之间能发送和接收流量
注意这里定义name 或 label 时不要和其他的重复,k8s不会检查这个,需要人工自己确认
要创建此部署,执行下面的命令(在这之前我们提前下好nginx相关的镜像,docker pull nginx:1.7.9,我这里是使用自己的镜像库,请参考:docker镜像库搭建)
[root@master yaml]# kubectl create -f tomcat-deployment.yaml --record deployment "tomcat-deployment" created
- --record会记录操作历史,以便于后面回滚操作
查看deployments
[root@k4614v /data/yaml]# kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE tomcat-deployment 3 3 3 3 2h
- NAME:在集群中的部署名称
- DESIRED:显示配置里定义的副本数量,这是应该达到的副本数量
- CURRENT:当前正在运行的副本数量
- UP-TO-DATE:更新到当前所需状态的副本数量
- AVAILABLE:可供使用的副本数量
- AEG:显示app存活的时间
这里注意如果定义了.spec.minReadySeconds,那么必须经过定义的时间才会达到AVAILABLE 状态
通过下面语句可查追踪部署情况
[root@k4614v /data/yaml]# kubectl rollout status deployment/tomcat-deployment deployment "tomcat-deployment" successfully rolled out
通过下面的命令查看Deployment创建的ReplicaSet(rs)
[root@k4614v /data/yaml]# kubectl get rs NAME DESIRED CURRENT READY AGE tomcat-deployment-5cd68b69d 3 3 3 2h
注意ReplicaSet的名称格式为[DEPLOYMENT-NAME]-[POD-TEMPLATE-HASH-VALUE],后面的hash值是由Deployment自动创建的
查看Pods
[root@k4614v /data/yaml]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS tomcat-deployment-5cd68b69d-9wnjn 1/1 Running 0 1h app=tomcat,pod-template-hash=178246258 tomcat-deployment-5cd68b69d-k4ffb 1/1 Running 0 2h app=tomcat,pod-template-hash=178246258 tomcat-deployment-5cd68b69d-vxzjd 1/1 Running 0 1h app=tomcat,pod-template-hash=178246258
创建的ReplicaSet 会确保时刻有3个nginx Pods的副本在运行
更新Deployment
假设我们想把tomcat更新到正式的appupdate2018418,有以下3种方式
1. 直接set命令设置变更的部分
$ kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v2 deployment "tomcat-deployment" image updated
以上命令会自动回滚更改Pods,即停止一定量的老的,新建新的,直到来的终止完,新的启动完
通过describe即可查看所有的细节
[root@k4152v /data/yaml]# kubectl describe deployment/tomcat-deployment Name: tomcat-deployment Namespace: default CreationTimestamp: Thu, 12 Apr 2018 14:54:49 +0800 Labels: app=tomcat Annotations: deployment.kubernetes.io/revision=6 kubernetes.io/change-cause=kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v3 Selector: app=tomcat Replicas: 3 desired | 2 updated | 4 total | 3 available | 1 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=tomcat Containers: tomcat: Image: 10.209.3.81/library/appupdate2018418:v3 Port: 8080/TCP Command: /root/run.sh Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True ReplicaSetUpdated OldReplicaSets: tomcat-deployment-5cd68b69d (2/2 replicas created) NewReplicaSet: tomcat-deployment-799f9cc9 (2/2 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 7m deployment-controller Scaled up replica set tomcat-deployment-7d5fd4b5d6 to 1 Normal DeploymentRollback 4m deployment-controller Rolled back deployment "tomcat-deployment" to revision 3 Normal ScalingReplicaSet 4m deployment-controller Scaled down replica set tomcat-deployment-7d5fd4b5d6 to 0 Normal ScalingReplicaSet 18s deployment-controller Scaled up replica set tomcat-deployment-799f9cc9 to 1 Normal ScalingReplicaSet 14s deployment-controller Scaled down replica set tomcat-deployment-5cd68b69d to 2 Normal ScalingReplicaSet 14s deployment-controller Scaled up replica set tomcat-deployment-799f9cc9 to 2
- 可见image已经变了
- 另外Events可查看滚动更新的过程
- 另外上面的说的停止和新建的比例在这里体现RollingUpdateStrategy: 25% max unavailable, 25% max surge,25% max unavailable确保在更新时只有部分会关闭(这里是pod数的25%会关闭)。25% max surge确保创建新的pod也在一定比例上(这里默认也是25%)
2. 通过直接修改线上的配置也可直接修改
kubectl edit deployment/tomcat-deployment
会打开一个编辑器,修改指定的部分即可,这里是.spec.template.spec.containers[0].image
3. 修改yaml文件,通过apply重新部署
[root@master yaml]# kubectl apply -f tomcat-deployment.yaml Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply deployment "tomcat-deployment" configured
但这里有个警告: 也就是apply方式更新的资源应该是由kubectl create 加--save-config参数创建的 或 由apply创建的 (apply当资源不存在时会创建)
这时我们查看rs,会显示新起了一个rs并将副本扩到3个,旧的rs都缩减为0
[root@k4152v /data/yaml]# kubectl get rs NAME DESIRED CURRENT READY AGE tomcat-deployment-587854b948 0 0 0 5d tomcat-deployment-5cd68b69d 0 0 0 6d tomcat-deployment-799f9cc9 3 3 3 1h tomcat-deployment-7d5fd4b5d6 0 0 0 1h
回滚Deployment
有时需要回滚的操作,比如发版本的时候docker镜像出问题了
比如上面的操作更新到10.209.3.81/library/appupdate2018418:v2
[root@master yaml]# kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v2 deployment "tomcat-deployment" image updated
追踪状态
[root@master yaml]# kubectl rollout status deployments tomcat-deployment Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
可见卡住不动了, Ctrl+C终止,查看rs如下
[root@k4152v /data/yaml]# kubectl get rs NAME DESIRED CURRENT READY AGE tomcat-deployment-587854b948 0 0 0 5d tomcat-deployment-5cd68b69d 0 0 0 6d tomcat-deployment-799f9cc9 3 3 3 1h tomcat-deployment-7d5fd4b5d6 0 0 0 1h
新的rs只启动了Pod但没有处于READY状态
查看Pods
[root@k4152v /data/yaml]# kubectl get pods NAME READY STATUS RESTARTS AGE tomcat-deployment-799f9cc9-9mmwv 1/1 Running 0 16m tomcat-deployment-799f9cc9-hz2nv 1/1 Running 0 16m tomcat-deployment-799f9cc9-mf4z2 1/1 Running 0 16m
可发现ImagePullBackOff,实际就是镜像不存在
要修复这个,我们就需要rollback到前一个ok的版本
查看操作历史
[root@k4152v /data/yaml]# kubectl rollout history deployment/tomcat-deployment deployments "tomcat-deployment" REVISION CHANGE-CAUSE 2 kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate20180413:v1.0 4 kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v2 7 kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/os7tom7jdk8:v1.0 8 kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v3
要查看每个版本的详细情况,指定--revision
[root@k4152v /data/yaml]# kubectl rollout history deployment/tomcat-deployment --revision=4 deployments "tomcat-deployment" with revision #4 Pod Template: Labels: app=tomcat pod-template-hash=3819806182 Annotations: kubernetes.io/change-cause=kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v2 Containers: tomcat: Image: 10.209.3.81/library/appupdate2018418:v2 Port: 8080/TCP Command: /root/run.sh Environment: <none> Mounts: <none> Volumes: <none>
接下来进行回滚的操作
不指定版本,默认回滚到上一个版本
[root@master yaml]# kubectl rollout undo deployment/tomcat-deployment deployment "tomcat-deployment" rolled back
指定版本,通过--to-revision指定
[root@master yaml]# kubectl rollout undo deployment/tomcat-deployment --to-revision=6 deployment "nginx-deployment" rolled back
查看
[root@k4152v /data/yaml]# kubectl describe deployment/tomcat-deployment Name: tomcat-deployment Namespace: default CreationTimestamp: Thu, 12 Apr 2018 14:54:49 +0800 Labels: app=tomcat Annotations: deployment.kubernetes.io/revision=8 kubernetes.io/change-cause=kubectl set image deployment/tomcat-deployment tomcat=10.209.3.81/library/appupdate2018418:v3 Selector: app=tomcat 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=tomcat Containers: tomcat: Image: 10.209.3.81/library/appupdate2018418:v3 Port: 8080/TCP Command: /root/run.sh Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: tomcat-deployment-799f9cc9 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal DeploymentRollback 54m deployment-controller Rolled back deployment "tomcat-deployment" to revision 5 Normal ScalingReplicaSet 54m (x2 over 6d) deployment-controller Scaled up replica set tomcat-deployment-5cd68b69d to 1 Normal ScalingReplicaSet 54m deployment-controller Scaled down replica set tomcat-deployment-799f9cc9 to 2 Normal ScalingReplicaSet 54m (x2 over 6d) deployment-controller Scaled up replica set tomcat-deployment-5cd68b69d to 2 Normal ScalingReplicaSet 54m (x2 over 6d) deployment-controller Scaled up replica set tomcat-deployment-5cd68b69d to 3 Normal ScalingReplicaSet 54m deployment-controller Scaled down replica set tomcat-deployment-799f9cc9 to 1 Normal ScalingReplicaSet 54m deployment-controller Scaled down replica set tomcat-deployment-799f9cc9 to 0 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled up replica set tomcat-deployment-799f9cc9 to 1 Normal DeploymentRollback 18m deployment-controller Rolled back deployment "tomcat-deployment" to revision 6 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled up replica set tomcat-deployment-799f9cc9 to 2 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled down replica set tomcat-deployment-5cd68b69d to 2 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled down replica set tomcat-deployment-5cd68b69d to 1 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled up replica set tomcat-deployment-799f9cc9 to 3 Normal ScalingReplicaSet 18m (x2 over 1h) deployment-controller Scaled down replica set tomcat-deployment-5cd68b69d to 0
可看到有DeploymentRollback Reason的事件
扩展Deployment
可通过如下的命令进行扩展
[root@master yaml]# kubectl scale deployment tomcat-deployment --replicas=5 deployment "tomcat-deployment" scaled
查看Pods
[root@k4152v /data/yaml]# kubectl get pods NAME READY STATUS RESTARTS AGE tomcat-deployment-799f9cc9-9mmwv 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2nv 1/1 Running 0 19m tomcat-deployment-799f9cc9-mf4z2 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2df 1/1 Running 0 3m tomcat-deployment-799f9cc9-mf5t2 1/1 Running 0 3m
可见已扩展到5个
使用autoscale还可设置自动水平扩展(hpa),可根据机器负载之类的信息自动扩展或缩减,这个后面细讲
$ kubectl autoscale deployment tomcat-deployment --min=10 --max=15 --cpu-percent=80 deployment "tomcat-deployment" autoscaled
暂停和恢复Deployment
有时需要修改多个部分,而不是上面的只修改image,这样的话每次改完都自动部署,显然不好,通过pause即可暂停Deployment,更改完了,通过resume即可恢复部署
暂停
[root@master yaml]# kubectl rollout pause deployment/tomcat-deployment deployment "tomcat-deployment" paused
修改
[root@master yaml]# kubectl set image deploy/tomcat-deployment nginx=10.209.3.81/library/appupdate2018418:v3 deployment "tomcat-deployment" image updated
查看Pods
[root@k4152v /data/yaml]# kubectl get pods NAME READY STATUS RESTARTS AGE tomcat-deployment-799f9cc9-9mmwv 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2nv 1/1 Running 0 19m tomcat-deployment-799f9cc9-mf4z2 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2df 1/1 Running 0 3m tomcat-deployment-799f9cc9-mf5t2 1/1 Running 0 3m
注意后面的AGE还是之前的Pod,这里就不会自动更新了
恢复
[root@master yaml]# kubectl rollout resume deploy/tomcat-deployment deployment "tomcat-deployment" resumed
这时再查看Pods
[root@k4152v /data/yaml]# kubectl get pods NAME READY STATUS RESTARTS AGE tomcat-deployment-799f9cc9-9mmwv 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2nv 1/1 Running 0 19m tomcat-deployment-799f9cc9-mf4z2 1/1 Running 0 19m tomcat-deployment-799f9cc9-hz2df 1/1 Running 0 3m tomcat-deployment-799f9cc9-mf5t2 1/1 Running 0 3m
可见已经更新部署了
内部Deployment部分大概就讲完了,下面把nginx服务暴露到外面
删除Deployment
[root@master yaml]# kubectl delete -f tomcat-deployment.yaml deployment "tomcat-deployment" deleted
部署service
服务的暴露需要Service,它是Pod的抽象代理(具体机制见这里)。见nginx-service.yaml
apiVersion: v1 kind: Service metadata: name: tomcat-service spec: type: NodePort sessionAffinity: ClientIP selector: app: tomcat ports: - port: 80 nodePort: 32709
- 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 tomcat-service.yaml service "tomcat-service" created
可看到启动了一个svc
[root@k4152v /data/yaml]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE tomcat-service NodePort 10.102.246.240 <none> 8080:32097/TCP 6d
浏览器测试
由于没有在代理上安装docker,所有临时加了个node节点测试,把流量切过去后,就可以看到负载情况,如下图:
再看看日志:
- 安卓客户端下载
- 微信扫一扫
- 微信公众号
- 微信公众号扫一扫