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

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Kubernetes中所有操作的內容,我們都稱爲“資源對象”,是由API Server基於HTTP/HTTPS接收並響應客戶端的操作請求,是一種Restful風格的接口,將各種組件及操作內容都抽象成爲標準的REST資源,如Namespace、Pod等,其中操作內容以JSON或yml格式數據進行操作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文講解的是Kubernetes中的最爲重要的一節——","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"資源清單","attrs":{}},{"type":"text","text":",我們想要在Kubernetes中部署Pod、Service等資源對象,都需要通過資源清單的方式來部署,無論是通過命令kubectl,還是可視化控制檯,都是離不開資源清單的定義,本文重點講述資源清單如何定義、如何創建及使用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1、資源分類","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據資源的功能進行資源分類,Kubernetes資源對象可分爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"工作負載(Workload)","attrs":{}},{"type":"text","text":":Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"發現和負載均衡(Discovery & LB)","attrs":{}},{"type":"text","text":":Service 、Ingress。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"配置和存儲(Config & Storage)","attrs":{}},{"type":"text","text":": Volume(存儲卷)、CSI(容器存儲接口,可以擴展各種各樣的第三方存儲卷)。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"集羣(Cluster)","attrs":{}},{"type":"text","text":":Namespace、Node、Role、ClusterRole、RoleBinding(角色綁定)、ClusterRoleBinding(集羣角色綁定)。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"元數據(Metadata)","attrs":{}},{"type":"text","text":":HPA、PodTemplate(Pod模板,用於讓控制器創建Pod時使用的模板)、LimitRange(用來定義硬件資源限制的)。","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個應用通常需要多個資源的支撐,例如,使用Deployment資源管理應用實例(Pod)、使用ConfigMap資源保存應用配置、使用Service或Ingress資源暴露服務、使用Volume資源提供外部存儲等。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2.資源清單","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"資源清單,等同於一個劇本,能夠告訴我們每一步應該怎麼去做,Kubernetes接收到這麼一個劇本,就能夠按照這個劇本去執行,以達到我們的預期。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Kubernetes中,一般都是通過定義資源清單的方式去創建資源。一般使用yaml格式的文件來創建符合我們預期期望的資源,這樣的yaml文件我們稱爲","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"資源清單","attrs":{}},{"type":"text","text":"。(也可以定義爲json格式)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如,創建一個Pod資源:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"apiVersion: v1\nkind: Pod\nmetadata:\n name: vue-frontend\n namespace: test\n labels:\n app: vue-frontend\nspec:\n containers:\n - name: vue-frontend\n image: xcbeyond/vue-frontend:latest\n ports:\n - name: port\n containerPort: 80\n hostPort: 8080","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下來,以Pod資源定義爲例展開對資源清單的詳細說明。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1 資源清單定義","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"yaml格式的Pod資源清單定義文件的完整內容如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"apiVersion: v1\nkind: Pod\t\t# 資源類別\nmetadata:\t\t# 資源元數據\n name: string\n namespace: string\n labels:\n - name: string\n annotations:\n - name: string\nspec:\t\t\t# 資源期望的狀態\n containers:\t\t# 容器列表\n - name: string\t\t# 容器名稱,下面的屬性均屬於對該容器的定義或約束\n image: string\n imagePullPolicy: [Always|Never|IfNotPresent]\n command: [string]\n args: [string]\n workingDir: string\n volumeMounts:\n - name: string\n mountPath: string\n readOnly: boolean\n ports:\n - name: string\n containerPort: int\n hostPort: int\n protocol: string\n env:\n - name: string\n value: string\n resources:\n limits:\n cpu: string\n memory: string\n requests:\n cpu: string\n memory: string\n livenssProbe:\n exec:\n command: [string]\n httpGet:\n path: string\n port: number\n host: string\n scheme: string\n httpHeaders:\n - name: string\n value: string\n tcpSocket:\n port: number\n initialDelaySeconds: 0\n timeoutSeconds: 0\n periodSeconds: 0\n successThreshold: 0\n failureThreshold: 0\n…… ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對各屬性的詳細說明如下表所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(必選屬性,是必須存在的,否則創建失敗。)","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/11/114c3a975a28b486bb23290bb6048eb8.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/69/69356a89b92eb2579cf60dba80a9d490.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b3/b3d269b74f0cc256eeee4f3d44cf3755.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d8/d8d9fd47865dc5c34ab3a0fecee4b658.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述列舉的是常用的屬性,如果想查看全部屬性,可以使用命令","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl explain pod","attrs":{}}],"attrs":{}},{"type":"text","text":":","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"[xcbeyond@bogon ~]$ kubectl explain pod\nKIND: Pod\nVERSION: v1\n\nDESCRIPTION:\n Pod is a collection of containers that can run on a host. This resource is\n created by clients and scheduled onto hosts.\n\nFIELDS:\n apiVersion\t\n APIVersion defines the versioned schema of this representation of an\n object. Servers should convert recognized schemas to the latest internal\n value, and may reject unrecognized values. More info:\n https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources\n\n kind\t\n Kind is a string value representing the REST resource this object\n represents. Servers may infer this from the endpoint the client submits\n requests to. Cannot be updated. In CamelCase. More info:\n https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds\n\n metadata\t\n Standard object's metadata. More info:\n https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata\n\n spec\t\n Specification of the desired behavior of the pod. More info:\n https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status\n\n status\t\n Most recently observed status of the pod. This data may not be up to date.\n Populated by the system. Read-only. More info:\n https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看屬性說明,使用如下命令,如:查看pod.spec.containers","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"[xcbeyond@bogon ~]$ kubectl explain pod.spec.containers\nKIND: Pod\nVERSION: v1\n\nRESOURCE: containers \n\nDESCRIPTION:\n List of containers belonging to the pod. Containers cannot currently be\n added or removed. There must be at least one container in a Pod. Cannot be\n updated.\n\n A single application container that you want to run within a pod.\n\nFIELDS:\n args\t\n Arguments to the entrypoint. The docker image's CMD is used if this is not\n provided. Variable references $(VAR_NAME) are expanded using the\n container's environment. If a variable cannot be resolved, the reference in\n the input string will be unchanged. The $(VAR_NAME) syntax can be escaped\n with a double $$, ie: $$(VAR_NAME). Escaped references will never be\n expanded, regardless of whether the variable exists or not. Cannot be\n updated. More info:\n https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell\n\n command\t\n Entrypoint array. Not executed within a shell. The docker image's\n ENTRYPOINT is used if this is not provided. Variable references $(VAR_NAME)\n are expanded using the container's environment. If a variable cannot be\n resolved, the reference in the input string will be unchanged. The\n $(VAR_NAME) syntax can be escaped with a double $$, ie: $$(VAR_NAME).\n Escaped references will never be expanded, regardless of whether the\n variable exists or not. Cannot be updated. More info:\n https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell\n……","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2 示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在命名空間test中,定義一個名爲frontend的Pod。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(1)定義命名空間","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了便於後期測試,特定義一個新的命名空間test。(如果命名空間test已存在,則無需再建)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"命名空間test的資源清單文件test-namespace.yaml如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"apiVersion: v1\nkind: Namespace\nmetadata: \n name: test","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl create","attrs":{}}],"attrs":{}},{"type":"text","text":"命令創建該Namespace:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":""},"content":[{"type":"text","text":"[xcbeyond@bogon ~]$ kubectl create -f test-namespace.yaml \nnamespace/test created","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(2)定義Pod","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"定義一個名爲frontend的Pod,由一個容器組成,資源清單文件frontend-pod.yaml如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"apiVersion: v1\nkind: Pod\nmetadata:\n name: frontend\n namespace: test\n labels:\n app: frontend\nspec:\n containers:\n - name: frontend\n image: xcbeyond/vue-frontend:latest\n ports:\n - name: port\n containerPort: 80\n hostPort: 8080","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"執行","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl create","attrs":{}}],"attrs":{}},{"type":"text","text":"命令創建該Pod:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"[xcbeyond@bogon ~]$ kubectl create -f frontend-pod.yaml \npod/frontend created","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過命令","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"kubectl get pods -n ","attrs":{}}],"attrs":{}},{"type":"text","text":"查看,創建Pod的狀態:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"[xcbeyond@bogon ~]$ kubectl get pods -n test\nNAME READY STATUS RESTARTS AGE\nfrontend 1/1 Runing 0 79s","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章