Kubernetes常用資源物件
依據資源的主要功能作為分類標準,
Kubernetes的API物件大體可分為五個類別,如下:
| 型別 | 名稱 |
|---|---|
| 作業負載(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系統自行維護,對用戶來說為只讀欄位,
獲取物件的JSON格式的配置清單可以通過"kubectl get TYPE/NAME -o yaml"命令來獲取,
[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"
創建資源的方法
-
apiserver僅接受JSON格式的資源定義 -
yaml格式提供資源配置清單,apiserver可自動將其轉為json格式,而后再提交
大部分資源的配置清單由以下5個欄位組成
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自行維護
kubernetes存在內嵌的格式說明,定義資源配置清單時,可以使用kubectl explain命令進行查看,如查看Pod這個資源的定義:
[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
如果需要了解某一級欄位表示的物件之下的二級物件欄位時,只需要指定其二級欄位的物件名稱即可,三級和四級欄位物件等的查看方式依次類推,例如查看Pod資源的Spec物件支持嵌套使用的二級欄位:
[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. .....
配置清單模式創建Pod
[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=https://www.cnblogs.com/yanjieli/p/"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容器 / #
Pod資源spec的containers欄位決議
[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
1)給已有的pod添加標簽,通過kubectl label命令
[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
標簽選擇器
標簽選擇器用于選擇標簽的查詢條件或選擇標準,
kubernetes API目前支持兩個選擇器:基于等值關系以及基于集合關系,例如,env=production和env!=qa是基于等值關系的選擇器,而tier in(frontend,backend)則是基于集合關系的選擇器,使用標簽選擇器時還將遵循以下邏輯: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
處此之外,
kubernetes的諸多資源物件必須以標簽選擇器的方式關聯到pod資源物件,例如Service、Deployment和ReplicaSet型別的資源等,它們在spec欄位中嵌套使用嵌套的“selector”欄位,通過“matchlabels”來指定標簽選擇器,有的甚至還支持使用“matchExpressions”構建復雜的標簽選擇器機制,
matchLabels:通過直接給定鍵值對來指定標簽選擇器
matchExpressions:基于運算式指定的標簽選擇器串列,每個選擇器都形如“{key:KEY_NAME, operator:OPERATOR, values:[VALUE1,VALUE2,...]}”
節點選擇器
pod節點選擇器是標簽及標簽選擇器的一種應用,它能夠讓pod物件基于集群中作業節點的標簽來挑選傾向運行的目標節點,
#在定義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
修改yaml檔案,添加節點選擇器nodeSelector,然后重新創建pod
[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物件,僅可用于資源提供“元資料”資訊,另外,注解中的元資料不受字符數量的限制,它可大可小,可以為結構化或非結構化形式,也支持使用在標簽中禁止使用的其他字符,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/57250.html
標籤:其他
