Kubernetes nodeport无法正常工作

Rav*_*nix 3 kubernetes

我在一个pod中创建了一个YAML文件,其中包含三个图像(它们需要通过127.0.0.1进行通信)似乎它一切正常.我在yaml文件中定义了一个nodeport.

定义applications了一个部署,它包含三个映像:

  • contacts-db(一个MySQL数据库)
  • 前端(An Angular网站)
  • net-core(一个API)

我已经定义了三个服务,每个容器一个.在那里我已经定义了NodePort访问它的类型.

所以我检索了服务以获取端口号:

NAME          CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
contacts-db   10.103.67.74     <nodes>       3306:30241/TCP   1d
front-end     10.107.226.176   <nodes>       80:32195/TCP     1d
net-core      10.108.146.87    <nodes>       5000:30245/TCP   1d
Run Code Online (Sandbox Code Playgroud)

我在浏览器中导航到http://:32195,它只是继续加载.它没有连接.这是完整的Yaml文件:

---
apiVersion: v1
kind: Namespace
metadata:
  name: three-tier
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: applications
  labels:
    name: applications
  namespace: three-tier
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: applications
    spec:
      containers:
      - name: contacts-db
        image: mysql/mysql-server #TBD
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: quintor
          - name: MYSQL_DATABASE
            value: quintor #TBD
        ports:
        - name: mysql
          containerPort: 3306
      - name: front-end
        image: xanvier/angularfrontend #TBD
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 80
      - name: net-core
        image: xanvier/contactsapi #TBD
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: contacts-db
  labels:
    name: contacts-db
  namespace: three-tier
spec:
  type: NodePort
  ports:
    # the port that this service should serve on
  - port: 3306
    targetPort: 3306
  selector:
    name: contacts-db
---
apiVersion: v1
kind: Service
metadata:
  name: front-end
  labels:
    name: front-end
  namespace: three-tier
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80 #nodePort: 30001
  selector:
    name: front-end
---
apiVersion: v1
kind: Service
metadata:
  name: net-core
  labels:
    name: net-core
  namespace: three-tier
spec:
  type: NodePort
  ports:
  - port: 5000
    targetPort: 5000 #nodePort: 30001
  selector:
    name: net-core
---
Run Code Online (Sandbox Code Playgroud)

pag*_*gid 5

服务的选择器与您的pod的标签匹配.在您的情况下,定义的选择器指向容器,这些容器在选择pod时无效.

您必须重新定义服务以使用一个选择器或将容器拆分为不同的Deployments/Pods.

要查看为服务定义的选择器是否有效,您可以使用以下命令检查它们:

kubectl get pods -l key=value
Run Code Online (Sandbox Code Playgroud)

如果结果为空,那么您的服务也会遇到空白.

  • 您还可以使用`kubectl get endpoints`查看端点(pod)实际支持哪些服务 (4认同)