Label是kubernetes系统中的一个重要概念。它的作用就是在资源上添加标识,用来对它们进行区分和选择。
Label的特点:
可以通过Label实现资源的多维度分组,以便灵活、方便地进行资源分配、调度、配置、部署等管理工作。
一些常用的Label 示例如下:
标签定义完毕之后,还要考虑到标签的选择,这就要使用到Label Selector,即:
Label用于给某个资源对象定义标识
Label Selector用于查询和筛选拥有某些标签的资源对象
当前有两种Label Selector:
name = slave: 选择所有包含Label中key="name"且value="slave"的对象
env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象
name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象
name not in (frontend): 选择所有包含Label中的key=“name"且value不等于"frontend"的对象
标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号”,"进行分隔即可。
例如:
name=slave,env!=production
name not in (frontend),env!=production
# 为pod资源打标签
[root@master ~]# kubectl label pod nginx-pod version=1.0 -n dev
pod/nginx-pod labeled
# 为pod资源更新标签
[root@master ~]# kubectl label pod nginx-pod version=2.0 -n dev --overwrite
pod/nginx-pod labeled
# 查看标签
[root@master ~]# kubectl get pod nginx-pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod 1/1 Running 0 10m version=2.0
# 筛选标签
[root@master ~]# kubectl get pod -n dev -l version=2.0 --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod 1/1 Running 0 17m version=2.0
[root@master ~]# kubectl get pod -n dev -l version!=2.0 --show-labels
No resources found in dev namespace.
#删除标签
[root@master ~]# kubectl label pod nginx-pod -n dev tier-
pod/nginx unlabeled
# 匹配单个值,查找 app=hello 的 pod
kubectl get po -A -l app=hello
# 匹配多个值
kubectl get po -A -l 'k8s-app in (metrics-server, kubernetes-dashboard)'
或
# 查找 version!=1 and app=nginx 的 pod 信息
kubectl get po -l version!=1,app=nginx
# 不等值 + 语句
kubectl get po -A -l version!=1,'app in (busybox, nginx)'
配置文件:
在各类资源的 metadata.labels 中进行配置
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: dev
labels:
version: "3.0" #修改lebels中的 version为3.0
env: "test"
spec:
containers:
- image: nginx:latest
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
然后就可以执行对应的更新命令了:kubectl apply -f pod-nginx.yaml
# Pod定义
metadata:
labels:
app: frontend
environment: production
# Service定义
spec:
selector:
matchLabels:
app: frontend
# Deployment定义
spec:
template:
metadata:
labels:
app: myapp
version: v2
# Pod定义
metadata:
labels:
role: database
# Service定义
spec:
selector:
matchLabels:
app: backend
Pod是kubernetes的最小管理单元,在kubernetes中,按照pod的创建方式可以将其分为两类:
在kubernetes中,有很多类型的pod控制器,每种都有自己的适合的场景,常见的有下面这些:
为了更好的解决服务编排的问题,kubernetes在V1.2版本开始,引入了Deployment控制器。值得一提的是,这种控制器并不直接管理pod,而是通过管理ReplicaSet来简介管理Pod,即:Deployment管理ReplicaSet,ReplicaSet管理Pod。所以Deployment比ReplicaSet功能更加强大。
Deployment主要功能有下面几个:
#创建名为 nginx-deploy 的Deployment,并使用Nginx 1.7.9的镜像:
[root@k8s-master deployments]# kubectl create deploy nginx-deploy --image=nginx:1.7.9
deployment.apps/nginx-deploy created
#获取所有Deployments
[root@k8s-master deployments]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 16s
#获取所有Deployments
[root@k8s-master deployments]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 1/1 1 1 27s
#获取所有ReplicaSets
[root@k8s-master deployments]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-deploy-78d8bf4fd7 1 1 1 61s
#与这个Deployment关联的ReplicaSet是 nginx-deploy-78d8bf4fd7
#nginx-deploy-78d8bf4fd7: 由 nginx-deploy Deployment创建的ReplicaSet,
#同样负责维护运行Nginx容器的Pod的期望数量。
#每个ReplicaSet都有一个唯一的标识符(在这里是 78d8bf4fd7)。
#kubectl get po
[root@k8s-master deployments]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-deploy-78d8bf4fd7-xxzv4 1/1 Running 0 80s
#nginx-deploy-78d8bf4fd7-xxzv4: 是由 nginx-deploy-78d8bf4fd7 ReplicaSet创建的Pod的实例。
#每个Pod都有一个唯一的标识符(在这里是 xxzv4)。
#如果需要删除 nginx-deploy 的Deployment:
kubectl delete deployment nginx-deploy
#查看 pod 以及展示标签,可以看到是关联的那个 rs
[root@k8s-master deployments]# kubectl get po,rs,deploy --show-labels
NAME READY STATUS RESTARTS AGE LABELS
pod/nginx-deploy-78d8bf4fd7-xxzv4 1/1 Running 0 44m app=nginx-deploy,pod-template-hash=78d8bf4fd7
NAME DESIRED CURRENT READY AGE LABELS
replicaset.apps/nginx-deploy-78d8bf4fd7 1 1 1 44m app=nginx-deploy,pod-template-hash=78d8bf4fd7
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
deployment.apps/nginx-deploy 1/1 1 1 44m app=nginx-deploy
ReplicaSet(RS):
Deployment(DEPLOY):
这些资源共同协作,通过Deployment和ReplicaSet确保所需数量的Pod在运行。当进行扩展、滚
动更新或者需要重新启动Pod时,Deployment和ReplicaSet会协同工作以维护系统的期望状态。Pod是最小的可部署单元,它们托管应用程序的实际实例
#获取名为 nginx-deploy 的 Deployment 的详细配置信息,以 YAML 格式输出 Deployment 的配置.
[root@k8s-master deployments]# kubectl get deploy nginx-deploy -o yaml
#以下是可能的输出示例,展示了 nginx-deploy Deployment 的详细配置信息:
4apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2024-01-08T07:30:29Z"
generation: 1
apiVersion: apps/v1 # 版本号
kind: ReplicaSet # 类型
metadata: # 元数据
name: # rs名称
namespace: # 所属命名空间
labels: #标签
controller: rs
spec: # 详情描述
replicas: 3 # 副本数量
selector: # 选择器,通过它指定该控制器管理哪些pod
matchLabels: # Labels匹配规则
app: nginx-pod
matchExpressions: # Expressions匹配规则
- {key: app, operator: In, values: [nginx-pod]}
template: # 模板,当副本数量不足时,会根据下面的模板创建pod副本
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
[root@k8s-master deployments]# vi nginx-deploy.yaml
apiVersion: apps/v1 # 版本号
kind: Deployment #资源类型为Deployment
metadata: #原信息
labels: #标签
app: nginx-deploy # 具体的ky:value配置行驶
name: nginx-deploy #deployment的名字
namespace: default #所在命名空间
spec:
replicas: 1 # 期望副本数
revisionHistoryLimit: 10 #进行滚动更新后,保留的历史版本数
selector: # 选择器,用于找到匹配的RS
matchLabels: #按照标签匹配
app: nginx-deploy # 匹配的标签key /value
strategy: # 更新策略
rollingUpdate: #滚动更新配置
maxSurge: 25% # 进行滚动更新时,更新的个体数最多可以超过期望副本数的个数/比例
maxUnavailable: 25% # 进行滚动更新时,最大不可用比例更新比例,表示所在副本数中,最多可以有多少个不更新成功
type: RollingUpdate # 更新类型,采用滚动更新
template: #采用pod模版
metadata: #pod 的元信息
labels: #pod的标签
app: nginx-deploy
spec: #pod 期望信息
containers: #pod 的容器
- image: nginx:1.7.9 #镜像
imagePullPolicy: IfNotPresent #拉取策略
name: nginx #容器名称
restartPolicy: Always #重启策略
terminationGracePeriodSeconds: 30 #删除操作最多宽限多长时间
一般是取出一个或者多个服务器停止服务,执行更新,并重新将其投入使用。周而复始,直到集群中所有的实例都更新成新版本。
滚动更新是一种在Kubernetes中更新应用程序的策略,其中旧版本的Pod逐渐被新版本的Pod替代,以确保在整个更新过程中服务的可用性。这种更新策略有助于减小系统的冲击,保持应用程序的连续可用性,同时允许新版本逐步生效。
#编辑deployments
[root@k8s-master deployments]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited
labels:
app: nginx-deploy
test: '123'
[root@k8s-master deployments]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 1/1 1 1 68m app=nginx-deploy,test=123
[root@k8s-master deployments]#kubectl describe deploy nginx-deploy
Events: <none>
没有更新过程意味着没有进行滚动更新
#编辑rs的副本数量,修改replicas: 3即可
[root@k8s-master deployments]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited
# 查看deployment
# UP-TO-DATE 最新版本的pod的数量
# AVAILABLE 当前可用的pod的数量
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 3 3 78m app=nginx-deploy,test=123
#查看rs
#发现一个rs管理3个pod
[root@k8s-master deployments]# kubectl get rs --show-labels
NAME DESIRED CURRENT READY AGE LABELS
nginx-deploy-78d8bf4fd7 3 3 3 80m app=nginx-deploy,pod-template-hash=78d8bf4fd7
# 查看pod
[root@k8s-master deployments]# kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deploy-78d8bf4fd7-m2d8g 1/1 Running 0 3m29s app=nginx-deploy,pod-template-hash=78d8bf4fd7
nginx-deploy-78d8bf4fd7-qr9dd 1/1 Running 0 3m29s app=nginx-deploy,pod-template-hash=78d8bf4fd7
nginx-deploy-78d8bf4fd7-xxzv4 1/1 Running 0 80m app=nginx-deploy,pod-template-hash=78d8bf4fd7
# 编辑rs的nginx版本 - image: nginx:1.9.1
[root@k8s-master deployments]# kubectl edit deploy nginx-deploy
# 查看deployment
# UP-TO-DATE 最新版本的pod的数量
# AVAILABLE 当前可用的pod的数量
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 1 3 90m app=nginx-deploy,test=123
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 1 3 90m app=nginx-deploy,test=123
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 1 3 90m app=nginx-deploy,test=123
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 2 3 90m app=nginx-deploy,test=123
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 3 3 90m app=nginx-deploy,test=123
[root@k8s-master deployments]# kubectl get deploy nginx-deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-deploy 3/3 3 3 90m app=nginx-deploy,test=123
#多打印几次发现最新版本的pod的数量 前几次是1后面变为2 再到3表示全部更新完成
#READY数量一直是3没有变化
#通过更新Deployment的镜像版本,Kubernetes将自动启动滚动更新
[root@k8s-master deployments]# kubectl set image deployment/nginx-deploy nginx=nginx:1.7.9
deployment.apps/nginx-deploy image updated
#查看滚动更新的状态
[root@k8s-master deployments]# kubectl desribe deploy nginx-deploy
#打印了执行Kubernetes 部署(Deployment)的滚动更新 的过程
#以下显示了在更新过程中进行的缩放活动
# Scaled up replica set nginx-deploy-754898b577 to 1 运行一个新的rs
#Scaled down replica set nginx-deploy-78d8bf4fd7 to 2 将旧的rs 改为 2 就是减掉一个了
#Scaled up replica set nginx-deploy-754898b577 to 2 新的rs更细成两个
#Scaled down replica set nginx-deploy-78d8bf4fd7 to 1 将旧的rs 改为1个
#Scaled up replica set nginx-deploy-754898b577 to 3 最后把 新的更新成3个
#Scaled down replica set nginx-deploy-78d8bf4fd7 to 0 最后把旧的变为0个
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 19m deployment-controller Scaled up replica set nginx-deploy-78d8bf4fd7 to 3
Normal ScalingReplicaSet 7m56s deployment-controller Scaled up replica set nginx-deploy-754898b577 to 1
Normal ScalingReplicaSet 6m44s deployment-controller Scaled down replica set nginx-deploy-78d8bf4fd7 to 2
Normal ScalingReplicaSet 6m44s deployment-controller Scaled up replica set nginx-deploy-754898b577 to 2
Normal ScalingReplicaSet 6m43s deployment-controller Scaled down replica set nginx-deploy-78d8bf4fd7 to 1
Normal ScalingReplicaSet 6m43s deployment-controller Scaled up replica set nginx-deploy-754898b577 to 3
Normal ScalingReplicaSet 6m42s deployment-controller Scaled down replica set nginx-deploy-78d8bf4fd7 to 0
Normal ScalingReplicaSet 106s (x2 over 97m) deployment-controller Scaled up replica set nginx-deploy-78d8bf4fd7 to 1
Normal ScalingReplicaSet 104s deployment-controller Scaled down replica set nginx-deploy-754898b577 to 2
Normal ScalingReplicaSet 104s deployment-controller Scaled up replica set nginx-deploy-78d8bf4fd7 to 2
Normal ScalingReplicaSet 102s (x3 over 103s) deployment-controller (combined from similar events): Scaled down replica set nginx-deploy-754898b57
逐步替代: 滚动更新通过逐步替代旧版本的Pod来进行,而不是立即替换所有Pod。这有助于减小系统在更新期间的波动,降低潜在的服务中断。
假设当前有 5 个 nginx:1.7.9 版本,你想将版本更新为 1.9.1,当更新成功第三个以后,你马上又将期望更新的版本改为 1.9.2,那么此时会立马删除之前的三个,并且立马开启更新 1.9.2 的任务
# 1、镜像更新到1.161
[root@k8s-master deployments]# kubectl set image deployment/nginx-deploy nginx=nginx:1.161
deployment.apps/nginx-deploy image updated
# 2、更新失败(镜像拉取失败)
[root@k8s-master deployments]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-754898b577-7zhq4 1/1 Running 0 15h
nginx-deploy-754898b577-bcqsh 1/1 Running 0 15h
nginx-deploy-754898b577-f2r45 1/1 Running 0 15h
nginx-deploy-7fcf48c86f-xp6rk 0/1 ContainerCreating 0 6s
说明: Deployment 控制器自动停止有问题的上线过程,并停止对新的 ReplicaSet 扩容。 这行为取决于所指定的 rollingUpdate参数具体为 maxUnavailable默认情况下,Kubernetes 将此值设置为 25%。
# 3、查看Deployment详细信息:
[root@k8s-master deployments]# kubectl describe deployment nginx-deploy
# 4、检查 Deployment 修订历史
[root@k8s-master deployments]# kubectl rollout history deployment nginx-deploy
deployment支持两种更新策略:重建更新和滚动更新,可以通过strategy指定策略类型,支持两个属性:
1.重建更新.
编辑nginx-deploy,在spec节点下添加更新策略
spec:
strategy: # 指定新的Pod替换旧的Pod的策略, 支持两个属性:
type: Recreate # 重建更新 在创建出新的Pod之前会先杀掉所有已存在的Pod
2.滚动更新
编辑nginx-deploy,在spec节点下添加更新策略
spec:
strategy: # 指定新的Pod替换旧的Pod的策略, 支持两个属性:
type: RollingUpdate # 滚动更新策略 滚动更新,就是杀死一部分,就启动一部分,在更新过程中,存在两个版本Pod
rollingUpdate: #当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性:
maxSurge: 25% #用来指定在升级过程中不可用Pod的最大数量,默认为25%。
maxUnavailable: 25% #用来指定在升级过程中可以超过期望的Pod的最大数量,默认为25%。
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能,下面具体来看.
kubectl rollout: 版本升级相关功能,支持下面的选项:
# 查看当前升级版本的状态
[root@k8s-master deployments]# kubectl rollout status deploy nginx-deploy
deployment "nginx-deploy" successfully rolled out
# 查看升级历史记录
[root@k8s-master deployments]# kubectl rollout history deploy nginx-deploy
deployment.apps/nginx-deploy
REVISION CHANGE-CAUSE
2 <none>
3 <none>
# 可以发现有三次版本记录,说明完成过两次升级
# 版本回滚
# 这里直接使用--to-revision=1回滚到了1版本, 如果省略这个选项,就是回退到上个版本,就是2版本
[root@k8s-master deployments]# kubectl rollout undo deployment nginx-deploy --to-revision=2
deployment.apps/nginx-deploy rolled back
# 查看发现,通过nginx镜像版本可以发现到了第一版 nginx:1.9.1
[root@k8s-master deployments]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deploy 3/3 3 3 146m nginx nginx:1.9.1 app=nginx-deploy
# 查看rs,发现第一个rs中有3个pod运行
# 其实deployment之所以可是实现版本的回滚,就是通过记录下历史rs来实现的,
# 一旦想回滚到哪个版本,只需要将当前版本pod数量降为0,然后将回滚版本的pod提升为目标数量就可以了
[root@k8s-master deployments]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deploy-754898b577 3 3 3 57m
nginx-deploy-78d8bf4fd7 0 0 0 147m
#查看pod 都运行起来了
[root@k8s-master deployments]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-754898b577-7zhq4 1/1 Running 0 15h
nginx-deploy-754898b577-bcqsh 1/1 Running 0 15h
nginx-deploy-754898b577-f2r45 1/1 Running 0 15h
# 回滚到之前的修订版本
kubectl rollout undo deployment nginx-deploy
# 扩容5个副本
[root@k8s-master deployments]# kubectl scale deploy nginx-deploy --replicas=5
deployment.apps/nginx-deploy scaled
# 查看deployment 已经扩容为5
[root@k8s-master deployments]# kubectl get deploy nginx-deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 5/5 5 5 18h
# 查看pod
[root@k8s-master deployments]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-754898b577-7zhq4 1/1 Running 0 15h
nginx-deploy-754898b577-94zvp 1/1 Running 0 62s
nginx-deploy-754898b577-bcqsh 1/1 Running 0 15h
nginx-deploy-754898b577-f2r45 1/1 Running 0 15h
nginx-deploy-754898b577-nbhds 1/1 Running 0 62s
#缩容3个副本
[root@k8s-master deployments]# kubectl scale deployment --replicas=3 nginx-deploy
deployment.apps/nginx-deploy scaled
#或者修改配置文件
# 编辑deployment的副本数量,修改spec:replicas: 3即可
[root@k8s-master01 ~]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited
# 查看deployment
[root@k8s-master deployments]# kubectl get deploy nginx-deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 3/3 3 3 18h
# 查看pod
[root@k8s-master deployments]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deploy-754898b577-7zhq4 1/1 Running 0 15h
nginx-deploy-754898b577-bcqsh 1/1 Running 0 15h
nginx-deploy-754898b577-f2r45 1/1 Running 0 15h