一、Deployment
我们接着前面的文章说,如果不清楚的请查看之前的博文: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。
[[email protected] ~]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 2 2 2 2 4m56s
使用命令kubectl describe deploy
查看内部内容。
kubectl describe deploy nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Thu, 29 Nov 2018 17:47:16 +0800
Labels: run=nginx-deployment
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx-deployment
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: 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 NewReplicaSetAvailable
OldReplicaSets: <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。
[[email protected] ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-5fd98dbf5f 2 2 2 12m
使用命令kubectl describe rs
查看其详细信息。
kubectl describe rs nginx-deployment-5fd98dbf5f
Name: nginx-deployment-5fd98dbf5f
Namespace: default
Selector: pod-template-hash=5fd98dbf5f,run=nginx-deployment
Labels: pod-template-hash=5fd98dbf5f
run=nginx-deployment
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-deployment
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod 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。
[[email protected] ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5fd98dbf5f-58c4z 1/1 Running 0 19m
nginx-deployment-5fd98dbf5f-8g7nm 1/1 Running 0 19m
随便选择一个 Pod,查看其详细信息。
kubectl describe pod nginx-deployment-5fd98dbf5f-58c4z
Name: nginx-deployment-5fd98dbf5f-58c4z
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node02.wzlinux.com/172.18.8.202
Start Time: Thu, 29 Nov 2018 17:47:16 +0800
Labels: pod-template-hash=5fd98dbf5f
run=nginx-deployment
Annotations: <none>
Status: Running
IP: 10.244.2.3
Controlled By: ReplicaSet/nginx-deployment-5fd98dbf5f
Containers:
nginx-deployment:
Container ID: docker://69fa73ed16d634627b69b8968915d9a5704f159206ac0d3b2f1179fa99acd56f
Image: nginx:1.7.9
Image ID: docker-pullable://[email protected]: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: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
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。
从上图也可以看出,对象的命名方式是:子对象的名字 = 父对象名字 + 随机字符串或数字。
原文地址:http://blog.51cto.com/wzlinux/2323948