deployment 为Pod和RS提供了一个声明式定义方法,用来替代RC来方便管理应用,主要用于无状态服务的部署
定义Deployment来创建Pod 和Replicaset
滚动升级和回滚应用
扩容和缩容
暂停和继续Deployment
命令方式创建
$kubectl run nginx-deploy --image=nginx:1.8 --replicas=2
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/nginx-deploy created
将部署包含两个副本的 Deployment nginx-deploy,容器的 image 为 nginx:1.8。
注意:在K8S v1.18.0 以后,--replicas
已弃用 ,并且只创建一个nginx 容器实例,推荐用 deployment
创建 pods
HelloWorld
创建文件 nginx-deployment.yaml
apiVersion: apps/v1 #与k8s集群版本有关,使用 kubectl api-versions 即可查看当前集群支持的版本
kind: Deployment #该配置的类型, Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14
ports:
- containerPort: 80
参数说明
replicas:创建Pod的副本数。
selector:定义Deployment如何找到要管理的Pod,与template的label(标签)对应。
template字段包含以下字段:
app nginx使用label(标签)标记Pod。
spec表示Pod运行一个名字为nginx的容器。
image 运行此Pod使用的镜像。
Port容器用于发送和接收流量的端口
基础操作
启动deployment
✗ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
查看deployments
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 87s
状态解析
NAME:Deployment 名称
READY:pod的状态,已经READY的个数
UP-TO-DATE 已经达到预期的被更新的副本数量
AVAILABLE 已经可以用的副本数
AGE 显示应用程序运行的时间
或者使用缩写deploy
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 58s
查看ReplicaSet
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-8548f7fc4 2 2 2 2m9s
查看pod
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-8548f7fc4-8zs94 1/1 Running 0 2m22s
nginx-deployment-8548f7fc4-kzhkg 1/1 Running 0 2m22s
查看详细信息
$ kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Mon, 27 Jul 2020 15:05:03 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.14
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-8548f7fc4 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m2s deployment-controller Scaled up replica set nginx-deployment-8548f7fc4 to 2
Events 是 Deployment 的 日志,记录了 ReplicaSet 的启动过程。说明了 Deployment 通过 ReplicaSet 来管理 Pod 的事实
$ kubectl describe rs nginx-app-5f8c6846ff
Name: nginx-app-5f8c6846ff
Namespace: default
Selector: app=nginx,pod-template-hash=5f8c6846ff
Labels: app=nginx
pod-template-hash=5f8c6846ff
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-app
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
pod-template-hash=5f8c6846ff
Containers:
nginx:
Image: nginx:1.8
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 15m replicaset-controller Created pod: nginx-app-5f8c6846ff-ccxzs
Normal SuccessfulCreate 15m replicaset-controller Created pod: nginx-app-5f8c6846ff-bjmvp
Scaling/伸缩
伸缩 的实现可以通过更改.yaml 文件中部署的 replicas(副本数)来完成
apiVersion: apps/v1 #与k8s集群版本有关,使用 kubectl api-versions 即可查看当前集群支持的版本
kind: Deployment #该配置的类型, Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3 # Update the replicas from 2 to 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14
ports:
- containerPort: 80
查看pod
$ kubectl apply -f nginx-deployment-scale.yaml
deployment.apps/nginx-deployment configured
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-8548f7fc4-f6fdg 1/1 Running 0 8s
nginx-deployment-8548f7fc4-fj76q 1/1 Running 0 11s
nginx-deployment-8548f7fc4-tl2tt 1/1 Running 0 9s
Rolling Update/滚动更新
当且仅当Deployment的Pod模板(即.spec.template)更改时,才会触发Deployment更新,例如更新label(标签)或者容器的image(镜像)
升级Nginx版本号为1.8版本
apiVersion: apps/v1 #与k8s集群版本有关,使用 kubectl api-versions 即可查看当前集群支持的版本
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 80
升级
kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-767cbb69b8-cb48t 1/1 Running 0 26s
nginx-deployment-8548f7fc4-8zs94 1/1 Running 0 5m33s
nginx-deployment-8548f7fc4-kzhkg 1/1 Running 0 5m33s
使用kubectl rollout status查看更新状态:
更新注意事项
(1)清理策略在默认情况下,revision保留10个旧的ReplicaSet,其余的将在后台进行垃圾回收,可以在.spec.revisionHistoryLimit设置保留ReplicaSet的个数。当设置为0时,不保留历史记录。
(2)更新策略.spec.strategy.type==Recreate,表示重建,先删掉旧的Pod再创建新的Pod。.spec.strategy.type==RollingUpdate,表示滚动更新,可以指定maxUnavailable和maxSurge来控制滚动更新过程。 .spec.strategy.rollingUpdate.maxUnavailable,指定在回滚更新时最大不可用的Pod数量,可选字段,默认为25%,可以设置为数字或百分比,如果maxSurge为0,则该值不能为0。
spec.strategy.rollingUpdate.maxSurge可以超过期望值的最大Pod数,可选字段,默认为25%,可以设置成数字或百分比,如果maxUnavailable为0,则该值不能为0。
(3)Ready策略.spec.minReadySeconds是可选参数,指定新创建的Pod应该在没有任何容器崩溃的情况下视为Ready(就绪)状态的最小秒数,默认为0,即一旦被创建就视为可用,通常和容器探针连用。
spec.minReadySeconds 可选参数,制定新创建的Pod在没有任何容器奔溃的情况下视为Ready最小的秒数,默认是0,即一旦被创建就表示可用
matchExpressions
除了使用 matchLabels 属性外,还可以使用功能更加强大的 matchExpressions 属性,在这个属性中可以使用表达式,使标签匹配能够更加丰富
selector:
matchExpressions:
- key: app
operator: In
values:
- nginx
operator 运算符一般有 4 种:
- In: 标签的值必须与其中一个指定的 values 匹配
- NotIn: 标签的值必须与指定的 values 不匹配
- Exists: Pod 必须包含一个指定名称的标签(值不重要),这时候就不需要指定 values
- DoesNotExist: Pod 不能够包含有指定名称的标签,这时候也不需要指定 values
删除
$ kubectl delete deploy mysql
deployment.apps "mysql" deleted