【问题标题】:Kubernetes - services without selectorKubernetes - 没有选择器的服务
【发布时间】:2020-06-10 13:02:24
【问题描述】:

我正在为没有选择器的 Kubernetes 服务而苦苦挣扎。该集群使用 kops 安装在 AWS 上。我有 3 个 nginx pod 的部署,暴露了 80 端口:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: ngix-dpl                 # Name of the deployment object
 labels:
   app: nginx                     
spec:
 replicas: 3                    # Number of instances in the deployment
 selector:                      # Selector identifies pods to be
     matchLabels:               #     part of the deployment 
        app: nginx              #     by matching of the label "app" 
 template:                      # Templates describes pods of the deployment
   metadata:
     labels:                    # Defines key-value map
       app: nginx               # Label to be recognized by other objects
   spec:                        #     as deployment or service
     containers:                # Lists all containers in the pod
     - name: nginx-pod          # container name
       image: nginx:1.17.4      # container docker image
       ports:
       - containerPort: 80      # port exposed by container

创建部署后,我记下了 IP 地址:

$ kubectl get pods -o wide | awk {'print $1" " $3" " $6'} | column -t
                                                                           NAME                       STATUS   IP
curl                       Running  100.96.6.40
ngix-dpl-7d6b8c8944-8zsgk  Running  100.96.8.53
ngix-dpl-7d6b8c8944-l4gwk  Running  100.96.6.43
ngix-dpl-7d6b8c8944-pffsg  Running  100.96.8.54

并创建了一个应该为 IP 地址提供服务的服务:

apiVersion: v1
kind: Service
metadata:
  name: dummy-svc
  labels:
    app: nginx
spec:
 ports:
    - protocol: TCP
      port: 80
      targetPort: 80
---
apiVersion: v1
kind: Endpoints
metadata:
  name: dummy-svc 
subsets: 
  - addresses:
    - ip: 100.96.8.53
    - ip: 100.96.6.43
    - ip: 100.96.8.54
    ports:
    - port: 80
      name: http

服务创建成功:

$ kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
dummy-svc    ClusterIP   100.64.222.220   <none>        80/TCP    32m
kubernetes   ClusterIP   100.64.0.1       <none>        443/TCP   5d14h

不幸的是,我尝试通过服务从同一命名空间的另一个 pod 连接到 nginx 失败:

$ curl 100.64.222.220
curl: (7) Failed to connect to 100.64.222.220 port 80: Connection refused

我可以直接成功连接到 nginx pod:

$ curl 100.96.8.53
<!DOCTYPE html>
<html>
<head>
....

我注意到我的服务没有任何端点。但我不确定是否应该在此处显示手动端点:

$ kubectl get svc/dummy-svc -o yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |      
       {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"dummy-svc","namespace":"default"},"spec":{"ports":[{"port":80,"protocol":"TCP","targetPort":80}]}}
  creationTimestamp: "2019-11-22T08:41:29Z"
  labels:
    app: nginx
  name: dummy-svc
  namespace: default
  resourceVersion: "4406151"
  selfLink: /api/v1/namespaces/default/services/dummy-svc
  uid: e0aa9d01-0d03-11ea-a19c-0a7942f17bf8
spec:
  clusterIP: 100.64.222.220
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

我了解这不是服务的正确用例,使用 pod 选择器将使其正常工作。但我想了解为什么这种配置不起作用。我不知道在哪里寻找解决方案。任何提示将不胜感激。

【问题讨论】:

    标签: amazon-web-services docker nginx kubernetes kops


    【解决方案1】:

    如果您从端点配置中删除“名称”字段,它会起作用。它应该是这样的:

    apiVersion: v1
    kind: Endpoints
    metadata:
      name: dummy-svc 
    subsets: 
      - addresses:
        - ip: 172.17.0.4
        - ip: 172.17.0.5
        - ip: 172.17.0.6
        ports:
        - port: 80
    

    【讨论】:

    【解决方案2】:

    正如@iliefa 在他上面的评论中提到的,在这种情况下,定义的以下部分被视为标签。

    ports:
        - port: 80
          name: http
    

    在您的场景中,我们需要删除@iliefa 提到的“名称:http”,或者我们需要在服务定义中的“端口:”下添加“名称:http”,如下所示。

    apiVersion: v1
    kind: Service
    metadata:
      name: dummy-svc
      labels:
        app: nginx
    spec:
     ports:
        - protocol: TCP
          port: 80
          targetPort: 80
          name: http
    

    【讨论】:

      猜你喜欢
      • 2018-10-16
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      相关资源
      最近更新 更多