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資源:
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資源定義為例展開對資源清單的詳細說明。
2.1 資源清單定義
yaml格式的Pod資源清單定義文件的完整內容如下:
apiVersion: v1
kind: Pod # 資源類別
metadata: # 資源元數據
name: string
namespace: string
labels:
- name: string
annotations:
- name: string
spec: # 資源期望的狀態
containers: # 容器列表
- name: string # 容器名稱,下面的屬性均屬于對該容器的定義或約束
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
……
對各屬性的詳細說明如下表所示:
(必選屬性,是必須存在的,否則創建失敗。)
?
?
?
?
上述列舉的是常用的屬性,如果想查看全部屬性,可以使用命令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
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
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
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec
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
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
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
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
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 示例
總結
以上是生活随笔為你收集整理的Kubernetes资源清单篇:如何创建资源?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何让自己自信
- 下一篇: 京东物流CEO王振辉:供应链数字化是产业