参考:https://jimmysong.io/kubernetes-handbook/concepts/deployment.html
首先创建pod,我这有现成的,如果没有参考链接创建pod。
[root@master01 ~]# kubectl get deployment product-htjf-login-web-profile-a
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
product-htjf-login-web-profile-a 2 2 2 2 5h
[root@master01 ~]# kubectl get pod product-htjf-login-web-profile-a-586bf5dc8-nltkh -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: 2019-04-22T07:42:22Z
generateName: product-htjf-login-web-profile-a-586bf5dc8-
labels:
app: product-htjf-login-web
pod-template-hash: "142691874"
profile: profile-a
name: product-htjf-login-web-profile-a-586bf5dc8-nltkh
namespace: default
ownerReferences:
- apiVersion: extensions/v1beta1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: product-htjf-login-web-profile-a-586bf5dc8
uid: 103f4cd6-64c5-11e9-b41c-005056b678c1
resourceVersion: "43102672"
selfLink: /api/v1/namespaces/default/pods/product-htjf-login-web-profile-a-586bf5dc8-nltkh
uid: 2a3b5374-64d2-11e9-b41c-005056b678c1
spec:
containers:
- image: harbor.reg/xx/product-htjf-login-web:9 ##目前的版本是9
imagePullPolicy: IfNotPresent
name: product-htjf-login-web-profile-a
ports:
- containerPort: 8080
protocol: TCP
resources:
limits:
memory: 2000Mi
requests:
memory: 1500Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: xxx
name: cephfsmount
- mountPath: xxx
name: productmfs
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-mqtdl
readOnly: true
dnsPolicy: ClusterFirst
nodeName: node01
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
volumes:
- cephfs:
monitors:
- 10.100.101.xxx:16789
- 10.100.101.xxx:16789
- 10.100.101.xxx:16789
secretFile: /etc/ceph/admin.secret
user: admin
name: cephfsmount
- hostPath:
path: xxx
type: ""
name: productmfs
- name: default-token-mqtdl
secret:
defaultMode: 420
secretName: default-token-mqtdl
status:
conditions:
- lastProbeTime: null
lastTransitionTime: 2019-04-22T07:42:22Z
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: 2019-04-22T07:42:24Z
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: 2019-04-22T07:42:22Z
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://89f1004f5bc58b04492bca24b107c952fb3143d613da043ecede897ba063863f
image: harbor.reg/pro_jinfu/product-htjf-login-web:10
imageID: docker-pullable://harbor.reg/pro_jinfu/product-htjf-login-web@sha256:d88054e3836f172991134561af06c2f66ea6889f40b69c9b824c40a222ae4be4
lastState: {}
name: product-htjf-login-web-profile-a
ready: true
restartCount: 0
state:
running:
startedAt: 2019-04-22T07:42:23Z
hostIP: 10.100.10.190
phase: Running
podIP: 10.2.79.9
qosClass: Burstable
startTime: 2019-04-22T07:42:22Z
可以看到pod当前的版本是9
升级pod
kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:10 --record=true
[root@master01 ~]# kubectl rollout history deployment product-htjf-login-web-profile-a
deployments "product-htjf-login-web-profile-a"
REVISION CHANGE-CAUSE
3 kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:10 --record=true
4
因为我们创建 Deployment 的时候使用了–record参数可以记录命令,我们可以很方便的查看每次 revision 的变化。
查看pod的总体描述
[root@master01 ~]# kubectl rollout history deployment product-htjf-login-web-profile-a
deployments "product-htjf-login-web-profile-a"
REVISION CHANGE-CAUSE
3 kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:9 --record=true
4 kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:10 --record=true
[root@master01 ~]# kubectl describe deployment product-htjf-login-web-profile-a
Name: product-htjf-login-web-profile-a
Namespace: default
CreationTimestamp: Mon, 22 Apr 2019 10:59:02 +0800
Labels: app=product-htjf-login-web
profile=profile-a
Annotations: deployment.kubernetes.io/revision=4
kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"product-htjf-login-web-profile-a","namespace":"default"},"s...
kubernetes.io/change-cause=kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:10 --record=true
Selector: app=product-htjf-login-web,profile=profile-a
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: app=product-htjf-login-web
profile=profile-a
Containers:
product-htjf-login-web-profile-a:
Image: harbor.reg/pro_jinfu/product-htjf-login-web:10
Port: 8080/TCP
Limits:
memory: 2000Mi
Requests:
memory: 1500Mi
Environment: <none>
Mounts:
/data/WEBLOG from cephfsmount (rw)
/home/nfs from productmfs (rw)
Volumes:
cephfsmount:
Type: CephFS (a CephFS mount on the host that shares a pod's lifetime)
Monitors: [10.100.xx:16789 10.100.xx:16789 10.100.xx:16789]
Path:
User: admin
SecretFile: /xxx/admin.secret
SecretRef: <nil>
ReadOnly: false
productmfs:
Type: HostPath (bare host directory volume)
Path: xxx
HostPathType:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: product-htjf-login-web-profile-a-586bf5dc8 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 36m (x2 over 2h) deployment-controller Scaled up replica set product-htjf-login-web-profile-a-586bf5dc8 to 1
Normal ScalingReplicaSet 36m (x2 over 2h) deployment-controller Scaled down replica set product-htjf-login-web-profile-a-7c7dcbdd94 to 1
Normal ScalingReplicaSet 36m (x2 over 2h) deployment-controller Scaled up replica set product-htjf-login-web-profile-a-586bf5dc8 to 2
Normal DeploymentRollback 36m deployment-controller Rolled back deployment "product-htjf-login-web-profile-a" to revision 2
Normal ScalingReplicaSet 36m (x2 over 2h) deployment-controller Scaled down replica set product-htjf-login-web-profile-a-7c7dcbdd94 to 0
Image: harbor.reg/pro_jinfu/product-htjf-login-web:10
已经升级到10版本了
查看状态和历史升级的版本
kubectl rollout status deployment product-htjf-login-web-profile-a
[root@master01 ~]# kubectl rollout history deployment product-htjf-login-web-profile-a
deployments "product-htjf-login-web-profile-a"
REVISION CHANGE-CAUSE
3 kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:9 --record=true
4 kubectl set image deployment/product-htjf-login-web-profile-a product-htjf-login-web-profile-a=harbor.reg/xxx/product-htjf-login-web:10 --record=true
回退到历史版本
回退到上一个版本
kubectl rollout undo deployment product-htjf-login-web-profile-a
回退到指定版本(指定版本这里指history版本号 查看上面的history 可以回滚3或者4)
kubectl rollout undo deployment product-htjf-login-web-profile-b --to-revision=4
可以通过设置.spec.revisonHistoryLimit项来指定 deployment 最多保留多少 revision 历史记录。默认的会保留所有的 revision;如果将该项设置为0,Deployment就不允许回退了。
spec:
minReadySeconds: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
replicas: 2
revisionHistoryLimit: 10
template:
自动扩容
可以使用以下命令扩容 Deployment:
$ kubectl scale deployment nginx-deployment --replicas 10
deployment "nginx-deployment" scaled
假设您的集群中启用了horizontal pod autoscaling,您可以给 Deployment 设置一个 autoscaler,基于当前 Pod的 CPU 利用率选择最少和最多的 Pod 数。
$ kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
deployment "nginx-deployment" autoscaled