当前位置:网站首页>kubernetes之Deployment
kubernetes之Deployment
2022-07-28 07:16:00 【JavaShark】
1.什么是Deployment?
Deployment(简写为deploy)是kubernetes控制器的又一种实现,构建于ReplicasSet控制器之上,可以为Pod和ReplicaSet提供声明式更新。相比较而言,Pod和ReplicaSet很少用来直接使用,而是借助于控制器来使用。Deployment Controller核心功能也是保证Pod资源的正常使用,大部分功能调用ReplicaSet来实现。
1.2我们只需要描述Deployment中目标Pod期望状态,而Deployment控制器以控制更改为实际状态,使其变成期望状态。我们不需要直接使用Pod和ReplicaSet来实现,Deployment控制器在ReplicaSet的基础上增加了部分特性:
1.事件和状态查看: 可以通过特定的命令查看Deployment对象的更新进度和状态;
2.版本记录: 将Deployment对象的历史更新操作都进行保存,以便于后续执行回滚操作使用;
3.多种更新方案: Recreate重建,可以实现单批次更新所有的Pod。RollingUpdate可以实现多批次替换Pod至新版本。
2.Deployment的构成部分
Deployment是标准的k8s资源,Deployment构建于ReplicaSet之上,spec字段嵌套了包含了replicaset控制器支持的selector、replicas、template、minReadySeconds
2.1Selector: 标签选择器,匹配并关联Pod,并对其受控制的Pod进行管理;
2.2Replicas: 期望的Pod的副本数,期望在集群所运行的Pod对象的数量;
2.3template: Pod的模板;实际上定义了Pod的内容,相当于把一个Pod的描述信息以模板的方式嵌套在ReplicaSet;
3.Deployment的资源规范
apiVersion: apps/v1 # API群组及版本;
kind: Deployment # 资源类型;
metadata: # Pod元数据;
name: # 资源名称,在作用域中要唯一;
namespace: <string> # 名称空间,Deployment隶属名称空间级别;
spec:
minReadySeconds: <integer> # Pod就绪后多少秒内任一容器无Crash方可为就绪;
replicas: <integer> # 期望的Pod副本数,默认为1;
selector: <Object> # 标签选择器,必须匹配template字段中Pod模板中的标签;
template: <Object> # Pod模板对象;
revisionHistoryLimit: <integer> # 滚动更新历史记录数量,默认为10;
strategy: <Object> # 滚动更新策略;
type: <string> # 滚动更新类型,可用值有Recreate和Rollingupdate;
rollingUpdaate: <Object> # 滚动更新参数,专用于RollingUpdate类型;
maxSurge: <string> # 更新起见可比期望的Pod数量多出的数量和比例;
maxUnavailable: <string> # 更新期间可比期望的Pod数量缺少的数量或比例;
progressDeadlineSeconds: <integer> # 滚动更新故障超时时长,默认为600;
paused: <boolean> # 是否暂停部署过程;4.Deployment的配置示例:
# 以Nginx应用为示例配置
[email protected]:~# cat nginx-deployment-test.yaml
apiVersion: apps/v1 # 资源群组
kind: Deployment # 资源类型
metadata: # 元数据
name: deployment-nginx-test # Pod名称
namespace: default # Pod所在的名称空间
spec:
replicas: 2 # Pod的副本数
selector: # 标签选择器
matchLabels:
app: nginx-deployment # Pod的标签
template: # 定义Pod的模板
metadata: #
labels: # 标签同上要一致
app: nginx-deployment
spec: # 定义容器的
containers:
- name: nginx # 容器名称
image: nginx # 容器镜像
imagePullPolicy: IfNotPresent # 容器拉取策略
ports: # 定义容器的端口
- name: http
containerPort: 80 # 容器端口为80
#
[email protected]:~# kubectl apply -f nginx-deployment-test.yaml
deployment.apps/deployment-nginx-test created4.1查看Pod,Deployment的名称-Replicaset的名称-随机字符串hash值是由Deployment Controller自动生成。Pod对应的名称遵循ReplicaSet控制器的命名格式,它以Replicaset控制器的名称为签注,后跟5位随机字符。
[email protected]:~/cloud-Native/deployment/replicas# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-nginx-test-dd5bdc67f-b5dlz 1/1 Running 0 6m12s
deployment-nginx-test-dd5bdc67f-t94j7 1/1 Running 0 6m12s
# NAME: 列出了集群中Deployment的名称。
# READY: 显示应用程序的可用的"副本数"显示的模式是"就绪个数/期望个数"
# UP-TO-DATE: 标识已经达到期望状态的Pod的副本数量。
# AVAILABLE: 表示当前处于可用状态的Pod的数量。4.2查看replicaset,Deployment控制器会自动创建相关的ReplicaSet控制器资源,并以[DEPLOYMENT-NAME]-[POD-TEMPLATE-HASH-VALUE]格式为其命名,其中的hash值由Deployment控制器自动生成,由Deployment创建的ReplicaSet对象会自动使用相同的标签选择器。
[email protected]:~# kubectl get replicaset deployment-nginx-test-dd5bdc67f
NAME DESIRED CURRENT READY AGE
deployment-nginx-test-dd5bdc67f 2 2 2 9m34s5.为Pod创建Service资源以实现负载均衡。
[email protected]:~# cat nginx-service-test.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-deployment-test
namespace: default
spec:
type: ClusterIP # 类型为ClusterIP
selector: # 标签选择器跟Pod的标签相匹配,才能被识别为后端端点。
app: nginx-deployment
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
#
[email protected]:~# kubectl apply -f nginx-service-test.yaml
service/nginx-deployment-test created6.进行访问测试
6.1我提前修改了Nginx的index文件。方便效果。
[email protected]:~# echo "nginx-1-haitang.net" > /usr/share/nginx/html/index.html
[email protected]:~# echo "nginx2-haitang.com" > /usr/share/nginx/html/index.html6.2访问Service的IP,因为是ClusterIP,只能在集群内访问。也是没有问题的。
[email protected]:~# curl 10.107.246.117
nginx2-haitang.com
[email protected]:~# curl 10.107.246.117
nginx-1-haitang.net
[email protected]:~# curl 10.107.246.117
nginx2-haitang.com
[email protected]:~# curl 10.107.246.117
nginx-1-haitang.net7.Pod的伸缩
7.1比如现在两个副本有点无法应对突发的流量,可以通过命令的方式修改replicas也可以通过修改配置文件的方式去修改。
1.1# 命令方式来伸缩 [email protected]:~# kubectl scale deployment deployment-nginx-test --replicas=3 deployment.apps/deployment-nginx-test scaled 1.2# 缩容的话就是=多少副本数即可。 例如=2 [email protected]:~# kubectl scale deployment deployment-nginx-test --replicas=2 1.3# 修改配置文件vim nginx-deployment-test.yaml 修改spec字段的replicas即可。
7.2伸缩完成后,Service就会发现带有同样标签的Pod,Pod就绪后加入到后端的可用端点。
再次访问测试。也是没有问题。
[email protected]:~# curl 10.107.246.117
nginx2-haitang.com
[email protected]:~# curl 10.107.246.117
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[email protected]:~# curl 10.107.246.117
nginx-1-haitang.net7.3也可以通过describe命令来查看Events事件。
[email protected]:~# kubectl describe deploy deployment-nginx-test
Pod Template:
Labels: app=nginx-deployment
Containers:
nginx:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: deployment-nginx-test-dd5bdc67f (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 36s deployment-controller Scaled up replica set deployment-nginx-test-dd5bdc67f to 38.故障测试。
8.1现在删除Pod会发生什么?
[email protected]:~# kubectl delete pods deployment-nginx-test-dd5bdc67f-2vggw
pod "deployment-nginx-test-dd5bdc67f-2vggw" deleted
# 有控制器管理的Pod删除会自动重建,没有控制器管理的Pod删除就是删除了不可能会自动重建。我们创建的Pod受控于Deployment Controller。所以能重建。
[email protected]:~# kubectl get pods
deployment-nginx-test-dd5bdc67f-2vggw 1/1 Running 0 4s
deployment-nginx-test-dd5bdc67f-lc6nm 1/1 Running 0 4s边栏推荐
- How to write a JMeter script common to the test team
- postgresql查询【表字段类型】和库中【所有序列】
- Use of tkmapper - super detailed
- CAT1 4G+以太网开发板232数据通过4G模块TCP发到服务器
- 二维数组及操作
- What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared
- Viewing vantage's self drive from the "three good" kitchen electricity standard and the value proposition of "serious life"
- The five pictures tell you: why is there such a big gap between people in the workplace?
- pyspark 写入数据到iceberg
- File editing component
猜你喜欢

C#,入门教程——程序运行时的调试技巧与逻辑错误探针技术与源代码

机器学习如何做到疫情可视化——疫情数据分析与预测实战

Gbase 8A MPP and Galaxy Kirin (x86 version) complete deep adaptation

Characteristics of EMC EMI beads

uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)

Allure use

2022牛客多校第一场解题报告

GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览

Export SQL server query results to excel table

Understand the propagation process of EMI electromagnetic interference through five diagrams - the influence of square wave steepness on high-frequency components, the spectrum graph from time sequenc
随机推荐
tkMapper的使用-超详细
VK1620温控仪/智能电表LED数显驱动芯片3/4线接口内置 RC振荡器,提供技术支持
Hcip --- LDP and MPLS Technology (detailed explanation)
File operation of QT
Shell编程规范与变量
SQL Server查询结果导出到EXCEL表格
How to import and export Youxuan database
The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released
‘全局事件总线’&‘消息订阅与发布’
GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览
GBASE亮相联通云巡展(四川站) 以专业赋能云生态
leetcode/数组中和为0的三个不同数
第2章-14 求整数段和
PostgreSQL:无法更改视图或规则使用的列的类型
微信小程序----微信小程序浏览pdf文件
Analysis and recurrence of network security vulnerabilities
博客搭建七:hugo
The cooperation between starfish OS and metabell is just the beginning
Opencv+paddle Orc recognize pictures and extract table information
Gbase 8A MPP and Galaxy Kirin (x86 version) complete deep adaptation