如下列举的内容都是 kubernetes 中的 Object,这些对象均可以在 yaml 文件中做为一种 API 类型来配置。node
类别 | 名称 |
工做负载型资源对象 | Pod Replicaset ReplicationController Deployments StatefulSets Daemonset Job CronJob |
服务发现及负载均衡 | Service Ingress |
配置与存储 | Volume、Persistent Volume、CSl 、 configmap、 secret |
集群资源 | Namespace Node Role ClusterRole RoleBinding ClusterRoleBinding |
元数据资源 | HPA PodTemplate LimitRang |
在 Kubernetes 系统中,Kubernetes 对象 是持久化的条目。Kubernetes 使用这些条目去表示整个集群的状态。特别地,它们描述了以下信息:linux
Kubernetes 对象是 “目标性记录” —— 一旦建立对象,Kubernetes 系统将持续工做以确保对象存在。经过建立对象,能够有效地告知 Kubernetes 系统,所须要的集群工做负载看起来是什么样子的,这就是 Kubernetes 集群的 指望状态。git
与 Kubernetes 对象工做 —— 是否建立、修改,或者删除 —— 须要使用 Kubernetes API。当使用 kubectl
命令行接口时,好比,CLI 会使用必要的 Kubernetes API 调用,也能够在程序中直接使用 Kubernetes API。docker
每一个 Kubernetes 对象包含两个嵌套的对象字段,它们负责管理对象的配置:对象 spec 和 对象 status。spec 必须提供,它描述了对象的 指望状态—— 但愿对象所具备的特征。status 描述了对象的 实际状态,它是由 Kubernetes 系统提供和更新。在任什么时候刻,Kubernetes 控制平面一直处于活跃状态,管理着对象的实际状态以与咱们所指望的状态相匹配。json
例如,Kubernetes Deployment 对象可以表示运行在集群中的应用。当建立 Deployment 时,可能须要设置 Deployment 的 spec,以指定该应用须要有 3 个副本在运行。Kubernetes 系统读取 Deployment spec,启动咱们所指望的该应用的 3 个实例 —— 更新状态以与 spec 相匹配。若是那些实例中有失败的(一种状态变动),Kubernetes 系统经过修正来响应 spec 和状态之间的不一致 —— 这种状况,启动一个新的实例来替换。vim
当建立 Kubernetes 对象时,必须提供对象的 spec,用来描述该对象的指望状态,以及关于对象的一些基本信息(例如,名称)。当使用 Kubernetes API 建立对象时(或者直接建立,或者基于kubectl
),API 请求必须在请求体中包含 JSON 格式的信息。更经常使用的是,须要在 .yaml 文件中为 kubectl 提供这些信息。 kubectl
在执行 API 请求时,将这些信息转换成 JSON 格式。查看已经部署好的pod的资源定义格式:api
[root@k8s-master ~]# kubectl get pod myapp-848b5b879b-5f69p -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: 2018-08-24T07:40:57Z generateName: myapp-848b5b879b- labels: pod-template-hash: "4046164356" run: myapp name: myapp-848b5b879b-5f69p namespace: default ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: myapp-848b5b879b uid: caf2ec54-a76f-11e8-84d2-000c2972dc1f resourceVersion: "90507" selfLink: /api/v1/namespaces/default/pods/myapp-848b5b879b-5f69p uid: 09bc0ba1-a771-11e8-84d2-000c2972dc1f spec: containers: - image: ikubernetes/myapp:v1 imagePullPolicy: IfNotPresent name: myapp resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-j5pf5 readOnly: true dnsPolicy: ClusterFirst nodeName: k8s-node01 priority: 0 restartPolicy: Always schedulerName: default-scheduler securityContext: {} serviceAccount: default serviceAccountName: default terminationGracePeriodSeconds: 30 tolerations: - effect: NoExecute key: node.kubernetes.io/not-ready operator: Exists tolerationSeconds: 300 - effect: NoExecute key: node.kubernetes.io/unreachable operator: Exists tolerationSeconds: 300 volumes: - name: default-token-j5pf5 secret: defaultMode: 420 secretName: default-token-j5pf5 status: conditions: - lastProbeTime: null lastTransitionTime: 2018-08-24T07:40:57Z status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: 2018-08-24T07:40:59Z status: "True" type: Ready - lastProbeTime: null lastTransitionTime: null status: "True" type: ContainersReady - lastProbeTime: null lastTransitionTime: 2018-08-24T07:40:57Z status: "True" type: PodScheduled containerStatuses: - containerID: docker://a5e7004f45b1ec4a4297e50db6d0b5b11573e36ed8de814ea8b6cdacd13b8f9a image: ikubernetes/myapp:v1 imageID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513 lastState: {} name: myapp ready: true restartCount: 0 state: running: startedAt: 2018-08-24T07:40:58Z hostIP: 192.168.56.12 phase: Running podIP: 10.244.1.11 qosClass: BestEffort startTime: 2018-08-24T07:40:57Z
建立资源的方法:数组
大部分资源的配置清单格式都由5个一级字段组成:app
apiVersion: group/version 指明api资源属于哪一个群组和版本,同一个组能够有多个版本 $ kubectl api-versions kind: 资源类别,标记建立的资源类型,k8s主要支持如下资源类别 Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob metadata:元数据,主要提供如下字段 name:同一类别下的name是惟一的 namespace:对应的对象属于哪一个名称空间 labels:标签,每个资源均可以有标签,标签是一种键值数据 annotations:资源注解 每一个的资源引用方式(selflink): /api/GROUP/VERSION/namespace/NAMESPACE/TYPE/NAME spec: 定义目标资源的指望状态(disired state),资源对象中最重要的字段 status: 显示资源的当前状态(current state),本字段由kubernetes进行维护
K8s存在内嵌的格式说明,可使用kubectl explain 进行查看,如查看Pod这个资源的定义:负载均衡
[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
从上面能够看到apiVersion,kind等定义的键值都是<string>,而metadata和spec看到是一个<Object>,当看到存在<Object>的提示,说明该字段能够存在多个二级字段,那么可使用以下命令继续查看二级字段的定义方式:
[root@k8s-master ~]# kubectl explain pods.metadata
[root@k8s-master ~]# kubectl explain pods.spec
二级字段下,每一种字段都有对应的键值类型,经常使用类型大体以下:
<[]string>:表示是一个字串列表,也就是字串类型的数组
<Object>:表示是能够嵌套的字段
<map[string]string>:表示是一个由键值组成映射
<[]Object>:表示是一个对象列表
<[]Object> -required-:required表示该字段是一个必选的字段
[root@k8s-master ~]# mkdir mainfests [root@k8s-master ~]# cd mainfrests [root@k8s-master mainfrests]# vim pod-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name:myapp image: ikubernetes/myapp:v1 - name: busybox image: busybox:latest command: - "/bin/sh" - "-c" - "sleep 3600" [root@k8s-master mainfrests]# kubectl create -f pod-demo.yaml [root@k8s-master mainfrests]# kubectl get pods [root@k8s-master mainfrests]# kubectl describe pods pod-demo #获取pod详细信息 [root@k8s-master mainfrests]# kubectl logs pod-demo myapp [root@k8s-master mainfrests]# kubectl exec -it pod-demo -c myapp -- /bin/sh
[root@k8s-master ~]# kubectl explain pods.spec.containers name <string> -required- #containers 的名字 image <string> #镜像地址 imagePullPolicy <string> #若是标签是latest 就是Always(老是下载镜像) IfNotPresent(先看本地是否有此镜像,若是没有就下载) Never (就是使用本地镜像) ports <[]Object> #是给对象列表 能够暴露多个端口 能够对每一个端口的属性定义 例如:(名称(可后期调用)端口号 协议 暴露在的地址上) 暴露端口只是提供额外信息的,不能限制系统是否真的暴露 - containerPort 容器端口 hostIP 主机地址(基本不会使用) hostPort 节点端口 name 名称 protocol (默认是TCP) args <[]string> 传递参数给command 至关于docker中的CMD command <[]string> 至关于docker中的ENTRYPOINT
若是Pod不提供command
或args
使用Container,则使用Docker镜像中的cmd或者ENTRYPOINT。
若是Pod提供command
但不提供args
,则仅使用提供 command
的。将忽略Docker镜像中定义EntryPoint和Cmd。
若是Pod中仅提供args
,则args
将做为参数提供给Docker镜像中EntryPoint。
若是提供了command
和args
,则Docker镜像中的ENTRYPOINT和CMD都将不会生效,Pod中的args
将做为参数给command运行
。
一、标签
key=value
[root@k8s-master mainfests]# kubectl get pods --show-labels #查看pod标签 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 0 25s app=myapp,tier=frontend
[root@k8s-master mainfests]# kubectl get pods -l app #过滤包含app的标签 NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 1m [root@k8s-master mainfests]# kubectl get pods -L app NAME READY STATUS RESTARTS AGE APP pod-demo 2/2 Running 0 1m myapp
[root@k8s-master mainfests]# kubectl label pods pod-demo release=canary #给pod-demo打上标签 pod/pod-demo labeled [root@k8s-master mainfests]# kubectl get pods -l app --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 0 1m app=myapp,release=canary,tier=frontend
[root@k8s-master mainfests]# kubectl label pods pod-demo release=stable --overwrite #修改标签 pod/pod-demo labeled [root@k8s-master mainfests]# kubectl get pods -l release NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 2m [root@k8s-master mainfests]# kubectl get pods -l release,app NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 2m
二、标签选择器
许多资源支持内嵌字段
三、节点标签选择器
[root@k8s-master mainfests]# kubectl explain pod.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/
nodeSelector能够限定pod建立在哪一个节点上,举个例子,给节点k8s-node01打上标签disktype=ssd,让pod-demo指定建立在k8s-node01上
(1)给k8s-node01节点打标签
[root@k8s-master mainfests]# kubectl label nodes k8s-node01 disktype=ssd node/k8s-node01 labeled [root@k8s-master mainfests]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 10d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-master,node-role.kubernetes.io/master= k8s-node01 Ready <none> 10d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=k8s-node01 k8s-node02 Ready <none> 9d v1.11.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8s-node02
(2)修改yaml文件,增长标签选择器 [root@k8s-master mainfests]# cat pod-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 - name: busybox image: busybox:latest command: - "/bin/sh" - "-c" - "sleep 3600" nodeSeletor: disktype: ssd
(3)从新建立pod-demo,能够看到固定调度在k8s-node01节点上 [root@k8s-master mainfests]# kubectl delete -f pod-demo.yaml pod "pod-demo" deleted [root@k8s-master mainfests]# kubectl create -f pod-demo.yaml pod/pod-demo created [root@k8s-master mainfests]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-demo 2/2 Running 0 20s 10.244.1.13 k8s-node01 [root@k8s-master mainfests]# kubectl describe pod pod-demo ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 42s default-scheduler Successfully assigned default/pod-demo to k8s-node01 ......
annotations:
与label不一样的地方在于,annotations不能用于挑选资源对象,仅用于为对象提供"元数据",没有键值长度限制。