美图齐众专注资阳网站设计 资阳网站制作 资阳网站建设
资阳网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Kubernetes资源清单篇:如何创建资源?

 在Kubernetes中所有操作的内容,我们都称为“资源对象”,是由API Server基于HTTP/HTTPS接收并响应客户端的操作请求,是一种Restful风格的接口,将各种组件及操作内容都抽象成为标准的REST资源,如Namespace、Pod等,其中操作内容以JSON或yml格式数据进行操作。本文讲解的是Kubernetes中的最为重要的一节——资源清单,我们想要在Kubernetes中部署Pod、Service等资源对象,都需要通过资源清单的方式来部署,无论是通过命令kubectl,还是可视化控制台,都是离不开资源清单的定义,本文重点讲述资源清单如何定义、如何创建及使用。

1、资源分类

根据资源的功能进行资源分类,Kubernetes资源对象可分为:

  • 工作负载(Workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob。
  • 发现和负载均衡(Discovery & LB):Service 、Ingress。
  • 配置和存储(Config & Storage):Volume(存储卷)、CSI(容器存储接口,可以扩展各种各样的第三方存储卷)。
  • 集群(Cluster):Namespace、Node、Role、ClusterRole、RoleBinding(角色绑定)、ClusterRoleBinding(集群角色绑定)。
  • 元数据(Metadata):HPA、PodTemplate(Pod模板,用于让控制器创建Pod时使用的模板)、LimitRange(用来定义硬件资源限制的)。

一个应用通常需要多个资源的支撑,例如,使用Deployment资源管理应用实例(Pod)、使用ConfigMap资源保存应用配置、使用Service或Ingress资源暴露服务、使用Volume资源提供外部存储等。

2.资源清单

资源清单,等同于一个剧本,能够告诉我们每一步应该怎么去做,Kubernetes接收到这么一个剧本,就能够按照这个剧本去执行,以达到我们的预期。在Kubernetes中,一般都是通过定义资源清单的方式去创建资源。一般使用yaml格式的文件来创建符合我们预期期望的资源,这样的yaml文件我们称为资源清单。(也可以定义为json格式)如,创建一个Pod资源:

 
 
 
 
  1. apiVersion: v1 
  2. kind: Pod 
  3. metadata: 
  4.   name: vue-frontend 
  5.   namespace: test 
  6.   labels: 
  7.     app: vue-frontend 
  8. spec: 
  9.   containers: 
  10.   - name: vue-frontend 
  11.     image: xcbeyond/vue-frontend:latest 
  12.     ports: 
  13.       - name: port 
  14.         containerPort: 80 
  15.         hostPort: 8080 

接下来,以Pod资源定义为例展开对资源清单的详细说明。

2.1 资源清单定义

yaml格式的Pod资源清单定义文件的完整内容如下:

 
 
 
 
  1. apiVersion: v1 
  2. kind: Pod    # 资源类别 
  3. metadata:    # 资源元数据 
  4.   name: string 
  5.   namespace: string 
  6.   labels: 
  7.     - name: string 
  8.   annotations: 
  9.     - name: string 
  10. spec:      # 资源期望的状态 
  11.   containers:    # 容器列表 
  12.     - name: string    # 容器名称,下面的属性均属于对该容器的定义或约束 
  13.       image: string 
  14.         imagePullPolicy: [Always|Never|IfNotPresent] 
  15.       command: [string] 
  16.       args: [string] 
  17.       workingDir: string 
  18.       volumeMounts: 
  19.         - name: string 
  20.           mountPath: string 
  21.           readOnly: boolean 
  22.       ports: 
  23.         - name: string 
  24.           containerPort: int 
  25.           hostPort: int 
  26.           protocol: string 
  27.       env: 
  28.         - name: string 
  29.           value: string 
  30.       resources: 
  31.         limits: 
  32.           cpu: string 
  33.           memory: string 
  34.         requests: 
  35.           cpu: string 
  36.           memory: string 
  37.       livenssProbe: 
  38.         exec: 
  39.           command: [string] 
  40.         httpGet: 
  41.           path: string 
  42.           port: number 
  43.           host: string 
  44.           scheme: string 
  45.           httpHeaders: 
  46.             - name: string 
  47.               value: string 
  48.           tcpSocket: 
  49.             port: number 
  50.           initialDelaySeconds: 0 
  51.           timeoutSeconds: 0 
  52.           periodSeconds: 0 
  53.           successThreshold: 0 
  54.           failureThreshold: 0 
  55. ……  

对各属性的详细说明如下表所示:(必选属性,是必须存在的,否则创建失败。)

上述列举的是常用的属性,如果想查看全部属性,可以使用命令kubectl explain pod:

 
 
 
 
  1. [xcbeyond@bogon ~]$ kubectl explain pod 
  2. KIND:     Pod 
  3. VERSION:  v1 
  4.  
  5. DESCRIPTION: 
  6.      Pod is a collection of containers that can run on a host. This resource is 
  7.      created by clients and scheduled onto hosts. 
  8.  
  9. FIELDS: 
  10.    apiVersion   
  11.      APIVersion defines the versioned schema of this representation of an 
  12.      object. Servers should convert recognized schemas to the latest internal 
  13.      value, and may reject unrecognized values. More info: 
  14.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources 
  15.  
  16.    kind   
  17.      Kind is a string value representing the REST resource this object 
  18.      represents. Servers may infer this from the endpoint the client submits 
  19.      requests to. Cannot be updated. In CamelCase. More info: 
  20.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds 
  21.  
  22.    metadata   
  23.      Standard object's metadata. More info: 
  24.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata 
  25.  
  26.    spec   
  27.      Specification of the desired behavior of the pod. More info: 
  28.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status 
  29.  
  30.    status   
  31.      Most recently observed status of the pod. This data may not be up to date. 
  32.      Populated by the system. Read-only. More info: 
  33.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status 
  34. 查看属性说明,使用如下命令,如:查看pod.spec.containers

     
     
     
     
    1. [xcbeyond@bogon ~]$ kubectl explain pod.spec.containers 
    2. KIND:     Pod 
    3. VERSION:  v1 
    4.  
    5. RESOURCE: containers <[]Object> 
    6.  
    7. DESCRIPTION: 
    8.      List of containers belonging to the pod. Containers cannot currently be 
    9.      added or removed. There must be at least one container in a Pod. Cannot be 
    10.      updated. 
    11.  
    12.      A single application container that you want to run within a pod. 
    13.  
    14. FIELDS: 
    15.    args  <[]string> 
    16.      Arguments to the entrypoint. The docker image's CMD is used if this is not 
    17.      provided. Variable references $(VAR_NAME) are expanded using the 
    18.      container's environment. If a variable cannot be resolved, the reference in 
    19.      the input string will be unchanged. The $(VAR_NAME) syntax can be escaped 
    20.      with a double $$, ie: $$(VAR_NAME). Escaped references will never be 
    21.      expanded, regardless of whether the variable exists or not. Cannot be 
    22.      updated. More info: 
    23.      https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell 
    24.  
    25.    command  <[]string> 
    26.      Entrypoint array. Not executed within a shell. The docker image's 
    27.      ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME) 
    28.      are expanded using the container's environment. If a variable cannot be 
    29.      resolved, the reference in the input string will be unchanged. The 
    30.      $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME). 
    31.      Escaped references will never be expanded, regardless of whether the 
    32.      variable exists or not. Cannot be updated. More info: 
    33.      https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell 
    34. …… 

    2.2 示例

    在命名空间test中,定义一个名为frontend的Pod。

    (1)定义命名空间

    为了便于后期测试,特定义一个新的命名空间test。(如果命名空间test已存在,则无需再建)

    命名空间test的资源清单文件test-namespace.yaml如下:

     
     
     
     
    1. apiVersion: v1 
    2. kind: Namespace 
    3. metadata:    
    4.   name: test 

    执行kubectl create命令创建该Namespace:

     
     
     
     
    1. [xcbeyond@bogon ~]$ kubectl create -f test-namespace.yaml  
    2. namespace/test created 

    (2)定义Pod

    定义一个名为frontend的Pod,由一个容器组成,资源清单文件frontend-pod.yaml如下:

     
     
     
     
    1. apiVersion: v1 
    2. kind: Pod 
    3. metadata: 
    4.   name: frontend 
    5.   namespace: test 
    6.   labels: 
    7.     app: frontend 
    8. spec: 
    9.   containers: 
    10.   - name: frontend 
    11.     image: xcbeyond/vue-frontend:latest 
    12.     ports: 
    13.       - name: port 
    14.         containerPort: 80 
    15.         hostPort: 8080 

    执行kubectl create命令创建该Pod:

     
     
     
     
    1. [xcbeyond@bogon ~]$ kubectl create -f frontend-pod.yaml  
    2. pod/frontend created 

    通过命令kubectl get pods -n 查看,创建Pod的状态:

     
     
     
     
    1. [xcbeyond@bogon ~]$ kubectl get pods -n test 
    2. NAME       READY   STATUS   RESTARTS   AGE 
    3. frontend   1/1     Runing   0          79s 

    标题名称:Kubernetes资源清单篇:如何创建资源?
    本文路径:http://www.zsjierui.cn/article/djooegg.html

    免费获取网站建设与品牌策划方案报价

    *主要业务范围包括:高端网站建设, 集团网站建设(网站建设网站制作)找网站建设公司就上美图齐众。
    提交需求

      联系我们

      028-86922220
    • 手机:13518219792
    • 地址:成都市太升南路288号锦天国际A幢1002号
    • 24小时服务热线:028-86922220

      网站建设服务

    • 网页设计
    • 网站制作
    • 网站开发

      网站推广服务

    • 营销网站建设
    • 百度快速排名
    • 整站网站推广

      网站运维服务

    • 基础维护
    • 网站改版
    • 网站维护

      FOLLOW US

    • 微信二维码

      微信二维码

    Copyright © 2025 资阳站青羊区美图齐众设计工作室(个体工商户) 资阳网站建设公司-选网站建设公司美图齐众!专业的网站制作公司!
    All Rights Reserved 版权所有 蜀ICP备2025119604号-1