成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Kubernetes資源清單篇:如何創建資源?

開發 前端
在Kubernetes中所有操作的內容,我們都稱為“資源對象”,是由API Server基于HTTP/HTTPS接收并響應客戶端的操作請求,是一種Restful風格的接口,將各種組件及操作內容都抽象成為標準的REST資源,如Namespace、Pod等,其中操作內容以JSON或yml格式數據進行操作。

 在Kubernetes中所有操作的內容,我們都稱為“資源對象”,是由API Server基于HTTP/HTTPS接收并響應客戶端的操作請求,是一種Restful風格的接口,將各種組件及操作內容都抽象成為標準的REST資源,如Namespace、Pod等,其中操作內容以JSON或yml格式數據進行操作。本文講解的是Kubernetes中的最為重要的一節——資源清單,我們想要在Kubernetes中部署Pod、Service等資源對象,都需要通過資源清單的方式來部署,無論是通過命令kubectl,還是可視化控制臺,都是離不開資源清單的定義,本文重點講述資源清單如何定義、如何創建及使用。

[[353555]]

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  <string> 
  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  <string> 
  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  <Object> 
  23.      Standard object's metadata. More info: 
  24.      https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata 
  25.  
  26.    spec  <Object> 
  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  <Object> 
  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 

查看屬性說明,使用如下命令,如:查看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 

 

責任編輯:華軒 來源: 程序猿技術大咖
相關推薦

2021-07-29 06:37:55

KubernetesKubeLinter工具

2022-03-24 08:04:50

Kubernetes資源限制

2024-11-15 08:30:23

2020-07-31 07:00:00

Kubernetes容器Linux

2020-09-09 07:00:00

Kubernetes集群容器

2021-01-12 15:19:23

Kubernetes

2022-06-27 10:25:55

Kubernetes調度CPU

2022-12-19 07:28:53

Kubernetes資源請求限制

2018-12-18 09:00:26

Kubernetes工作負載測試

2021-11-22 16:21:28

Kubernetes 運維開源

2021-12-26 18:23:10

Kubernetes集群命令

2018-11-23 21:01:03

RancherKubernetes金風

2022-06-21 08:03:49

RBAC 限制容器

2015-09-08 10:28:52

數據中心資源清單

2022-09-07 15:57:41

KubernetesCRD

2020-09-01 08:06:54

Kubernetes資源

2021-05-25 09:00:00

Kubernetes容器集群

2025-06-11 09:28:22

2025-04-29 10:00:00

Kubernete云原生Helm

2022-08-09 18:26:04

KubernetesLinux
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产最新视频在线 | 亚洲经典一区 | 午夜影院普通用户体验区 | 成人精品一区二区 | 亚洲一区二区三区视频 | 日本电影一区二区 | 国产精品久久久久久久久久久免费看 | 亚洲福利网站 | 亚洲一区二区三区视频 | 综合色播| 欧美性受 | 国产精品永久久久久 | 人人人人干 | 亚洲精品电影网在线观看 | 精品在线播放 | com.国产 | 91精品国产综合久久国产大片 | 久久精品久久久久久 | 精品日韩一区 | 视频在线h | 国产在线精品一区 | 国产免费一级一级 | 色综合av| 天天干人人 | 五月香婷婷 | 午夜精品久久久久久久久久久久久 | 久草热在线 | 91视频一88av | 一区二区三区中文字幕 | 中文字幕av亚洲精品一部二部 | 精品一区二区在线观看 | www.婷婷亚洲基地 | 男女羞羞视频在线 | 久久lu | 国产精品视频一区二区三区四区国 | 国产精品极品美女在线观看免费 | 国产成人免费视频网站视频社区 | 日韩中文字幕 | 国产成人精品一区二区三区四区 | 亚洲国产欧美一区 | 国产精品美女久久久久久免费 |