依據資源的主要功能做爲分類標準,
Kubernetes
的API
對象大致可分爲五個類別,以下:html
名稱 | |
---|---|
工做負載(Workload) | Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、Cronjob |
負載均衡(Discovery &LB) | Service、Ingress |
配置和存儲(Config&Storage) | Volume、CSI、ConfigMap、Secret、DownwardAPI |
集羣(Cluster) | Namespace、Node、Role、ClusterRole、RoleBinding、ClusterRoleBinding |
元數據(metadata) | HPA、PodTemplate、LimitRange |
Kubernetes API
僅接受及響應JSON
格式的數據(JSON
對象),同時,爲了便於使用,它也容許用戶提供YAML
格式的POST
對象,但API Server
須要實現自行將其轉換爲JSON
格式後方能提交。API Server
接受和返回的全部JSON
對象都遵循同一個模式,它們都具備kind
和apiVersion
字段,用於標識對象所屬的資源類型、API
羣組及相關的版本。前端大多數的對象或列表類型的資源提供元數據信息,如名稱、隸屬的名稱空間和標籤等;
spec
則用於定義用戶指望的狀態,不一樣的資源類型,其狀態的意義也各有不一樣,例如Pod
資源最爲核心的功能在於運行容器;而status
則記錄着活動對象的當前狀態信息,它由Kubernetes
系統自行維護,對用戶來講爲只讀字段。node
獲取對象的JSON
格式的配置清單能夠經過"kubectl get TYPE/NAME -o yaml
"命令來獲取。linux
[root@k8s-master ~]# kubectl get pod nginx-67685f79b5-8rjk7 -o yaml #獲取該pod的配置清單 apiVersion: v1 kind: Pod metadata: creationTimestamp: "2019-08-30T07:00:30Z" generateName: nginx-67685f79b5- labels: pod-template-hash: 67685f79b5 run: nginx name: nginx-67685f79b5-8rjk7 namespace: default ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: nginx-67685f79b5 uid: 6de479a9-52f6-4581-8e06-884a84dab593 resourceVersion: "244953" selfLink: /api/v1/namespaces/default/pods/nginx-67685f79b5-8rjk7 uid: 0b6f5a87-4129-4b61-897a-6020270a846e spec: containers: - image: nginx:1.12 imagePullPolicy: IfNotPresent name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-s8mbf readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: k8s-node1 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-s8mbf secret: defaultMode: 420 secretName: default-token-s8mbf status: conditions: - lastProbeTime: null lastTransitionTime: "2019-08-30T07:00:30Z"
建立資源的方法nginx
apiserver
僅接受JSON
格式的資源定義git
yaml
格式提供資源配置清單,apiserver
可自動將其轉爲json
格式,然後再提交docker
大部分資源的配置清單由如下5個字段組成json
apiVersion: 指明api資源屬於哪一個羣組和版本,同一個組能夠有多個版本 group/version # kubectl api-versions 命令能夠獲取 kind: 資源類別,標記建立的資源類型,k8s主要支持如下資源類別 Pod、ReplicaSet、Deployment、StatefulSet、DaemonSet、Job、Cronjob metadata: 用於描述對象的屬性信息,主要提供如下字段: name: 指定當前對象的名稱,其所屬的名稱空間的同一類型中必須惟一 namespace: 指定當前對象隸屬的名稱空間,默認值爲default labels: 設定用於標識當前對象的標籤,鍵值數據,常被用做挑選條件 annotations: 非標識型鍵值數據,用來做爲挑選條件,用於labels的補充 spec: 用於描述所指望的對象應該具備的狀態(disired state),資源對象中最重要的字段。 status: 用於記錄對象在系統上的當前狀態(current state),本字段由kubernetes自行維護
vim
[root@k8s-master ~]# kubectl explain pods 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/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/api-conventions.md#types-kinds metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata spec <Object> Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/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/api-conventions.md#spec-and-status
api
[root@k8s-master ~]# kubectl explain pods.spec RESOURCE: spec <Object> DESCRIPTION: Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status PodSpec is a description of a pod. FIELDS: activeDeadlineSeconds <integer> Optional duration in seconds the pod may be active on the node relative to StartTime before the system will actively try to mark it failed and kill associated containers. Value must be a positive integer. affinity <Object> If specified, the pod's scheduling constraints automountServiceAccountToken <boolean> AutomountServiceAccountToken indicates whether a service account token should be automatically mounted. .....
[root@k8s-master ~]# mkdir manfests [root@k8s-master ~]# cd manfests/ [root@k8s-master manfests]# vim 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" [root@k8s-master manfests]# kubectl create -f pod-demo.yaml pod/pod-demo created [root@k8s-master manfests]# [root@k8s-master manfests]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 0 15s [root@k8s-master manfests]# kubectl describe pods pod-demo #查看pod詳細信息 [root@k8s-master manfests]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-demo 2/2 Running 0 102s 10.244.1.17 k8s-node1 <none> <none> [root@k8s-master manfests]# [root@k8s-master manfests]# curl 10.244.1.17 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> [root@k8s-master manfests]# [root@k8s-master manfests]# kubectl logs pod-demo myapp #查看pod-demo下myapp的日誌 10.244.0.0 - - [03/Sep/2019:02:32:52 +0000] "GET / HTTP/1.1" 200 65 "-" "curl/7.29.0" "-" [root@k8s-master manfests]# [root@k8s-master manfests]# kubectl exec -it pod-demo -c myapp -- /bin/sh #進入myapp容器 / #
[root@k8s-master ~]# kubectl explain pods.spec.containers name <string> 指定容器名稱 image <string> 指定容器所需鏡像倉庫及鏡像名,例如ikubernetes/myapp:v1 imagePullPolicy <string> (可取如下三個值Always,Never,IfNotpresent) Always:鏡像標籤爲「latest」時,老是去指定的倉庫中獲取鏡像 Never:禁止去倉庫中下載鏡像,即僅使用本地鏡像 IfNotpresent:若是本地沒有該鏡像,則去鏡像倉庫中下載鏡像 ports <[]Object> 值是一個列表,由一到多個端口對象組成。例如:(名稱(可後期調用) 端口號 協議 暴露在的地址上) 暴露端口只是提供額外信息的,不能限制系統是否真的暴露 containerPort <integer> 指定暴露的容器端口 name <string> 當前端口的名稱 hostIP <string> 主機端口要綁定的主機IP hostPort <integer> 主機端口,它將接收到請求經過NAT轉發至containerPort字段指定的端口 protocol <string> 端口的協議,默認是TCP args <[]string> 傳遞參數給command 至關於docker中的CMD command <[]string> 至關於docker中的ENTRYPOINT
鏡像中的命令和pod中定義的命令關係說明:
若是pod
中沒有提供command
或者args
,則使用docker
中的CMD
和ENTRYPOINT
。
若是pod
中提供了command
但不提供args
,則使用提供的command
,忽略docker
中的Cmd
和Entrypoint
。
若是pod
中只提供了args
,則args
將做爲參數提供給docker
中的Entrypoint
使用。
若是pod
中同時提供了command
和args
,則docker
中的cmd
和Entrypoint
將會被忽略,pod
中的args
將最爲參數給cmd
使用。
標籤是
Kubernetes
極具特點的功能之一,它可以附加於Kubernetes
的任何資源對象之上。簡單來講,標籤就是「鍵值」類型的數據,能夠在資源建立時直接指定,也能夠隨時按需添加到活動對象中。然後便可由標籤選擇器進行匹配度檢查從而完成資源挑選。一個對象可擁有不止一個標籤,而同一個標籤也能夠被添加到至多個資源之上。
key=value key:字母、數字、_、-、. 只能以字母或者數字開頭 value:能夠爲空,只能以字母或者數字開頭及結尾,中間可使用字母、數字、_、-、. 在實際環境中,儘可能作到見名知意,且儘量保持簡單
[root@k8s-master ~]# kubectl get pods --show-labels #查看pod信息時,並顯示對象的標籤信息 NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 5 5h13m app=myapp,tier=frontend [root@k8s-master ~]# kubectl get pods -l app #過濾包含app標籤的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 5 5h20m [root@k8s-master ~]# kubectl get pods -l app,tier #過濾同時包含app,tier標籤的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 5 5h20m [root@k8s-master ~]# kubectl get pods -L app #顯示有app鍵的標籤信息 NAME READY STATUS RESTARTS AGE APP pod-demo 2/2 Running 5 5h21m myapp [root@k8s-master ~]# kubectl get pods -L app,tier #顯示有app和tier鍵的標籤信息 NAME READY STATUS RESTARTS AGE APP TIER pod-demo 2/2 Running 5 5h21m myapp frontend
[root@k8s-master ~]# kubectl label --help Usage: kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version] [options] [root@k8s-master ~]# kubectl label pods/pod-demo env=production #給pod資源pod-demo添加env標籤值爲production pod/pod-demo labeled [root@k8s-master ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 5 5h32m app=myapp,env=production,tier=frontend
2)修改已有的標籤的值
[root@k8s-master ~]# kubectl label pods/pod-demo env=testing --overwrite #同上面添加標籤同樣,只是添加--overwrite參數 pod/pod-demo labeled [root@k8s-master ~]# [root@k8s-master ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS pod-demo 2/2 Running 5 5h39m app=myapp,env=testing,tier=frontend
1)同時指定的多個選擇器之間的邏輯關係爲「與」操做
2)使用空值的標籤選擇器意味着每一個資源對象都將被選中
3)空的標籤選擇器將沒法選出任何資源。
等值關係標籤選擇器:
"="、「==」和「!=」三種,其中前兩個意義相同,都表示等值關係;最後一個表示不等關係。
集合關係標籤選擇器:
KEY in(VALUE1,VALUE2,...):指定的健名的值存在於給定的列表中即知足條件
KEY notin(VALUE1,VALUE2,...):指定的鍵名的值不存在與給定的列表中即知足條件
KEY:全部存在此健名標籤的資源。
!KEY:全部不存在此健名標籤的資源。
1)等值關係示例:
[root@k8s-master ~]# kubectl get pods -l app=myapp #過濾標籤鍵爲app值爲myapp的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h11m [root@k8s-master ~]# kubectl get pods -l app=myapp,env=testing #過濾標籤鍵爲app值爲myqpp,而且標籤鍵爲env值爲testing的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h11m [root@k8s-master ~]# kubectl get pods -l app!=my #過濾標籤鍵爲app值不爲my的全部pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h17m
2)集合關係示例:
[root@k8s-master ~]# kubectl get pods -l "app in (myapp)" #過濾鍵爲app值有myapp的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h51m [root@k8s-master ~]# kubectl get pods -l "app notin (my)" #過濾鍵爲app值沒有my的pod NAME READY STATUS RESTARTS AGE pod-demo 2/2 Running 6 6h59m
matchLabels:經過直接給定鍵值對來指定標籤選擇器
matchExpressions:基於表達式指定的標籤選擇器列表,每一個選擇器都形如「{key:KEY_NAME, operator:OPERATOR, values:[VALUE1,VALUE2,...]}」
#在定義pod資源清單時,能夠經過nodeName來指定pod運行的節點,或者經過nodeSelector來挑選傾向的節點 [root@k8s-master ~]# kubectl explain pods.spec nodeName <string> NodeName is a request to schedule this pod onto a specific node. If it is non-empty, the scheduler simply schedules this pod onto that node, assuming that it fits resource requirements. nodeSelector <map[string]string> NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
查看節點默認的標籤
[root@k8s-master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-node1 Ready <none> 6d1h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux k8s-node2 Ready <none> 6d1h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
給節點添加標籤
[root@k8s-master ~]# kubectl label nodes/k8s-node1 disktype=ssd node/k8s-node1 labeled [root@k8s-master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready master 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master= k8s-node1 Ready <none> 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux k8s-node2 Ready <none> 6d2h v1.15.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master ~]# vim manfests/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 ports: - name: http containerPort: 80 - name: busybox image: busybox:latest command: - "/bin/sh" - "-c" - "sleep 3600" nodeSelector: disktype: ssd [root@k8s-master ~]# kubectl delete -f manfests/pod-demo.yaml #刪除上面建立的pod資源 pod "pod-demo" deleted [root@k8s-master ~]# kubectl create -f manfests/pod-demo.yaml #從新建立pod-demo資源 pod/pod-demo created [root@k8s-master ~]# kubectl get pods -o wide #查看pod,能夠看到分配到了k8s-node1節點(也就是上面打上disktype標籤的節點) NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod-demo 2/2 Running 0 16s 10.244.1.19 k8s-node1 <none> <none> [root@k8s-master ~]# kubectl describe pods pod-demo ...... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 58s default-scheduler Successfully assigned default/pod-demo to k8s-node1 ......
除了標籤(label)以外,Pod與其餘各類資源還能使用資源註解(annotation)。與標籤相似,註解也是「鍵值」類型的數據,不過它不能用於標籤及挑選Kubernetes對象,僅可用於資源提供「元數據」信息。另外,註解中的元數據不受字符數量的限制,它可大可小,能夠爲結構化或非結構化形式,也支持使用在標籤中禁止使用的其餘字符。