0

我在云中创建了两台机器。一个给领导者,一个给追随者。

我已经为两台机器安装了 docker 和 kubeadm 以在 Kubernetes 中部署应用程序,并添加了 ingress-nginx 控制器。

该应用程序使用节点端口类型成功部署,并且能够使用 IP 和端口号公开。

和 DNS

testnginx.com

是为该 IP 创建的,因此我可以公开该应用程序:

testnginx.com:3001

部署和服务的配置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tool-deployment
  namespace: bot
  labels:
    app: tool
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tool
  template:
    metadata:
      labels:
        app: tool
    spec:
      hostNetwork: true
      restartPolicy: Always
      containers:
      - name: tool
        image: myimage:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3001

---
apiVersion: v1
kind: Service
metadata:
  name: tool-service
  namespace: bot
spec:
  type: NodePort
  selector:
    app: tool
  ports:
    - protocol: TCP
      port: 3001
      targetPort: 3001
      nodePort: 32001

接下来,我尝试将入口部署到服务,这样我就可以在没有端口的情况下公开应用程序。

下面给出了入口的 YAML 配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tool-ingress
  namespace: bot
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: testnginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: tool-service
            port:
              number: 32001

但是入口不起作用:

This page isn’t working
testnginx.com didn’t send any data.
ERR_EMPTY_RESPONSE

有没有需要添加的配置?如何实现入口方法?

4

1 回答 1

0

首先,如果您想使用 ingress 公开您的应用程序,那么您应该使用 ClusterIP 服务。那么你的清单文件应该是这样的。

apiVersion: v1
kind: Service
metadata:
  name: tool-service
  namespace: bot
spec:
  type: ClusterIP
  selector:
    app: tool
  ports:
    - protocol: TCP
      port: 3001
      targetPort: 3001

您还必须更改入口清单文件。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tool-ingress
  namespace: bot
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: testnginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: tool-service
            port:
              number: 3001
于 2021-03-03T16:04:58.623 回答