您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Kubernetes中怎么創建資源的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
在Kubernetes中所有操作的內容,我們都稱為“資源對象”,是由API Server基于HTTP/HTTPS接收并響應客戶端的操作請求,是一種Restful風格的接口,將各種組件及操作內容都抽象成為標準的REST資源,如Namespace、Pod等,其中操作內容以JSON或yml格式數據進行操作。本文講解的是Kubernetes中的最為重要的一節——資源清單,我們想要在Kubernetes中部署Pod、Service等資源對象,都需要通過資源清單的方式來部署,無論是通過命令kubectl,還是可視化控制臺,都是離不開資源清單的定義,本文重點講述資源清單如何定義、如何創建及使用。
根據資源的功能進行資源分類,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資源提供外部存儲等。
資源清單,等同于一個劇本,能夠告訴我們每一步應該怎么去做,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 <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中,定義一個名為frontend的Pod。
(1)定義命名空間
為了便于后期測試,特定義一個新的命名空間test。(如果命名空間test已存在,則無需再建)
命名空間test的資源清單文件test-namespace.yaml如下:
apiVersion: v1 kind: Namespace metadata: name: test
執行kubectl create命令創建該Namespace:
[xcbeyond@bogon ~]$ kubectl create -f test-namespace.yaml namespace/test created
(2)定義Pod
定義一個名為frontend的Pod,由一個容器組成,資源清單文件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
執行kubectl create命令創建該Pod:
[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
感謝各位的閱讀!關于“Kubernetes中怎么創建資源”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。