一、Deployment 定义

  1、简介

[root@k8smaster manifests]# kubectl explain deploy(也可以写作deployment)
KIND: Deployment
VERSION: extensions/v1beta1 #属于该群组,但是1.10包括1.11版本中其已经被摞到另外一个群组 apps/v1 版中,因此这个文档的版本是落后于我们k8s自身的。 DESCRIPTION:
DEPRECATED - This group version of Deployment is deprecated by
apps/v1beta2/Deployment. #此版本也是老版的,当前1.11版本为apps/v1 See the release notes for more information.
Deployment enables declarative updates for Pods and ReplicaSets. FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds metadata <Object>
Standard object metadata. spec <Object>
Specification of the desired behavior of the Deployment. status <Object>
Most recently observed status of the Deployment.

    deployment.spec 简介

[root@k8smaster manifests]# kubectl explain deployment.spec
KIND: Deployment
VERSION: extensions/v1beta1 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 (pod will be considered available as soon as it is ready) paused <boolean> #暂停,默认都是不暂停的上来就更新
Indicates that the deployment is paused and will not be processed by the
deployment controller. 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. This is not set by
default. replicas <integer>
Number of desired pods. This is a pointer to distinguish between explicit
zero and not specified. Defaults to . 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. rollbackTo <Object>
DEPRECATED. The config this deployment is rolling back to. Will be cleared
after rollback is done. selector <Object>
Label selector for pods. Existing ReplicaSets whose pods are selected by
this will be the ones affected by this deployment. 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.

    deployment.spec.strategy #更新策略

[root@k8smaster manifests]# kubectl explain deployment.spec.strategy
KIND: Deployment
VERSION: extensions/v1beta1 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,若使用RollingUpdate,还可以控制更新粒度

    deployment.spec.strategy.rollingUpdate  #更新粒度

[root@k8smaster manifests]# kubectl explain deployment.spec.strategy.rollingUpdate
KIND: Deployment
VERSION: extensions/v1beta1 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: ) or a percentage of desired
pods (ex: %). This can not be if MaxUnavailable is . Absolute number
is calculated from percentage by rounding up. By default, a value of is
used. Example: when this is set to %, the new RC can be scaled up
immediately when the rolling update starts, such that the total number of
old and new pods do not exceed % of desired pods. Once old pods have
been killed, new RC can be scaled up further, ensuring that total number of
pods running at any time during the update is atmost % of desired pods. maxUnavailable <string>#表示最多有几个不可用,和上述一样也是两种取值方式
The maximum number of pods that can be unavailable during the update. Value
can be an absolute number (ex: ) or a percentage of desired pods (ex:
%). Absolute number is calculated from percentage by rounding down. This
can not be if MaxSurge is . By default, a fixed value of is used.
Example: when this is set to %, the old RC can be scaled down to % of
desired pods immediately when the rolling update starts. Once new pods are
ready, old RC can be scaled down further, followed by scaling up the new
RC, ensuring that the total number of pods available at all times during
the update is at least % of desired pods.

  2、deployment具体操作

[root@k8smaster manifests]# cat deploy-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas:
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: [root@k8smaster manifests]# kubectl get deployment -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
myapp-deploy 1m myapp ikubernetes/myapp:v1 app=myapp,release=canary
[root@k8smaster manifests]# kubectl get rs -o wide #会发现自动创建了rs
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-69b47bc96d #后面的数值为模板的哈希值 1m myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=,release=canary [root@k8smaster manifests]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
liveness-httpget-pod / Running 1d 10.244.2.18 k8snode2 <none>
myapp-deploy-69b47bc96d-47b9t #会发现pod命名由rs + 后面一串随机字符组成 / Running 5m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Running 5m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
poststart-pod / Running 1d 10.244.2.21 k8snode2 <none>
readiness-httpget-pod / Running 1d 10.244.2.19 k8snode2 <none>

    a、在更新应用时可以直接通过编辑配置文件来实现,编辑后再次执行apply -f  deploy-demo.yaml即可,但是create就不行了。其原理为每一次变化其将同步至api server中,api server发现与etcd中的状态不同从而改变etcd从而实现修改他的期望状态。从而将现有状态不断逼近期望状态。

[root@k8smaster manifests]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
liveness-httpget-pod / Running 1d 10.244.2.18 k8snode2 <none>
myapp-deploy-69b47bc96d-47b9t / Running 5m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Running 5m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
poststart-pod / Running 1d 10.244.2.21 k8snode2 <none>
readiness-httpget-pod / Running 1d 10.244.2.19 k8snode2 <none>
[root@k8smaster manifests]# sed -i 's/replicas: 2/replicas: 3/g' deploy-demo.yaml
[root@k8smaster manifests]# kubectl apply -f deploy-demo.yaml
deployment.apps/myapp-deploy configured
[root@k8smaster manifests]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
liveness-httpget-pod / Running 1d 10.244.2.18 k8snode2 <none>
myapp-deploy-69b47bc96d-47b9t / Running 15m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Running 16s 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Running 15m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
poststart-pod / Running 1d 10.244.2.21 k8snode2 <none>
readiness-httpget-pod / Running 1d 10.244.2.19 k8snode2 <none>
[root@k8smaster manifests]# kubectl describe deploy myapp-deploy
Name: myapp-deploy
Namespace: default
CreationTimestamp: Thu, May :: +
Labels: <none>
Annotations(注释): deployment.kubernetes.io/revision=1 #我们每一次apply版本发生变化时的信息都会被保存在其中。
kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"myapp-deploy","namespace":"default"
},"spec":{"replicas":,"selector":{...Selector: app=myapp,release=canary
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate #默认的更新策略为滚动更新
MinReadySeconds:
RollingUpdateStrategy: % max unavailable, % max surge #滚动更新的中心策略是最多不可用25%,最多多出25%
Pod Template:
Labels: app=myapp
release=canary
Containers:
myapp:
Image: ikubernetes/myapp:v1
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: myapp-deploy-69b47bc96d (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 18m deployment-controller Scaled up replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set myapp-deploy-69b47bc96d to

  3、滚动更新

    a、直接修改yaml文件后再执行 kubectl apply -f deploy-demo.yaml

[root@k8smaster manifests]# sed -i 's#image: ikubernetes/myapp:v1#image: ikubernetes/myapp:v2#g' deploy-demo.yaml 

[root@k8smaster manifests]# kubectl apply -f deploy-demo.yaml
deployment.apps/myapp-deploy configured #另起一个窗口动态监测更新过程
[root@k8smaster manifests]# kubectl get pods -o wide --show-labels -l app=myapp -w
NAME READY STATUS RESTARTS AGE IP NODE LABELS
myapp-deploy-69b47bc96d-47b9t / Running 24m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Running 8m 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Running 24m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-6hbjh / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-6hbjh / Pending 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-6hbjh / ContainerCreating 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-6hbjh / Running 1s 10.244.1.31 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Terminating 14m 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Pending 0s <none> k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / ContainerCreating 0s <none> k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Terminating 14m 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Running 2s 10.244.2.25 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-47b9t / Terminating 30m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Pending 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / ContainerCreating 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Terminating 14m 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-75jgw / Terminating 14m 10.244.1.30 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Running 1s 10.244.1.32 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-47b9t / Terminating 30m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Terminating 30m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Terminating 30m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-47b9t / Terminating 30m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-47b9t / Terminating 30m 10.244.1.29 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Terminating 30m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d-bnqk9 / Terminating 30m 10.244.2.24 k8snode2 app=myapp,pod-template-hash=,release=canary [root@k8smaster manifests]# kubectl get rs -o wide #发现老版本rs依然存在,只是运行的pod为0
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-67f6f6b4dc 2m myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d 33m myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=,release=canary #查看deployment的滚动历史
[root@k8smaster manifests]# kubectl rollout history deployment myapp-deploy
deployments "myapp-deploy"
REVISION CHANGE-CAUSE
<none>
<none>

    b、通过patch命令,其补的一定是对象的json格式的内容

[root@k8smaster manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}'
deployment.extensions/myapp-deploy patched
[root@k8smaster manifests]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE LABELS
liveness-httpget-pod / Running 1d 10.244.2.18 k8snode2 <none>
myapp-deploy-67f6f6b4dc-6hbjh / Running 14m 10.244.1.31 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / Running 36s 10.244.1.33 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Running 14m 10.244.2.25 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Running 14m 10.244.1.32 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / Running 36s 10.244.2.26 k8snode2 app=myapp,pod-template-hash=,release=canary
poststart-pod / Running 1d 10.244.2.21 k8snode2 <none>
readiness-httpget-pod / Running 1d 10.244.2.19 k8snode2 <none> #另起一个shell查看更新过程
[root@k8smaster manifests]# kubectl get pods -o wide --show-labels -l app=myapp -w
NAME READY STATUS RESTARTS AGE IP NODE LABELS
myapp-deploy-67f6f6b4dc-6hbjh / Running 11m 10.244.1.31 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Running 11m 10.244.2.25 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Running 11m 10.244.1.32 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / Pending 0s <none> k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / Pending 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / ContainerCreating 0s <none> k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / ContainerCreating 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / Running 2s 10.244.2.26 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / Running 2s 10.244.1.33 k8snode1 app=myapp,pod-template-hash=,release=canary

    c、设置更新过程中最多超出1个,最多0个不可用

[root@k8smaster manifests]# kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
deployment.extensions/myapp-deploy patched
[root@k8smaster manifests]# kubectl edit deployment myapp-deploy #可以进入看到更新后的属性
Edit cancelled, no changes made.
[root@k8smaster manifests]# kubectl describe deployment myapp-deploy
Name: myapp-deploy
Namespace: default
CreationTimestamp: Thu, May :: +
Labels: app=myapp
release=canary
Annotations: deployment.kubernetes.io/revision=
kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"myapp-deploy","namespace":"default"
},"spec":{"replicas":,"selector":{...Selector: app=myapp,release=canary
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: max unavailable, max surge
Pod Template:
Labels: app=myapp
release=canary
Containers:
myapp:
Image: ikubernetes/myapp:v2
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: myapp-deploy-67f6f6b4dc (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 56m deployment-controller Scaled up replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 40m deployment-controller Scaled up replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 26m deployment-controller Scaled up replica set myapp-deploy-67f6f6b4dc to
Normal ScalingReplicaSet 26m deployment-controller Scaled down replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 26m deployment-controller Scaled up replica set myapp-deploy-67f6f6b4dc to
Normal ScalingReplicaSet 26m deployment-controller Scaled down replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 26m deployment-controller Scaled up replica set myapp-deploy-67f6f6b4dc to
Normal ScalingReplicaSet 26m deployment-controller Scaled down replica set myapp-deploy-69b47bc96d to
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set myapp-deploy-67f6f6b4dc to

    d、通过使用set image命令

[root@k8smaster manifests]# kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause  #设置后立马暂停deployment myapp-deploy,这样就只会更新一个镜像为v3的pod。(金丝雀发布)
deployment.extensions/myapp-deploy image updated
deployment.extensions/myapp-deploy paused #另起一个窗口观察
[root@k8smaster manifests]# kubectl get pods -o wide --show-labels -l app=myapp -w
NAME READY STATUS RESTARTS AGE IP NODE LABELS
myapp-deploy-67f6f6b4dc-6hbjh / Running 23m 10.244.1.31 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-9hhb4 / Running 9m 10.244.1.33 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-bqtpz / Running 23m 10.244.2.25 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-xb6qw / Running 23m 10.244.1.32 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-67f6f6b4dc-z54x6 / Running 9m 10.244.2.26 k8snode2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / Pending 0s <none> <none> app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / Pending 0s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / ContainerCreating 1s <none> k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / ErrImagePull 2s 10.244.1.34 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / ImagePullBackOff 3s 10.244.1.34 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / ErrImagePull 29s 10.244.1.34 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / ImagePullBackOff 39s 10.244.1.34 k8snode1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d-n4qbt / Running 46s 10.244.1.34 k8snode1 app=myapp,pod-template-hash=,release=canary
[root@k8smaster manifests]# kubectl rollout status deployment myapp-deploy  #监视更新变化目前显示一个被更新
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated... #当确定新版本使用没问题时,解除暂停更新的操作继续更新
[root@k8smaster ~]# kubectl rollout resume deployment myapp-deploy
deployment.extensions/myapp-deploy resumed #另一个窗口查看更新情况
[root@k8smaster manifests]# kubectl rollout status deployment myapp-deploy
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment spec update to be observed...
Waiting for deployment spec update to be observed...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: out of new replicas have been updated...
Waiting for deployment "myapp-deploy" rollout to finish: old replicas are pending termination...
Waiting for deployment "myapp-deploy" rollout to finish: old replicas are pending termination...
deployment "myapp-deploy" successfully rolled out
[root@k8smaster manifests]# kubectl get rs -o wide #此时查看rs版本
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-67f6f6b4dc 51m myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d 1h myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d 17m myapp ikubernetes/myapp:v3 app=myapp,pod-template-hash=,release=canary [root@k8smaster manifests]# kubectl rollout history deployment myapp-deploy #查看历史更新版本
deployments "myapp-deploy"
REVISION CHANGE-CAUSE
<none>
<none>
<none>

  4、回滚(可在上面的kubectl中加入--record=true参数,这样CHANG-CAUSE就可以记录当前版本中所进行的操作了。)

[root@k8smaster ~]# kubectl rollout history deployment myapp-deploy #查看当前版本
deployments "myapp-deploy"
REVISION CHANGE-CAUSE
<none>
<none>
<none> [root@k8smaster ~]# kubectl rollout undo deployment myapp-deploy --to-revision= #回滚到版本1
deployment.extensions/myapp-deploy
[root@k8smaster ~]# kubectl rollout history deployment myapp-deploy #查看回滚后的版本,当前版本变为了4,前一个为版本3
deployments "myapp-deploy"
REVISION CHANGE-CAUSE
<none>
<none>
<none> [root@k8smaster ~]# kubectl get rs -o wide #可以看到当前pod运行的镜像为v1
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy-67f6f6b4dc 1h myapp ikubernetes/myapp:v2 app=myapp,pod-template-hash=,release=canary
myapp-deploy-69b47bc96d 1h myapp ikubernetes/myapp:v1 app=myapp,pod-template-hash=,release=canary
myapp-deploy-6bdcd6755d 27m myapp ikubernetes/myapp:v3 app=myapp,pod-template-hash=,release=canary

二、DaemonSet,其作用在于,在整个集群的每一个节点上只运行某个指定pod的一个并且只能是一个副本,或者只能是在集群中某些符合选择器的节点上每一个节点只运行一个指定的副本。用于实现系统级的管理功能,可以直接把节点上的某个目录作为存储卷关联至pod中让pod实现某些管理功能。比如EFK中的Filebeat

  1、现在启动一个redis并通过filebeat收集日志

[root@k8smaster manifests]# cat ds-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
namespace: default
spec:
replicas:
selector:
matchLabels:
app: redis
role: logstor
template:
metadata:
labels:
app: redis
role: logstor
spec:
containers:
- name: redis
image: redis:4.0-alpine
ports:
- name: redis
containerPort: ---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat-ds
namespace: default
spec:
selector:
matchLabels:
app: filebeat
release: stable
template:
metadata:
labels:
app: filebeat
release: stable
spec:
containers:
- name: filebeat
image: ikubernetes/filebeat:5.6.-alpine #该镜像有个要求就是其内部必须要使用环境变量,该环境变量必须要指明我们redis主机和redis日志级别是什么
env:
- name: REDIS_HOST
value: redis.default.svc.cluster.local #这是一个域名,redis(服务名).default(名称空间).svc.cluster.local(这是本地k8s集群内建的本地域名后缀)
- name: REDIS_LOG_LEVEL
value: info [root@k8smaster manifests]# kubectl expose deployment redis --port= #通过命名创建service
service/redis exposed
[root@k8smaster manifests]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> /TCP 8d
myapp NodePort 10.106.171.207 <none> :/TCP 7d
nginx ClusterIP 10.103.127.92 <none> /TCP 8d
redis ClusterIP 10.99.71.69 <none> /TCP 6s

    a、各pod之间的调用方式为通过service的主机名,而且调用时传递给调用者的方法为使用环境变量来进行配置。

    b、各filebeat一定是各自运行在一个节点上

[root@k8smaster manifests]# kubectl get pods -l app=filebeat -o wide
NAME READY STATUS RESTARTS AGE IP NODE
filebeat-ds-2dh99 / Running 19m 10.244.2.32 k8snode2
filebeat-ds-vbsmg / Running 19m 10.244.1.42 k8snode1

    c、ds也支持滚动更新

    d、通过set image更新镜像

[root@k8smaster manifests]# kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.6.-alpine
daemonset.extensions/filebeat-ds image updated
[root@k8smaster manifests]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
filebeat-ds-2dh99 / Running 25m
filebeat-ds-f5drs / ContainerCreating 11s
liveness-httpget-pod / Running 3d
myapp-deploy-69b47bc96d-6x987 / Running 1d
myapp-deploy-69b47bc96d-f2cjq / Running 1d
myapp-deploy-69b47bc96d-tlq6v / Running 1d
myapp-deploy-69b47bc96d-vx46z / Running 1d
myapp-deploy-69b47bc96d-vzdpt / Running 1d
poststart-pod / Running 2d
readiness-httpget-pod / Running 2d
redis-5b5d6fbbbd-kk782 / Running 28m
filebeat-ds-f5drs / Running 30s
filebeat-ds-2dh99 / Terminating 26m
filebeat-ds-2dh99 / Terminating 26m
filebeat-ds-2dh99 / Terminating 26m
filebeat-ds-2dh99 / Terminating 26m
filebeat-ds-n9hgz / Pending 0s
filebeat-ds-n9hgz / ContainerCreating 0s
filebeat-ds-n9hgz / Running 34s

    e、ds中的pod可以设置共享宿主机的名称空间(hostNetwork),IPC,PID等

[root@k8smaster manifests]# kubectl explain pods.spec
KIND: Pod
VERSION: v1 RESOURCE: spec <Object> DESCRIPTION:
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status PodSpec is a description of a pod. FIELDS:
activeDeadlineSeconds <integer>
Optional duration in seconds the pod may be active on the node relative to
StartTime before the system will actively try to mark it failed and kill
associated containers. Value must be a positive integer. affinity <Object>
If specified, the pod's scheduling constraints automountServiceAccountToken <boolean>
AutomountServiceAccountToken indicates whether a service account token
should be automatically mounted. containers <[]Object> -required-
List of containers belonging to the pod. Containers cannot currently be
added or removed. There must be at least one container in a Pod. Cannot be
updated. dnsConfig <Object>
Specifies the DNS parameters of a pod. Parameters specified here will be
merged to the generated DNS configuration based on DNSPolicy. dnsPolicy <string>
Set DNS policy for the pod. Defaults to "ClusterFirst". Valid values are
'ClusterFirstWithHostNet', 'ClusterFirst', 'Default' or 'None'. DNS
parameters given in DNSConfig will be merged with the policy selected with
DNSPolicy. To have DNS options set along with hostNetwork, you have to
specify DNS policy explicitly to 'ClusterFirstWithHostNet'. hostAliases <[]Object>
HostAliases is an optional list of hosts and IPs that will be injected into
the pod's hosts file if specified. This is only valid for non-hostNetwork
pods. hostIPC <boolean> #共享宿主机的IPC
Use the host's ipc namespace. Optional: Default to false. hostNetwork <boolean> #共享宿主机的网络名称空间
Host networking requested for this pod. Use the host's network namespace.
If this option is set, the ports that will be used must be specified.
Default to false. hostPID <boolean> #共享宿主机的PID
Use the host's pid namespace. Optional: Default to false. hostname <string>
Specifies the hostname of the Pod If not specified, the pod's hostname will
be set to a system-defined value. imagePullSecrets <[]Object>
ImagePullSecrets is an optional list of references to secrets in the same
namespace to use for pulling any of the images used by this PodSpec. If
specified, these secrets will be passed to individual puller
implementations for them to use. For example, in the case of docker, only
DockerConfig type secrets are honored. More info:
https://kubernetes.io/docs/concepts/containers/images#specifying-imagepullsecrets-on-a-pod initContainers <[]Object>
List of initialization containers belonging to the pod. Init containers are
executed in order prior to containers being started. If any init container
fails, the pod is considered to have failed and is handled according to its
restartPolicy. The name for an init container or normal container must be
unique among all containers. Init containers may not have Lifecycle
actions, Readiness probes, or Liveness probes. The resourceRequirements of
an init container are taken into account during scheduling by finding the
highest request/limit for each resource type, and then using the max of of
that value or the sum of the normal containers. Limits are applied to init
containers in a similar fashion. Init containers cannot currently be added
or removed. Cannot be updated. More info:
https://kubernetes.io/docs/concepts/workloads/pods/init-containers/ nodeName <string>
NodeName is a request to schedule this pod onto a specific node. If it is
non-empty, the scheduler simply schedules this pod onto that node, assuming
that it fits resource requirements. nodeSelector <map[string]string>
NodeSelector is a selector which must be true for the pod to fit on a node.
Selector which must match a node's labels for the pod to be scheduled on
that node. More info:
https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ priority <integer>
The priority value. Various system components use this field to find the
priority of the pod. When Priority Admission Controller is enabled, it
prevents users from setting this field. The admission controller populates
this field from PriorityClassName. The higher the value, the higher the
priority. priorityClassName <string>
If specified, indicates the pod's priority. "system-node-critical" and
"system-cluster-critical" are two special keywords which indicate the
highest priorities with the former being the highest priority. Any other
name must be defined by creating a PriorityClass object with that name. If
not specified, the pod priority will be default or zero if there is no
default. readinessGates <[]Object>
If specified, all readiness gates will be evaluated for pod readiness. A
pod is ready when all its containers are ready AND all conditions specified
in the readiness gates have status equal to "True" More info:
https://github.com/kubernetes/community/blob/master/keps/sig-network/0007-pod-ready%2B%2B.md restartPolicy <string>
Restart policy for all containers within the pod. One of Always, OnFailure,
Never. Default to Always. More info:
https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy schedulerName <string>
If specified, the pod will be dispatched by specified scheduler. If not
specified, the pod will be dispatched by default scheduler. securityContext <Object>
SecurityContext holds pod-level security attributes and common container
settings. Optional: Defaults to empty. See type description for default
values of each field. serviceAccount <string>
DeprecatedServiceAccount is a depreciated alias for ServiceAccountName.
Deprecated: Use serviceAccountName instead. serviceAccountName <string>
ServiceAccountName is the name of the ServiceAccount to use to run this
pod. More info:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/ shareProcessNamespace <boolean>
Share a single process namespace between all of the containers in a pod.
When this is set containers will be able to view and signal processes from
other containers in the same pod, and the first process in each container
will not be assigned PID . HostPID and ShareProcessNamespace cannot both
be set. Optional: Default to false. This field is alpha-level and is
honored only by servers that enable the PodShareProcessNamespace feature. subdomain <string>
If specified, the fully qualified Pod hostname will be
"<hostname>.<subdomain>.<pod namespace>.svc.<cluster domain>". If not
specified, the pod will not have a domainname at all. terminationGracePeriodSeconds <integer>
Optional duration in seconds the pod needs to terminate gracefully. May be
decreased in delete request. Value must be non-negative integer. The value
zero indicates delete immediately. If this value is nil, the default grace
period will be used instead. The grace period is the duration in seconds
after the processes running in the pod are sent a termination signal and
the time when the processes are forcibly halted with a kill signal. Set
this value longer than the expected cleanup time for your process. Defaults
to seconds. tolerations <[]Object>
If specified, the pod's tolerations. volumes <[]Object>
List of volumes that can be mounted by containers belonging to the pod.
More info: https://kubernetes.io/docs/concepts/storage/volumes

Kubernetes 学习9 Pod控制器的更多相关文章

  1. Kubernetes 学习8 Pod控制器

    一.回顾 1.Pod是标准的kubernetes资源,因此其遵循为其资源清单配置定义的基本格式,包含:apiVersion,kind,metadata,spec,status(只读) 2.spec的内 ...

  2. Kubernetes 学习6 Pod控制器应用进阶

    一.资源配置清单 1.自主式Pod资源 2.资源的清单格式,大多数清单格式都遵循如下条件: a.一级字段:apiVersion(group/version),kind,metadata(name,na ...

  3. Kubernetes 学习7 Pod控制器应用进阶2

    一.容器探测器 1.所谓的容器探测无非就是我们在里面设置了一些探针,或者称之为传感器来获取相应的数据作为判定其存活与否或就绪与否的标准,目前k8s所支持的存活性和就绪性探测方式都是一样的. 2.k8s ...

  4. kubernetes系列07—Pod控制器详解

    本文收录在容器技术学习系列文章总目录 1.Pod控制器 1.1 介绍 Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无 ...

  5. 【一】kubernetes学习笔记-Pod概念

    一.Pod 控制器类型 Pod概念 当一个 Pod 创建后,Pause 容器就会随着 Pod 启动,只要是有 Pod,Pause 容器就要被启动. 在同一个 Pod 里面的容器不能出现端口冲突,否则这 ...

  6. 【三】Kubernetes学习笔记-Pod 生命周期与 Init C 介绍

    一.容器生命周期 Init C(初始化容器)只是用于 Pod 初始化的,不会一直随着 Pod 生命周期存在,Init C 在初始化完成之后就会死亡. 一个 Pod 可以有多个 Init C,也可以不需 ...

  7. Kubernetes学习之路(十二)之Pod控制器--ReplicaSet、Deployment

    一.Pod控制器及其功用 Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无效,则会重新新建pod的资源. pod控制器 ...

  8. k8s学习笔记之六:Pod控制器(kube-controller-manager)

    第一章.什么是kube-controller-manager? Controller Manager 由 kube-controller-manager 和 cloud-controller-mana ...

  9. kubernetes 的pod控制器

    转载于网络   pod是kubernetes的最小单元,自主式创建的pod删除就没有了,但是通过资源控制器创建的pod如果删除还会重建.pod控制器就是用于实现代替我们去管理pod的中间层,并帮我们确 ...

随机推荐

  1. Redis学习笔记(一)— 基本命令和数据类型

    MacOs环境 Redis基本命令 启动服务:redis-server 连接服务:redis-cli -h 指定主机/IP -p 指定端口 -a 指定密码 关闭服务:先shutdown 保存数据并关闭 ...

  2. vue --- axios拦截器+form格式请求体

    在vue2.x中使用CLI生成的模板有很大改变,需要自己手动在main.ts同级目录下新建interceptors.ts interceptors.ts import axios from 'axio ...

  3. Spring MVC前端控制器不拦截静态资源配置

  4. nginx.conf指令详解

    #redis.conf # Redis configuration file example. # ./redis-server /path/to/redis.conf ############### ...

  5. oralce数据表空间满了

    --切换至oralce用户 su - oracle--执行sqlplus / as sysdba --查询表空间使用情况SELECT Upper(F.TABLESPACE_NAME) "表空 ...

  6. 扩展JS

    //JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展            var aClass = function(){} //1 定义这个类的静态方法            aC ...

  7. 10 查询字符串,X字段必须包含(不包含)XX;_all原理

    指定某个字段,必须要包含XX字符 GET /beauties/my/_search?q=Name:Chang Wei   搜出 某个字段不包含XX字符 的所有内容 GET /beauties/my/_ ...

  8. vue中如何判断checkbox是否选中

    console.log(event.target.checked)     例:  

  9. Node学习之(第三章:仿Apache显示目录列表的功能)

    前言 今天咱们用Node.js中的核心模块以及上节学习的模板引擎art-template来实现服务器软件Apache的大体功能.用过Apache的朋友都知道,我们只需把本地文件放置在Apache的ww ...

  10. 怎么让自己的本地php网站让别人访问到

    怎么样才能把本地的web网站项目让别人访问到呢?我来给分享一下. 第一:下载jnat工具: 第二:注册一个key; 第三:jnat工具初始化(一键注册本地的Apache+PHP环境): 第四:在jna ...