您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
17、Kubernetes - 资源清单 - 常用字段说明
发布时间:2022-12-11 21:31:25编辑:雪饮阅读()
Step1
查看pod模板
[root@k8s-master01 ~]# kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the pod. This data may not be up to date.
Populated by the system. Read-only. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
查看某个pod模板更详细的信息
[root@k8s-master01 ~]# kubectl explain pod.apiVersion
KIND: Pod
VERSION: v1
FIELD: apiVersion <string>
DESCRIPTION:
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
示例2,这个pod模板详细内容太多就不贴命令回显结果了
kubectl explain pod.spec
step2
定义一个pod
[root@k8s-master01 ~]# vim pod.yaml
[root@k8s-master01 ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: app
image: hub.atguigu.com/library/myapp:v1
- name: test
image: hub.atguigu.com/library/myapp:v1
该pod的容器列表是两个都使用80端口的镜像(那么会有冲突的)
该pod定义文件中键与值之间需要有空格(冒号后面)
并且不要用tab缩进,自己写空格进行缩进。
然后应用该pod定义文件
[root@k8s-master01 ~]# kubectl apply -f pod.yaml
pod/myapp-pod created
然后查看pod列表中,刚才这个pod是有问题的
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/2 CrashLoopBackOff 4 2m48s
nginx-deployment-85756b779-dljnk 1/1 Running 2 24h
nginx-deployment-85756b779-f45wk 1/1 Running 1 24h
nginx-deployment-85756b779-hgr9q 1/1 Running 3 2d22h
restarts字段显示的是重启的次数,就是说遇到了错误尝试重启了4次还是失败。(毕竟是端口占用呗)
Step3
那么到底是那个容器启动失败了呢?因为我们这个pod内部包含两个容器的
[root@k8s-master01 ~]# kubectl describe pod myapp-pod
Name: myapp-pod
Namespace: default
Priority: 0
Node: k8s-node01/192.168.66.20
Start Time: Sun, 11 Dec 2022 21:15:42 +0800
Labels: app=myapp
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp-pod","namespace":"default"},"spec":{"c...
Status: Running
IP: 10.224.1.17
Containers:
app:
Container ID: docker://ea7a8d65a89e44491e1209aecbb1d31154e920c862ba3dd105ff5310b7428b54
Image: hub.atguigu.com/library/myapp:v1
Image ID: docker-pullable://hub.atguigu.com/library/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
Port: <none>
Host Port: <none>
State: Running
Started: Sun, 11 Dec 2022 21:15:43 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xt842 (ro)
test:
Container ID: docker://5dfc8f8e898ba22a7546abf3f01a87dd53ddf550a6ae5b2a3e416248f9fc3e72
Image: hub.atguigu.com/library/myapp:v1
Image ID: docker-pullable://hub.atguigu.com/library/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e
Port: <none>
Host Port: <none>
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 1
Started: Sun, 11 Dec 2022 21:18:48 +0800
Finished: Sun, 11 Dec 2022 21:18:51 +0800
Ready: False
Restart Count: 5
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xt842 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-xt842:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xt842
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m16s default-scheduler Successfully assigned default/myapp-pod to k8s-node01
Normal Pulled 5m15s kubelet, k8s-node01 Container image "hub.atguigu.com/library/myapp:v1" already present on machine
Normal Created 5m15s kubelet, k8s-node01 Created container app
Normal Started 5m15s kubelet, k8s-node01 Started container app
Normal Pulled 3m40s (x5 over 5m15s) kubelet, k8s-node01 Container image "hub.atguigu.com/library/myapp:v1" already present on machine
Normal Created 3m40s (x5 over 5m15s) kubelet, k8s-node01 Created container test
Normal Started 3m40s (x5 over 5m15s) kubelet, k8s-node01 Started container test
Warning BackOff 4s (x24 over 5m9s) kubelet, k8s-node01 Back-off restarting failed container
可以看到app容器没有问题,test容器挂了,这里猜测应该是按pod定义上面的容器顺序进行运行的,所以app优先占用了80端口,然后test运行时候就没有办法运行了。
查看当前pod的test容器的日志
[root@k8s-master01 ~]# kubectl log myapp-pod -c test
log is DEPRECATED and will be removed in a future version. Use logs instead.
2022/12/11 13:21:33 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address in use )
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2022/12/11 13:21:33 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address in use )
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2022/12/11 13:21:33 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address in use )
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2022/12/11 13:21:33 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address in use )
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2022/12/11 13:21:33 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address in use )
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address in use)
2022/12/11 13:21:33 [emerg] 1#1: still could not bind()
nginx: [emerg] still could not bind()
果然如此,相同的pod里面就是相同的网络栈,不占用才怪哈。
Step4
那么现在将pod定义中的test容器移除
[root@k8s-master01 ~]# vim pod.yaml
[root@k8s-master01 ~]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: app
image: hub.atguigu.com/library/myapp:v1
[root@k8s-master01 ~]# kubectl delete pod myapp-pod
pod "myapp-pod" deleted
这次就正常运行了
[root@k8s-master01 ~]# kubectl apply -f pod.yaml
pod/myapp-pod created
[root@k8s-master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 5s
nginx-deployment-85756b779-dljnk 1/1 Running 2 24h
nginx-deployment-85756b779-f45wk 1/1 Running 1 24h
nginx-deployment-85756b779-hgr9q 1/1 Running 3 2d22h
然后master本地访问是没有问题的
[root@k8s-master01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-pod 1/1 Running 0 41s 10.224.2.6 k8s-node02 <none> <none>
nginx-deployment-85756b779-dljnk 1/1 Running 2 24h 10.224.1.14 k8s-node01 <none> <none>
nginx-deployment-85756b779-f45wk 1/1 Running 1 24h 10.224.2.5 k8s-node02 <none> <none>
nginx-deployment-85756b779-hgr9q 1/1 Running 3 2d22h 10.224.1.15 k8s-node01 <none> <none>
[root@k8s-master01 ~]# curl 10.224.2.6
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
关键字词:Kubernetes,资源,清单,常用,字段,说明
相关文章
- 14、Kubernetes - 集群安装 - 配置私有仓库、集群功能
- 13、Kubernetes - 集群安装
- 12、Kubernetes - 集群安装(内核升级)
- 11、Kubernetes - 集群安装准备 - 安装软路由(koolsha
- 10、Kubernetes - 集群安装准备 - 说明
- 05-Maven常用命令(compile、clean、package、install)
- 6_Filter_细节_过滤器拦截路径配置(servlet拦截与目录
- 3_Filter_快速入门(类似php中常用的中间件)
- 18_JSTL_常用标签_foreach(普通foreach与容器foreach)
- 17_JSTL_常用标签_choose(类似于switch case)