目錄
一、K8S中常見的資源
二、創(chuàng)建資源清單
2.1 創(chuàng)建資源的方法:
2.2 常用資源的配置清單
2.3 利用資源清單創(chuàng)建Pod
2.4 kubectl命令管理資源有三種用法
一、K8S中常見的資源
Kubernetes中把資源實例化以后稱之為對象,這里先介紹K8S中常見的核心資源有哪些:
- 工作負載型資源(workload):Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、CronJob。(ReplicationController在v1.11版本被廢棄)
- 服務發(fā)現(xiàn)及負載均衡型資源(ServiceDiscovery、LoadBalance) : Service 、Ingress, ...
-
配置與存儲型資源: Volume(存儲卷)、CSI(容器存儲接口,可以擴展各種各樣的第三方存儲卷)
- 特殊類型的存儲卷:ConfigMap(當配置中心來使用的資源類型)、Secret(保存敏感數(shù)據(jù))、DownwardAPI(把外部環(huán)境中的信息輸出給容器)
以上這些資源都是配置在名稱空間級別。
- 集群級資源(都是配置在名): Namespace、Node、Role、ClusterRole、RoleBinding(角色綁定)、ClusterRoleBinding(集群角色綁定)、
- 元數(shù)據(jù)型資源:HPA、PodTemplate(Pod模板,用于讓控制器創(chuàng)建Pod時使用的模板。)、LimitRange(用來定義硬件資源限制的)
下面是利用資源清單創(chuàng)建一個Pod的資源清單內(nèi)容:
[root@k8s-master ~]# kubectl get pod myapp-848b5b879b-6mq5c -o yaml
[root@k8s-master ~]# kubectl get pod myapp-848b5b879b-6mq5c -o yaml
apiVersion: v1 # K8S API版本,應該由兩部分組成:group/version,group省略表示默認為core
kind: Pod # 資源類別: Pod、Deployment、Service等等
metadata: # 資源元數(shù)據(jù)
creationTimestamp: 2018-09-26T11:49:23Z
generateName: myapp-848b5b879b-
labels:
pod-template-hash: "4046164356"
run: myapp
name: myapp-848b5b879b-6mq5c
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: myapp-848b5b879b
uid: e1f7b7be-c17d-11e8-8968-000c29eced73
resourceVersion: "117793"
selfLink: /api/v1/namespaces/default/pods/myapp-848b5b879b-6mq5c
uid: 367b77fc-c182-11e8-8968-000c29eced73
spec: # specifications, 資源規(guī)格。(定義資源對象期望的狀態(tài)),這個是最重要的字段,用于規(guī)定接下來要創(chuàng)建的資源對象應該擁有的特性。然后依靠控制器確保這些特性能夠被滿足。
containers:
- image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
name: myapp
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-rqmtb
readOnly: true
dnsPolicy: ClusterFirst
nodeName: k8s-node1.fhw.com
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations: # 容忍度,能夠容忍哪些污點
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-rqmtb
secret:
defaultMode: 420
secretName: default-token-rqmtb
status: # 用于顯示這個資源對象當前的狀態(tài),這個字段是只讀的。
conditions:
- lastProbeTime: null
lastTransitionTime: 2018-09-26T11:49:23Z
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: 2018-09-26T11:49:25Z
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: null
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: 2018-09-26T11:49:23Z
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://cf1a7d53dfe93b679ed3213a94ccc94974ee87514e6e1cf049904934e9795f4d
image: ikubernetes/myapp:v1
imageID: docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
lastState: {}
name: myapp
ready: true
restartCount: 0
state:
running:
startedAt: 2018-09-26T11:49:24Z
hostIP: 192.168.100.136
phase: Running
podIP: 10.244.1.11
qosClass: BestEffort
startTime: 2018-09-26T11:49:23Z
命令:kubectl api-versions可以查看所有API 群組/版本

image.png
二、創(chuàng)建資源清單
2.1 創(chuàng)建資源的方法:
apiserver僅接收JSON格式的資源定義,利用yaml格式提供配置清單,apiserver可自動將其轉(zhuǎn)為json格式,而后再提交并執(zhí)行。
2.2 常用資源的配置清單
apiVersion: group/version
$ kubectl api-versions
kind: 資源類別
metadata: 元數(shù)據(jù)
name: 資源名稱
namespace: 名稱空間
labels: 標簽,鍵值數(shù)據(jù)。數(shù)據(jù)大小有限制。
annotations: 注解,也是鍵值數(shù)據(jù),但是它的數(shù)據(jù)沒有大小限制。
spec: 期望的狀態(tài),disired state,由用戶定義,最重要。每種資源支持的字段不一樣。
status: 當前狀態(tài),current state, 本字段由K8S集群維護。
每個資源的引用PATH:
/api/GROUP/VERSION/namespace/NAMESPACE/TYPE/NAME (大寫單詞替換為具體名稱),可以通過這個PATH獲取資源的信息。
查看某個資源類型支持的字段:
命令: kubectl explain <resource_type>.<fieldName>[.<fieldName>]

image.png
每個字段的值都標記有對應的類型:
字段標記有-required-表示必選字段。
| 值類型 | 簡述 |
|---|---|
<string> |
字符串 |
<[]string> |
字符串列表,所有的列表數(shù)據(jù)都可以放在[]中。 |
<integer> |
整數(shù) |
<Object> |
對象,也就是可以嵌套二級或三級字段。。。 |
<[]Object> |
對象列表, |
<map[string]string> |
映射,多個k=v類型的json數(shù)組,也就是鍵值對,key=value,所有映射數(shù)據(jù)都可以直接放在{}中。 |
<boolean> |
布爾值,true或false |
2.3 利用資源清單創(chuàng)建Pod
先創(chuàng)建一個資源清單:
[root@k8s-master manifests]# cat pod-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
- name: busybox
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
執(zhí)行命令以創(chuàng)建Pod:
[root@k8s-master manifests]# kubectl create -f pod-demo.yaml
pod/pod-demo created
如圖:

image.png
訪問pod-demo pod中的myapp容器并查看其日志:
[root@k8s-master manifests]# curl 10.244.2.10
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master manifests]#
[root@k8s-master manifests]#
[root@k8s-master manifests]# kubectl logs pod-demo myapp
10.244.0.0 - - [27/Sep/2018:09:14:59 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-"
刪除資源清單里面的資源:
[root@k8s-master manifests]# kubectl delete -f pod-demo.yaml
pod "pod-demo" deleted
事實上使用kubectl命令管理資源有三種用法:
- 命令式用法,《Kubernetes 學習筆記(二)--- K8S應用快速入門》中講的。
- 命令式資源清單用法,就是本篇 2.3節(jié)《 利用資源清單創(chuàng)建Pod》;
- 聲明式資源清單。使用聲明式資源清單,可以確保資源盡可能的向我們聲明的狀態(tài)改變,這樣我們就可以隨時改變聲明,并隨時應用。