目录
2、ReplicationController(简称 RC)
1. Kubernetes 基本管理单元,每个 Pod 是一个或多个容器的一组集合
2. 一个Pod 作为一个整体运行在一个节点(node)上
3. Pod 内的容器共享存储和网络
1. Kubernetes 需要管理大量的 Pod,而显而易见的是通常情况下一个应用不会以单独的一个Pod完成。比较常见的情况是使用大量的Pod 组成一个简单应用。管理这些大量的Pod的一个方式就是RC
2. RC 可以指定 Pod 的副本数量,并且在其中有 Pod 故障时可以自动拉起新的 Pod,大大简化了管理难度
1. ReplicaSet 是新一代的 RC,主要功能和 RC 一样,维持 Pod 的数量稳定,指定Pod 的运行位置等,使用方法也相似,主要区别是更新了 api,支持更多功能
2. ReplicaSet 不建议直接使用,而是用更上层的概念 Deployment 调用 ReplicaSet
1. 目前最常用的控制器就是Deployment,创建 Deployment 时也会自动创建 ReplicaSet
2. Deployment 可以管理一个或多个 RS,并且通过 RS 来管理 Pod
3. 从小到大的管理逻辑 容器 < Pod < ReplicaSet < Deployment
4. 通常情况下
· Pod中 包含一个容器,或关系特别紧密的几个容器(根据场景来设置)
· 一个 ReplicaSet 中包含多个相同的Pod
· Deployment 中包含一个或几个不同的 RS
$ kubectl create deployment mydep --image=nginx
$ kubectl get deployment
1. 在Kubernetes 中的操作很多都是用 kubectl 来完成,通过其命令可以管理 Deployment,Replicaset,ReplicationController,Pod等,进行操作,扩容,删除等等全生命周期操作。同时可以对管理对象进行查看或者监控资源使用情况
2. Kubectl 的语法 kubectl [command] [TYPE] [NAME] [flags]
· command:指定你希望进行的操作,如 create,get,describe,delete等
· TYPE: 指定操作对象的类型,如deployment,RS,Pod等
· NAME: 指定对象的名字
· flags: 可选的标志位
常用 Command:
· create:创建资源
· apply:应用资源的配置变更,也可以代替create创建新的资源
· get:查看资源
· describe:查看资源的详细描述
· delete:删除资源
命令方式创建只是一种简单的形式,大量个性化参数没有定义,后续对该 Deployment 的升级管理有诸多问题。实际使用中更偏向于使用yaml 文件来创建和管理各类资源
- $ vim deployment-v1.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: nginx-deployment
- labels:
- app: nginx
- spec:
- replicas: 2
- selector:
- matchLabels:
- app: nginx
- template:
- metadata:
- labels:
- app: nginx
- spec:
- containers:
- - name: nginx
- image: nginx:1.14.2
- ports:
- - containerPort: 80
$ kubectl apply -f deployment-v1.yaml
- $ kubectl get pods
- NAME READY STATUS RESTARTS AGE
- nginx-deployment-6595874d85-fkzhb 1/1 Running 0 29s
- nginx-deployment-6595874d85-z5r47 1/1 Running 0 29s
若describe查看pod没有明显提示问题,可能是之前反复添加过,添加之前需要清除下网络
在对应节点操作后,pod会自动恢复
'在对应失败节点操作' kiosk@k8s-worker2:~$ kubeadm reset kiosk@k8s-worker2:~$ systemctl stop kubelet kiosk@k8s-worker2:~$ systemctl stop docker kiosk@k8s-worker2:~$ rm -rf /var/lib/cni/ /var/lib/kubelet/* /etc/cni/ kiosk@k8s-worker2:~$ sudo systemctl start docker.socket kiosk@k8s-worker2:~$ sudo systemctl start kubelet
关键字 释义 apiVersion 版本号,固定为apps/v1,如果使用1.9.0以前版本的kubernetes,填写 apps/v1beta2 kind 类型,选择创建资源类型,可以填写pod,replicaset等 metadata 元数据,其中name 项指定了名称,label 项指定标签 spec deployment 规格,其中replicas 指定 pod 副本数量,选择器选择标签匹配为 app : nginx template 对pod模板的定义,其中至少要定义一个label Spec 描述 pod 的规格 Containers 定义容器的属性,如容器名、镜像名:版本、端口等 注意格式,缩进一般使用两个空格,千万不要使用Tab !
1. 容器对比虚拟机,做大的优势在于可以(容器占用资源非常少)灵活的弹性伸缩,而这一部分工作由 kubernetes进行调度
2. Deployment 弹性伸缩本质是 Pod 数量增加或减少
3.弹性伸缩可以支持自动化部署,并在很短时间内实现数量变更
4.修改 deployment 中的 replicas 参数实现
$ kubectl edit deployments.apps nginx-deployment
5.使用 scale 命令应用变更完成扩容或减容
kubectl scale deployment nginx-deployment --replicas=3 #将副本扩容为3个
1. 当使用的 deployment 需要升级时(如软件版本更新),可以使用 rolling update 功能滚动升级 deployment 中所有 pod
2. 已有一个 nginx-deployment,让我们查看它现在的状态
$ kubectl get replicasets.apps NAME DESIRED CURRENT READY AGE nginx-deployment-6595874d85 3 3 3 4h15m $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-6595874d85-7tl7p 1/1 Running 0 121m nginx-deployment-6595874d85-fkzhb 1/1 Running 0 4h16m nginx-deployment-6595874d85-z5r47 1/1 Running 0 4h16m3. 修改yaml 文件(只要版本有升级或变化都会生成新的rs)
'编写deployment-v2.yml文件' $ vim deployment-v2.yml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.20.2 ports: - containerPort: 804. 执行rolling-update
$ kubectl apply -f deployment-v2.yml
5. 升级后,再次查看状态,会发现 replicaset 和 pod 的状态都发生了变化
- 出现了一个新的 replicaset,原有 replicaset 中无 pod
- 三个 pod 的名字发生了变更
- 通过观察默认是依次删除再重建
$ kubectl get replicasets.apps NAME DESIRED CURRENT READY AGE nginx-deployment-644dfd858 1 1 0 7s nginx-deployment-6595874d85 2 2 2 4h20m $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-644dfd858-gt7x2 1/1 Running 0 3m22s nginx-deployment-644dfd858-svjtk 1/1 Running 0 3m7s6. 再次查看该 deployment 详细信息,可以发现在滚动更新中系统所做的操作
$ kubectl describe deployments.apps nginx-deployment Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 41m deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 2 Normal ScalingReplicaSet 41m deployment-controller Scaled up replica set nginx-deployment-644dfd858 to 1 Normal ScalingReplicaSet 41m deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 1 Normal ScalingReplicaSet 41m deployment-controller Scaled up replica set nginx-deployment-644dfd858 to 2 Normal ScalingReplicaSet 40m deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 0
1. 使用 kubernetes 滚动更新后,kubernetes 会记录下本次更新,并且保存为一个历史版本,如果更新后出现应用异常,可以通过回滚操作回到之前版本
2. 使用命令查看历史版本
$ kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 1 2 '发现历史都是none,因为没做记录 --record'3. 系统记录下了三条信息,分别是:
· 第一次是创建 nginx-deployment
· 第二次是滚动更新 nginx-deployment
· 第三次是再次滚动更新 nginx-deployment
'编写deployment-v3.yml文件' $ cat deployment-v3.yml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21.6 ports: - containerPort: 80
$ kubectl apply -f deployment-v1.yml --record Flag --record has been deprecated, --record will be removed in the future deployment.apps/nginx-deployment configured $ kubectl apply -f deployment-v2.yml --record Flag --record has been deprecated, --record will be removed in the future deployment.apps/nginx-deployment configured $ kubectl apply -f deployment-v3.yml --record Flag --record has been deprecated, --record will be removed in the future deployment.apps/nginx-deployment configured
$ kubectl rollout history deployment nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 3 kubectl apply --filename=deployment-v1.yml --record=true 4 kubectl apply --filename=deployment-v2.yml --record=true 5 kubectl apply --filename=deployment-v3.yml --record=true4. 通过 --revision= 命令可以查看某个历史版本的详细信息
$ kubectl rollout history deployment nginx-deployment --revision=4 deployment.apps/nginx-deployment with revision #4 Pod Template: Labels: app=nginx pod-template-hash=644dfd858 Annotations: kubernetes.io/change-cause: kubectl apply --filename=deployment-v2.yml --record=true Containers: nginx: Image: nginx:1.20.2 Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes:5. 使用 rollout undo 命令回滚到指定版本
$ kubectl rollout undo deployment nginx-deployment --to-revision=4
6. 查看deployment信息
$ kubectl describe deployments.apps nginx-deployment Name: nginx-deployment Namespace: default CreationTimestamp: Wed, 02 Nov 2022 09:20:23 +0800 Labels: app=nginx Annotations: deployment.kubernetes.io/revision: 6 kubernetes.io/change-cause: kubectl apply --filename=deployment-v2.yml --record=true Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.20.2 '回滚成功' Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: NewReplicaSet: nginx-deployment-644dfd858 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 56m (x2 over 3h13m) deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 2 Normal ScalingReplicaSet 8m30s deployment-controller Scaled up replica set nginx-deployment-6595874d85 to 1 Normal ScalingReplicaSet 8m29s (x2 over 5h17m) deployment-controller Scaled up replica set nginx-deployment-6595874d85 to 2 Normal ScalingReplicaSet 8m29s deployment-controller Scaled down replica set nginx-deployment-644dfd858 to 1 Normal ScalingReplicaSet 8m28s deployment-controller Scaled down replica set nginx-deployment-644dfd858 to 0 Normal ScalingReplicaSet 8m26s (x2 over 56m) deployment-controller Scaled up replica set nginx-deployment-644dfd858 to 1 Normal ScalingReplicaSet 8m25s (x2 over 56m) deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 1 Normal ScalingReplicaSet 8m25s (x2 over 56m) deployment-controller Scaled up replica set nginx-deployment-644dfd858 to 2 Normal ScalingReplicaSet 8m23s (x2 over 56m) deployment-controller Scaled down replica set nginx-deployment-6595874d85 to 0 Normal ScalingReplicaSet 8m19s deployment-controller Scaled up replica set nginx-deployment-644dfd858 to 3 Normal ScalingReplicaSet 96s (x2 over 8m19s) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-6494477f49 to 0