我们接着前面的文章说,如果不清楚的请查看之前的博文:http://blog.51cto.com/wzlinux/2322616
前面我们已经了解到,Kubernetes 通过各种 Controller 来管理 Pod 的生命周期。为了满足不同业务场景,Kubernetes 开发了 Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等多种 Controller。我们首先学习最常用的 Deployment。
1、运行 Deployment
先从例子开始,运行一个 Deployment:
kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2
上面的命令将部署包含两个副本的 Deployment nginx-deployment
,容器的 image 为 nginx:1.7.9
。
2、查看 Deployment(deploy)
查看刚刚创建的 deployment,其可以简写为deploy。
[root@master ~]# kubectl get deployNAME ??????????????DESIRED ??CURRENT ??UP-TO-DATE ??AVAILABLE ??AGEnginx-deployment ??2 ????????2 ????????2 ???????????2 ??????????4m56s
使用命令kubectl describe deploy
查看内部内容。
kubectl describe deploy nginx-deployment
Name: ??????????????????nginx-deploymentNamespace: ?????????????defaultCreationTimestamp: ?????Thu, 29 Nov 2018 17:47:16 +0800Labels: ????????????????run=nginx-deploymentAnnotations: ???????????deployment.kubernetes.io/revision: 1Selector: ??????????????run=nginx-deploymentReplicas: ??????????????2 desired | 2 updated | 2 total | 2 available | 0 unavailableStrategyType: ??????????RollingUpdateMinReadySeconds: ???????0RollingUpdateStrategy: ?25% max unavailable, 25% max surgePod Template: ?Labels: ?run=nginx-deployment ?Containers: ??nginx-deployment: ???Image: ???????nginx:1.7.9 ???Port: ????????<none> ???Host Port: ???<none> ???Environment: ?<none> ???Mounts: ??????<none> ?Volumes: ???????<none>Conditions: ?Type ??????????Status ?Reason ?---- ??????????------ ?------ ?Available ?????True ???MinimumReplicasAvailable ?Progressing ???True ???NewReplicaSetAvailableOldReplicaSets: ?<none>NewReplicaSet: ??nginx-deployment-5fd98dbf5f (2/2 replicas created)Events: ?Type ???Reason ????????????Age ???From ??????????????????Message ?---- ???------ ????????????---- ??---- ??????????????????------- ?Normal ?ScalingReplicaSet ?6m11s ?deployment-controller ?Scaled up replica set nginx-deployment-5fd98dbf5f to 2
展示的内容大部分都是描述信息,我们看最后一行,这里告诉我们创建了一个 ReplicaSet nginx-deployment-5fd98dbf5f
,Events 是 Deployment 的日志,记录了 ReplicaSet 的启动过程。
通过上面的分析,也验证了 Deployment 通过 ReplicaSet 来管理 Pod 的事实。
3、查看 ReplicaSet(rs)
查看我们有哪些 rs。
[root@master ~]# kubectl get rsNAME ?????????????????????????DESIRED ??CURRENT ??READY ??AGEnginx-deployment-5fd98dbf5f ??2 ????????2 ????????2 ??????12m
使用命令kubectl describe rs
查看其详细信息。
kubectl describe rs nginx-deployment-5fd98dbf5f
Name: ??????????nginx-deployment-5fd98dbf5fNamespace: ?????defaultSelector: ??????pod-template-hash=5fd98dbf5f,run=nginx-deploymentLabels: ????????pod-template-hash=5fd98dbf5f ???????????????run=nginx-deploymentAnnotations: ???deployment.kubernetes.io/desired-replicas: 2 ???????????????deployment.kubernetes.io/max-replicas: 3 ???????????????deployment.kubernetes.io/revision: 1Controlled By: ?Deployment/nginx-deploymentReplicas: ??????2 current / 2 desiredPods Status: ???2 Running / 0 Waiting / 0 Succeeded / 0 FailedPod Template: ?Labels: ?pod-template-hash=5fd98dbf5f ??????????run=nginx-deployment ?Containers: ??nginx-deployment: ???Image: ???????nginx:1.7.9 ???Port: ????????<none> ???Host Port: ???<none> ???Environment: ?<none> ???Mounts: ??????<none> ?Volumes: ???????<none>Events: ?Type ???Reason ???????????Age ??From ??????????????????Message ?---- ???------ ???????????---- ?---- ??????????????????------- ?Normal ?SuccessfulCreate ?13m ??replicaset-controller ?Created pod: nginx-deployment-5fd98dbf5f-8g7nm ?Normal ?SuccessfulCreate ?13m ??replicaset-controller ?Created pod: nginx-deployment-5fd98dbf5f-58c4z
我们可以看到Controlled By: ?Deployment/nginx-deployment
,说明此 ReplicaSet 由 Deployment nginx-deployment
。
在Events
记录了两个副本 Pod 的创建,那我们查看一下 Pod。
4、查看 Pod
查看目前的 Pod。
[root@master ~]# kubectl get podsNAME ???????????????????????????????READY ??STATUS ???RESTARTS ??AGEnginx-deployment-5fd98dbf5f-58c4z ??1/1 ????Running ??0 ?????????19mnginx-deployment-5fd98dbf5f-8g7nm ??1/1 ????Running ??0 ?????????19m
随便选择一个 Pod,查看其详细信息。
kubectl describe pod nginx-deployment-5fd98dbf5f-58c4z
Name: ??????????????nginx-deployment-5fd98dbf5f-58c4zNamespace: ?????????defaultPriority: ??????????0PriorityClassName: ?<none>Node: ??????????????node02.wzlinux.com/172.18.8.202Start Time: ????????Thu, 29 Nov 2018 17:47:16 +0800Labels: ????????????pod-template-hash=5fd98dbf5f ???????????????????run=nginx-deploymentAnnotations: ???????<none>Status: ????????????RunningIP: ????????????????10.244.2.3Controlled By: ?????ReplicaSet/nginx-deployment-5fd98dbf5fContainers: ?nginx-deployment: ???Container ID: ??docker://69fa73ed16d634627b69b8968915d9a5704f159206ac0d3b2f1179fa99acd56f ???Image: ?????????nginx:1.7.9 ???Image ID: ??????docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451 ???Port: ??????????<none> ???Host Port: ?????<none> ???State: ?????????Running ?????Started: ?????Thu, 29 Nov 2018 17:47:28 +0800 ???Ready: ?????????True ???Restart Count: ?0 ???Environment: ???<none> ???Mounts: ?????/var/run/secrets/kubernetes.io/serviceaccount from default-token-sm664 (ro)Conditions: ?Type ?????????????Status ?Initialized ??????True ??Ready ????????????True ??ContainersReady ??True ??PodScheduled ?????True Volumes: ?default-token-sm664: ???Type: ???????Secret (a volume populated by a Secret) ???SecretName: ?default-token-sm664 ???Optional: ???falseQoS Class: ??????BestEffortNode-Selectors: ?<none>Tolerations: ????node.kubernetes.io/not-ready:NoExecute for 300s ????????????????node.kubernetes.io/unreachable:NoExecute for 300sEvents: ?Type ???Reason ????Age ??From ????????????????????????Message ?---- ???------ ????---- ?---- ????????????????????????------- ?Normal ?Scheduled ?20m ??default-scheduler ???????????Successfully assigned default/nginx-deployment-5fd98dbf5f-58c4z to node02.wzlinux.com ?Normal ?Pulling ???20m ??kubelet, node02.wzlinux.com ?pulling image "nginx:1.7.9" ?Normal ?Pulled ????20m ??kubelet, node02.wzlinux.com ?Successfully pulled image "nginx:1.7.9" ?Normal ?Created ???20m ??kubelet, node02.wzlinux.com ?Created container ?Normal ?Started ???20m ??kubelet, node02.wzlinux.com ?Started container
我们可以看到Controlled By: ?????ReplicaSet/nginx-deployment-5fd98dbf5f
,说明此 Pod 是由 ReplicaSet nginx-deployment-5fd98dbf5f
创建的。Events
记录了 Pod 的启动过程。
5、总结
- 用户通过 kubectl 创建 Deployment。
- Deployment 创建 ReplicaSet。
- ReplicaSet 创建 Pod。
从上图也可以看出,对象的命名方式是:子对象的名字 = 父对象名字 + 随机字符串或数字。
Kubernetes Controller 之 Deployment 介绍
原文地址:http://blog.51cto.com/wzlinux/2323948