POD --> RC --> RS --> Deployment
什么是Deployment资源
Deployment 表示用户对k8s集群的一次更新操作 Deployment是一个比RS应用模式更广的API对象 用于保证Pod的副本数量
Deployment资源的作用
可以创建一个新的服务 更新一个新的服务 滚动升级一个新的服务 实际是创建一个新的RS 把新的RS中副本数增加到理想状态 将旧的RS中的副本数减小到0的复合操作
Deployment需要解决的问题
RC、RS和Deployment只是保证了支撑服务的POD数量,但是没有解决如何访问这些服务的问题。一个POD只是一个运行服务的实例,随时可以能在一个节点上停止,在另一个节点以一个新的IP启动一个新的POD,因此不能以确定的IP和端口号提供服务。
要稳定地提供服务需要服务发现和负载均衡能力。服务发现完成的工作,是针对客户端访问的服务,找到对应的后端服务实例。
在K8S的集中当中,客户端需要访问的服务就是Service对象。每个Service会对应一个集群内部有效的虚拟IP,集群内部通过虚拟IP访问一个服务。
1.创建第一个k8s的应用
[root@rstx-203 ~]# kubectl run net-test --image=alpine --replicas=2 sleep 36000
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/net-test created
[root@rstx-203 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
net-test-7dc6fd954d-fwjgc 0/1 Error 8 19m 172.7.214.2 rstx4-214.host.com <none> <none>
net-test-7dc6fd954d-xnq26 0/1 CrashLoopBackOff 8 19m 172.7.241.2 rstx4-241.host.com <none> <none>
[root@rstx-203 ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
net-test 0/2 2 0 19m
nginx-deployment 3/3 1 3 15h
**kubectl get deployment命令可以查看net-test的状态,输出显示两个副本正常运行。还可以在创建的过程中,通过kubectl describe deployment net-test了解详细的信息。**
[root@rstx-203 ~]# kubectl describe deployment net-test
Name: net-test
Namespace: default
CreationTimestamp: Tue, 20 Jul 2021 11:00:26 +0800
Labels: run=net-test
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=net-test
Replicas: 2 desired | 2 updated | 2 total | 0 available | 2 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=net-test
Containers:
net-test:
Image: alpine
Port: <none>
Host Port: <none>
Args:
sleep
36000
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available False MinimumReplicasUnavailable
Progressing True ReplicaSetUpdated
OldReplicaSets: <none>
NewReplicaSet: net-test-7dc6fd954d (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m26s deployment-controller Scaled up replica set net-test-7dc6fd954d to 2
RS来控制pod的数量 deployment控制rs
[root@rstx-203 ~]# kubectl get rs #获取副本集的信息
NAME DESIRED CURRENT READY AGE
net-test-7dc6fd954d 2 2 0 34m
nginx-deployment-5455f758b9 1 1 0 16h
nginx-deployment-6cbc65fd69 0 0 0 16h
nginx-deployment-d75f79f4d 3 3 3 16h
[root@rstx-203 ~]# kubectl describe rs net-test-7dc6fd954d #查看副本集的详细信息
Name: net-test-7dc6fd954d
Namespace: default
Selector: pod-template-hash=7dc6fd954d,run=net-test
Labels: pod-template-hash=7dc6fd954d
run=net-test
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/net-test #指明 rs是由deployment名字是net-test的控制器控制
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=7dc6fd954d
run=net-test
Containers:
net-test:
Image: alpine
Port: <none>
Host Port: <none>
Args:
sleep
36000
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 35m replicaset-controller Created pod: net-test-7dc6fd954d-fwjgc
Normal SuccessfulCreate 35m replicaset-controller Created pod: net-test-7dc6fd954d-xnq26
[root@rstx-203 ~]# kubectl get pod #查看pod信息 可以看到两个pod都处于奔溃状态
NAME READY STATUS RESTARTS AGE
net-test-7dc6fd954d-fwjgc 0/1 CrashLoopBackOff 11 40m
net-test-7dc6fd954d-xnq26 0/1 CrashLoopBackOff 11 40m
[root@rstx-203 ~]# kubectl describe pod net-test-7dc6fd954d-xnq26 #查看pod的详细信息
Name: net-test-7dc6fd954d-xnq26
Namespace: default
Priority: 0
Node: rstx4-241.host.com/192.168.1.241
Start Time: Tue, 20 Jul 2021 10:56:37 +0800
Labels: pod-template-hash=7dc6fd954d
run=net-test
Annotations: <none>
Status: Running
IP: 172.7.241.2
Controlled By: ReplicaSet/net-test-7dc6fd954d
Containers:
net-test:
Container ID: docker://c9fc1b937b66d932bd4da376d1855c4cdac7b1c50fc04bd2e93ae66e604f4e57
Image: alpine
Image ID: docker-pullable://alpine@sha256:234cb88d3020898631af0ccbbcca9a66ae7306ecd30c9720690858c1b007d2a0
Port: <none>
Host Port: <none>
Args:
sleep
36000
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Started: Tue, 20 Jul 2021 11:37:12 +0800
Finished: Tue, 20 Jul 2021 11:37:12 +0800
Ready: False
Restart Count: 12
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-clf9q (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-clf9q:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-clf9q
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 Pulling 44m (x4 over 46m) kubelet, rstx4-241.host.com Pulling image "alpine"
Normal Created 44m (x4 over 46m) kubelet, rstx4-241.host.com Created container net-test
Normal Started 44m (x4 over 46m) kubelet, rstx4-241.host.com Started container net-test
Normal Scheduled 42m default-scheduler Successfully assigned default/net-test-7dc6fd954d-xnq26 to rstx4-241.host.com
Normal Pulled 11m (x12 over 46m) kubelet, rstx4-241.host.com Successfully pulled image "alpine"
Warning BackOff 6m44s (x171 over 45m) kubelet, rstx4-241.host.com Back-off restarting failed container #崩溃原因是没有程序在前台运行
Controlled By 指明此 Pod 是由 Controlled By: ReplicaSet/net-test-7dc6fd954d 创建。Events 记录了 Pod 的启动过程。如果操作失败(比如 image 不存在),也能在这里查看到原因。