1、在实际的生产环境中,不建议直接创建Pod
2、创建流程:
deployment ---> RS(副本集) ---> pod
创建出deployment后会自动创建出一个RS(副本集),而pod是由RS(副本集)创建的
3、优势:
自动维护pod的副本
支持滚动更新
# 以上两个优势都靠的是RS(副本集)
4、使用场景:频繁更新业务
5、标签选择器:(键值对的数据)
k8s在资源之间相互建立映射关系的时候都是用的标签选择器;
# 创建一个无状态负载的Pod
vim test-deployment.yaml
┌─[k8s-master]─[/k8s-learn/deployment-learn]
└──╼ cat test-deployment.yaml
# 在Kubernetes中,apps/v1表示API的版本号。其中,apps是Kubernetes中的一个API组(API Group),它包含了一组与应用程序相关的资源对象,如Deployment、StatefulSet等。而v1表示这个API组的版本号。
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deploy #定义depploy的name
spec:
replicas: 4 #指定pod的副本数
selector:
matchLabels:
app: dd1 #指定RS的标签选择器
template: #定义pod
metadata:
labels:
app: dd1 #定义pod的标签
spec:
containers:
- name: centos7-dd1
image: centos:7
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
kubectl create -f test-deployment.yaml
kubectl get pod -n default # 查看Pod的状态
#开始测试 k8s中deployment的特性
删除一个pod k8s会再创建出一个一模一样的pod
kubectl delete pod test-deploy-5897994f99-d5xxg -n default
# 删除后查看 pod的副本数量没有变化
Deployment测验滚动更新
vim nginx-deployment.yaml
┌─[k8s-master]─[/k8s-learn/deployment-learn]
└──╼ cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: test1-deploy
spec:
replicas: 6
selector:
matchLabels:
app: dd1-nginx
strategy:
type: RollingUpdate #使用滚动更新(RollingUpdate)策略进行部署
rollingUpdate:
maxSurge: 2 # 最大额外副本数(maxSurge)为2
maxUnavailable: 1 # 最大不可用副本数(maxUnavailable)为1
#这意味着在更新过程中,最多可以有3个副本(旧版本+新版本+额外的一个),同时保证至少有一个副本始终可用。
template:
metadata:
labels:
app: dd1-nginx
spec:
containers:
- name: nginx-dd1
image: nginx:1.18
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubectl create -f nginx-deployment.yaml
kubectl get pod -n default # 查看Pod的状态
#测试版本滚动更新
将nginx-deployment.yaml中的镜像改成nginx:1.18之后:
kubectl apply -f test2-nginx.yaml
重复使用kubectl get pod 查看pod的状态
# 查看版本
kubectl describe pod test1-deploy-7c4c47799b-8bqzf | grep image
显示1.16
#查看deployment的更新历史
┌─[k8s-master]─[/k8s-learn/deployment-learn]
└──╼ kubectl rollout history deploy test1-deploy
deployment.apps/test1-deploy
REVISION CHANGE-CAUSE
1 <none>
2 <none>
# 版本回退
kubectl rollout undo deployment test1-deploy --to-revision=1
#查看版本
kubectl describe pod test1-deploy-7c4c47799b-8bqzf | grep image
Nginx1.18
指令说明
1、template
下面所有配置与直接定义pod信息一样
2、replicas
指定容器的副本数
3、selector
指定标签选举器的工作方式,让选择器通过哪个标签来确认容器的副本数量正常
4、strategy
指定容器的滚动升级
maxSurge
每次升级的百分比,也可以是个绝对数字
maxUnavailable
升级失败的百分比,也可以是个绝对数字
支持的升级方式:
Rollupdateing 一次更新一批,一批完成后再继续更新
ReCreate 干掉所有容器,重新创建