theboyaply
theboyaply
发布于 2020-03-23 / 695 阅读
1
0

k8s 资源清单

k8s中的资源

k8s 中所有的内容都抽象为资源,资源实例化之后,叫做对象。k8s 中的资源可以根据生效范围划分为三种:名称空间资源集群资源元数据资源

  • 名称空间资源(namespace):名称空间资源只在当前的名称空间有效。使用 kubectl get pod -n kube-system 来查看集群中的组件,这里的 -n kube-system 就是指定了要查看的名称空间。如果不指定的话,默认是 default。因为系统组件是属于 kube-system 这个名称空间的,所以只能在这个空间看到。
  • 集群资源:集群资源在所有集群中有效。比如 namespacerole
  • 元数据资源:介于 "名称空间资源" 和 "集群资源",前面提到的 HAP 控制器就是属于这种资源。

名称空间资源

  • 工作负载型资源(workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob、ReplicationController(在 v1. 11 版本被废弃)等。
  • 服务发现及负载均衡型资源(ServiceDiscovery LoadBalance):Service、Ingress等。
  • 配置与存储型资源:Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷)。
  • 特殊类型的存储卷:ConfigMap(当配置中心来使用的资源类型)、Secret(保存敏感数据)、 DownwardAPI(把外部环境中的信息输出给容器)。

集群资源

Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding。

元数据资源

HAP、PodTemplate、LimitRange。

什么是资源清单

集群简单演示 这篇文章中,提到了创建 Pod、Deployment、SVC等资源的方式。但在 k8s 中,一般使用 yaml 格式的文件来创建符合我们预期的资源,这样的 yaml 被称为资源清单。

使用资源清单创建 Pod:

kubectl apply -f nginx.yaml

其中 nginx.yaml 内容为:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
  namespace: test
  labels:
    app: nginx-app
    version: v1
spec:
  containers:
  - name: test-nginx
    image: hub.xixihaha.com/library/mynginx:v1

值得注意的是,资源清单中自定义的名称只能使用 小写字母 或使用 - 字符连接。比如以下命名方式都是 错误 的(不是规范问题,是会报错):

# 错误的命名
metadata:
  name: testPod
spec:
  containers:
  - name: test_nginx

资源清单常用字段

我们可以使用 kubectl expalin xxx 查看每一种资源的 yaml 可用字段,比如查看 Pod、Deployment、Service、ReplicaSet 等等的可用字段。下面是查看 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

结果中列出了所有可用字段,其中有些字段类型为 Object 的,我们还可以继续往下看,比如查看上面类型为 Object 的 metadata:

kubectl explain pod.metadata

以下是一些常用的字段:

字段类型说明
apiVersionStringk8s api 的版本,目前基本上是 v1。可以用 kubectl api-versions 命令查看
kindString定义的资源类型。比如定义为一个 Pod 或者 Deployment等等。
metadataObject声明元数据对象,固定值:metadata。
metadata.nameString元数据对象的名字。我们自己定义,比如该资源被 kind 定义为一个 Pod,那么 Pod 的名称就是在这里定义。
metadata.namespaceString元数据对象的命名空间。我们自己定义,不写默认为 default。这里定义的命名空间必须是已存在的。
metadata.labelsObject标签。
metadata.labels.appString标签名称。
metadata.labels.versionString标签版本号。
SpecObject详细定义对象。固定值:Spec。
spec.containers[]List这里是Spec对象的容器列表。即这个资源可以声明多个容器。
spec.containers[].nameString容器的名字。建议不写,会自动创建。如果此处写了容器的名字,那么 k8s 对该容器进行扩容时会报错,因为不能存在相同名称的容器。
spec.containers[].imageString容器使用的镜像。
spec.containers[]
.imagePullPolicy
String镜像拉取策略。如果不设置,默认为 Always。Always:每次都拉取新的镜像;Never:仅使用本地镜像,如果本地镜像不存在则不使用该镜像;IfNotPresent:如果本地没有,就拉取新镜像。
spec.containers[].command[]List指定容器启动命令,可以指定多个。不指定则默认为容器打包时使用的启动命令,若指定了则会覆盖原命令。
spec.containers[].args[]List指定容器启动命令参数,可传入多个。
spec.containers[].workingDirString指定容器的工作目录。
spec.containers[]
.volumeMounts[]
List指定容器北部的存储卷配置。
spec.containers[]
.volumeMounts[].name
String指定可以被容器挂载的存储卷的名称。
spec.containers[]
.volumeMounts[].mountPath
String指定可以被容器挂载的存储卷的路径。
spec.containers[]
.volumeMounts[].readOnly
String设置存储卷路径的读写模式,ture 或 false,默认为读写模式。
spec.containers[].ports[]List指定容器需要用到的端口列表。
spec.containers[].ports[]
.name
String指定端口名称。
spec.containers[].ports[]
.containerPort
String指定容器需要监听的端口号。
spec.containers[].ports[]
.hostPort
String指定容器所在主机需要监听的端口号,默认跟 containerPort 相同。设置了 hostPort ,那么同一台主机就无法启动该容器的相同副本(同一主机端口号冲突)。
spec.containers[].ports[]
.protocol
String指定端口协议,支持 TCP 和 UDP,默认为 TCP。
spec.containers[].env[]List指定容器运行前需要设置的环境变量列表。
spec.containers[].env[].nameString指定环境变量名称。
spec.containers[].env[].valueString指定环境变量的值。
spec.containers[].resourcesObject指定资源限制和资源请求的值。
spec.containers[].resources
.limits
Object设置容器运行时的资源运行上限。
spec.containers[].resources
.limits.cpu
String指定 CPU 的限制,单位为 core 数,将用于 docker run -- cpu-shares 参数。
spec.containers[].resources
.limits.memory
String指定 MEM 内存的限制,单位为 MIB、GIB。
spec.restartPolicyStringPod 重启策略。Always:Pod 一旦终止运行,不论是以什么方式终止的,kubelet 都会重启它;OnFailure:如果 Pod 的退出码是0,kubelet 不会重启,其它任何非0退出码终止的,都会重启;Never:Pod 终止后,kubelet 将退出码报告给 master 并不会重启该 Pod。
spec.nodeSelectorObject定义 Node 的 Label 过滤标签,言外之意就是选择在哪些 Node 上执行。以 key:value 格式指定。
spec.imagePullSecretsObject定义 pull 镜像时使用的 secret 名称,格式为 name:secretkey 。
spec.hostNetworkBoolean是否使用主机网络模式,默认值为 false。ture 表示使用宿主机网络,不使用 docker 网桥,并且将无法在同一台宿主机上启动第二个副本,否则端口冲突。

排错思路

如果我们使用资源清单进行资源的创建时发生错误(某个容器启动失败),可以通过以下思路排查(以 Pod 为例):

# 查看 Pod,-n 指定命名空间名称
kubectl get pod -n test -o wide

# 查看 Pod 的详细信息(假设 Pod 名称为 test-pod),查询结果包括 Pod 中所有容器的运行情况,如果某个容器正常运行,其 State 属性会是 Running
kubectl describe pod test-pod -n test

# 查看 Pod 中容器的运行日志,找出容器运行失败的原因。-c 表示要查看的容器,若 Pod 里只有一个容器,不加也可以。
kubectl log test-pod -c test-nginx -n test

-- end --


评论