Kubernetes学习日记(二)
暑期加入了沃天宇老师的实验室进行暑期的实习。在正式开始工作之前,师兄先让我了解一下技术栈,需要了解的有docker、k8s、springboot、springcloud。
谨以一系列博客记录一下自己学习的笔记。更多内容见Github
- 上一篇 Kubernetes学习笔记(一)https://www.cnblogs.com/SnowPhoenix/p/14983656.html
2021/7/11
部署应用
用minikube启动集群,然后用kubectl打开dashboard后,创建应用:
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
跟上次一样,无法获取镜像,在dockerhub上换个镜像:
kubectl create deployment kubernetes-bootcamp --image=hhitzhl/kubernetes-bootcamp
可以从dashboard看到已经成功启动了(kubectl get pods也可以)。
此时,集群中的机器都运行在一个私有的、隔离的网络中,为了将其暴露出来,我们打开一个新的终端,使用以下工具:
kubectl proxy
该工具启动了一个服务,将集群和主机之间的通信进行转发,从控制台输出的信息中得知其服务位于127.0.0.1:8001
。
用浏览器进行访问(注意,不是localhost),可以看到这里列出了所有可以访问的url,随便打开了几个,都获得了结果:
通过dashboard或者kubectl get pods
获取到pods的NAME
属性,然后通过访问以下地址获取它的详细信息:
http://127.0.0.1:8001/api/v1/namespaces/default/pods/${NAME}
注意把${NAME}
替换为pods的NAME
。
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "kubernetes-bootcamp-69b8fbf65b-s245b",
"generateName": "kubernetes-bootcamp-69b8fbf65b-",
"namespace": "default",
"uid": "6d4c1f4b-6f5f-4039-8115-95083b2e9db2",
"resourceVersion": "1855",
"creationTimestamp": "2021-07-11T08:33:35Z",
"labels": {
"app": "kubernetes-bootcamp",
"pod-template-hash": "69b8fbf65b"
},
"ownerReferences": [
{
"apiVersion": "apps/v1",
"kind": "ReplicaSet",
"name": "kubernetes-bootcamp-69b8fbf65b",
"uid": "766d5f6c-9b2e-4895-a9d6-ccd3c9462389",
"controller": true,
"blockOwnerDeletion": true
}
],
"managedFields": [
{
"manager": "kube-controller-manager",
"operation": "Update",
"apiVersion": "v1",
"time": "2021-07-11T08:33:35Z",
"fieldsType": "FieldsV1",
"fieldsV1": {"f:metadata":{"f:generateName":{},"f:labels":{".":{},"f:app":{},"f:pod-template-hash":{}},"f:ownerReferences":{".":{},"k:{"uid":"766d5f6c-9b2e-4895-a9d6-ccd3c9462389"}":{".":{},"f:apiVersion":{},"f:blockOwnerDeletion":{},"f:controller":{},"f:kind":{},"f:name":{},"f:uid":{}}}},"f:spec":{"f:containers":{"k:{"name":"kubernetes-bootcamp"}":{".":{},"f:image":{},"f:imagePullPolicy":{},"f:name":{},"f:resources":{},"f:terminationMessagePath":{},"f:terminationMessagePolicy":{}}},"f:dnsPolicy":{},"f:enableServiceLinks":{},"f:restartPolicy":{},"f:schedulerName":{},"f:securityContext":{},"f:terminationGracePeriodSeconds":{}}}
},
{
"manager": "kubelet",
"operation": "Update",
"apiVersion": "v1",
"time": "2021-07-11T08:34:01Z",
"fieldsType": "FieldsV1",
"fieldsV1": {"f:status":{"f:conditions":{"k:{"type":"ContainersReady"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{"type":"Initialized"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{"type":"Ready"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}}},"f:containerStatuses":{},"f:hostIP":{},"f:phase":{},"f:podIP":{},"f:podIPs":{".":{},"k:{"ip":"172.17.0.6"}":{".":{},"f:ip":{}}},"f:startTime":{}}}
}
]
},
"spec": {
"volumes": [
{
"name": "default-token-klcvh",
"secret": {
"secretName": "default-token-klcvh",
"defaultMode": 420
}
}
],
"containers": [
{
"name": "kubernetes-bootcamp",
"image": "hhitzhl/kubernetes-bootcamp",
"resources": {
},
"volumeMounts": [
{
"name": "default-token-klcvh",
"readOnly": true,
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
}
],
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"serviceAccountName": "default",
"serviceAccount": "default",
"nodeName": "minikube",
"securityContext": {
},
"schedulerName": "default-scheduler",
"tolerations": [
{
"key": "node.kubernetes.io/not-ready",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
},
{
"key": "node.kubernetes.io/unreachable",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
}
],
"priority": 0,
"enableServiceLinks": true,
"preemptionPolicy": "PreemptLowerPriority"
},
"status": {
"phase": "Running",
"conditions": [
{
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2021-07-11T08:33:35Z"
},
{
"type": "Ready",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2021-07-11T08:34:01Z"
},
{
"type": "ContainersReady",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2021-07-11T08:34:01Z"
},
{
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2021-07-11T08:33:35Z"
}
],
"hostIP": "192.168.49.2",
"podIP": "172.17.0.6",
"podIPs": [
{
"ip": "172.17.0.6"
}
],
"startTime": "2021-07-11T08:33:35Z",
"containerStatuses": [
{
"name": "kubernetes-bootcamp",
"state": {
"running": {
"startedAt": "2021-07-11T08:34:01Z"
}
},
"lastState": {
},
"ready": true,
"restartCount": 0,
"image": "hhitzhl/kubernetes-bootcamp:latest",
"imageID": "docker-pullable://hhitzhl/kubernetes-bootcamp@sha256:1214cd7a1ad192e92505aa2162e84da00bed459b27fb840d51832ac111f7e641",
"containerID": "docker://b62f33fec376e8ae17d3299553dc0d7eca0dd34b7d39344e9cadd160629d3136",
"started": true
}
],
"qosClass": "BestEffort"
}
}
我们不仅可以看到这个pod的metadata,还有其spec(规约)和status,我们之前的学习知道它们分别代表了pod的期望状态和实际状态。
查看应用
当一个pod运行起来之后,我们可能需要查看这个pod的状态/日志,这是我们手动定位/处理错误所必须的手段。
- kubectl get - 列出资源
- kubectl describe - 显示有关资源的详细信息
- kubectl logs - 打印 pod 和其中容器的日志
- kubectl exec - 在 pod 中的容器上执行命令
前三个都是相对比较简单的,在后面加上需要查看的资源即可。资源可以包括(括号内为缩写,这里列出的不全):
- all
- pod(po)
- service(svc)
- deployment(deploy)
kubectl exec
比较复杂,但其用法和docker exec
有些相像:
kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...] [options]
一些示例:
- 一次性执行,查看环境变量:
kubectl exec kubernetes-bootcamp-xxxx -- env
- 交互式执行:
kubectl exec -it kubernetes-bootcamp-xxxx -- bash
使用Service将应用暴露到公网
Service(服务)是一个抽象概念,它定义了一个由pods组成的逻辑集合,同时定义了访问它们的协议。
我们通常用YAML文件来描述一个服务。在服务的spec(规约)中,我们可以通过type
属性指定其工作的方式:
- ClusterIP (默认) - 在集群的内部 IP 上公开 Service 。这种类型使得 Service 只能从集群内访问。
- NodePort - 使用 NAT 在集群中每个选定 Node 的相同端口上公开 Service 。使用
<NodeIP>:<NodePort>
从集群外部访问 Service。是 ClusterIP 的超集。 - LoadBalancer - 在当前云中创建一个外部负载均衡器(如果支持的话),并为 Service 分配一个固定的外部IP。是 NodePort 的超集。
- ExternalName - 通过返回带有该名称的 CNAME 记录,使用任意名称(由 spec 中的externalName指定)公开 Service。不使用代理。这种类型需要kube-dns的v1.7或更高版本。
我们使用以下命令来启动一个Service,这个Service将我们之前创建的deployment
暴露到minikube的8080端口中:
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
通过kubectl get services
可以看到多了一个叫做kubernetes-bootcamp
的服务,并且其端口号情况为8080:31761/TCP
,意思应该是将deployment中的8080端口映射到service的这个node的71761端口。
此时端口被映射到了node中,为了在我们的电脑上访问,我们还需要进行一次转发:
minikube service kubernetes-bootcamp
然后打开访问的URL:
使用标签
通过kubectl describe
指令我们可以看到我们启动的deployment有一个标签app=kubernetes-bootcamp
:
PS D:xxxLearning> kubectl describe deploy kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Sun, 11 Jul 2021 21:33:56 +0800
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: hhitzhl/kubernetes-bootcamp
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-69b8fbf65b (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set kubernetes-bootcamp-69b8fbf65b to 1
我们之前的学习了解到,标签是一个键值对,在我们启动的这个deployment中,app
为键,kubernetes-bootcamp
为值。
在使用get命令时,我们可以通过-l
选项来指定标签的筛选规则:
kubectl get svc -l app=kubenetes-bootcamp
相比于不加-l
选项,经过筛选的结果少了一个启动时默认的服务kubenetes
。
我们可以通过以下命令来添加标签:
kubectl label <TYPE> <NAME> <KEY_1>=<VAL_1> <KEY_2>=<VAL_2> ...
比如通过如下指令为我们启动的deployment添加一个version=v1
的标签:
kubectl label deploy kubernetes-bootcamp version=v1
再通过kubectl describe
来查看的时候就会发现多了一个标签:
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl describe deploy kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Sun, 11 Jul 2021 21:33:56 +0800
Labels: app=kubernetes-bootcamp
version=v1
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: hhitzhl/kubernetes-bootcamp
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-69b8fbf65b (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 28m deployment-controller Scaled up replica set kubernetes-bootcamp-69b8fbf65b to 1
利用这一点,我们就可以方便地停止我们所创建的deployment、service:
kubectl delete po,svc -l app=kubernetes-bootcamp
水平伸缩
启动了deployment和service后,输入kubectl get rs
可以看到如下信息:
PS D:xxxLearning> kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-69b8fbf65b 1 1 1 21m
其中DESIRED
表示期望能够运行多少个pods
实例,而CURRENT
表示实际上当前已经启动的实例数目,READY
表示已经启动并且处于可用状态的实力数量。若我们想增加实例数目,只需要修改DESIRED
数量即可:
kubectl scale deployments/kubernetes-bootcamp --replicas=4
我们趁实例还没进入可用状态,快速通过kubectl get rs
、kubectl get pods
查看信息:
PS D:xxxLearning> kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 24m
kubernetes-bootcamp-69b8fbf65b-qcldn 0/1 ContainerCreating 0 1s
kubernetes-bootcamp-69b8fbf65b-vzmr7 0/1 ContainerCreating 0 1s
kubernetes-bootcamp-69b8fbf65b-w54hq 0/1 ContainerCreating 0 1s
PS D:xxxLearning> kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-69b8fbf65b 4 4 1 24m
PS D:xxxLearning> kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 24m
kubernetes-bootcamp-69b8fbf65b-qcldn 0/1 ContainerCreating 0 7s
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Running 0 7s
kubernetes-bootcamp-69b8fbf65b-w54hq 0/1 ContainerCreating 0 7s
PS D:xxxLearning> kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-69b8fbf65b 4 4 4 24m
通过kubectl get pods -o wide
我们可以查看更多的信息:
PS D:xxxLearning> kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 27m 172.17.0.5 minikube <none>
<none>
kubernetes-bootcamp-69b8fbf65b-qcldn 1/1 Running 0 2m45s 172.17.0.7 minikube <none>
<none>
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Running 0 2m45s 172.17.0.6 minikube <none>
<none>
kubernetes-bootcamp-69b8fbf65b-w54hq 1/1 Running 0 2m45s 172.17.0.8 minikube <none>
<none>
可以看到,启动的这几个pods
都被分配到了同一个节点,但是拥有不同的IP。
通过kubectl describe pods kubernetes-bootcamp
查看详细信息可以看到:
PS D:xxxLearning> kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Sun, 11 Jul 2021 22:15:42 +0800
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: hhitzhl/kubernetes-bootcamp
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-69b8fbf65b (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 30m deployment-controller Scaled up replica set kubernetes-bootcamp-69b8fbf65b to 1
Normal ScalingReplicaSet 6m19s deployment-controller Scaled up replica set kubernetes-bootcamp-69b8fbf65b to 4
Replicas
中记录了kubectl get rs
中展示的信息,Events
中记录了我们将实力数量调整为4的事件。
通过minikube service kubernetes-bootcamp
将其转发到我们的机器上后,用不同的http客户端(浏览器/Postman/curl)打开,可以发现不同客户端的请求被分配到了不同的pods
实例中。
但是同一个客户端连续请求将会被转发到同一个实例中,最开始我以为是浏览器的缓存问题,但是在浏览器禁用缓存/使用Postman的连续请求仍旧使用的是同一个实例,但是间隔一段时间就可能会变成不同的实例。
通过kubectl scale
将期望实例数目削减到2,然后查看情况:
PS D:xxxLearning> kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
PS D:xxxLearning> kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 50m
PS D:xxxLearning> kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-69b8fbf65b 2 2 2 50m
PS D:xxxLearning> kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 50m 172.17.0.5 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-qcldn 1/1 Terminating 0 26m 172.17.0.7 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Running 0 26m 172.17.0.6 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-w54hq 1/1 Terminating 0 26m 172.17.0.8 minikube <none> <none>
可以看到有两个实例进入了Terminating
状态,一段时间后get po
就看不到这两个实例了。
更新与回滚
在有多个实例的时候,k8s会进行滚动更新,会有一部分实例进入不可用状态,以进行更新,这样会保证始终有可用实例提供服务。每一步更新的实例数默认为1,也可以设置为某个数值或者百分比,每次更新的实例数量越多,更新速度越快,但是更新期间的性能损失越大。
在k8s中,更新时经过版本控制的,任何 Deployment 更新都可以恢复到以前的(稳定)版本。
先将pods实例恢复为4,然后通过以下命令为我们已经启动的depolyments指定新的镜像:
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
然后马上查看其状态:
PS D:xxxLearningk8smarkdowns> kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
PS D:xxxLearning> kubectl get po
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-2xhsn 1/1 Running 0 109s
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 78m
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Running 0 54m
kubernetes-bootcamp-69b8fbf65b-x6rp6 1/1 Terminating 0 109s
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 0/1 ContainerCreating 0 11s
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 0/1 ContainerCreating 0 11s
PS D:xxxLearning> kubectl get po
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-2xhsn 1/1 Terminating 0 2m4s
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Running 0 78m
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Running 0 54m
kubernetes-bootcamp-69b8fbf65b-x6rp6 1/1 Terminating 0 2m4s
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 0/1 ContainerCreating 0 26s
kubernetes-bootcamp-6dbb8c9d8b-gnmln 0/1 ContainerCreating 0 2s
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Running 0 26s
PS D:xxxLearning> kubectl get po
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-2xhsn 1/1 Terminating 0 2m15s
kubernetes-bootcamp-69b8fbf65b-frk7w 1/1 Terminating 0 78m
kubernetes-bootcamp-69b8fbf65b-vzmr7 1/1 Terminating 0 54m
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Running 0 10s
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Running 0 37s
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Running 0 13s
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Running 0 37s
PS D:xxxLearning> kubectl get po
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Running 0 96s
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Running 0 2m3s
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Running 0 99s
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Running 0 2m3s
可以看到这个滚动更新的过程,始终有3个实例在运行(当更新错误的时候,比如镜像无法拉取到,似乎会一直卡在这个状态)。
此时再通过minikube service
转发并查看(如果之前没关,并不需要重新启动),可以看到现在的信息已经被更新了(之前是v=1
,现在是v=2
):
此时查看服务的情况,Endpoints
自动更新成了新的实例的IP:
PS D:xxxLearning> 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.100.166.67
IPs: 10.100.166.67
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31087/TCP
Endpoints: 172.17.0.10:8080,172.17.0.11:8080,172.17.0.12:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
PS D:xxxLearning> kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Running 0 7m39s 172.17.0.12 minikube <none>
<none>
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Running 0 8m6s 172.17.0.10 minikube <none>
<none>
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Running 0 7m42s 172.17.0.11 minikube <none>
<none>
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Running 0 8m6s 172.17.0.9 minikube <none>
<none>
我们还可以通过以下指令来确认滚动更新已经完成:
kubectl rollout status deploy/kubernetes-bootcamp
通过以下指令可以进行回滚:
kubectl rollout undo deployments/kubernetes-bootcamp
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl rollout status deploy/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-b66p8 1/1 Running 0 12s
kubernetes-bootcamp-69b8fbf65b-cncrl 0/1 ContainerCreating 0 4s
kubernetes-bootcamp-69b8fbf65b-n8fgr 0/1 ContainerCreating 0 1s
kubernetes-bootcamp-69b8fbf65b-wn8sq 1/1 Running 0 12s
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Running 0 15m
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-69b8fbf65b-b66p8 1/1 Running 0 15s
kubernetes-bootcamp-69b8fbf65b-cncrl 1/1 Running 0 7s
kubernetes-bootcamp-69b8fbf65b-n8fgr 0/1 ContainerCreating 0 4s
kubernetes-bootcamp-69b8fbf65b-wn8sq 1/1 Running 0 15s
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Terminating 0 15m
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Terminating 0 15m
PS D:桌面杂项学习资料2020~2021-3Learning> kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-69b8fbf65b-b66p8 1/1 Running 0 27s 172.17.0.6 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-cncrl 1/1 Running 0 19s 172.17.0.7 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-n8fgr 1/1 Running 0 16s 172.17.0.8 minikube <none> <none>
kubernetes-bootcamp-69b8fbf65b-wn8sq 1/1 Running 0 27s 172.17.0.5 minikube <none> <none>
kubernetes-bootcamp-6dbb8c9d8b-5cljf 1/1 Terminating 0 15m 172.17.0.12 minikube <none> <none>
kubernetes-bootcamp-6dbb8c9d8b-dcrkb 1/1 Terminating 0 15m 172.17.0.10 minikube <none> <none>
kubernetes-bootcamp-6dbb8c9d8b-gnmln 1/1 Terminating 0 15m 172.17.0.11 minikube <none> <none>
kubernetes-bootcamp-6dbb8c9d8b-m2vz6 1/1 Terminating 0 15m 172.17.0.9 minikube <none> <none>
相比于更新,回滚的速度还是蛮快的,可能是因为不需要从远程仓库中拉取镜像的原因。
此时再通过minikube service
转发并查看(如果之前没关,并不需要重新启动),可以看到现在的信息已经恢复了(由v=2
变回了v=1
):
总结
前面的docker用来创建容器,而k8s则用来批量管理容器。
我们了解了k8s的基本结构,主要有2个部分,一个是控制面板,另一个是kubelet管理的一个个节点,前者负责总体的控制,后者是实际运行业务的部分。和这两个部分平行的还有一个云服务提供商。
在使用上,我们利用kubectl工具,进行了deployment的创建,然后将其暴露为service,进行了水平的伸缩操作,进行了滚动更新/回退,完成了k8s的基本操作。
k8s官网的教程还没有结束,我粗略看了一下,后面开始结合微服务了,所以等后面学习springboot、springcloud的时候再回来继续。
下一篇 Kubernetes学习日记(三)https://www.cnblogs.com/SnowPhoenix/p/15009070.html