Kubernetes API 仅接受及响应JSON格式的数据(JSON对象),同时,为了便于使用,它也允许用户提供YAML格式的POST对象,但API Server需要实现自行将其转换为JSON格式后方能提交。API Server接受和返回的所有JSON对象都遵循同一个模式,它们都具有kind和apiVersion字段,用于标识对象所属的资源类型、API群组及相关的版本。
[root@k8s-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
DESCRIPTION: Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
PodSpec is a description of a pod.
FIELDS: activeDeadlineSeconds <integer> Optional duration in seconds the pod may be active on the node relative to StartTime before the system will actively try to mark it failed and kill associated containers. Value must be a positive integer.
affinity <Object> If specified, the pod's scheduling constraints
automountServiceAccountToken <boolean> AutomountServiceAccountToken indicates whether a service account token should be automatically mounted. .....
[root@k8s-master ~]# kubectl get pods --show-labels #查看pod信息时,并显示对象的标签信息 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 5 5h13m app=myapp,tier=frontend
[root@k8s-master ~]# kubectl get pods -l app #过滤包含app标签的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 5 5h20m
[root@k8s-master ~]# kubectl get pods -l app,tier #过滤同时包含app,tier标签的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 5 5h20m
[root@k8s-master ~]# kubectl get pods -L app #显示有app键的标签信息 NAME READY STATUS RESTARTS AGE APP pod-demo 2/2 Running 5 5h21m myapp
[root@k8s-master ~]# kubectl get pods -L app,tier #显示有app和tier键的标签信息 NAME READY STATUS RESTARTS AGE APP TIER pod-demo 2/2 Running 5 5h21m myapp frontend
[root@k8s-master ~]# kubectl get pods -l app=myapp #过滤标签键为app值为myapp的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h11m
[root@k8s-master ~]# kubectl get pods -l app=myapp,env=testing #过滤标签键为app值为myqpp,并且标签键为env值为testing的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h11m
[root@k8s-master ~]# kubectl get pods -l app!=my #过滤标签键为app值不为my的所有pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h17m
2)集合关系示例:
1 2 3 4 5 6 7
[root@k8s-master ~]# kubectl get pods -l "app in (myapp)" #过滤键为app值有myapp的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h51m
[root@k8s-master ~]# kubectl get pods -l "app notin (my)" #过滤键为app值没有my的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h59m
#在定义pod资源清单时,可以通过nodeName来指定pod运行的节点,或者通过nodeSelector来挑选倾向的节点 [root@k8s-master ~]# kubectl explain pods.spec nodeName <string> NodeName is a request to schedule this pod onto a specific node. If it is non-empty, the scheduler simply schedules this pod onto that node, assuming that it fits resource requirements.
nodeSelector <map[string]string> NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
查看节点默认的标签
1 2 3 4 5
[root@k8s-master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-node1 Ready <none> 6d1h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux k8s-node2 Ready <none> 6d1h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
给节点添加标签
1 2 3 4 5 6 7
[root@k8s-master ~]# kubectl label nodes/k8s-node1 disktype=ssd node/k8s-node1 labeled [root@k8s-master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-node1 Ready <none> 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux k8s-node2 Ready <none> 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux