05-资源清单定义入门
kubernetes
资源类型主要有:
service
pod
workload:运行程序对外提供服务de, Pod
服务发现及均衡
配置与存储:Volume,CSI 存储卷
查看pods 资源配置清单
创建资源的方法:
apiserver 仅接受JSON格式的资源定义;
yaml格式来提供配置清单,apiserver 可自动将其转为json格式,而后在执行;
大部分资源的配置清单都有5个字段组成:
字段一:apiVersion 标识方式是 group/version 的方式,
通过 kubectl api-versions 查看 apiversion 所支持的版本
字段二:kind: 资源类别
字段三:metadata: 元数据
name
namespace
labels 标签
annotations 资源注解
每个资源的引用PATH
/api/$GROUP/$VERSION/$NAMESPACE/$TYPE/$NAME
字段四: spec: 期望的状态,disired state
status: 当前状态,current state,本字段由 kubernetes 集群维护
kubernetes有内建的格式 使用kubectl explain pods/node 查看格式需要的字段

[root@master ~]# kubectl explain pods 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
如果要查看 二级字段的内建格式可以使用

[root@master ~]# kubectl explain pods.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
创建一个简单的资源清单
apiVersion: v1 kind: Pod metadata: name: pod-demo-new namespace: default labels: app: myapp tier: fronetend spec: containers: - name: myapp-new image: nginx:1.15-alpine - name: busybox image: busybox:latest command: - "/bin/sh" - "-c" - "echo $(date) >>/usr/share/nginx/html/index.html; sleep 5"
使用kubectl create -f pod-demo.yaml创建资源
kubectl create -f pod-demo.yaml
通过kubectl describe pods pod-demo-new 查看新创建的资源详细信息

[root@master mainfests]# kubectl describe pods pod-demo-new Name: pod-demo-new Namespace: default Priority: 0 PriorityClassName: <none> Node: node01/192.168.122.221 Start Time: Fri, 12 Apr 2019 11:42:26 +0800 Labels: app=myapp tier=fronetend Annotations: <none> Status: Running IP: 10.244.1.17 Containers: myapp-new: Container ID: docker://567a2553f2b83da37c11829814bc37c79e6460cc701cdc9f13286c662cc0f692 Image: nginx:1.15-alpine Image ID: docker-pullable://nginx@sha256:b05adda06363b850ade704048e15e076d493429badf0b3df275acd8ca428b3fe Port: <none> Host Port: <none> State: Running Started: Fri, 12 Apr 2019 11:42:28 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-dvm6l (ro) busybox: Container ID: docker://0ce6d58ba6a6f405e9a1b68bc5c9ac5b31df5bf0587c9dccb2dd85de086a8320 Image: busybox:latest Image ID: docker-pullable://busybox@sha256:954e1f01e80ce09d0887ff6ea10b13a812cb01932a0781d6b0cc23f743a874fd Port: <none> Host Port: <none> Command: /bin/sh -c echo $(date) >>/usr/share/nginx/html/index.html; sleep 5 State: Waiting Reason: CrashLoopBackOff Last State: Terminated Reason: Completed Exit Code: 0 Started: Fri, 12 Apr 2019 11:46:41 +0800 Finished: Fri, 12 Apr 2019 11:46:46 +0800 Ready: False Restart Count: 5 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-dvm6l (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-dvm6l: Type: Secret (a volume populated by a Secret) SecretName: default-token-dvm6l 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 5m50s default-scheduler Successfully assigned default/pod-demo-new to node01 Normal Pulled 5m49s kubelet, node01 Container image "nginx:1.15-alpine" already present on machine Normal Created 5m48s kubelet, node01 Created container myapp-new Normal Started 5m48s kubelet, node01 Started container myapp-new Normal Pulling 4m29s (x4 over 5m48s) kubelet, node01 Pulling image "busybox:latest" Normal Pulled 4m23s (x4 over 5m40s) kubelet, node01 Successfully pulled image "busybox:latest" Normal Created 4m23s (x4 over 5m39s) kubelet, node01 Created container busybox Normal Started 4m23s (x4 over 5m39s) kubelet, node01 Started container busybox Warning BackOff 48s (x17 over 5m18s) kubelet, node01 Back-off restarting failed container [root@master mainfests]#

[root@master mainfests]# kubectl logs pod-demo-new myapp-new 10.244.0.0 - - [12/Apr/2019:03:46:05 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" 10.244.0.0 - - [12/Apr/2019:03:46:07 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" 10.244.0.0 - - [12/Apr/2019:03:46:07 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" 10.244.0.0 - - [12/Apr/2019:03:46:08 +0000] "GET / HTTP/1.1" 200 612 "-" "ELinks/0.12pre6 (textmode; Linux; -)" "-" [root@master mainfests]#
本文来自博客园,作者:IT老登,转载请注明原文链接:https://www.cnblogs.com/nb-blog/p/10697564.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)