K8s中所有的内容都抽象为资源(Resources),资源实例化之后叫做对象
资源分类
名称空间:
工作负载型资源(workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob(ReplicationController在v1.11版本被废弃)
服务发现及负载均衡型资源(ServiceDiscoveryLoadBalance):Service、Ingress、…
配置与存储型资源:Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷)
特殊类型的存储卷:ConfigMap(当配置中心来使用的资源类型)、Secret(保存敏感数据)、DownwardAPI(把外部环境中的信息输出给容器)
集群资源:
Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding
元数据型资源:
HPA、PodTemplate、LimitRange
通过命令 kubectl api-resources 来获取 K8S 支持的 resource 类
$ kubectl api-resourcesNAME SHORTNAMES APIGROUP NAMESPACED KINDbindings true Bindingcomponentstatuses cs false ComponentStatusconfigmaps cm true ConfigMapendpoints ep true Endpointsevents ev true Eventlimitranges limits true LimitRangenamespaces ns false Namespacenodes no false Nodepersistentvolumeclaims pvc true PersistentVolumeClaimpersistentvolumes pv false PersistentVolumepods po true Podpodtemplates true PodTemplatereplicationcontrollers rc true ReplicationController....
- SHORTNAMES - kubectl 可以使用的缩写,比如 namepace -> ns,当然不是每种 resource 都有缩写的
- APIGROUP - 在 yaml 文件中可以看到其使用
<APIGROUP>/v1 - KIND - resource 的 name,同样也是在 yaml 中常见的字段
通过输入一些 options 给 kubectl 来获取更多的信息
$ kubectl api-resources --api-group apps -o wideNAME SHORTNAMES APIGROUP NAMESPACED KIND VERBScontrollerrevisions apps true ControllerRevision [create delete deletecollection get list patch update watch]daemonsets ds apps true DaemonSet [create delete deletecollection get list patch update watch]deployments deploy apps true Deployment [create delete deletecollection get list patch update watch]replicasets rs apps true ReplicaSet [create delete deletecollection get list patch update watch]statefulsets sts apps true StatefulSet [create delete deletecollection get list patch update watch]
yaml基本语法
缩进不允许使用Tab键,只允许使用空格
缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
表示注释使用井号(#)
字符后缩进一个空格,比如冒号
yaml 数据类型
对象 对象的一组键值对,使用冒号结构表示
name: xiaomingage: 20
数组 一组连词线开头的行,构成一个数组
animal- Cat- Dog
yam资源清单字段
在k8s中,一般使用yaml 格式的文件创建符合我们预期期望的pod,这样的yaml 文件我们称为资源清单。
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxspec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:stableresources:limits:memory: "128Mi"cpu: "500m"ports:- containerPort: 80
查看资源配置
查看一个配置文件的信息:
kubectl get cm cluster-info -n kube-public -o yaml
查看节点信息
kubectl get node kube-master -o yaml
命令解释
- get: 查看某个资源
- -o yaml: 输出格式为 yaml, 也可以选择 json
- node/cm: 想要查看的资源的类型, node 表示节点, cm(configmap) 表示配置文件
- -n
: 查看某个 namespace 下的资源,后续会详细介绍 必须存在的属性
| 参数名 | 字段类型 | 说明 |
|---|---|---|
| apiVersion | String | k8s API的版本,目前基本是v1,使用kubectl api-version命令查询 |
| kind | String | yaml文件定义的资源类型和角色 |
| metadata | Object | 元数据对象 |
| metadata.name | String | |
| metadata.namespace | String | |
| Spec | Object | 定义对象 |
| spec.containers[] | list | |
| spec.containers[].name | String | |
| spec.containers[].image | String |
apiVersion
apiVersion字段标记Kubernetes资源版本。Kubernetes API服务器的特定版本(例如V1.13.0)可以与不同资源的不同版本一起使用。资源版本包括两部分:API组(在本例中为apps)和版本号(v1),版本号可能包含alpha或beta字样:
使用 kubectl api-versions 查看当前集群支持的版本
$ kubectl api-versionsadmissionregistration.k8s.io/v1beta1apiextensions.k8s.io/v1beta1apiregistration.k8s.io/v1apiregistration.k8s.io/v1beta1apps/v1apps/v1beta1apps/v1beta2authentication.k8s.io/v1authentication.k8s.io/v1beta1authorization.k8s.io/v1authorization.k8s.io/v1beta1autoscaling/v1autoscaling/v2beta1autoscaling/v2beta2batch/v1batch/v1beta1certificates.k8s.io/v1beta1compose.docker.com/v1alpha3compose.docker.com/v1beta1compose.docker.com/v1beta2coordination.k8s.io/v1coordination.k8s.io/v1beta1events.k8s.io/v1beta1extensions/v1beta1networking.k8s.io/v1networking.k8s.io/v1beta1node.k8s.io/v1beta1policy/v1beta1rbac.authorization.k8s.io/v1rbac.authorization.k8s.io/v1beta1scheduling.k8s.io/v1scheduling.k8s.io/v1beta1storage.k8s.io/v1storage.k8s.io/v1beta1v1
resource 可以有多个 apiVersion,explain 可能会输出旧的 group/version,可以通过显示的添加 --api-version 来控制输出哪个版本的信息
kubectl explain replicaset --api-version apps/v1KIND: ReplicaSetVERSION: apps/v1DESCRIPTION:ReplicaSet ensures that a specified number of pod replicas are running atany given time.FIELDS:apiVersion <string>APIVersion defines the versioned schema of this representation of anobject. Servers should convert recognized schemas to the latest internalvalue, and may reject unrecognized values. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resourceskind <string>Kind is a string value representing the REST resource this objectrepresents. Servers may infer this from the endpoint the client submitsrequests to. Cannot be updated. In CamelCase. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kindsmetadata <Object>If the Labels of a ReplicaSet are empty, they are defaulted to be the sameas the Pod(s) that the ReplicaSet manages. Standard object's metadata. Moreinfo:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadataspec <Object>Spec defines the specification of the desired behavior of the ReplicaSet.More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-statusstatus <Object>Status is the most recently observed status of the ReplicaSet. This datamay be out of date by some window of time. Populated by the system.Read-only. More info:https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
某个 group/version 是否可用于某个 resource。可以通过下面这个命令来检查。deployments.v1.apps 分别对应 <API_RESOURCE_NAME>.<API_VERSION>.<API_GROUP>。
kubectl get deployments.v1.apps -n kube-systemNAME READY UP-TO-DATE AVAILABLE AGEcoredns 2/2 2 2 2d18h
kind
kind: 表示类型,指定我们要处理的资源或API对象,比如节点是 Node , 配置文件是 ConfigMap
metadata
metadata: 基本信息,所有的 resource 都会有这个字段,且结构一样name: 这个 resource 的名称resourceVersion: 版本,修改此 resource 后会向上增加selfLink: 通过 http api 访问此 resource 的路径uid: uuid,唯一标示creationTimestamp: 创建时间labels: 这个 resource 的标签,我们可以用这些标签来做过滤匹配等等annotations:类似于 labels, 记录一些附加信息。但 annotaitons 记录的信息不能用来做过滤匹配等等。namespace: 资源所属的命名空间。有的资源有,有的资源没有。
spec
spec: 一般都是这个 resource 自己的特定信息,比如如果是节点的话会记录自己的 ip、内存等等信息,配置文件的话会记录具体的配置信息。有的资源可能没这个字段(可能叫别的名字)。
资源限制
