Kubernetes k8s基础 学习笔记
Kubernetes k8s学习笔记Kubernetes 可以为您做些什么?通过现代的 Web 服务,用户希望应用程序能够 24/7 全天候使用,开发人员希望每天可以多次发布部署新版本的应用程序。 容器化可以帮助软件包达成这些目标,使应用程序能够以简单快速的方式发布和更新,而无需停机。Kubernetes 帮助您确保这些容器化的应用程序在您想要的时间和地点运行,并帮助应用程序找到它们需要的资源和工
Kubernetes k8s学习笔记
Kubernetes 可以为您做些什么?
通过现代的 Web 服务,用户希望应用程序能够 24/7 全天候使用,开发人员希望每天可以多次发布部署新版本的应用程序。 容器化可以帮助软件包达成这些目标,使应用程序能够以简单快速的方式发布和更新,而无需停机。Kubernetes 帮助您确保这些容器化的应用程序在您想要的时间和地点运行,并帮助应用程序找到它们需要的资源和工具。Kubernetes 是一个可用于生产的开源平台,根据 Google 容器集群方面积累的经验,以及来自社区的最佳实践而设计。
1. 查看k8s版本
minikube version
2. 运行k8s
minikube start
3. 查看客户端的版本
kubectl version
4. 查看所有nodes(节点)
kubectl get nodes
输出
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 48s v1.20.2
master和nodes
Master 负责管理整个集群。 Master 协调集群中的所有活动,例如调度应用、维护应用的所需状态、应用扩容以及推出新的更新。
Node 是一个虚拟机或者物理机,它在 Kubernetes 集群中充当工作机器的角色 每个Node都有 Kubelet , 它管理 Node 而且是 Node 与 Master 通信的代理。 Node 还应该具有用于处理容器操作的工具,例如 Docker 或 rkt 。处理生产级流量的 Kubernetes 集群至少应具有三个 Node 。
Master 管理集群,Node 用于托管正在运行的应用。
在 Kubernetes 上部署应用时,您告诉 Master 启动应用容器。 Master 就编排容器在集群的 Node 上运行。 Node 使用 Master 暴露的 Kubernetes API 与 Master 通信。终端用户也可以使用 Kubernetes API 与集群交互。
Kubernetes 既可以部署在物理机上也可以部署在虚拟机上。您可以使用 Minikube 开始部署 Kubernetes 集群。 Minikube 是一种轻量级的 Kubernetes 实现,可在本地计算机上创建 VM 并部署仅包含一个节点的简单集群。 Minikube 可用于 Linux , macOS 和 Windows 系统。Minikube CLI 提供了用于引导集群工作的多种操作,包括启动、停止、查看状态和删除。在本教程里,您可以使用预装有 Minikube 的在线终端进行体验。
既然您已经知道 Kubernetes 是什么,让我们转到在线教程并启动我们的第一个 Kubernetes 集群!
5. 查看所有Pod
kubectl get pods
Kubernetes Pods
在模块 2创建 Deployment 时, Kubernetes 添加了一个 Pod 来托管你的应用实例。Pod 是 Kubernetes 抽象出来的,表示一组一个或多个应用程序容器(如 Docker),以及这些容器的一些共享资源。这些资源包括:
共享存储,当作卷
网络,作为唯一的集群 IP 地址
有关每个容器如何运行的信息,例如容器映像版本或要使用的特定端口。
Pod 为特定于应用程序的“逻辑主机”建模,并且可以包含相对紧耦合的不同应用容器。例如,Pod 可能既包含带有 Node.js 应用的容器,也包含另一个不同的容器,用于提供 Node.js 网络服务器要发布的数据。Pod 中的容器共享 IP 地址和端口,始终位于同一位置并且共同调度,并在同一工作节点上的共享上下文中运行。
Pod是 Kubernetes 平台上的原子单元。 当我们在 Kubernetes 上创建 Deployment 时,该 Deployment 会在其中创建包含容器的 Pod (而不是直接创建容器)。每个 Pod 都与调度它的工作节点绑定,并保持在那里直到终止(根据重启策略)或删除。 如果工作节点发生故障,则会在群集中的其他可用工作节点上调度相同的 Pod。
6. 部署一个app
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get pods
输出
$ kubectl get podsNAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-57978f5f5d-4rmpx 1/1 Running 0 47s
7. 查看deployment(app)
kubectl get deployment
输出
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 3m11s
8. 打开proxy
proxy是nodes和宿主机通信的渠道,我们需要先打开proxy才能范围nodes里面的app
kubectl proxy
9. 查看pods的描述
kubectl describe pods
$ kubectl describe pods
Name: kubernetes-bootcamp-fb5c67579-p592z
Namespace: default
Priority: 0
Node: minikube/172.17.0.53
Start Time: Tue, 11 Jan 2022 01:00:18 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.5
IPs:
IP: 172.18.0.5
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://0f5ebb522eb3b976122f505824dca18bb9f31ef05329f89cfc821c8534b92fac
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 01:00:21 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-v8wdv (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-v8wdv:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-v8wdv
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 6m20s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-p592z to minikube
Normal Pulled 6m18s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 6m18s kubelet Created container kubernetes-bootcamp
Normal Started 6m17s kubelet Started container kubernetes-bootcamp
$
10. 查看pods的日志
注意: 这里直接指定了pods的名字,而没有指定容器的名字,这是因为这个pod里面有且仅有一个容器.
kubectl logs $POD_NAME
11. 进入容器的相关命令
进入pods里面容器的相关命令如下所示:
注意: 同理这里直接指定了pods的名字,而没有指定容器的名字,这是因为这个pod里面有且仅有一个容器.
查看容器环境变量
kubectl exec $POD_NAME -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-fb5c67579-p592z
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
进入容器的命令
kubectl exec -ti $POD_NAME -- bash
12. Kubernetes中的Service
Kubernetes Service 总览
Kubernetes Pod 是转瞬即逝的。 Pod 实际上拥有 生命周期。 当一个工作 Node 挂掉后, 在 Node 上运行的 Pod 也会消亡。 ReplicaSet 会自动地通过创建新的 Pod 驱动集群回到目标状态,以保证应用程序正常运行。 换一个例子,考虑一个具有3个副本数的用作图像处理的后端程序。这些副本是可替换的; 前端系统不应该关心后端副本,即使 Pod 丢失或重新创建。也就是说,Kubernetes 集群中的每个 Pod (即使是在同一个 Node 上的 Pod )都有一个唯一的 IP 地址,因此需要一种方法自动协调 Pod 之间的变更,以便应用程序保持运行。
Kubernetes 中的服务(Service)是一种抽象概念,它定义了 Pod 的逻辑集和访问 Pod 的协议。Service 使从属 Pod 之间的松耦合成为可能。 和其他 Kubernetes 对象一样, Service 用 YAML (更推荐) 或者 JSON 来定义. Service 下的一组 Pod 通常由 LabelSelector (请参阅下面的说明为什么您可能想要一个 spec 中不包含selector的服务)来标记。
尽管每个 Pod 都有一个唯一的 IP 地址,但是如果没有 Service ,这些 IP 不会暴露在集群外部。Service 允许您的应用程序接收流量。Service 也可以用在 ServiceSpec 标记type的方式暴露
- ClusterIP (默认) - 在集群的内部 IP 上公开 Service 。这种类型使得 Service 只能从集群内访问。
- NodePort - 使用 NAT 在集群中每个选定 Node 的相同端口上公开 Service 。使用: 从集群外部访问 Service。是 ClusterIP 的超集。
- LoadBalancer - 在当前云中创建一个外部负载均衡器(如果支持的话),并为 Service 分配一个固定的外部IP。是 NodePort 的超集。
ExternalName - 通过返回带有该名称的 CNAME 记录,使用任意名称(由 spec 中的externalName指定)公开 Service。不使用代理。这种类型需要kube-dns的v1.7或更高版本。
更多关于不同 Service 类型的信息可以在使用源 IP 教程。 也请参阅 连接应用程序和 Service 。
另外,需要注意的是有一些 Service 的用例没有在 spec 中定义selector。 一个没有selector创建的 Service 也不会创建相应的端点对象。这允许用户手动将服务映射到特定的端点。没有 selector 的另一种可能是您严格使用type: ExternalName来标记。
12.1 获取集群里面的services
kubectl get services
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m
$
12.2 暴露一个应用
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
查看这个已经暴露的应用,kubernetes-bootcamp是应用名字,NodePort 32402是暴露在外面的端口名称
kubectl describe services/kubernetes-bootcamp
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.107.150.239
IPs: 10.107.150.239
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32402/TCP
Endpoints: 172.18.0.4:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
13. label的相关操作
13.1 查看应用的deployment
下面的语句可以查看所有的deployment,其中Name字段即为label的名称
kubectl describe deployment
$ kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Tue, 11 Jan 2022 01:39:01 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 44m deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
13.2 使用label去查pod
kubectl get pods -l app=kubernetes-bootcamp
$ kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-k8296 1/1 Running 0 8m56s
13.3 新增为pod一个label
kubectl label pods $POD_NAME version=v1
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-fb5c67579-5kv4c
使用label去查看访问pods
kubectl describe pods $POD_NAME
14. 删除service
kubectl delete service -l app=kubernetes-bootcamp
15. deployment应用的扩容,高可用
把一个应用复制4份
查看应用 deployment
kubectl get deployments
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 0/1 0 0 7s
$
- NAME lists the names of the Deployments in the cluster.
- READY shows the ratio of CURRENT/DESIRED replicas
- UP-TO-DATE displays the number of replicas that have been updated to achieve the desired state.
- AVAILABLE displays how many replicas of the application are available to your users.
AGE displays the amount of time that the application has been running.
查看应用的副本
kubectl get rs
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-fb5c67579 1 1 1 60s
- DESIRED displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.
- CURRENT displays how many replicas are currently running.
将应用复制成4份
kubectl scale deployments/kubernetes-bootcamp --replicas=4
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
重新查看应用,已经有4个副本
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 3m51s
Pods也被复制成4个
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-kcl2v 1/1 Running 0 4m24s 172.18.0.4 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-mp5x5 1/1 Running 0 95s 172.18.0.9 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-nl48g 1/1 Running 0 95s 172.18.0.7 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-ppzw5 1/1 Running 0 95s 172.18.0.8 minikube <none> <none>
查看deployment的描述,发现有4个replicas了
$ kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Tue, 11 Jan 2022 03:28:11 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m30s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
Normal ScalingReplicaSet 3m40s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 4
16. deployment应用的负载均衡
查看系统中的的deployment
kubectl describe services/kubernetes-bootcamp
获取k8s外暴露端口
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
访问程序,发现每次访问的pod是不一样的
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-fgqdc | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-fgqdc | v=1
$ curl $(minikube ip):$NODE_PORT
curl $(minikube ip):$NODE_PORT
curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-fgqdc | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-cw855 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-8hmn7 | v=1
17. 缩减应用的副本
下面把应用缩减到2个副本
kubectl scale deployments/kubernetes-bootcamp --replicas=2
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
查看deployment发现已经缩减成2
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 7m49s
查看pods数量发现剩下2个
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-cw855 1/1 Running 0 6m51s 172.18.0.7 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-fgqdc 1/1 Running 0 7m57s 172.18.0.5 minikube <none> <none>
17. k8s的滚动更新
查看所有的deployment
可见是1个deployment+3个replicas
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 0/4 0 0 9s
查看所有的pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-hjwkm 1/1 Running 0 41s
kubernetes-bootcamp-fb5c67579-jpckv 1/1 Running 0 41s
kubernetes-bootcamp-fb5c67579-lkwxf 1/1 Running 0 41s
kubernetes-bootcamp-fb5c67579-nnmmn 1/1 Running 0 41s
查看pods的介绍
这里一共有4个pods,然后相对应的Image为:
gcr.io/google-samples/kubernetes-bootcamp:v1
$ kubectl describe pods
Name: kubernetes-bootcamp-fb5c67579-hjwkm
Namespace: default
Priority: 0
Node: minikube/172.17.0.15
Start Time: Tue, 11 Jan 2022 06:38:53 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.3
IPs:
IP: 172.18.0.3
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://9bb4fcdc18fefe521bbae0ebab4f2c0073488307be6a4398c440dcbf352889d3
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 06:38:57 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-j6nkn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-j6nkn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-j6nkn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-hjwkm to minikube
Normal Pulled 2m2s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 2m2s kubelet Created container kubernetes-bootcamp
Normal Started 2m1s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-jpckv
Namespace: default
Priority: 0
Node: minikube/172.17.0.15
Start Time: Tue, 11 Jan 2022 06:38:53 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.2
IPs:
IP: 172.18.0.2
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://bc3f20b0764db96462fb9b908cd47dba61d2aec102658b0104232ded9d542b36
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 06:38:57 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-j6nkn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-j6nkn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-j6nkn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-jpckv to minikube
Normal Pulled 2m2s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 2m2s kubelet Created container kubernetes-bootcamp
Normal Started 2m1s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-lkwxf
Namespace: default
Priority: 0
Node: minikube/172.17.0.15
Start Time: Tue, 11 Jan 2022 06:38:53 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.4
IPs:
IP: 172.18.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://7bfe66523f83812467912fe95c7f9b9c9ba30f9a58a0ebe3f7fa39857d0fccb1
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 06:38:58 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-j6nkn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-j6nkn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-j6nkn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-lkwxf to minikube
Normal Pulled 2m2s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 2m2s kubelet Created container kubernetes-bootcamp
Normal Started 2m kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-nnmmn
Namespace: default
Priority: 0
Node: minikube/172.17.0.15
Start Time: Tue, 11 Jan 2022 06:38:53 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.5
IPs:
IP: 172.18.0.5
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://6975f847f6662dfc4c63b865b15ec6a6f39ea3bcee00d5091294dd7bff248925
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 06:38:58 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-j6nkn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-j6nkn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-j6nkn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-nnmmn to minikube
Normal Pulled 2m2s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 2m2s kubelet Created container kubernetes-bootcamp
Normal Started 2m kubelet Started container kubernetes-bootcamp
升级pods的deployment
这里升级到V2的版本
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
查看更新的过程
可以看到无感更新的过程,旧的pod被terminated新的被created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-4l5ws 0/1 ContainerCreating 0 0s
kubernetes-bootcamp-7d44784b7c-9fxz5 0/1 ContainerCreating 0 5s
kubernetes-bootcamp-7d44784b7c-xg5cr 1/1 Running 0 5s
kubernetes-bootcamp-fb5c67579-n7xwk 1/1 Running 0 49s
kubernetes-bootcamp-fb5c67579-p6gs8 1/1 Running 0 50s
kubernetes-bootcamp-fb5c67579-rq88z 1/1 Terminating 0 49s
kubernetes-bootcamp-fb5c67579-zvpk5 1/1 Terminating 0 49s
再次查看pods发现Image的版本已经是v2
$ kubectl describe pods
Name: kubernetes-bootcamp-7d44784b7c-4l5ws
Namespace: default
Priority: 0
Node: minikube/172.17.0.116
Start Time: Tue, 11 Jan 2022 06:50:47 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.12
IPs:
IP: 172.18.0.12
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://a255b25824d3aeb0925c87be994039fb85b9abcf36b599b4cb66acf58cd662de
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 11 Jan 2022 06:50:51 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-twlzs (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-twlzs:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-twlzs
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m42s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-4l5ws to minikube
Normal Pulled 2m40s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 2m39s kubelet Created container kubernetes-bootcamp
Normal Started 2m38s kubelet Started container kubernetes-bootcamp
验证是否升级成功
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.109.66.156
IPs: 10.109.66.156
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32079/TCP
Endpoints: 172.18.0.10:8080,172.18.0.11:8080,172.18.0.12:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
记录访问端口
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
访问以后看到已经是v2版本
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7d44784b7c-h6plc | v=2
18. 回滚更新
先升级一个不存在的版本
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.apps/kubernetes-bootcamp image updated
发现部分节点报错了
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-59b7598c77-gnp7q 0/1 ErrImagePull 0 45s
kubernetes-bootcamp-59b7598c77-qs5x4 0/1 ErrImagePull 0 46s
kubernetes-bootcamp-7d44784b7c-4l5ws 1/1 Running 0 27m
kubernetes-bootcamp-7d44784b7c-9fxz5 1/1 Running 0 27m
kubernetes-bootcamp-7d44784b7c-xg5cr 1/1 Running
参看pods的详情
kubectl describe pods
发现了v10版本不存在的报错
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 85s default-scheduler Successfully assigned default/kubernetes-bootcamp-59b7598c77-qs5x4 to minikube
Normal Pulling 39s (x3 over 80s) kubelet Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 39s (x3 over 80s) kubelet Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found: manifest unknown: Failed to fetch "v10" from request "/v2/google-samples/kubernetes-bootcamp/manifests/v10".
Warning Failed 39s (x3 over 80s) kubelet Error: ErrImagePull
Normal BackOff 10s (x5 over 79s) kubelet Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 10s (x5 over 79s) kubelet Error: ImagePullBackOff
回滚到上一个stable的版本
kubectl rollout undo deployments/kubernetes-bootcamp
$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
再次查看pods发现回滚已经成功了
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-4l5ws 1/1 Running 0 30m
kubernetes-bootcamp-7d44784b7c-9fxz5 1/1 Running 0 30m
kubernetes-bootcamp-7d44784b7c-tdht7 1/1 Running 0 42s
kubernetes-bootcamp-7d44784b7c-xg5cr 1/1 Running 0 30m
更多推荐
所有评论(0)