deployment(部署)
部署应用的对象,一个 Deployment 控制器为 Pods 和 ReplicaSets 提供声明式的更新能力,也就是应用的滚动更新和回滚。也可以进行设定数。
deployment是一个控制器,用于部署无状态应用。
控制器用于管理pod, 通过标签来管理pod.
下面是 Deployment 示例。负责启动3个 nginx Pods deployment.yaml文件内容为
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.14.2
ports:
- containerPort: 80
创建deployment
kubectl apply -f deployment.yaml
查看deployments状态, 现在还没有Ready:
[root@zhang1 k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 0/3 3 0 27s
过了一段时间,查看deployments状态, 现在已经有两个Ready
[root@zhang1 k8s]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/3 3 2 5m13s
在检查集群中的 Deployment 时,所显示的字段有:
NAME 列出了集群中 Deployment 的名称。
READY 显示应用程序的可用的副本数。显示的模式是“就绪个数/期望个数”。
UP-TO-DATE
AVAILABLE 显示应用可供用户使用的副本数。
AGE 显示应用程序运行的时间。
查看部署状态
$ kubectl rollout status deployment.v1.apps/nginx-deployment
deployment "nginx-deployment" successfully rolled out
使用describe获取 Deployment 的更多信息
[root@zhang1 ]# kubectl describe deployments nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Mon, 09 Nov 2020 01:15:15 -0500
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 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.14.2
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-66b6c48dd5 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 34m deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 3
扩缩容
缩放 Deployment, 可以使用如下指令缩放 Deployment
$ kubectl scale deployment.v1.apps/nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled
更新(滚动升级)
会部署新的版本,待成功后再有序删除旧版本,命令如下
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1 --record
可以使用 kubectl get pod -o yaml来查看容器版本,看看是否升级ok。
涉及到以下几个命令
kubectl scale # 扩容
kubectl rollout status
kubectl rollout history #
kubectl rollout undo # 回到先前版本。