当前位置:   article > 正文

玩转k8s:yaml介绍

玩转k8s:yaml介绍

一.Yaml文件详解

1.Yaml文件格式

(1)Kubernetes 支持 YAML 和 JSON 格式管理资源对象

(2)JSON 格式:主要用于 api 接口之间消息的传递

(3)YAML 格式:用于配置和管理,YAML 是一种简洁的非标记性语言,内容格式人性化,较易读

2.YAML 语法格式

(1)大小写敏感

(2)使用缩进表示层级关系

(3)不支持Tab键制表符缩进,只使用空格缩进

(4)缩进的空格数目不重要,只要相同层级的元素左侧对齐即可,通常开头缩进两个空格

(5)符号字符后缩进一个空格,如冒号,逗号,短横杆(-)等

(6)“---”表示YAML格式,一个文件的开始,用于分隔文件间

(7)“#”表示注释

二.Yaml文件编写及相关概念

1.查看 api 资源版本标签

kubectl api-versions
  1. [root@k8s-master-136 ~]# 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/v1beta1
  11. 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. crd.projectcalico.org/v1
  23. discovery.k8s.io/v1
  24. discovery.k8s.io/v1beta1
  25. events.k8s.io/v1
  26. events.k8s.io/v1beta1
  27. extensions/v1beta1
  28. flowcontrol.apiserver.k8s.io/v1beta1
  29. networking.k8s.io/v1
  30. networking.k8s.io/v1beta1
  31. node.k8s.io/v1
  32. node.k8s.io/v1beta1
  33. policy/v1
  34. policy/v1beta1
  35. rbac.authorization.k8s.io/v1
  36. rbac.authorization.k8s.io/v1beta1
  37. scheduling.k8s.io/v1
  38. scheduling.k8s.io/v1beta1
  39. storage.k8s.io/v1
  40. storage.k8s.io/v1beta1
  41. v1

2.yaml编写案例

  1. #查看deployment的版本定义
  2. kubectl explain deployment
  1. [root@k8s-master-136 ~]# kubectl explain deployment
  2. KIND: Deployment
  3. VERSION: apps/v1
  4. DESCRIPTION:
  5. Deployment enables declarative updates for Pods and ReplicaSets.
  6. FIELDS:
  7. apiVersion <string>
  8. APIVersion defines the versioned schema of this representation of an
  9. object. Servers should convert recognized schemas to the latest internal
  10. value, and may reject unrecognized values. More info:
  11. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  12. kind <string>
  13. Kind is a string value representing the REST resource this object
  14. represents. Servers may infer this from the endpoint the client submits
  15. requests to. Cannot be updated. In CamelCase. More info:
  16. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  17. metadata <Object>
  18. Standard object metadata.
  19. spec <Object>
  20. Specification of the desired behavior of the Deployment.
  21. status <Object>
  22. Most recently observed status of the Deployment.
  1. #查看api的版本
  2. kubectl explain deployment.apiVersion
  3. [root@k8s-master-136 ~]# kubectl explain deployment.apiVersion
  4. KIND: Deployment
  5. VERSION: apps/v1
  6. FIELD: apiVersion <string>
  7. DESCRIPTION:
  8. APIVersion defines the versioned schema of this representation of an
  9. object. Servers should convert recognized schemas to the latest internal
  10. value, and may reject unrecognized values. More info:
  11. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  1. #查看元数据信息
  2. kubectl explain deployment.apiVersion
  1. #定义标签介绍
  2. kubectl explain deployment.spec.selector
  3. [root@k8s-master-136 ~]# kubectl explain deployment.spec.selector
  4. KIND: Deployment
  5. VERSION: apps/v1
  6. RESOURCE: selector <Object>
  7. DESCRIPTION:
  8. Label selector for pods. Existing ReplicaSets whose pods are selected by
  9. this will be the ones affected by this deployment. It must match the pod
  10. template's labels.
  11. A label selector is a label query over a set of resources. The result of
  12. matchLabels and matchExpressions are ANDed. An empty label selector matches
  13. all objects. A null label selector matches no objects.
  14. FIELDS:
  15. matchExpressions <[]Object>
  16. matchExpressions is a list of label selector requirements. The requirements
  17. are ANDed.
  18. matchLabels <map[string]string>
  19. matchLabels is a map of {key,value} pairs. A single {key,value} in the
  20. matchLabels map is equivalent to an element of matchExpressions, whose key
  21. field is "key", the operator is "In", and the values array contains only
  22. "value". The requirements are ANDed.
  23. #对matchLabels标签介绍
  24. kubectl explain deployment.spec.selector.matchLabels
  25. [root@k8s-master-136 ~]# kubectl explain deployment.spec.selector.matchLabels
  26. KIND: Deployment
  27. VERSION: apps/v1
  28. FIELD: matchLabels <map[string]string>
  29. DESCRIPTION:
  30. matchLabels is a map of {key,value} pairs. A single {key,value} in the
  31. matchLabels map is equivalent to an element of matchExpressions, whose key
  32. field is "key", the operator is "In", and the values array contains only
  33. "value". The requirements are ANDed.

Deployment类型编写nginx服务

创建pod

  1. vim nginx-deployment.yaml
  2. apiVersion: apps/v1 #指定api版本标签
  3. kind: Deployment #定义资源的类型/角色,deployment为副本控制器,此处资源类型可以是Deployment、Job、Ingress、Service等
  4. metadata: #定义资源的元数据信息,比如资源的名称、namespace、标签等信息
  5. name: nginx-deployment #定义资源的名称,在同一个namespace空间中必须是唯一的
  6. namespace: default #默认就是default,可以不用写
  7. labels: #定义Deployment资源标签
  8. app: nginx
  9. spec: #定义deployment资源需要的参数属性,诸如是否在容器失败时重新启动容器的属性
  10. replicas: 3 #定义副本数量
  11. selector: #定义标签选择器
  12. matchLabels: #定义匹配标签
  13. app: nginx #需与 .spec.template.metadata.labels 定义的标签保持一致
  14. template: #定义业务模板,如果有多个副本,所有副本的属性会按照模板的相关配置进行匹配
  15. metadata:
  16. labels: #定义Pod副本将使用的标签,需与 .spec.selector.matchLabels 定义的标签保持一致
  17. app: nginx
  18. spec:
  19. containers: #定义容器属性
  20. - name: nginx #定义一个容器名,一个 - name: 定义一个容器
  21. image: nginx:1.15.4 #定义容器使用的镜像以及版本
  22. ports:
  23. - containerPort: 80 #定义容器的对外的端口
  1. #创建资源对象
  2. kubectl create -f nginx-deployment.yaml
  3. kubectl apply -f nginx-deployment.yaml
  4. #查看创建的资源对象,创建需等待running
  5. kubectl get pod

容器如果想对外提供访问,需创建service 发布

  1. #创建service服务对外提供访问并测试
  2. vim nginx-service.yaml
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: nginx-service
  7. labels:
  8. app: nginx
  9. spec:
  10. selector:
  11. app: nginx
  12. type: NodePort
  13. ports:
  14. - port: 80
  15. targetPort: 80
  1. #创建资源对象
  2. kubectl create -f nginx-service.yaml
  3. kubectl apply -f nginx-service.yaml
  4. #查看创建的service
  5. kubectl get svc

k8s集群中的port介绍

详解k8s中的port:

●port

port 是 k8s 集群内部访问service的端口,即通过 clusterIP: port 可以从 Pod 所在的 Node 上访问到 service

●nodePort

nodePort 是外部访问 k8s 集群中 service 的端口,通过 nodeIP: nodePort 可以从外部访问到某个 service。

●targetPort

targetPort 是 Pod 的端口,从 port 或 nodePort 来的流量经过 kube-proxy 反向代理负载均衡转发到后端 Pod 的 targetPort 上,最后进入容器。

●containerPort

containerPort 是 Pod 内部容器的端口,targetPort 映射到 containerPort。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42403
推荐阅读
相关标签
  

闽ICP备14008679号