K8S的学习之旅-基础知识
K8S的学习之旅-基础知识使用minikube创建集群启动docker后,启动minikubeC:\Users\Caisi>minikube start* Microsoft Windows 10 Education 10.0.18363 Build 18363 上的 minikube v1.16.0* 根据现有的配置文件使用 docker 驱动程序* Starting control pl
·
K8S的学习之旅-基础知识
使用minikube创建集群
- 启动docker后,启动minikube
C:\Users\Caisi>minikube start
* Microsoft Windows 10 Education 10.0.18363 Build 18363 上的 minikube v1.16.0
* 根据现有的配置文件使用 docker 驱动程序
* Starting control plane node minikube in cluster minikube
* Restarting existing docker container for "minikube" ...
! This container is having trouble accessing https://k8s.gcr.io
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* 正在 Docker 20.10.0 中准备 Kubernetes v1.20.0…
* Verifying Kubernetes components...
* Enabled addons: storage-provisioner, default-storageclass, dashboard
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
- 查看集群详情
C:\Users\Caisi>kubectl cluster-info
Kubernetes master is running at https://127.0.0.1:55000
KubeDNS is running at https://127.0.0.1:55000/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
- 查看结点
C:\Users\Caisi>kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 4d1h v1.20.0
部署应用
- 什么是deployments
- 应用高可用。deployment提供了一种自我修复机制来解决机器故障维护问题
- deployment就像是Hadoop的zookeeper一样,监控各个应用,如果某个节点宕掉了,deployment将在集群的另外一个节点上部署。这就是想要删除应用必须先删除deployment的应用,否则节点的应用被删除后,deployment将启动故障修复应用。
- 创建deployment
C:\Users\Caisi>kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
# 查看deployments
C:\Users\Caisi>kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
hello-minikube 4/4 4 4 3d23h
kubernetes-bootcamp 0/1 1 0 2m29s
# 查看pods,不出意外,还是拉取失败了
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-8947b65df-2n7x9 1/1 Running 2 3d18h
hello-minikube-8947b65df-g78l7 1/1 Running 3 3d23h
hello-minikube-8947b65df-ptzbp 1/1 Running 2 3d17h
hello-minikube-8947b65df-ql5vm 1/1 Running 2 3d17h
kubernetes-bootcamp-57978f5f5d-pwvs7 0/1 ImagePullBackOff 0 2m11s
# 查看pods详情
C:\Users\Caisi>kubectl describe pods kubernetes-bootcamp-57978f5f5d-pwvs7
Normal Pulling 2m35s (x4 over 4m41s) kubelet Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v1"
Warning Failed 2m20s (x4 over 4m25s) kubelet Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v1": rpc error: code = Unknown desc = Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Warning Failed 2m20s (x4 over 4m25s) kubelet Error: ErrImagePull
Normal BackOff 111s (x6 over 4m25s) kubelet Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v1"
Warning Failed 97s (x7 over 4m25s) kubelet Error: ImagePullBackOff
# 删除deployments和pods,换阿里云的kubernetes-bootcamp镜像
阿里云的kubernetes-bootcamp在线镜像
# 这里的镜像可以是本地的docker镜像,也可以是远程的docker hub上的镜像,也可以是在线的url镜像
C:\Users\Caisi>kubectl create deployment kubernetes-bootcamp --image=registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-8947b65df-2n7x9 1/1 Running 2 3d18h
hello-minikube-8947b65df-g78l7 1/1 Running 3 4d
hello-minikube-8947b65df-ptzbp 1/1 Running 2 3d18h
hello-minikube-8947b65df-ql5vm 1/1 Running 2 3d18h
kubernetes-bootcamp-ccd8cdbf6-ftz2n 0/1 ContainerCreating 0 5s
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-minikube-8947b65df-2n7x9 1/1 Running 2 3d18h
hello-minikube-8947b65df-g78l7 1/1 Running 3 4d
hello-minikube-8947b65df-ptzbp 1/1 Running 2 3d18h
hello-minikube-8947b65df-ql5vm 1/1 Running 2 3d18h
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 13m
查看部署的程序
- 首先需要明确一点的是刚刚创建的kubernetes-bootcamp只能进行内部之间的访问,也可以通过kubectl API进行访问。
- 下面将开启一个代理来让外部可以进行访问。
- 开启代理
# 注意:保持此窗口
C:\Users\Caisi>kubectl proxy
Starting to serve on 127.0.0.1:8001
- 通过API查询版本
C:\Users\Caisi>curl http://localhost:8001/version
{
"major": "1",
"minor": "20",
"gitVersion": "v1.20.0",
"gitCommit": "af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38",
"gitTreeState": "clean",
"buildDate": "2020-12-08T17:51:19Z",
"goVersion": "go1.15.5",
"compiler": "gc",
"platform": "linux/amd64"
}
- 获取pod名称,并保存到环境变量中
# 注意:这里在Windows下要使用powershell工具
PS C:\Users\Caisi> $env:POD_NAME=kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{end}}'
PS C:\Users\Caisi> $env:POD_NAME
kubernetes-bootcamp-ccd8cdbf6-ftz2n
C:\Users\Caisi> kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 47m
pod和工作节点
什么是pod?
- Pod是kubernetes抽象出来的,表示一组一个或多个应用程序容器,以及一些容器的一些共享资源。如,共享存储、网络IP、运行信息。
- Pod是kubernetes平台上的原子单元。当创建deployment是首先会在其中创建包含容器的Pod,而不是直接创建容器。
什么是工作节点
- 通常一个pod总是运行在工作节点上。而工作节点又由主节点管理。
查看应用信息
- 查看Pod
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 97m
- 查看Pod详情
C:\Users\Caisi>kubectl describe pods
Name: kubernetes-bootcamp-ccd8cdbf6-ftz2n
Namespace: default
Priority: 0
Node: minikube/192.168.49.2
Start Time: Thu, 21 Jan 2021 11:19:41 +0800
Labels: app=kubernetes-bootcamp
pod-template-hash=ccd8cdbf6
Annotations: <none>
Status: Running
IP: 172.17.0.9
IPs:
IP: 172.17.0.9
Controlled By: ReplicaSet/kubernetes-bootcamp-ccd8cdbf6
Containers:
kubernetes-bootcamp:
Container ID: docker://6f05bd5069e740c331f5d65ebb770745d7f91919733dacaf81e434563308c367
Image: registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v1
Image ID: docker-pullable://registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp@sha256:34e5a47d302ee20039e5f0eb1e2f49785dafee3d97cac704befba6c1c7c938fc
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 21 Jan 2021 11:20:13 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-47bf2 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-47bf2:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-47bf2
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: <none>
- 使用curl查看应用输出
# 这里需要开启代理(如果您已经关闭,重新开启:kubectl proxy)
# 这里直接使用kubernetes-bootcamp-ccd8cdbf6-ftz2n的默认80端口无法访问,在GitHub issue上看到可以用8080端口实现
C:\Users\Caisi>curl http://localhost:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-ccd8cdbf6-ftz2n:8080/proxy/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-ccd8cdbf6-ftz2n | v=1
Issue with k8s.io/docs/tutorials/kubernetes-basics/explore/explore-interactive/#18079
- 查看日志
# 这里使用了上一节的环境变量,也可以直接用kubernetes-bootcamp-ccd8cdbf6-ftz2n替换
PS C:\Users\Caisi> kubectl logs $env:POD_NAME
Kubernetes Bootcamp App Started At: 2021-01-21T03:20:14.032Z | Running On: kubernetes-bootcamp-ccd8cdbf6-ftz2n
Running On: kubernetes-bootcamp-ccd8cdbf6-ftz2n | Total Requests: 1 | App Uptime: 7002.931 seconds | Log Time: 2021-01-21T05:16:56.963Z
- 列出Pod的所有环境变量
PS C:\Users\Caisi> kubectl exec $env:POD_NAME env
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-ccd8cdbf6-ftz2n
HELLO_MINIKUBE_SERVICE_HOST=10.102.25.156
HELLO_MINIKUBE_PORT_8080_TCP=tcp://10.102.25.156:8080
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
HELLO_MINIKUBE_SERVICE_PORT=8080
HELLO_MINIKUBE_PORT_8080_TCP_PROTO=tcp
HELLO_MINIKUBE_PORT_8080_TCP_PORT=8080
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP_PORT=443
HELLO_MINIKUBE_PORT=tcp://10.102.25.156:8080
HELLO_MINIKUBE_PORT_8080_TCP_ADDR=10.102.25.156
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
- 开启一个bash会话,相当于进入虚拟机的shell中
PS C:\Users\Caisi> kubectl exec -ti $env:POD_NAME bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@kubernetes-bootcamp-ccd8cdbf6-ftz2n:/#
- 查看Node.js应用代码
root@kubernetes-bootcamp-ccd8cdbf6-ftz2n:/# cat server.js
var http = require('http');
var requests=0;
var podname= process.env.HOSTNAME;
var startTime;
var host;
var handleRequest = function(request, response) {
response.setHeader('Content-Type', 'text/plain');
response.writeHead(200);
response.write("Hello Kubernetes bootcamp! | Running on: ");
response.write(host);
response.end(" | v=1\n");
console.log("Running On:" ,host, "| Total Requests:", ++requests,"| App Uptime:", (new Date() - startTime)/1000 , "seconds", "| Log Time:",new Date());
}
var www = http.createServer(handleRequest);
www.listen(8080,function () {
startTime = new Date();;
host = process.env.HOSTNAME;
console.log ("Kubernetes Bootcamp App Started At:",startTime, "| Running On: " ,host, "\n" );
});
- 查看Node.js应用是否在运行
# 从这里也可以看出pod内部对应的端口为8080
root@kubernetes-bootcamp-ccd8cdbf6-ftz2n:/# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-ccd8cdbf6-ftz2n | v=1
- 退出bash使用:exit。这一点与docker进入容器相似。
使用services公开暴露应用
什么是service?
- kubernetes中的service是一种抽象概念,它定义Pod的逻辑集和访问Pod的协议。
- service是为了解决工作节点挂掉后Pod也会消亡的问题,以保证程序正常运行。
service的暴露方式
- clusterIP(默认):在集群的内部IP上公开service。这种方式只能从集群内访问。
- NodePort:使用NAT在集群中每个选定Node的相同端口上公开service。这种方式可以使用:从集群外部访问service。
- LoadBalancer:创建一个外部负载均衡器,为service分配一个固定的外部IP。
- externalName:通过返回带有该名称的CNAME记录,使用任意名称公开Service。不使用代理。
service和label
- service匹配一组pod是使用label和selector,label是附加在对象上的键/值对。
暴露应用
- 使用expose命令暴露NodePort类型
C:\Users\Caisi>kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service/kubernetes-bootcamp exposed
C:\Users\Caisi>kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d5h
kubernetes-bootcamp NodePort 10.108.3.185 <none> 8080:31491/TCP 20s
- 使用curl从外部访问程序(这里又有一个docker desktop的坑了)
C:\Users\Caisi>curl 127.0.0.1:31491
curl: (7) Failed to connect to 127.0.0.1 port 31491: Connection refused
Failed to connect to 127.0.0.1 port 50502: Connection refused #8651:
NodePort deployments don’t work with Docker Desktop, unfortunately. Only with Docker Engine.
没有找到合适的解决方法,下面换tunnel暴露应用
# 开启tunnel,保存窗口状态
PS C:\Users\Caisi> minikube tunnel
* Starting tunnel for service kubernetes-bootcamp.
- 使用LoadBalancer暴露
C:\Users\Caisi>
service/kubernetes-bootcamp exposed
C:\Users\Caisi>kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d6h
kubernetes-bootcamp LoadBalancer 10.96.125.12 127.0.0.1 8080:30530/TCP 21s
C:\Users\Caisi>curl 127.0.0.1:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-ccd8cdbf6-ftz2n | v=1
# 如果没有开启tunnel,EXTERNAL-IP为pending等待
C:\Users\Caisi>kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d6h
kubernetes-bootcamp LoadBalancer 10.96.125.12 <pending> 8080:30530/TCP 14m
- 这种方式每个服务将获得自己的外部IP。
- 这种方式有点类似与
C:\Users\Caisi>minikube service kubernetes-bootcamp --url
* Starting tunnel for service kubernetes-bootcamp.
|-----------|---------------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|---------------------|-------------|------------------------|
| default | kubernetes-bootcamp | | http://127.0.0.1:63528 |
|-----------|---------------------|-------------|------------------------|
http://127.0.0.1:63528
# 另外一个窗口
C:\Users\Caisi>curl 127.0.0.1:49187
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-ccd8cdbf6-ftz2n | v=1
- 可以使用label查看service或pod信息
# 这里查看的默认label为app=kubernetes-bootcamp
C:\Users\Caisi>kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Thu, 21 Jan 2021 11:19:41 +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: registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v1
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-ccd8cdbf6 (1/1 replicas created)
Events: <none>
C:\Users\Caisi>kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 4h43m
C:\Users\Caisi>kubectl get services -l app=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp LoadBalancer 10.96.125.12 127.0.0.1 8080:30530/TCP 29m
- 也可以添加新的标签,以附加的方式,下面对pod添加一个标签
# 这里已经存在一个app的键了
C:\Users\Caisi>kubectl label pod kubernetes-bootcamp-ccd8cdbf6-ftz2n app=v1
error: 'app' already has a value (kubernetes-bootcamp), and --overwrite is false
C:\Users\Caisi>kubectl label pod kubernetes-bootcamp-ccd8cdbf6-ftz2n run=v1
pod/kubernetes-bootcamp-ccd8cdbf6-ftz2n labeled
C:\Users\Caisi>kubectl describe pods kubernetes-bootcamp-ccd8cdbf6-ftz2n
Name: kubernetes-bootcamp-ccd8cdbf6-ftz2n
Namespace: default
Priority: 0
Node: minikube/192.168.49.2
Start Time: Thu, 21 Jan 2021 11:19:41 +0800
Labels: app=kubernetes-bootcamp
pod-template-hash=ccd8cdbf6
run=v1
Annotations: <none>
Status: Running
IP: 172.17.0.9
IPs:
···
C:\Users\Caisi>kubectl get pods -l run=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 4h51m
注:在这里添加的标签是pod的,所以查看service不能使用此标签,因为service中没有次标签。
- 删除服务
C:\Users\Caisi>kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d6h
kubernetes-bootcamp LoadBalancer 10.96.125.12 127.0.0.1 8080:30530/TCP 44m
C:\Users\Caisi>kubectl delete service -l app=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted
C:\Users\Caisi>kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d6h
# 集群外部无法访问应用
C:\Users\Caisi>curl 127.0.0.1:8080
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
# 使用kubectlAPI访问集群中应用正常运行
C:\Users\Caisi>kubectl exec -ti kubernetes-bootcamp-ccd8cdbf6-ftz2n curl localhost:8080
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-ccd8cdbf6-ftz2n | v=1
缩放应用
为什么需要缩放?
deployment只创建了一个Pod,当流量增加时,就需要扩容应用程序以满足用户需求了。
其实际是通过改变deployment中的副本数量来实现的。
当pod数量为0时,deployment上的颇多将终止。
- 使用scale扩展为四个副本
C:\Users\Caisi>kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
C:\Users\Caisi>kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-ccd8cdbf6 4 4 4 5h42m
C:\Users\Caisi>kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-ccd8cdbf6-8z74q 1/1 Running 0 39s 172.17.0.2 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-ftz2n 1/1 Running 0 5h42m 172.17.0.9 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-q5mh4 1/1 Running 0 39s 172.17.0.4 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-rmjqc 1/1 Running 0 39s 172.17.0.3 minikube <none> <none>
- 缩减副本为2
C:\Users\Caisi>kubectl scale deployment/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
C:\Users\Caisi>kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d7h
kubernetes-bootcamp LoadBalancer 10.96.228.52 <pending> 8080:31177/TCP 4m30s
C:\Users\Caisi>kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-ccd8cdbf6-8xc6c 1/1 Terminating 0 9m19s 172.17.0.4 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-fp5vz 1/1 Running 0 13m 172.17.0.5 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-hkdj8 1/1 Terminating 0 9m19s 172.17.0.3 minikube <none> <none>
kubernetes-bootcamp-ccd8cdbf6-ksfzf 1/1 Running 0 9m19s 172.17.0.2 minikube <none> <none>
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-ccd8cdbf6-8xc6c 1/1 Terminating 0 9m26s
kubernetes-bootcamp-ccd8cdbf6-fp5vz 1/1 Running 0 13m
kubernetes-bootcamp-ccd8cdbf6-hkdj8 1/1 Terminating 0 9m26s
kubernetes-bootcamp-ccd8cdbf6-ksfzf 1/1 Running 0 9m26s
更新应用,滚动更新
-
滚动更新允许应用程序从一个环境提升到另一个环境、回滚到以前的版本、持续集成和持续交付。
-
更新到v2
C:\Users\Caisi>kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
# 容器创建中
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-769746fd4-zbvvs 0/1 ContainerCreating 0 21s
kubernetes-bootcamp-ccd8cdbf6-fp5vz 1/1 Running 0 22m
kubernetes-bootcamp-ccd8cdbf6-ksfzf 1/1 Running 0 18m
# 创建成功后,就得pod将终止
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-769746fd4-qf22h 1/1 Running 0 27s
kubernetes-bootcamp-769746fd4-zbvvs 1/1 Running 0 75s
kubernetes-bootcamp-ccd8cdbf6-fp5vz 1/1 Terminating 0 23m
kubernetes-bootcamp-ccd8cdbf6-ksfzf 1/1 Terminating 0 19m
# 确认升级成功
C:\Users\Caisi>kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out
# pod的image为v2版本
C:\Users\Caisi>kubectl describe pods
···
Containers:
kubernetes-bootcamp:
Container ID: docker://b46d0b7c1059485c241d674a9d646cd923aa55619ac30a7ee3e377ec85503b1a
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
Controlled By: ReplicaSet/kubernetes-bootcamp-769746fd4
Containers:
kubernetes-bootcamp:
Container ID: docker://6bd647432eaab9573c6d68210a79999804f452f50e7ce8abdd6192193840ad1c
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
- 升级失败
# 这里升级到v10版本
C:\Users\Caisi>kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10
deployment.apps/kubernetes-bootcamp image updated
C:\Users\Caisi>kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 1 2 33m
# 镜像拉取失败
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-769746fd4-qf22h 1/1 Running 0 10m
kubernetes-bootcamp-769746fd4-zbvvs 1/1 Running 0 11m
kubernetes-bootcamp-799d5964b-mzvcg 0/1 ErrImagePull 0 37s
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-769746fd4-qf22h 1/1 Running 0 10m
kubernetes-bootcamp-769746fd4-zbvvs 1/1 Running 0 11m
kubernetes-bootcamp-799d5964b-mzvcg 0/1 ImagePullBackOff 0 47s
# 有一个v10的image版本拉取失败
C:\Users\Caisi>kubectl describe pods
···
Controlled By: ReplicaSet/kubernetes-bootcamp-769746fd4
Containers:
kubernetes-bootcamp:
Container ID: docker://b46d0b7c1059485c241d674a9d646cd923aa55619ac30a7ee3e377ec85503b1a
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
Controlled By: ReplicaSet/kubernetes-bootcamp-769746fd4
Containers:
kubernetes-bootcamp:
Container ID: docker://6bd647432eaab9573c6d68210a79999804f452f50e7ce8abdd6192193840ad1c
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
Controlled By: ReplicaSet/kubernetes-bootcamp-799d5964b
Containers:
kubernetes-bootcamp:
Container ID:
Image: registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10
Image ID:
Port: <none>
···
Normal Scheduled 65s default-scheduler Successfully assigned default/kubernetes-bootcamp-799d5964b-mzvcg to minikube
Normal Pulling 21s (x3 over 64s) kubelet Pulling image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10"
Warning Failed 20s (x3 over 63s) kubelet Failed to pull image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10 not found: manifest unknown: manifest unknown
Warning Failed 20s (x3 over 63s) kubelet Error: ErrImagePull
Normal BackOff 5s (x3 over 62s) kubelet Back-off pulling image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10"
Warning Failed 5s (x3 over 62s) kubelet Error: ImagePullBackOff
- 回滚
# 使用undo进行回滚
C:\Users\Caisi>kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
C:\Users\Caisi>kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-769746fd4-qf22h 1/1 Running 0 13m
kubernetes-bootcamp-769746fd4-zbvvs 1/1 Running 0 14m
kubernetes-bootcamp-799d5964b-mzvcg 0/1 Terminating 0 3m8s
# 这里v10版本已经没有了
C:\Users\Caisi>kubectl describe pods
···
Containers:
kubernetes-bootcamp:
Container ID: docker://b46d0b7c1059485c241d674a9d646cd923aa55619ac30a7ee3e377ec85503b1a
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
Containers:
kubernetes-bootcamp:
Container ID: docker://6bd647432eaab9573c6d68210a79999804f452f50e7ce8abdd6192193840ad1c
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: <none>
Host Port: <none>
···
Pod 'kubernetes-bootcamp-799d5964b-mzvcg': error 'pods "kubernetes-bootcamp-799d5964b-mzvcg" not found', but found events.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m20s default-scheduler Successfully assigned default/kubernetes-bootcamp-799d5964b-mzvcg to minikube
Normal Pulling 114s (x4 over 3m19s) kubelet Pulling image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10"
Warning Failed 113s (x4 over 3m18s) kubelet Failed to pull image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10 not found: manifest unknown: manifest unknown
Warning Failed 113s (x4 over 3m18s) kubelet Error: ErrImagePull
Normal BackOff 90s (x6 over 3m17s) kubelet Back-off pulling image "registry.cn-beijing.aliyuncs.com/typ/kubernetes-bootcamp:v10"
Warning Failed 76s (x7 over 3m17s) kubelet Error: ImagePullBackOff
总结
- 这一节学习了创建minikube集群、部署Node.js应用、一些基本的查看pod的命令、公开暴露应用、缩放应用、更新与回滚应用。
- 值得注意的是docker desktop不支持service的NodePort模式这个坑,不过Linux的docker engine是支持的。
- 还有就是label的作用。
- 通过这次的了解,对deployment和pod,以及service又有了一些新的看法。
更多推荐
已为社区贡献2条内容
所有评论(0)