我无法卷曲我部署在 k8s 集群上的 nginx

时间:2021-04-07 09:25:04

标签: kubernetes deployment service

我的部署 yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels: 
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

我的服务 yaml:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

enter image description here

enter image description here

然后我curl 10.104.239.140,但得到一个错误curl: (7) Failed connect to 10.104.239.140:80; Connection timed out

谁能告诉我出了什么问题?

1 个答案:

答案 0 :(得分:1)

欢迎来到 SO。您部署的服务属于 ClusterIP 类型,这意味着它只能从集群内部访问。在您的情况下,您似乎正在尝试从集群外部访问它,因此 connection timed out

您可以做的是,部署 NodePortLoadBalancer 类型的服务以从集群外部访问它。您可以详细了解不同的服务类型 here

你的服务最终会是这样的:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort      ## or LoadBalancer(supported by Cloud providers like AWS)    
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    # Optional field
    # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
    nodePort: 30001