Kubernetes資源清單篇:如何創(chuàng)建資源?
在Kubernetes中所有操作的內(nèi)容,我們都稱為“資源對(duì)象”,是由API Server基于HTTP/HTTPS接收并響應(yīng)客戶端的操作請(qǐng)求,是一種Restful風(fēng)格的接口,將各種組件及操作內(nèi)容都抽象成為標(biāo)準(zhǔn)的REST資源,如Namespace、Pod等,其中操作內(nèi)容以JSON或yml格式數(shù)據(jù)進(jìn)行操作。本文講解的是Kubernetes中的最為重要的一節(jié)——資源清單,我們想要在Kubernetes中部署Pod、Service等資源對(duì)象,都需要通過資源清單的方式來部署,無論是通過命令kubectl,還是可視化控制臺(tái),都是離不開資源清單的定義,本文重點(diǎn)講述資源清單如何定義、如何創(chuàng)建及使用。
1、資源分類
根據(jù)資源的功能進(jìn)行資源分類,Kubernetes資源對(duì)象可分為:
- 工作負(fù)載(Workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob。
 - 發(fā)現(xiàn)和負(fù)載均衡(Discovery & LB):Service 、Ingress。
 - 配置和存儲(chǔ)(Config & Storage):Volume(存儲(chǔ)卷)、CSI(容器存儲(chǔ)接口,可以擴(kuò)展各種各樣的第三方存儲(chǔ)卷)。
 - 集群(Cluster):Namespace、Node、Role、ClusterRole、RoleBinding(角色綁定)、ClusterRoleBinding(集群角色綁定)。
 - 元數(shù)據(jù)(Metadata):HPA、PodTemplate(Pod模板,用于讓控制器創(chuàng)建Pod時(shí)使用的模板)、LimitRange(用來定義硬件資源限制的)。
 
一個(gè)應(yīng)用通常需要多個(gè)資源的支撐,例如,使用Deployment資源管理應(yīng)用實(shí)例(Pod)、使用ConfigMap資源保存應(yīng)用配置、使用Service或Ingress資源暴露服務(wù)、使用Volume資源提供外部存儲(chǔ)等。
2.資源清單
資源清單,等同于一個(gè)劇本,能夠告訴我們每一步應(yīng)該怎么去做,Kubernetes接收到這么一個(gè)劇本,就能夠按照這個(gè)劇本去執(zhí)行,以達(dá)到我們的預(yù)期。在Kubernetes中,一般都是通過定義資源清單的方式去創(chuàng)建資源。一般使用yaml格式的文件來創(chuàng)建符合我們預(yù)期期望的資源,這樣的yaml文件我們稱為資源清單。(也可以定義為json格式)如,創(chuàng)建一個(gè)Pod資源:
- apiVersion: v1
 - kind: Pod
 - metadata:
 - name: vue-frontend
 - namespace: test
 - labels:
 - app: vue-frontend
 - spec:
 - containers:
 - - name: vue-frontend
 - image: xcbeyond/vue-frontend:latest
 - ports:
 - - name: port
 - containerPort: 80
 - hostPort: 8080
 
接下來,以Pod資源定義為例展開對(duì)資源清單的詳細(xì)說明。
2.1 資源清單定義
yaml格式的Pod資源清單定義文件的完整內(nèi)容如下:
- apiVersion: v1
 - kind: Pod # 資源類別
 - metadata: # 資源元數(shù)據(jù)
 - name: string
 - namespace: string
 - labels:
 - - name: string
 - annotations:
 - - name: string
 - spec: # 資源期望的狀態(tài)
 - containers: # 容器列表
 - - name: string # 容器名稱,下面的屬性均屬于對(duì)該容器的定義或約束
 - image: string
 - imagePullPolicy: [Always|Never|IfNotPresent]
 - command: [string]
 - args: [string]
 - workingDir: string
 - volumeMounts:
 - - name: string
 - mountPath: string
 - readOnly: boolean
 - ports:
 - - name: string
 - containerPort: int
 - hostPort: int
 - protocol: string
 - env:
 - - name: string
 - value: string
 - resources:
 - limits:
 - cpu: string
 - memory: string
 - requests:
 - cpu: string
 - memory: string
 - livenssProbe:
 - exec:
 - command: [string]
 - httpGet:
 - path: string
 - port: number
 - host: string
 - scheme: string
 - httpHeaders:
 - - name: string
 - value: string
 - tcpSocket:
 - port: number
 - initialDelaySeconds: 0
 - timeoutSeconds: 0
 - periodSeconds: 0
 - successThreshold: 0
 - failureThreshold: 0
 - ……
 
對(duì)各屬性的詳細(xì)說明如下表所示:(必選屬性,是必須存在的,否則創(chuàng)建失敗。)
上述列舉的是常用的屬性,如果想查看全部屬性,可以使用命令kubectl explain pod:
- [xcbeyond@bogon ~]$ 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/sig-architecture/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/sig-architecture/api-conventions.md#types-kinds
 - metadata <Object>
 - Standard object's metadata. More info:
 - https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
 - spec <Object>
 - Specification of the desired behavior of the pod. More info:
 - https://git.k8s.io/community/contributors/devel/sig-architecture/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/sig-architecture/api-conventions.md#spec-and-status
 
查看屬性說明,使用如下命令,如:查看pod.spec.containers
- [xcbeyond@bogon ~]$ kubectl explain pod.spec.containers
 - KIND: Pod
 - VERSION: v1
 - RESOURCE: containers <[]Object>
 - DESCRIPTION:
 - List of containers belonging to the pod. Containers cannot currently be
 - added or removed. There must be at least one container in a Pod. Cannot be
 - updated.
 - A single application container that you want to run within a pod.
 - FIELDS:
 - args <[]string>
 - Arguments to the entrypoint. The docker image's CMD is used if this is not
 - provided. Variable references $(VAR_NAME) are expanded using the
 - container's environment. If a variable cannot be resolved, the reference in
 - the input string will be unchanged. The $(VAR_NAME) syntax can be escaped
 - with a double $$, ie: $$(VAR_NAME). Escaped references will never be
 - expanded, regardless of whether the variable exists or not. Cannot be
 - updated. More info:
 - https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
 - command <[]string>
 - Entrypoint array. Not executed within a shell. The docker image's
 - ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)
 - are expanded using the container's environment. If a variable cannot be
 - resolved, the reference in the input string will be unchanged. The
 - $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).
 - Escaped references will never be expanded, regardless of whether the
 - variable exists or not. Cannot be updated. More info:
 - https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
 - ……
 
2.2 示例
在命名空間test中,定義一個(gè)名為frontend的Pod。
(1)定義命名空間
為了便于后期測(cè)試,特定義一個(gè)新的命名空間test。(如果命名空間test已存在,則無需再建)
命名空間test的資源清單文件test-namespace.yaml如下:
- apiVersion: v1
 - kind: Namespace
 - metadata:
 - name: test
 
執(zhí)行kubectl create命令創(chuàng)建該Namespace:
- [xcbeyond@bogon ~]$ kubectl create -f test-namespace.yaml
 - namespace/test created
 
(2)定義Pod
定義一個(gè)名為frontend的Pod,由一個(gè)容器組成,資源清單文件frontend-pod.yaml如下:
- apiVersion: v1
 - kind: Pod
 - metadata:
 - name: frontend
 - namespace: test
 - labels:
 - app: frontend
 - spec:
 - containers:
 - - name: frontend
 - image: xcbeyond/vue-frontend:latest
 - ports:
 - - name: port
 - containerPort: 80
 - hostPort: 8080
 
執(zhí)行kubectl create命令創(chuàng)建該P(yáng)od:
- [xcbeyond@bogon ~]$ kubectl create -f frontend-pod.yaml
 - pod/frontend created
 
通過命令kubectl  get pods -n 
- [xcbeyond@bogon ~]$ kubectl get pods -n test
 - NAME READY STATUS RESTARTS AGE
 - frontend 1/1 Runing 0 79s
 



















 
 
 













 
 
 
 