kubernetes配置清單知識點
阿新 • • 發佈:2021-08-24
資源及其在API中的組織形式
1 [root@k8s-master01 ~]# kubectl api-versions 2 admissionregistration.k8s.io/v1 3 admissionregistration.k8s.io/v1beta1 4 apiextensions.k8s.io/v1 5 apiextensions.k8s.io/v1beta1 6 apiregistration.k8s.io/v1 7 apiregistration.k8s.io/v1beta1 8 apps/v1 9 authentication.k8s.io/v1 10 authentication.k8s.io/v1beta111 authorization.k8s.io/v1 12 authorization.k8s.io/v1beta1 13 autoscaling/v1 14 autoscaling/v2beta1 15 autoscaling/v2beta2 16 batch/v1 17 batch/v1beta1 18 certificates.k8s.io/v1 19 certificates.k8s.io/v1beta1 20 coordination.k8s.io/v1 21 coordination.k8s.io/v1beta1 22 discovery.k8s.io/v1beta1 23 events.k8s.io/v124 events.k8s.io/v1beta1 25 extensions/v1beta1 26 networking.k8s.io/v1 27 networking.k8s.io/v1beta1 28 node.k8s.io/v1beta1 29 policy/v1beta1 30 rbac.authorization.k8s.io/v1 31 rbac.authorization.k8s.io/v1beta1 32 scheduling.k8s.io/v1 33 scheduling.k8s.io/v1beta1 34 storage.k8s.io/v1 35 storage.k8s.io/v1beta1 36 v1
在當前叢集中,API Server所支援的API群組及其相關版本資訊可以通過kubectl api-servions命令獲取,如上命令結果顯示的多數API群組會在配置清單時用到。
例如:
apiVersion:v1
apiVersion:apps/v1
物件類資源配置規範(配置清單由5個(核心)一級欄位組成)
1 [root@k8s-master01 ~]# kubectl get namespace kube-system -o yaml 2 apiVersion: v1 #API群組及相關版本 3 kind: Namespace #kind欄位:標識物件所屬資源型別;常用的型別有namespace、deployment、service及pod等等
apiVersion和kind欄位可合稱為型別元資料(TypeMeta) 4 metadata: #metadata欄位為資源提供元資料資訊,例如:名稱、隸屬的名稱空間和標籤(labels) 5 creationTimestamp: "2021-08-18T05:40:55Z" 6 managedFields: 7 - apiVersion: v1 8 fieldsType: FieldsV1 9 fieldsV1: 10 f:status: 11 f:phase: {} 12 manager: kube-apiserver 13 operation: Update 14 time: "2021-08-18T05:40:55Z" 15 name: kube-system 16 resourceVersion: "4" 17 selfLink: /api/v1/namespaces/kube-system 18 uid: 96bc536f-f992-4822-b31f-204c3b002619 19 spec: #spec欄位則是由使用者負責宣告物件期望狀態的欄位,不同資源型別的期望狀態描述方式各不相同,因此其巢狀支援的欄位也不盡相同。 20 finalizers: 21 - kubernetes 22 status: #status欄位則記錄活動物件當前狀態資訊,它由kubernetes系統自行維護,對使用者來說為只讀欄位,不需要在配置清單提供。 23 phase: Active
獲取資源配置清單格式文件
命令:kubectl explain
kubectl explain pods|service|namespace|deployment 顯示一級資源欄位
kubectl explain pods.metadata 顯示巢狀二級欄位
kubectl explain pods.metadata.tables 顯示巢狀三級欄位
新增標籤:
命令直接管理活動物件的標籤
1 [root@k8s-master01 yaml]# cat pod-demo-with-cmd-and-args.yaml 2 apiVersion: v1 3 kind: Pod 4 metadata: 5 name: pod-demo-with-cmd-and-args 6 namespace: dev 7 spec: 8 containers: 9 - name: demo 10 image: ikubernetes/demoapp:v1.0 11 imagePullPolicy: IfNotPresent 12 command: ["/bin/sh","-c"] 13 args: ["python3 /usr/local/bin/demo.py -p 8081"] 14 15 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args release=beta 16 Error from server (NotFound): pods "pod-demo-with-cmd-and-args" not found 17 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args -n dev release=beta 18 pod/pod-demo-with-cmd-and-args labeled
修改標籤與刪除標籤
1 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args -n dev release=canary 2 error: 'release' already has a value (beta), and --overwrite is false 3 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args -n dev release=canary --overwrite 4 pod/pod-demo-with-cmd-and-args labeled #修改標籤必須帶上引數--overwrite 表示強制覆蓋原有標籤 5 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args -n dev --show-labels 6 Error: unknown flag: --show-labels 7 See 'kubectl label --help' for usage. 8 [root@k8s-master01 yaml]# kubectl get pod/pod-demo-with-cmd-and-args -n dev --show-labels #檢視標籤 9 NAME READY STATUS RESTARTS AGE LABELS 10 pod-demo-with-cmd-and-args 1/1 Running 0 74m release=canary 11 [root@k8s-master01 yaml]# kubectl label pod/pod-demo-with-cmd-and-args -n dev release- #刪除標籤,標籤名稱後面並緊跟一個-號 12 pod/pod-demo-with-cmd-and-args labeled 13 [root@k8s-master01 yaml]# kubectl get pod/pod-demo-with-cmd-and-args -n dev --show-labels 14 NAME READY STATUS RESTARTS AGE LABELS 15 pod-demo-with-cmd-and-args 1/1 Running 0 75m <none>
資源註解(annotations)
資源註解:資源註解也是鍵值型資料,類似於標籤,但是它不能用作標籤,可以使用者自行新增,也可以由工具程式自動附加並使用。
kubectl get和kubectl describe命令均能顯示資源的註解資訊
[root@k8s-master01 yaml]# kubectl describe pod/pod-demo-with-cmd-and-args -n dev Name: pod-demo-with-cmd-and-args Namespace: dev Priority: 0 Node: k8s-node02/192.168.3.22 Start Time: Sun, 22 Aug 2021 23:05:29 +0800 Labels: release=canary Annotations: <none> Status: Running IP: 10.244.2.10 IPs: IP: 10.244.2.10 Containers: demo: Container ID: docker://373799e5b51d33bb086ca63936be2193b95e2161c3b6f570abdda2e663172e85 Image: ikubernetes/demoapp:v1.0 Image ID: docker-pullable://ikubernetes/demoapp@sha256:6698b205eb18fb0171398927f3a35fe27676c6bf5757ef57a35a4b055badf2c3 Port: <none> Host Port: <none> Command: /bin/sh -c Args: python3 /usr/local/bin/demo.py -p 8081 State: Running Started: Sun, 22 Aug 2021 23:05:57 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-7p2m9 (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-7p2m9: Type: Secret (a volume populated by a Secret) SecretName: default-token-7p2m9 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: <none> [root@k8s-master01 yaml]# [root@k8s-master01 yaml]# [root@k8s-master01 yaml]# clear [root@k8s-master01 yaml]# kubectl get pod/pod-demo-with-cmd-and-args -n dev -o yaml apiVersion: v1 kind: Pod metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"pod-demo-with-cmd-and-args","namespace":"dev"},"spec":{"containers":[{"args":["python3 /usr/local/bin/demo.py -p 8081"],"command":["/bin/sh","-c"],"image":"ikubernetes/demoapp:v1.0","imagePullPolicy":"IfNotPresent","name":"demo"}]}} creationTimestamp: "2021-08-24T02:45:43Z" labels: release: canary managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:annotations: .: {} f:kubectl.kubernetes.io/last-applied-configuration: {} f:spec: f:containers: k:{"name":"demo"}: .: {} f:args: {} f:command: {} f:image: {} f:imagePullPolicy: {} f:name: {} f:resources: {} f:terminationMessagePath: {} f:terminationMessagePolicy: {} f:dnsPolicy: {} f:enableServiceLinks: {} f:restartPolicy: {} f:schedulerName: {} f:securityContext: {} f:terminationGracePeriodSeconds: {} manager: kubectl-client-side-apply operation: Update time: "2021-08-24T02:45:43Z" - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:status: f:conditions: k:{"type":"ContainersReady"}: .: {} f:lastProbeTime: {} f:lastTransitionTime: {} f:status: {} f:type: {} k:{"type":"Initialized"}: .: {} f:lastProbeTime: {} f:lastTransitionTime: {} f:status: {} f:type: {} k:{"type":"Ready"}: .: {} f:lastProbeTime: {} f:lastTransitionTime: {} f:status: {} f:type: {} f:containerStatuses: {} f:hostIP: {} f:phase: {} f:podIP: {} f:podIPs: .: {} k:{"ip":"10.244.2.10"}: .: {} f:ip: {} f:startTime: {} manager: kubelet operation: Update time: "2021-08-24T02:46:09Z" - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:labels: .: {} f:release: {} manager: kubectl-label operation: Update time: "2021-08-24T04:06:32Z" name: pod-demo-with-cmd-and-args namespace: dev resourceVersion: "907621" selfLink: /api/v1/namespaces/dev/pods/pod-demo-with-cmd-and-args uid: f52c1976-2f59-4351-92cd-93a970a48a73 spec: containers: - args: - python3 /usr/local/bin/demo.py -p 8081 command: - /bin/sh - -c image: ikubernetes/demoapp:v1.0 imagePullPolicy: IfNotPresent name: demo resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-7p2m9 readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: k8s-node02 preemptionPolicy: PreemptLowerPriority 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-7p2m9 secret: defaultMode: 420 secretName: default-token-7p2m9 status: conditions: - lastProbeTime: null lastTransitionTime: "2021-08-22T15:05:29Z" status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: "2021-08-22T15:05:58Z" status: "True" type: Ready - lastProbeTime: null lastTransitionTime: "2021-08-22T15:05:58Z" status: "True" type: ContainersReady - lastProbeTime: null lastTransitionTime: "2021-08-24T02:45:43Z" status: "True" type: PodScheduled containerStatuses: - containerID: docker://373799e5b51d33bb086ca63936be2193b95e2161c3b6f570abdda2e663172e85 image: ikubernetes/demoapp:v1.0 imageID: docker-pullable://ikubernetes/demoapp@sha256:6698b205eb18fb0171398927f3a35fe27676c6bf5757ef57a35a4b055badf2c3 lastState: {} name: demo ready: true restartCount: 0 started: true state: running: startedAt: "2021-08-22T15:05:57Z" hostIP: 192.168.3.22 phase: Running podIP: 10.244.2.10 podIPs: - ip: 10.244.2.10 qosClass: BestEffort startTime: "2021-08-22T15:05:29Z"
annotations可以在資源建立時由metadata.annotations欄位指定,也可以隨時在資源上使用命令kubectl annotate 進行新增(和標籤使用方法差不多)。如下例項:
[root@k8s-master01 yaml]# kubectl annotate pod/pod-demo-with-cmd-and-args -n dev ilinux.io/created-by="cluster admin" pod/pod-demo-with-cmd-and-args annotated [root@k8s-master01 yaml]# kubectl annotate pod/pod-demo-with-cmd-and-args -n dev ilinux.io/created-by- pod/pod-demo-with-cmd-and-args annotated [root@k8s-master01 yaml]#