四、kubernetes資源清單快速入門190625

1、資源清單概念

  • 資源/對象的類型
    • 工做負載型資源:Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet, Job, Cronjob, ...
    • 服務發現及均衡性資源:Service, Ingress, ...
    • 配置與存儲型資源:Volume, CSI, ConfigMap, DownwardAPI
    • 集羣級資源:Namespace, Node, Role, ClusterRole, RoleBinding, ClusterRoleBinding
    • 元數據型資源:HPA, PodTemplate, LimitRange

2、配置清單入門

一、配置清單簡介

  • 輸出配置清單
~]# kubectl get pod nginx-deploy-bc9ff65dd-m8k46 -o yaml
apiVersion: v1  #對象屬於哪一個組,此對象屬於核心組,core/v1
kind: Pod  #具體資源對象
metadata:  #元數據
  creationTimestamp: "2019-06-24T13:33:09Z"
  generateName: nginx-deploy-bc9ff65dd-
  labels:
    pod-template-hash: bc9ff65dd
    run: nginx-deploy
  name: nginx-deploy-bc9ff65dd-m8k46
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-deploy-bc9ff65dd
    uid: 0e2bf48a-822f-4a05-8779-fa97998f0eb3
  resourceVersion: "86123"
  selfLink: /api/v1/namespaces/default/pods/nginx-deploy-bc9ff65dd-m8k46
  uid: 817f0411-67c6-43db-aef0-54ac0465bc94
spec:  #規格,定義資源對象的特性或規範,也叫指望狀態
  containers:
  - image: nginx:1.14
    imagePullPolicy: IfNotPresent
    name: nginx-deploy
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-4q4c9
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: node01
  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-4q4c9
    secret:
      defaultMode: 420
      secretName: default-token-4q4c9
status:  #顯示資源當前狀態,當前狀態無限向指望狀態靠近
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-06-24T13:33:09Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-06-24T13:40:27Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-06-24T13:40:27Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-06-24T13:33:09Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://fa614bd334c9985121436b1ef3bf3c2cab6ca77e8e2a8171ad37172872f6147b
    image: nginx:1.14
    imageID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    lastState: {}
    name: nginx-deploy
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-06-24T13:40:26Z"
  hostIP: 192.168.100.61
  phase: Running
  podIP: 10.244.1.2
  qosClass: BestEffort
  startTime: "2019-06-24T13:33:09Z"
  • 建立資源的方法
    • apiserver僅接受json格式的資源定義
    • yaml格式提供配置清單,apiserver可自動將其轉爲json格式再提交
  • 資源引用方式
/api/GROUP/VERSION/namespaces/NAMESPACE/TYPE/NAME
  • 資源定義幫助獲取
~]# kubectl explain pods

二、配置清單的經常使用字段定義

  • apiVersion:group/version
~]# kubectl api-versions  #顯示全部支持的api版本
  • kind:資源類別,Pod, Service, ...
  • metadata:元數據
name:資源名稱
namespace:kubernetes級別的名稱空間
labels:標籤
annotaions:註解
  • spec:指望狀態,disired state
  • status:當前狀態,current state,此字段有kubernetes集羣自動維護,無需定義

3、Pod資源配置清單定義

  1. 建立一個自主式Pod
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend  #層次:前端
spec:
  containers:
  - name: myapp
    image: dongfeimg/myapp:v1
  1. 根據配置清單啓動/刪除pod
# kubectl create -f pod-demo.yaml
# kubectl describe pods pod-demo  #查看詳細信息
# kubectl delete -f pod-demo.yaml
相關文章
相關標籤/搜索