我无法将入口与服务连接

时间:2018-10-25 20:38:30

标签: kubernetes google-cloud-platform

我的入口和服务有问题,当我连接到服务器的IP时,我无法重定向到与端口80相关联的服务,这是我的网站。我向您传递了配置文件和入口的描述:

apiVersion: v1
kind: Namespace
metadata:
  name: bookstack
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    service: mysql
  name: mysql
  namespace: bookstack
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        service: mysql
    spec:
      containers:
      - env:
        - name: MYSQL_DATABASE
          value: bookstack
        - name: MYSQL_PASS
          value: pass
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: MYSQL_USER
          value: user
        image: mysql:5.7
        name: mysql
        ports:
        - containerPort: 3306
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  labels:
    service: mysql
  name: mysql
  namespace: bookstack
spec:
  type: NodePort
  ports:
  - name: "3306"
    port: 3306
    targetPort: 3306
  selector:
    service: mysql
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: bookstack
  name: bookstack
  namespace: bookstack
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: bookstack
    spec:
      containers:
      - env:
        - name: namespace
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: podname
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: nodename
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: DB_DATABASE
          value: bookstack
        - name: DB_HOST
          value: mysql
        - name: DB_PASSWORD
          value: root
        - name: DB_USERNAME
          value: root
        image: solidnerd/bookstack:latest
        name: bookstack
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: bookstack
  name: bookstack
  namespace: bookstack
spec:
  type: NodePort
  ports:
  - name: http-port
    port: 80
    protocol: TCP
  selector:
    app: bookstack
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: http
  namespace: bookstack
spec:
  backend:
    serviceName: bookstack
    servicePort: http-port

这是我的入口上显示的内容:

Name:             http
Namespace:        bookstack
Address:
Default backend:  bookstack:http-port (10.36.0.22:80)
Rules:
  Host  Path  Backends
  ----  ----  --------
  *     *     bookstack:http-port (10.36.0.22:80)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"http","namespace":"bookstack"},"spec":{"backend":{"serviceName":"bookstack","servicePort":"http-port"}}}

Events:  <none>

它不返回任何外部IP来连接我,为什么会这样?我想避免将LoadBalancer用作服务类型。

2 个答案:

答案 0 :(得分:1)

将没有外部IP,因为NodePort代表群集上该特定端口上的所有节点。因此,从本质上讲,您必须将外部负载平衡器或该流量源指向该特定NodePort上群集中的每个节点。

请注意,如果您使用的是ExternalTrafficPolicy=Local,则只有具有服务吊舱的节点才会答复。

答案 1 :(得分:1)

主要问题是我没有激活Google Kubernetes Engine默认提供的均衡器,没有激活它,因为没有均衡器,所以我无法生成外部ip。有两种解决方案,要么激活GKE的默认负载均衡器,要么创建一种服务类型:LoadBalancer。

在准备就绪状态下也要激活激活探针 和livenessProbe。

一个例子:

    readinessProbe:
      httpGet:
        path: /login
        port: 80
      initialDelaySeconds: 5
      timeoutSeconds: 1
      periodSeconds: 15
    livenessProbe:
      httpGet:
        path: /login
        port: 80
      initialDelaySeconds: 15
      timeoutSeconds: 1
      periodSeconds: 15