第一章、k8s中的资源

1.什么叫资源?

  1. k8s中所有的内容都抽象为资源, 资源实例化之后,叫做对象

2.在k8s中有哪些资源?

  1. 工作负载型资源(workload) Pod ReplicaSet Deployment StatefulSet DaemonSet Job CronJob (ReplicationControllerv1.11版本被废弃)
  2. 服务发现及负载均衡型资源(ServiceDiscovery LoadBalance): Service Ingress, ...
  3. 配置与存储型资源: Volume(存储卷) CSI(容器存储接口,可以扩展各种各样的第三方存储卷)
  4. 特殊类型的存储卷:ConfigMap(当配置中心来使用的资源类型)Secret(保存敏感数据) DownwardAPI(把外部环境中的信息输出给容器)
  5. 以上这些资源都是配置在名称空间级别
  6. 集群级资源Namespace Node Role ClusterRole RoleBinding(角色绑定) ClusterRoleBinding(集群角色绑定)
  7. 元数据型资源HPA(Pod水平扩展) PodTemplate(Pod模板,用于让控制器创建Pod时使用的模板) LimitRange(用来定义硬件资源限制的)

第二章、资源清单

1.什么是资源清单

  1. k8s中,一般使用yaml格式的文件来创建符合我们预期期望的pod,这样的yaml文件我们一般称为资源清单

2.资源清单的格式

  1. apiVersion: group/apiversion # 如果没有给定group名称,那么默认为croe,可以使用kubectl api-versions 获取当前k8s版本上所有的apiVersion版本信息(每个版本可能不同)
  2. kind: #资源类别
  3. metadata #资源元数据
  4. name
  5. namespace #k8s自身的namespace
  6. lables
  7. annotations #主要目的是方便用户阅读查找
  8. spec:期望的状态(disired state
  9. status:当前状态,本字段有kubernetes自身维护,用户不能去定义

#配置清单主要有五个一级字段,其中status用户不能定义,有k8s自身维护

3.获取资源的apiVersion版本及资源配置的帮助

1)获取apiVersion版本信息

  1. [root@k8s-master01 ~]# kubectl api-versions
  2. admissionregistration.k8s.io/v1beta1
  3. apiextensions.k8s.io/v1beta1
  4. apiregistration.k8s.io/v1
  5. apiregistration.k8s.io/v1beta1
  6. apps/v1
  7. apps/v1beta1
  8. apps/v1beta2
  9. authentication.k8s.io/v1
  10. authentication.k8s.io/v1beta1
  11. authorization.k8s.io/v1
  12. authorization.k8s.io/v1beta1
  13. autoscaling/v1
  14. autoscaling/v2beta1
  15. batch/v1
  16. ......(以下省略)

2)获取资源的apiVersion版本信息

  1. [root@k8s-master01 ~]# kubectl explain pod
  2. KIND: Pod
  3. VERSION: v1
  4. .....(以下省略)
  5. [root@k8s-master01 ~]# kubectl explain Ingress
  6. KIND: Ingress
  7. VERSION: extensions/v1beta1

#可以看到出来,不同的资源可能属于不同的apiVersion版本

3)获取资源配置清单中字段设置帮助文档(以pod为例)

获取pod资源的配置清单一级字段

  1. [root@k8s-master01 ~]# kubectl explain pod
  2. KIND: Pod
  3. VERSION: v1
  4.  
  5. DESCRIPTION:
  6. Pod is a collection of containers that can run on a host. This resource is
  7. created by clients and scheduled onto hosts.
  8.  
  9. FIELDS:
  10. apiVersion <string>
  11. APIVersion defines the versioned schema of this representation of an
  12. object. Servers should convert recognized schemas to the latest internal
  13. value, and may reject unrecognized values. More info:
  14. https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
  15.  
  16. kind <string>
  17. Kind is a string value representing the REST resource this object
  18. represents. Servers may infer this from the endpoint the client submits
  19. requests to. Cannot be updated. In CamelCase. More info:
  20. https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
    ........
    ........

获取pod资源的配置清单二级级其他级别的字段

  1. [root@k8s-master01 ~]# kubectl explain pod.metadata #查看一级字段中有哪些二级字段,字段的上下级以 "." 定义
  2. KIND: Pod
  3. VERSION: v1
  4.  
  5. RESOURCE: metadata <Object>
  6.  
  7. DESCRIPTION:
  8. Standard object's metadata. More info:
  9. https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
  10.  
  11. ObjectMeta is metadata that all persisted resources must have, which
  12. includes all objects users must create.
  13. ........
  14.  
  15. -------------
  16.  
  17. [root@k8s-master01 ~]# kubectl explain pod.metadata.labels #查看二级字段中有哪些三级字段
  18. KIND: Pod
  19. VERSION: v1
  20.  
  21. FIELD: labels <map[string]string>
  22.  
  23. DESCRIPTION:
  24. Map of string keys and values that can be used to organize and categorize
  25. (scope and select) objects. May match selectors of replication controllers
  26. and services. More info: http://kubernetes.io/docs/user-guide/labels

字段配置的格式

  1. 帮助信息中常见格式如下:
  2. apiVersion <string> #表示字符串类型
  3. metadata <Object> #表示需要嵌套多层字段
  4. labels <map[string]string> #表示由k:v组成的映射
  5. finalizers <[]string> #表示字串列表
  6. ownerReferences <[]Object> #表示对象列表
    hostPID <boolean> #布尔类型
    priority <integer> #整型
    name <string> -required- #如果类型后面接 -required-,表示为必填字段

第四章、创建一个配置清单实例

1.以pod为例,创建一个简单的yaml文件

  1. [root@k8s-master01 ~]# mkdir manifests
  2. [root@k8s-master01 ~]# cd manifests/
  3. [root@k8s-master01 manifests]# cat pod-demo.yaml
  4. apiVersion: v1
  5. kind: Pod
  6. metadata:
  7. name: pod-demo
  8. labels:
  9. app: myapp #给自己打上标签
  10. tier: frontend
  11. spec:
  12. containers: #创建了两个容器
  13. - name: nginx
  14. image: ikubernetes/myapp:v1
  15. - name: tomcat
  16. image: tomcat:-alpine
  17. [root@k8s-master01 manifests]# kubectl create -f pod-demo.yaml #使用create 子命令以yaml文件的方式启动pod
  18. [root@k8s-master01 manifests]# kubectl get pod #主要查看pod的状态是否支持,因为有一个以上的pod,READY段需要注意pod中的容器是否全部就绪
  19. NAME READY STATUS RESTARTS AGE
  20. ......
  21. pod-demo / Running 2h

为了便于访问,我们再创建一个service便于外部访问测试

  1. [root@k8s-master01 manifests]# cat svc-demo.yaml
  2. apiVersion: v1
  3. kind: Service #主要类型
  4. metadata:
  5. name: test-service
  6. labels:
  7. app1: nginx
  8. app2: tomcat
  9. spec:
  10. ports: #暴露的端口设置
  11. - name: nginx
  12. port: 80 #service的端口
  13. targetPort: 80 #pod上暴露的端口
  14. nodePort: 32080 #Node上暴露的端口,需要注意的是,Node只能暴露30000-32767之间的端口
  15. - name: tomcat
  16. port:
  17. targetPort:
  18. nodePort:
  19. selector:
  20. app: myapp
  21. type: NodePort #service 端口暴露的类型,默认是ClusterIP
  22. [root@k8s-master01 manifests]# kubectl create -f svc-demo.yaml

[root@k8s-master01 manifests]# kubectl get svc -o wide #查看svc的状态
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
.......
test-service  NodePort  10.108.230.27  <none>  80:32080/TCP,8080:32088/TCP  22m  app=myapp   #根据暴露的端口,加上任意集群的IP地址进行访问

2.pod资源清单示例

  1. [root@k8s-master01 ~]# kubectl get pod #查看集群中pod的状态,选取一个之前使用命令行状态下创建的pod
  2. NAME READY STATUS RESTARTS AGE
  3. client / Completed 19h
  4. myapp-6d6f569fd5-rtgt9 / Running 19h
  5. myapp-6d6f569fd5-tjpfn / Running 19h
  6. myapp-6d6f569fd5-tqq5z / Running 19h
  7. nginx / Running 16h
  8. nginx-deploy-7db697dfbd-2qh7v / Running 20h
  9. nginx-deploy-7db697dfbd-gskcv / Running 20h
  10. nginx-deploy-7db697dfbd-ssws8 / Running 20h
  11. [root@k8s-master01 ~]# kubectl get pod nginx-deploy-7db697dfbd-2qh7v -o yaml #使用 -o 参数 加yaml,可以将资源的配置以 yaml的格式输出出来,也可以使用json,输出为json格式
  12. apiVersion: v1
  13. kind: Pod
  14. metadata:
  15. creationTimestamp: --30T05::55Z
  16. generateName: nginx-deploy-7db697dfbd-
  17. labels:
  18. pod-template-hash: ""
  19. run: nginx-deploy
  20. name: nginx-deploy-7db697dfbd-2qh7v
  21. namespace: default
  22. ownerReferences:
  23. - apiVersion: extensions/v1beta1
  24. blockOwnerDeletion: true
  25. controller: true
  26. kind: ReplicaSet
  27. name: nginx-deploy-7db697dfbd
  28. uid: 0eef9e1c-dbf0-11e8--5254001b07db
  29. resourceVersion: ""
  30. selfLink: /api/v1/namespaces/default/pods/nginx-deploy-7db697dfbd-2qh7v
  31. uid: 5ee94f2a-dc06-11e8--5254001b07db
  32. spec:
  33. containers:
  34. - image: nginx:1.14-alpine
  35. imagePullPolicy: IfNotPresent
  36. name: nginx-deploy
  37. ports:
  38. - containerPort:
  39. protocol: TCP
  40. resources: {}
  41. terminationMessagePath: /dev/termination-log
  42. terminationMessagePolicy: File
  43. volumeMounts:
  44. - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
  45. name: default-token-tcwjz
  46. readOnly: true
  47. dnsPolicy: ClusterFirst
  48. nodeName: k8s-node02
  49. restartPolicy: Always
  50. schedulerName: default-scheduler
  51. securityContext: {}
  52. serviceAccount: default
  53. serviceAccountName: default
  54. terminationGracePeriodSeconds:
  55. tolerations:
  56. - effect: NoExecute
  57. key: node.kubernetes.io/not-ready
  58. operator: Exists
  59. tolerationSeconds:
  60. - effect: NoExecute
  61. key: node.kubernetes.io/unreachable
  62. operator: Exists
  63. tolerationSeconds:
  64. volumes:
  65. - name: default-token-tcwjz
  66. secret:
  67. defaultMode:
  68. secretName: default-token-tcwjz
  69. status:
  70. conditions:
  71. - lastProbeTime: null
  72. lastTransitionTime: --30T05::55Z
  73. status: "True"
  74. type: Initialized
  75. - lastProbeTime: null
  76. lastTransitionTime: --30T05::06Z
  77. status: "True"
  78. type: Ready
  79. - lastProbeTime: null
  80. lastTransitionTime: --30T05::55Z
  81. status: "True"
  82. type: PodScheduled
  83. containerStatuses:
  84. - containerID: docker://b75740e5919bd975755b256c83e03b63ea95cf2307ffc606abd03b59fea6634a
  85. image: docker.io/nginx:1.14-alpine
  86. imageID: docker-pullable://docker.io/nginx@sha256:8976218be775f4244df2a60a169d44606b6978bac4375192074cefc0c7824ddf
  87. lastState: {}
  88. name: nginx-deploy
  89. ready: true
  90. restartCount:
  91. state:
  92. running:
  93. startedAt: --30T05::06Z
  94. hostIP: 172.16.150.214
  95. phase: Running
  96. podIP: 10.244.2.7
  97. qosClass: BestEffort
  98. startTime: --30T05::55Z

k8s学习笔记之四:资源清单定义入门的更多相关文章

  1. Kubernetes 学习5 kubernetes资源清单定义入门

    一.kubernetes是有一个restful风格的 API,把各种操作对象都一律当做资源来管理.并且可通过标准的HTTP请求的方法 GET,PUT,DELETE,POST,等方法来完成操作,不过是通 ...

  2. k8s资源清单定义入门

    1.资源分类 a.workload型资源:service.pod.deployment.ReplicaSet.StatefulSet.Job.Cronjob; b.服务发现及服务均衡资源型资源:Ser ...

  3. 四,k8s集群资源清单定义入门

    目录 资源对象 创建资源的方法 清单帮助命令 创建测试清单 资源的三种创建方式 资源对象 workload:Pod, ReplicaSet, Deployment, StatefulSet, Daem ...

  4. Kubenetes 资源清单定义入门

    Kubernetes 常用资源 资源  对象 工作负载型资源对象(workload): Pod  Replicaset  ReplicationController  Deployments Stat ...

  5. Oracle学习笔记之四,SQL语言入门

    1. SQL语言概述 1.1 SQL语言特点 集合性,SQL可以的高层的数据结构上进行工作,工作时不是单条地处理记录,而对数据进行成组的处理. 统一性,操作任务主要包括:查询数据:插入.修改和删除数据 ...

  6. 04-kubernetes 资源清单定义入门

    目录 资源对象 创建资源的方法 清单帮助命令 创建测试清单 资源的三种创建方式 资源对象 workload:Pod, ReplicaSet, Deployment, StatefulSet, Daem ...

  7. kubernetes 资源清单定义入门

    k8s中的资源 什么叫资源? k8s中所有的内容都抽象为资源, 资源实例化之后,叫做对象 在k8s中有哪些资源? 工作负载型资源(workload): Pod ReplicaSet Deploymen ...

  8. 5、kubernetes资源清单定义入门

    使用配置清单创建资源 定义pod时使用yaml格式 master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE client / Error 1 ...

  9. kubernetes系列06—kubernetes资源清单定义入门

    本文收录在容器技术学习系列文章总目录 1.认识kubernetes资源 1.1 常用资源/对象 workload工作负载型资源:pod,ReplicaSet,Deployment,StatefulSe ...

随机推荐

  1. Python字符编码的发展、cmd寻找路径

    字符编码的发展: ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示 ...

  2. c函数 文件名通配符

    static bool IsMatched(CONST TCHAR* p, CONST TCHAR* q) { CONST TCHAR *r, *z, *x = _T(""); f ...

  3. encodeURIComponent编码与解码

    问题:JavaScript用encodeURIComponentt编码后无法再后台解码的问题. 目前写法: window.self.location="list.jsp?searchtext ...

  4. 多任务Forth系统内存布局

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  5. ubuntu14.04安装 Apache2 并配置https

    一.安装 Apache2 sudo apt-get update sudo apt-get install apache2 安装完apache2,默认根目录在/var/www/html 下,点击其下的 ...

  6. OSI七层模型与TCP/IP五层模型(转)

    reference:https://www.cnblogs.com/qishui/p/5428938.html         博主是搞是个FPGA的,一直没有真正的研究过以太网相关的技术,现在终于能 ...

  7. MySQL:函数

    函数 一.数学函数 1.绝对值函数ABS(x): x为插入的数据,返回绝对值 2.返回圆周率函数PI(): 无需插入数据,返回圆周率的值,默认为小数点后6位 3.平方根函数SQRT(x): 返回非负数 ...

  8. 最短路,floyd算法,图的最短路径

    题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线 ...

  9. 通过pid杀死进程

    bool ****::KillProcess(DWORD pid) { // When the all operation fail this function terminate the " ...

  10. ios 中pickerView用法之国旗选择

    QRViewController控制器 // // QRViewController.m // #import "QRViewController.h" #import " ...