4、kubernetes资源清单快速入门190625

一、资源清单概念

  • 资源/对象的类型
    • 工作负载型资源:Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet, Job, Cronjob, ...
    • 服务发现及均衡性资源:Service, Ingress, ...
    • 配置与存储型资源:Volume, CSI, ConfigMap, DownwardAPI
    • 集群级资源:Namespace, Node, Role, ClusterRole, RoleBinding, ClusterRoleBinding
    • 元数据型资源:HPA, PodTemplate, LimitRange

二、配置清单入门

1、配置清单简介

  • 输出配置清单
~]# 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

2、配置清单的常用字段定义

  • apiVersion:group/version
~]# kubectl api-versions  #显示所有支持的api版本
  • kind:资源类别,Pod, Service, ...
  • metadata:元数据
name:资源名称
namespace:kubernetes级别的名称空间
labels:标签
annotaions:注解
  • spec:期望状态,disired state
  • status:当前状态,current state,此字段有kubernetes集群自动维护,无需定义

三、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
posted @ 2019-06-25 23:28  生生不息.连绵不绝  阅读(345)  评论(0编辑  收藏  举报