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 #容器對外的端口