English 中文(简体)
Kubernetes service file not working (Connection Refused) while CLI deployment exposing works
原标题:

I have created a local Minikube cluster, then a deployment for a hello-world example. After it is active, a service is created at the same time.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  selector:
    matchLabels:
      run: hello-world-example
  replicas: 2
  template:
    metadata:
      labels:
        run: hello-world-example
    spec:
      containers:
        - name: hello-world
          image: gcr.io/google-samples/hello-app:2.0 # Works
          ports:
            - containerPort: 8080
              protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: hello-app-service
spec:
  # type: NodePort
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: hello-world
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
    # nodePort: 31579

I apply the deployment and service with kubectl apply-f hello-app.yaml I end up with the following example services:

NAMESPACE NAME TARGET PORT URL
default hello-app-service 8080 http://IP:31813
default kubernetes No node port
kube-system kube-dns No node port

Note: My actual IP is not actually "IP"

When I curl the URL for the hello-app-service, I end up with this:

curl: (7) Failed to connect to IP port 31813 after 0 ms: Connection refused

However, when I expose the deployment service manually in CLI with kubectl expose deployment hello-world --type=LoadBalancer --port=8080

I get the following result:

NAMESPACE NAME TARGET PORT URL
default hello-app-service 8080 http://IP:31813
default hello-world 8080 http://IP:32168
default kubernetes No node port
kube-system kube-dns No node port

And after I curl the URL for new service "hello-world", I end with the proper result:

Hello, world! Version: 2.0.0 Hostname: hello-world-5bb7fff796-fmwl8

Can somebody please explain to me what I am doing wrong with the service? Why is the CLI service working, while the .yaml file service is not working, despite using the same configuration. I have tested both with the same exact service settings as the CLI command (Using LoadBalancer type) and also with NodePort and setting the specific port.

Versions: OS: Ubuntu 22.04.2 LTS Docker version: 24.0.2 Kubectl version: Client Version: v1.27.2 Kustomize Version: v5.0.1 Server Version: v1.26.3 Minikube version: v1.30.1

问题回答

I think the issue is the selector you re using for your service. This created the deployment and service in one-step for me:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  selector:
    matchLabels:
      run: hello-world-example
  replicas: 2
  template:
    metadata:
      labels:
        run: hello-world-example
    spec:
      containers:
        - name: hello-world
          image: gcr.io/google-samples/hello-app:2.0 # Works
          ports:
            - containerPort: 8080
              protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: hello-app-service
spec:
  # type: NodePort
  type: LoadBalancer
  selector:
    run: hello-world-example
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080

If this doesn t work, at the expose step (the one that worked) use a dry-run to output the svc into a yaml file without actually creating the svc. Helps me troubleshoot

kubectl expose deployment hello-world --type=LoadBalancer --port=8080 --dry-run=client -o yaml > hello-app-service.yaml




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签