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

I can't curl nginx which I deployed on k8s cluster

我的部署 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

谁能告诉我这是怎么回事?

欢迎来到 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