Sri*_*vas 6 service selector kubernetes
我希望启动特定数量的独立且不负载平衡的 Pod。(目的是使用它们向某些外部端点发送和接收某些流量。)我计划执行此操作的方法是显式创建 Pod(yaml 片段如下)
apiVersion: v1
kind: Pod
metadata:
name: generator-agent-pod-1
labels:
app: generator-agent
version: v1
spec:
containers:
...
Run Code Online (Sandbox Code Playgroud)
(在此,名称将自动生成为generator-agent-pod-1, generator-agent-pod-2等)
然后,我希望为每个 Pod 创建一个服务:所以本质上,会有一个generator-agent-service-1, generator-agent-service-2等等,这样我就可以使用该服务从外部访问 Pod。
我现在有两个问题: 1. 在服务中,如何通过名称(而不是标签)选择特定的 Pod?相当于:
apiVersion: v1
kind: Service
metadata:
name: generator-agent-service-1
labels:
app: agent-service
spec:
type: NodePort
ports:
- port: 8085
protocol: TCP
selector:
metadata.name: generator-agent-pod-1
Run Code Online (Sandbox Code Playgroud)
(我认为该服务没有获得任何端点,因此选择器不正确。)
谢谢!
小智 8
我认为您正在使用 StatefulSet 来控制 Pod。如果是这样,您可以使用 labelstatefulset.kubernetes.io/pod-name来选择服务中的 pod。
举例说明:
apiVersion: v1
kind: Service
metadata:
name: generator-agent-service-1
labels:
app: agent-service
spec:
type: NodePort
ports:
- port: 8085
protocol: TCP
selector:
statefulset.kubernetes.io/pod-name: generator-agent-pod-1
Run Code Online (Sandbox Code Playgroud)
您还可以选择定义不带 pod 选择器的服务。\n然后通过手动添加 Endpoint 对象,将服务手动映射到其运行的网络地址和端口\xe2\x80\x99。
\n\n供您参考的示例:
\n\n创建了两个 nginx 类型的 pod
\n\n$ kubectl get all -o wide\nNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES\npod/nginx-one 1/1 Running 0 4m56s 192.168.58.199 k8s-node02 <none> <none>\npod/nginx-two 1/1 Running 0 4m50s 192.168.85.193 k8s-node01 <none> <none>\n\nNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR\nservice/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29m <none>\nRun Code Online (Sandbox Code Playgroud)\n\n使用下面的 yaml 创建两个服务,注意下面的 yaml 上没有使用 Pod 选择器字段
\n\nservice1.yaml
\n\napiVersion: v1\nkind: Service\nmetadata:\n name: nginx-one-service\nspec:\n ports:\n - protocol: TCP\n port: 80\nRun Code Online (Sandbox Code Playgroud)\n\nservice2.yaml
\n\napiVersion: v1\nkind: Service\nmetadata:\n name: nginx-two-service\nspec:\n ports:\n - protocol: TCP\n port: 80\n\n\n$ kubectl get svc\nNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE\nkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 32m\nnginx-one-service ClusterIP 10.102.230.78 <none> 80/TCP 7m16s\nnginx-two-service ClusterIP 10.98.86.67 <none> 80/TCP 6m56s\nRun Code Online (Sandbox Code Playgroud)\n\n描述服务,由于我们没有提供选择器,因此没有映射端点。
\n\nubuntu@k8s-master:~$ kubectl describe service nginx-one-service\nName: nginx-one-service\nNamespace: default\nLabels: <none>\nAnnotations: <none>\nSelector: <none>\nType: ClusterIP\nIP: 10.102.230.78\nPort: <unset> 80/TCP\nTargetPort: 80/TCP\nEndpoints: <none>\nSession Affinity: None\nEvents: <none>\n\nubuntu@k8s-master:~$ kubectl describe service nginx-two-service\nName: nginx-two-service\nNamespace: default\nLabels: <none>\nAnnotations: <none>\nSelector: <none>\nType: ClusterIP\nIP: 10.98.86.67\nPort: <unset> 80/TCP\nTargetPort: 80/TCP\nEndpoints: <none>\nSession Affinity: None\nEvents: <none>\nRun Code Online (Sandbox Code Playgroud)\n\n现在您可以选择使用以下 yaml 手动映射终点。
\n\n端点1.yaml
\n\napiVersion: v1\nkind: Endpoints\nmetadata:\n name: nginx-one-service\nsubsets:\n - addresses:\n - ip: 192.168.85.193\n ports:\n - port: 80\nRun Code Online (Sandbox Code Playgroud)\n\n端点2.yaml
\n\napiVersion: v1\nkind: Endpoints\nmetadata:\n name: nginx-two-service\nsubsets:\n - addresses:\n - ip: 192.168.85.193\n ports:\n - port: 80\nRun Code Online (Sandbox Code Playgroud)\n\n现在获取创建端点
\n\n$ kubectl get endpoints\nNAME ENDPOINTS AGE\nkubernetes 131.160.188.46:6443 35m\nnginx-one-service 192.168.58.199:80 5m30s\nnginx-two-service 192.168.85.193:80 4m59s\nRun Code Online (Sandbox Code Playgroud)\n\n并列出应映射的服务和端点,如下所示
\n\nubuntu@k8s-master:~$ kubectl describe service nginx-one-service\nName: nginx-one-service\nNamespace: default\nLabels: <none>\nAnnotations: <none>\nSelector: <none>\nType: ClusterIP\nIP: 10.102.230.78\nPort: <unset> 80/TCP\nTargetPort: 80/TCP\nEndpoints: 192.168.58.199:80\nSession Affinity: None\nEvents: <none>\n\nubuntu@k8s-master:~$ kubectl describe service nginx-two-service\nName: nginx-two-service\nNamespace: default\nLabels: <none>\nAnnotations: <none>\nSelector: <none>\nType: ClusterIP\nIP: 10.98.86.67\nPort: <unset> 80/TCP\nTargetPort: 80/TCP\nEndpoints: 192.168.85.193:80\nSession Affinity: None\nEvents: <none>\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
11480 次 |
| 最近记录: |