当前位置:网站首页>【云原生 | Kubernetes篇】深入了解Deployment(八)
【云原生 | Kubernetes篇】深入了解Deployment(八)
2022-06-30 11:31:00 【Lanson】
深入了解Deployment
一、什么是Deployment
一个 Deployment 为 Pods 和 ReplicaSets 提供声明式的更新能力。
你负责描述 Deployment 中的 _目标状态_,而 Deployment 控制器(Controller) 以受控速率更改实际状态, 使其变为期望状态;控制循环。 for(){ xxx controller.spec()}
不要管理 Deployment 所拥有的 ReplicaSet
我们部署一个应用一般不直接写Pod,而是部署一个Deployment
Deploy编写规约 Deployments | Kubernetes
二、Deployment创建
基本格式
.metadata.name指定deploy名字replicas指定副本数量selector指定匹配的Pod模板。template声明一个Pod模板
编写一个Deployment的yaml 赋予Pod自愈和故障转移能力
在检查集群中的 Deployment 时,所显示的字段有:
NAME列出了集群中 Deployment 的名称。READY显示应用程序的可用的 副本 数。显示的模式是“就绪个数/期望个数”。UP-TO-DATE显示为了达到期望状态已经更新的副本数。AVAILABLE显示应用可供用户使用的副本数。AGE显示应用程序运行的时间。
ReplicaSet 输出中包含以下字段:
NAME列出名字空间中 ReplicaSet 的名称;DESIRED显示应用的期望副本个数,即在创建 Deployment 时所定义的值。 此为期望状态;CURRENT显示当前运行状态中的副本个数;READY显示应用中有多少副本可以为用户提供服务;AGE显示应用已经运行的时间长度。注意:ReplicaSet 的名称始终被格式化为
[Deployment名称]-[随机字符串]。 其中的随机字符串是使用 pod-template-hash 作为种子随机生成的。
一个Deploy产生三个
Deployment资源
replicaset资源
Pod资源
Deployment控制RS,RS控制Pod的副本数 ReplicaSet: 只提供了副本数量的控制功能 Deployment: 每部署一个新版本就会创建一个新的副本集,利用他记录状态,回滚也是直接让指定的rs生效
三、Deployment 更新机制
仅当 Deployment Pod 模板(即
.spec.template)发生改变时,例如模板的标签或容器镜像被更新, 才会触发 Deployment 上线。 其他更新(如对 Deployment 执行扩缩容的操作)不会触发上线动作。上线动作 原理: 创建新的rs,准备就绪后,替换旧的rs(此时不会删除,因为**
revisionHistoryLimit** 指定了保留几个版本)常用的kubectl 命令
################更新#################################
#kubectl set image deployment资源名 容器名=镜像名
kubectl set image deployment.apps/nginx-deployment php-redis=tomcat:8 --record
## yaml提取可更新的关键所有字段计算的hash。
web---- /hello
postman aservice- /hello
#或者直接修改定义也行
kubectl edit deployment.v1.apps/nginx-deployment
#查看状态
kubectl rollout status deployment.v1.apps/nginx-deployment
################查看历史并回滚####################################
#查看更新历史-看看我们设置的历史总记录数是否生效了
kubectl rollout history deployment.v1.apps/nginx-deployment
#回滚
kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2
###############累计更新##############
#暂停记录版本
kubectl rollout pause deployment.v1.apps/nginx-deployment
#多次更新操作。
##比如更新了资源限制
kubectl set resources deployment.v1.apps/nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi
##比如更新了镜像版本
kubectl set image deployment.apps/nginx-deployment php-redis=tomcat:8
##在继续操作多次
##看看历史版本有没有记录变化
kubectl rollout history deployment.v1.apps/nginx-deployment
#让多次累计生效
kubectl rollout resume deployment.v1.apps/nginx-deployment
1、比例缩放(Proportional Scaling)
maxSurge(最大增量):除当前数量外还要添加多少个实例。
maxUnavailable(最大不可用量):滚动更新过程中的不可用实例数。

2、HPA(动态扩缩容)
实战:HorizontalPodAutoscaler 演练 | Kubernetes

2.1、需要先安装metrics-server
2.2、安装步骤
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
name: system:aggregated-metrics-reader
rules:
- apiGroups:
- metrics.k8s.io
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- pods
- nodes
- nodes/stats
- namespaces
- configmaps
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --kubelet-insecure-tls
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
image: registry.cn-hangzhou.aliyuncs.com/lanson_k8s_images/metrics-server:v0.4.3
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 4443
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
periodSeconds: 10
securityContext:
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 100
kubectl apply 即可
全部runnning 用
kubectl top nodes --use-protocol-buffers
kubectl top pods --use-protocol-buffers
2.3、配置hpa测试
### 测试镜像 registry.cn-hangzhou.aliyuncs.com/lanson_k8s_images/php-hpa:latest
##应用的yaml已经做好
apiVersion: v1
kind: Service
metadata:
name: php-apache
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: php-apache
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: php-apache
name: php-apache
spec:
replicas: 1
selector:
matchLabels:
run: php-apache
template:
metadata:
creationTimestamp: null
labels:
run: php-apache
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/lanson_k8s_images/php-hpa:latest
name: php-apache
ports:
- containerPort: 80
resources:
requests:
cpu: 200m
##hpa配置 hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
targetCPUUtilizationPercentage: 50
#3、进行压力测试
kubectl run -i --tty load-generator --image=busybox /bin/sh
#回车然后敲下面的命令
kubectl run -i --tty load-generator --rm --image=busybox --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
3、Canary(金丝雀部署)
3.1、蓝绿部署VS金丝雀部署
蓝绿部署

金丝雀部署

3.2、金丝雀的简单测试
使用这个镜像测试registry.cn-hangzhou.aliyuncs.com/lanson_k8s_images/nginx-test这个镜像docker run 的时候 -e msg=aaaa,访问这个nginx页面就是看到aaaa
步骤原理
准备一个Service,负载均衡Pod
准备版本v1的deploy,准备版本v2的deploy
4、Deployment状态与排错
边栏推荐
猜你喜欢

How to analyze native crash through GDB

论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》

10 reasons for system performance failure

Filter error in dplyr: can't transform a data frame with duplicate names

Boost study: boost log

R language de duplication operation unique duplicate filter
![Speech recognition - Fundamentals (I): introduction [speech to text]](/img/28/eb45bf27fffaa29108de84aa72a335.png)
Speech recognition - Fundamentals (I): introduction [speech to text]

一瓶水引发的“战争”

Limited time appointment | Apache pulsar Chinese developer and user group meeting in June

建立自己的网站(13)
随机推荐
如何通过GDB分析Native Crash
It's time for the kotlin coroutine to schedule thread switching to solve the mystery
基于视觉的机器人抓取:从物体定位、物体姿态估计到平行抓取器抓取估计
数据库 事务
[applet practice series] Introduction to the registration life cycle of the applet framework page
NoSQL——Redis的配置与优化
Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!
建立自己的网站(13)
Customize an annotation to get a link to the database
Summer vacation study record
Win10 R package installation error: not installed in arch=i386
PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
R语言ggplot2可视化:gganimate包基于transition_time函数创建动态散点图动画(gif)
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
Goto statement jump uninitialized variable: c2362
Shutter from zero 004 button assembly
Shell first command result is transferred to the second command delete
构造函数、类成员、析构函数调用顺序
shell第一个命令结果传入第二个命令删除