Deployment官方文档:
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
扩展:声明式定义是指直接修改资源清单yaml文件,然后通过kubectl apply -f 资源清单yaml文件,就可以更改资源
Deployment控制器是建立在rs之上的一个控制器,可以管理多个rs,每次更新镜像版本,都会生成一个新的rs,把旧的rs替换掉,多个rs同时存在,但是只有一个rs运行。
rs v1控制三个pod,删除一个pod,在rs v2上重新建立一个,依次类推,直到全部都是由rs v2控制,如果rs v2有问题,还可以回滚,Deployment是建构在rs之上的,多个rs组成一个Deployment,但是只有一个rs处于活跃状态.
Deployment可以使用声明式定义,直接在命令行通过纯命令的方式完成对应资源版本的内容的修改,也就是通过打补丁的方式进行修改;Deployment能提供滚动式自定义自控制的更新;对Deployment来讲,我们在实现更新时还可以实现控制更新节奏和更新逻辑。
比如说Deployment控制5个pod副本,pod的期望值是5个,但是升级的时候需要额外多几个pod,那我们控制器可以控制在5个pod副本之外还能再增加几个pod副本;比方说能多一个,但是不能少,那么升级的时候就是先增加一个,再删除一个,增加一个删除一个,始终保持pod副本数是5个;还有一种情况,最多允许多一个,最少允许少一个,也就是最多6个,最少4个,第一次加一个,删除两个,第二次加两个,删除两个,依次类推,可以自己控制更新方式,这种滚动更新需要加readinessProbe和livenessProbe探测,确保pod中容器里的应用都正常启动了才删除之前的pod。
启动第一步,刚更新第一批就暂停了也可以;假如目标是5个,允许一个也不能少,允许最多可以10个,那一次加5个即可;这就是我们可以自己控制节奏来控制更新的方法。
通过Deployment对象,你可以轻松的做到以下事情:
1、创建ReplicaSet和Pod
2、滚动升级(不停止旧服务的状态下升级)和回滚应用(将应用回滚到之前的版本)
3、平滑地扩容和缩容
4、暂停和继续Deployment
deployment是一个三级结构,deployment管理replicaset,replicaset管理pod,
用deployment创建一个pod
把myapp-blue-v1.tar.gz和myapp-blue-v2.tar.gz上传到节点服务器
链接:https://pan.baidu.com/s/1lkh76DYEhb4mVfN-xCJCkQ?pwd=w69c
提取码:w69c
ctr -n=k8s.io images import myapp-blue-v1.tar.gz
ctr -n=k8s.io images import myapp-blue-v2.tar.gz
cat deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-v1
spec:
replicas: 2
selector:
matchLabels:
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec:
containers:
- name: myapp
image: janakiramm/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
startupProbe:
periodSeconds: 5
initialDelaySeconds: 20
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 80
path: /
livenessProbe:
periodSeconds: 5
initialDelaySeconds: 20
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 80
path: /
readinessProbe:
periodSeconds: 5
initialDelaySeconds: 20
timeoutSeconds: 10
httpGet:
scheme: HTTP
port: 80
path: /
kubectl apply -f deploy-demo.yaml
kubectl apply:表示声明式的定义,既可以创建资源,也可以动态更新资源
查看deploy状态:
kubectl get deploy
显示如下:
NAME READY UP-TO-DATE AVAILABLE AGE
myapp-v1 2/2 2 2 60s
1.NAME :列出名称空间中deployment的名称。
2.READY:显示deployment有多少副本数。它遵循ready/desired的模式。
3.UP-TO-DATE: 显示已更新到所需状态的副本数。
4.AVAILABLE: 显示你的可以使用多少个应用程序副本。
5.AGE :显示应用程序已运行的时间。
kubectl get rs
显示如下:
AME DESIRED CURRENT READY AGE
myapp-v1-67fd9fc9c8 2 2 2 2m35s
#创建deploy的时候也会创建一个rs(replicaset),67fd9fc9c8 这个随机数字是我们引用pod的模板template的名字的hash值
1.NAME: 列出名称空间中ReplicaSet资源
2.DESIRED:显示应用程序的所需副本数,这些副本数是在创建时定义的。这是所需的状态。
3.CURRENT: 显示当前正在运行多少个副本。
4.READY: 显示你的用户可以使用多少个应用程序副本。
5.AGE :显示应用程序已运行的时间。
请注意,ReplicaSet的名称始终设置为[DEPLOYMENT-NAME]-[RANDOM-STRING]。RANDOM-STRING是随机生成的
kubectl get pods
显示如下:
myapp-v1-67fd9fc9c8-fcprr 1/1 Running 0 3s
myapp-v1-67fd9fc9c8-hw4f9 1/1 Running 0 2m21s
kubectl get pods -o wide | grep myapp
myapp-v1-67fd9fc9c8-fcprr 1/1 Running 0 10.244.187.78 k8s2
myapp-v1-67fd9fc9c8-hw4f9 1/1 Running 0 10.244.209.136 k8s1
请求刚才创建的pod资源
curl 10.244.187.78
…
background-color: blue;
apiVersion: apps/v1 #deployment对应的api版本
kind: Deployment #创建的资源是deployment
metadata:
name: myapp-v1 #deployment的名字
spec:
replicas: 2 #deployment管理的pod副本数
selector: #标签选择器
matchLabels: # matchLabels下定义的标签需要跟template.metadata.labels定义的标签一致
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec: #定义容器的属性
containers:
- name: myapp
image: janakiramm/myapp:v1 #容器使用的镜像
imagePullPolicy: IfNotPresent #镜像拉取策略
ports:
- containerPort: 80 #容器里的应用的端口
滚动更新是一种自动化程度较高的发布方式,用户体验比较平滑,是目前成熟型技术组织所采用的主流发布方式,一次滚动发布一般由若干个发布批次组成,每批的数量一般是可以配置的(可以通过发布模板定义),例如第一批1台,第二批10%,第三批50%,第四批100%。每个批次之间留观察间隔,通过手工验证或监控反馈确保没有问题再发下一批次,所以总体上滚动式发布过程是比较缓慢的
10个pod:
第一我更新1个pod:
第二次更新:1个
第三次更新:5个
第四次:3个
首先看下Deployment资源对象的组成
kubectl explain deployment.spec
KIND: Deployment
VERSION: apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
Specification of the desired behavior of the Deployment.
DeploymentSpec is the specification of the desired behavior of the
Deployment.
FIELDS:
minReadySeconds <integer>
Minimum number of seconds for which a newly created pod should be ready
without any of its container crashing, for it to be considered available.
Defaults to 0 (pod will be considered available as soon as it is ready)
paused <boolean>
Indicates that the deployment is paused.
#暂停,当我们更新的时候创建pod先暂停,不是立即更新
progressDeadlineSeconds <integer>
The maximum time in seconds for a deployment to make progress before it is
considered to be failed. The deployment controller will continue to process
failed deployments and a condition with a ProgressDeadlineExceeded reason
will be surfaced in the deployment status. Note that progress will not be
estimated during the time a deployment is paused. Defaults to 600s.
replicas <integer>
Number of desired pods. This is a pointer to distinguish between explicit
zero and not specified. Defaults to 1.
revisionHistoryLimit <integer>
#保留的历史版本数,默认是10个
The number of old ReplicaSets to retain to allow rollback. This is a
pointer to distinguish between explicit zero and not specified. Defaults to
10.
selector <Object> -required-
Label selector for pods. Existing ReplicaSets whose pods are selected by
this will be the ones affected by this deployment. It must match the pod
template's labels.
strategy <Object>
#更新策略,支持的滚动更新策略
The deployment strategy to use to replace existing pods with new ones.
template <Object> -required-
Template describes the pods that will be created.
kubectl explain deploy.spec.strategy
KIND: Deployment
VERSION: apps/v1
RESOURCE: strategy <Object>
DESCRIPTION:
The deployment strategy to use to replace existing pods with new ones.
DeploymentStrategy describes how to replace existing pods with new ones.
FIELDS:
rollingUpdate <Object>
Rolling update config params. Present only if DeploymentStrategyType =
RollingUpdate.
type <string>
Type of deployment. Can be "Recreate" or "RollingUpdate". Default is
RollingUpdate.
#支持两种更新,Recreate和RollingUpdate
#Recreate是重建式更新,删除一个更新一个
#RollingUpdate 滚动更新,定义滚动更新的更新方式的,也就是pod能多几个,少几个,控制更新力度的
kubectl explain deploy.spec.strategy.rollingUpdate
KIND: Deployment
VERSION: apps/v1
RESOURCE: rollingUpdate <Object>
DESCRIPTION:
Rolling update config params. Present only if DeploymentStrategyType =
RollingUpdate.
Spec to control the desired behavior of rolling update.
FIELDS:
maxSurge <string>
The maximum number of pods that can be scheduled above the desired number
of pods. Value can be an absolute number (ex: 5) or a percentage of desired
pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
is calculated from percentage by rounding up. Defaults to 25%. Example:
when this is set to 30%, the new ReplicaSet can be scaled up immediately
when the rolling update starts, such that the total number of old and new
pods do not exceed 130% of desired pods. Once old pods have been killed,
new ReplicaSet can be scaled up further, ensuring that total number of pods
running at any time during the update is at most 130% of desired pods.
#我们更新的过程当中最多允许超出的指定的目标副本数有几个;
它有两种取值方式,第一种直接给定数量,第二种根据百分比,百分比表示原本是5个,最多可以超出20%,那就允许多一个,最多可以超过40%,那就允许多两个
maxUnavailable <string>
The maximum number of pods that can be unavailable during the update. Value
can be an absolute number (ex: 5) or a percentage of desired pods (ex:
10%). Absolute number is calculated from percentage by rounding down. This
can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set
to 30%, the old ReplicaSet can be scaled down to 70% of desired pods
immediately when the rolling update starts. Once new pods are ready, old
ReplicaSet can be scaled down further, followed by scaling up the new
ReplicaSet, ensuring that the total number of pods available at all times
during the update is at least 70% of desired pods.
#最多允许几个不可用
假设有5个副本,最多一个不可用,就表示最少有4个可用
deployment是一个三级结构,deployment控制replicaset,replicaset控制pod,
vim deploy-demo.yaml
spec:
replicas: 3
kubectl apply -f deploy-demo.yaml
注意:apply不同于create,apply可以执行多次;create执行一次,再执行就会报错有重复
kubectl get pods
显示如下:
NAME READY STATUS RESTARTS AGE
myapp-v1-67fd9fc9c8-tsl92 1/1 Running 0 8m18s
myapp-v1-67fd9fc9c8-4bv5n 1/1 Running 0 8m18s
myapp-v1-67fd9fc9c8-cw59c 1/1 Running 0 18s
上面可以看到pod副本数变成了3个
查看myapp-v1这个控制器的详细信息
kubectl describe deploy myapp-v1
#显示如下:
Name: myapp-v1
Namespace: default
CreationTimestamp: Sun, 21 Mar 2021 18:46:52 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=myapp,version=v1
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
#默认的更新策略rollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
#最多允许多25%个pod,25%表示不足一个,可以补一个
Pod Template:
Labels: app=myapp
version=v1
Containers:
myapp:
Image: janakiramm/myapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: myapp-v1-67fd9fc9c8 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m26s deployment-controller Scaled down replica set myapp-v1-67fd9fc9c8 to 2
Normal ScalingReplicaSet 2m1s (x2 over 10m) deployment-controller Scaled up replica set myapp-v1-67fd9fc9c8 to 3
在终端执行如下:
kubectl get pods -l app=myapp -w
打开一个新的终端窗口更改镜像版本,按如下操作:
vim deploy-demo.yaml
把image: janakiramm/myapp:v1 变成image: janakiramm/myapp:v2
kubectl apply -f deploy-demo.yaml
NAME READY STATUS RESTARTS AGE
myapp-v1-67fd9fc9c8-tsl92 1/1 Running 0 22m
myapp-v1-67fd9fc9c8-4bv5n 1/1 Running 0 22m
myapp-v1-67fd9fc9c8-cw59c 1/1 Running 0 14m
myapp-v1-75fb478d6c-24tbp 0/1 Pending 0 0s
myapp-v1-75fb478d6c-24tbp 0/1 Pending 0 0s
myapp-v1-75fb478d6c-24tbp 0/1 ContainerCreating 0 0s
myapp-v1-75fb478d6c-24tbp 1/1 Running 0 11s
myapp-v1-67fd9fc9c8-cw59c 1/1 Terminating 0 15m
myapp-v1-75fb478d6c-f52l6 0/1 Pending 0 0s
myapp-v1-75fb478d6c-f52l6 0/1 Pending 0 0s
myapp-v1-75fb478d6c-f52l6 0/1 ContainerCreating 0 0s
myapp-v1-67fd9fc9c8-cw59c 0/1 Terminating 0 15m
myapp-v1-75fb478d6c-f52l6 1/1 Running 0 11s
myapp-v1-67fd9fc9c8-4bv5n 1/1 Terminating 0 23m
myapp-v1-75fb478d6c-jlw28 0/1 Pending 0 0s
myapp-v1-75fb478d6c-jlw28 0/1 Pending 0 0s
myapp-v1-75fb478d6c-jlw28 0/1 ContainerCreating 0 0s
myapp-v1-75fb478d6c-jlw28 1/1 Running 0 1s
pending表示正在进行调度,ContainerCreating表示正在创建一个pod,running表示运 行一个pod,running起来一个pod之后再Terminating(停掉)一个pod,以此类推,直 到所有pod完成滚动升级
在另外一个窗口执行
kubectl get rs
显示如下:
NAME DESIRED CURRENT READY AGE
myapp-v1-75fb478d6c 3 3 3 2m7s
myapp-v1-67fd9fc9c8 0 0 0 25m
上面可以看到rs有两个,下面那个是升级之前的,已经被停掉,但是可以随时回滚
kubectl rollout history deployment myapp-v1
查看myapp-v1这个控制器的滚动历史,显示如下:
deployment.apps/myapp-v1
REVISION CHANGE-CAUSE
1 <none>
2 <none>
kubectl rollout undo deployment/myapp-v1 --to-revision=2
maxSurge和maxUnavailable用来控制滚动更新的更新策略
取值范围
数值
1. maxUnavailable: [0, 副本数]
2. maxSurge: [0, 副本数]
注意:两者不能同时为0。
1. maxUnavailable: [0%, 100%] 向下取整,比如10个副本,5%的话==0.5个,但计算按照0个;
2. maxSurge: [0%, 100%] 向上取整,比如10个副本,5%的话==0.5个,但计算按照1个;
注意:两者不能同时为0。
建议配置
1. maxUnavailable == 0
2. maxSurge == 1
这是我们生产环境提供给用户的默认配置。即“一上一下,先上后下”最平滑原则:
1个新版本pod ready(结合readiness)后,才销毁旧版本pod。此配置适用场景是平滑更新、保证服务平稳,但也有缺点,就是“太慢”了。
maxUnavailable:和期望的副本数比,不可用副本数最大比例(或最大值),这个值越小,越能保证服务稳定,更新越平滑;
maxSurge:和期望的副本数比,超过期望副本数最大比例(或最大值),这个值调的越大,副本更新速度越快。
自定义策略:
修改更新策略:maxUnavailable=1,maxSurge=1
kubectl patch deployment myapp-v1 -p '{"spec":{"strategy":{"rollingUpdate": {"maxSurge":1,"maxUnavailable":1}}}}'
kubectl describe deployment myapp-v1
查看myapp-v1这个控制器的详细信息
显示如下:
RollingUpdateStrategy: 1 max unavailable, 1 max surge
上面可以看到RollingUpdateStrategy: 1 max unavailable, 1 max surge
这个rollingUpdate更新策略变成了刚才设定的,因为我们设定的pod副本数是3,1和1表示最少不能少于2个pod,最多不能超过4个pod
这个就是通过控制RollingUpdateStrategy这个字段来设置滚动更新策略的
kubectl apply -f deploy-demo.yaml
打开新的终端,看pod更新过程
kubectl get pods -w
NAME READY STATUS RESTARTS AGE
myapp-v1-59c458cb84-gglnb 1/1 Running 0 5m20s
myapp-v1-59c458cb84-nvjhq 1/1 Running 0 5m20s
myapp-v1-59c458cb84-zslxg 1/1 Running 0 4m59s
############33
myapp-v1-59c458cb84-zslxg 1/1 Terminating 0 5m3s
myapp-v1-59c458cb84-nvjhq 1/1 Terminating 0 5m24s
myapp-v1-59c458cb84-gglnb 1/1 Terminating 0 5m24s
myapp-v1-59c458cb84-gglnb 1/1 Terminating 0 5m24s
myapp-v1-59c458cb84-nvjhq 1/1 Terminating 0 5m24s
myapp-v1-59c458cb84-zslxg 1/1 Terminating 0 5m3s
myapp-v1-59c458cb84-gglnb 0/1 Terminating 0 5m25s
myapp-v1-59c458cb84-gglnb 0/1 Terminating 0 5m25s
myapp-v1-59c458cb84-gglnb 0/1 Terminating 0 5m25s
myapp-v1-59c458cb84-zslxg 0/1 Terminating 0 5m4s
myapp-v1-59c458cb84-zslxg 0/1 Terminating 0 5m4s
myapp-v1-59c458cb84-zslxg 0/1 Terminating 0 5m4s
myapp-v1-59c458cb84-nvjhq 0/1 Terminating 0 5m25s
myapp-v1-59c458cb84-nvjhq 0/1 Terminating 0 5m25s
myapp-v1-59c458cb84-nvjhq 0/1 Terminating 0 5m25s
myapp-v1-6bd64fd79-h668z 0/1 Pending 0 0s
myapp-v1-6bd64fd79-d8v55 0/1 Pending 0 0s
myapp-v1-6bd64fd79-h668z 0/1 Pending 0 0s
myapp-v1-6bd64fd79-wkvmp 0/1 Pending 0 0s
myapp-v1-6bd64fd79-d8v55 0/1 Pending 0 0s
myapp-v1-6bd64fd79-wkvmp 0/1 Pending 0 0s
myapp-v1-6bd64fd79-h668z 0/1 ContainerCreating 0 0s
myapp-v1-6bd64fd79-d8v55 0/1 ContainerCreating 0 0s
myapp-v1-6bd64fd79-wkvmp 0/1 ContainerCreating 0 0s
myapp-v1-6bd64fd79-h668z 0/1 ContainerCreating 0 1s
myapp-v1-6bd64fd79-d8v55 0/1 ContainerCreating 0 1s
myapp-v1-6bd64fd79-wkvmp 0/1 ContainerCreating 0 1s
myapp-v1-6bd64fd79-d8v55 0/1 Running 0 1s
myapp-v1-6bd64fd79-h668z 0/1 Running 0 2s
myapp-v1-6bd64fd79-wkvmp 0/1 Running 0 2s
蓝色系统不对外提供服务,用来做什么呢
用来做发布前测试,测试过程中发现任何问题,可以直接在蓝色系统上修改,不干扰用户正在使用的系统。(注意,两套系统没有耦合的时候才能百分百保证不干扰)
蓝色系统经过反复的测试、修改、验证,确定达到上线标准之后,直接将用户切换到蓝色系统:
1、更新过程无需停机,风险较少
2、回滚方便,只需要更改路由或者切换DNS服务器,效率较高
1、成本较高,需要部署两套环境。如果新版本中基础服务出现问题,会瞬间影响全网用户;如果新版本有问题也会影响全网用户。
2、需要部署两套机器,费用开销大
3、在非隔离的机器(Docker、VM)上操作时,可能会导致蓝绿环境被摧毁风险
4、负载均衡器/反向代理/路由/DNS处理不当,将导致流量没有切换过来情况出现
把镜像压缩包上传到k8s的各个工作节点,ctr -n=k8s.io images import解压
链接:https://pan.baidu.com/s/1Kpip05Foysq19tx0R52sTA?pwd=dgia
提取码:dgia
链接:https://pan.baidu.com/s/1dFr-ZDlwivYyG3tdVvMY7Q?pwd=13nk
提取码:13nk
ctr -n=k8s.io images import myapp-lan.tar.gz
ctr -n=k8s.io images import myapp-lv.tar.gz
Kubernetes不支持内置的蓝绿部署。目前最好的方式是创建新的deployment,然后更新应用程序的service以指向新的deployment部署的应用
cat lv.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-v1
namespace: blue-green
spec:
replicas: 3
selector:
matchLabels:
app: myapp
version: v2
template:
metadata:
labels:
app: myapp
version: v2
spec:
containers:
- name: myapp
image: janakiramm/myapp:v2
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
可以使用kubectl命令创建部署。
kubectl create ns blue-green
kubectl apply -f lv.yaml
kubectl get pods -n blue-green
NAME READY STATUS RESTARTS AGE
myapp-v1-75d7db5cf7-4qnjz 1/1 Running 0 8s
myapp-v1-75d7db5cf7-5sk6j 1/1 Running 0 8s
myapp-v1-75d7db5cf7-wmhs4 1/1 Running 0 8s
kubectl get pods -n blue-green --show-labels
NAME READY STATUS RESTARTS AGE LABELS
myapp-v1-75d7db5cf7-4qnjz 1/1 Running 0 35s app=myapp,pod-template-hash=75d7db5cf7,version=v2
myapp-v1-75d7db5cf7-5sk6j 1/1 Running 0 35s app=myapp,pod-template-hash=75d7db5cf7,version=v2
myapp-v1-75d7db5cf7-wmhs4 1/1 Running 0 35s app=myapp,pod-template-hash=75d7db5cf7,version=v2
创建前端service
cat service_lanlv.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-lan-lv
namespace: blue-green
labels:
app: myapp
spec:
type: NodePort
ports:
- port: 80
nodePort: 30062
name: http
selector:
app: myapp
version: v2
更新服务:
kubectl apply -f service_lanlv.yaml
kubectl get svc -n blue-green
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-lan-lv NodePort 10.107.213.1 <none> 80:30062/TCP 36s
cat lan.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-v2
namespace: blue-green
spec:
replicas: 3
selector:
matchLabels:
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec:
containers:
- name: myapp
image: janakiramm/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubectl apply -f lan.yaml
kubectl get pods -n blue-green
NAME READY STATUS RESTARTS AGE
myapp-v1-75d7db5cf7-4qnjz 1/1 Running 0 13m
myapp-v1-75d7db5cf7-5sk6j 1/1 Running 0 13m
myapp-v1-75d7db5cf7-wmhs4 1/1 Running 0 13m
myapp-v2-85cc897d89-hp5j2 1/1 Running 0 10s
myapp-v2-85cc897d89-jpgbm 1/1 Running 0 10s
myapp-v2-85cc897d89-q94g4 1/1 Running 0 10s
修改service_lanlv.yaml 配置文件,修改标签,让其匹配到蓝程序(升级之后的程序)
cat service_lanlv.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-lan
namespace: blue-green
labels:
app: myapp
spec:
type: NodePort
ports:
- port: 80
nodePort: 30062
name: http
selector:
app: myapp
version: v1
更新资源清单文件:
kubectl apply -f service_lanlv.yaml
kubectl get svc -n blue-green
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-lan-lv NodePort 10.107.213.1 <none> 80:30062/TCP 9m50s
在浏览器访问http://k8s-master节点ip:30062 显示如下:
实验完成之后,把资源先删除,以免影响后面实验:
kubectl delete -f lan.yaml
kubectl delete -f lv.yaml
kubectl delete -f service_lanlv.yaml
100个pod:
更新了一个pod:用的新的代码做的镜像
99个pod:没有更新
打开一个标签1监测更新过程
kubectl apply -f lv.yaml
kubectl get pods -l app=myapp -n blue-green -w
NAME READY STATUS RESTARTS AGE
myapp-v1-75d7db5cf7-c7z5d 1/1 Running 0 5s
myapp-v1-75d7db5cf7-km5bg 1/1 Running 0 5s
myapp-v1-75d7db5cf7-p7c8j 1/1 Running 0 5s
打开另一个标签2执行如下操作
kubectl set image deployment myapp-v1 myapp=docker.io/xianchao/nginx:v1 -n blue-green && kubectl rollout pause deployment myapp-v1 -n blue-green
回到标签1观察,显示如下:
NAME READY STATUS RESTARTS AGE
myapp-v1-67fd9fc9c8-5fd2f 1/1 Running 0 86s
myapp-v1-67fd9fc9c8-92mdr 1/1 Running 0 86s
myapp-v1-75fb478d6c-wddds 0/1 Pending 0 0s
myapp-v1-75fb478d6c-wddds 0/1 Pending 0 0s
myapp-v1-75fb478d6c-wddds 0/1 ContainerCreating 0 0s
myapp-v1-75fb478d6c-wddds 0/1 ContainerCreating 0 1s
myapp-v1-75fb478d6c-wddds 1/1 Running 0 2s
注:上面的解释说明把myapp这个容器的镜像更新到docker.io/xianchao/nginx:v1版本 更新镜像之后,创建一个新的pod就立即暂停,这就是我们说的金丝雀发布;如果暂停几个小时之后没有问题,那么取消暂停,就会依次执行后面步骤,把所有pod都升级。
解除暂停:回到标签1继续观察:
打开标签2执行如下:
kubectl rollout resume deployment myapp-v1 -n blue-green
在标签1可以看到如下一些信息,下面过程是把余下的pod里的容器都更的版本:
NAME READY STATUS RESTARTS AGE
myapp-v1-67fd9fc9c8-5fd2f 1/1 Running 0 86s
myapp-v1-67fd9fc9c8-92mdr 1/1 Running 0 86s
myapp-v1-75fb478d6c-wddds 0/1 Pending 0 0s
myapp-v1-75fb478d6c-wddds 0/1 Pending 0 0s
myapp-v1-75fb478d6c-wddds 0/1 ContainerCreating 0 0s
myapp-v1-75fb478d6c-wddds 0/1 ContainerCreating 0 1s
myapp-v1-75fb478d6c-wddds 1/1 Running 0 2s
myapp-v1-67fd9fc9c8-92mdr 1/1 Terminating 0 10m
myapp-v1-75fb478d6c-z6f5z 0/1 Pending 0 0s
myapp-v1-75fb478d6c-z6f5z 0/1 Pending 0 0s
myapp-v1-75fb478d6c-z6f5z 0/1 ContainerCreating 0 0s
myapp-v1-75fb478d6c-z6f5z 0/1 ContainerCreating 0 1s
myapp-v1-67fd9fc9c8-92mdr 0/1 Terminating 0 10m
myapp-v1-75fb478d6c-z6f5z 1/1 Running 0 2s
myapp-v1-67fd9fc9c8-5fd2f 1/1 Terminating 0 10m
myapp-v1-67fd9fc9c8-5fd2f 0/1 Terminating 0 10m
myapp-v1-67fd9fc9c8-5fd2f 0/1 Terminating 0 10m
myapp-v1-67fd9fc9c8-5fd2f 0/1 Terminating 0 10m
myapp-v1-67fd9fc9c8-92mdr 0/1 Terminating 0 10m
kubectl get rs -n blue-green
可以看到replicaset控制器有2个了
NAME DESIRED CURRENT READY AGE
myapp-v1-67fd9fc9c8 0 0 0 13m
myapp-v1-75fb478d6c 2 2 2 7m28s