简体   繁体   中英

Kubernetes cannot acess service

Failed to connect to localhost port 80: Connection refused

I recently switched from Windows 10 to Ubuntu 20.04.2 LTS and now I wanted to keep on learning kubernetes. However currently I cannot acess my kubernetes deployment for some reason.

Deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment-c51e9e6b
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
        - image: paulbouwer/hello-kubernetes:1.7
          name: hello-kubernetes
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-service-9878228b
spec:
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: hello-kubernetes
  type: LoadBalancer

I'm pretty sure that this deployment has worked before so I assume that kubernetes has maybe not permissions to expose the port or something like that?

Additional information

why do you think you can access it via localhost directly?

If you need, please do the port forward , such as:

kubectl port-forward service/hello-service-9878228b 80:80

and you need keeping the console on.

then you should be fine to access it via http://localhost

To expose privileged ports ( < 1024 ), you can give the container the CAP_NET_BIND_SERVICE - see here .

See an example for setting capabilities under K8S Pod security context in here :

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-4
spec:
  containers:
  - name: sec-ctx-4
    image: gcr.io/google-samples/node-hello:1.0
    securityContext:
      capabilities:
        add: ["NET_ADMIN", "SYS_TIME"]

You are using a service configuration that is align with a cloud configurations. If no external IPs are provided Kubernetes will try to provision an external TCP LoadBalancer which is available in Cloud Providers. For your configuration provide a external IP that you want to expose the service. This could be IP of a node in the cluster.

https://kubernetes.io/docs/concepts/services-networking/service/

type: LoadBalancer
externalIPs: 
- xxx.xxx.xxx

尝试创建 ClusterIp 服务类型而不是负载均衡器类型,这将使​​您能够从您的 PC 本地访问它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM