k8s 原理图
port详解
port:port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service
nodePort:nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service。
targetPort:targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器。
containerPort:containerPort是pod内部容器的端口,targetPort映射到containerPort。
apiVersion: apps/v1 # 1.9.0 之前的版本使用 apps/v1beta2,可通过命令 kubectl api-versions 查看 kind: Deployment #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: nginx-deployment #资源的名字,在同一个namespace中必须唯一 spec: replicas: 2 #副本数量2 selector: #定义标签选择器 matchLabels: app: web-server template: #这里Pod的定义 metadata: labels: #Pod的label app: web-server spec: # 指定该资源的内容 containers: - name: nginx #容器的名字 image: nginx:1.12.1 #容器的镜像地址 ports: - containerPort: 80 #容器对外的端口
apiVersion: v1 kind: Service # 指明资源类型是 service metadata: name: httpd-svc # service 的名字是 httpd-svc labels: name: httpd-svc spec: ports: # 将 service 8080 端口映射到 pod 的 80 端口,使用 TCP 协议 - port: 8080 targetPort: 80 protocol: TCP selector: run: httpd # 指明哪些 label 的 pod 作为 service 的后端
Label
Label是Kubernetes系列中另外一个核心概念。是一组绑定到K8s资源对象上的key/value对。
同一个对象的labels属性的key必须唯一。label可以附加到各种资源对象上,如Node,Pod,Service,RC等。
通过给指定的资源对象捆绑一个或多个不用的label来实现多维度的资源分组管理功能,以便于灵活,方便地进行资源分配,调度,配置,部署等管理工作。
示例如下:
版本标签:"release" : "stable" , "release" : "canary"...
环境标签:"environment" : "dev" , "environment" : "production"
架构标签:"tier" : "frontend" , "tier" : "backend" , "tier" : "middleware"
分区标签:"partition" : "customerA" , "partition" : "customerB"...
质量管控标签:"track" : "daily" , "track" : "weekly"
Selector
Label selector是Kubernetes核心的分组机制,通过label selector客户端/用户能够识别一组有共同特征或属性的资源对象。
符合这个标签的 Pod 会作为这个 Service 的 backend。
apiVersion: apps/v1 # 1.9.0 之前的版本使用 apps/v1beta2,可通过命令 kubectl api-versions 查看 kind: Deployment #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: nginx-deployment #资源的名字,在同一个namespace中必须唯一
#----------------------------------------- spec: replicas: 2 #副本数量2 selector: #定义标签选择器 matchLabels: app: web-server
#----------------------------------------- template: #这里Pod的定义 metadata: labels: #Pod的label app: web-server
#----------------------------------------- spec: # 指定该资源的内容 containers: - name: nginx #容器的名字 image: nginx:1.12.1 #容器的镜像地址 ports: - containerPort: 80 #容器对外的端口