Can't access to kubernetes services using service name

Viewed 436

I have local kubernetes cluster. I have deployed my application and exposed as a ClusterIP service.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp 
  template:
    metadata:
      labels:
        app: myapp
        version: v1 
    spec:
      containers:
      - name: myapp
        image: myrepo:5000/myapp
        imagePullPolicy: Always
        ports:
        - containerPort: 8088
      imagePullSecrets:
      - name: regcred

My service

apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
  labels:
    app: myapp
spec:
  selector:
    app: myapp 
  ports:
  - protocol: TCP
    port: 8088
    targetPort: 8088
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: myapp
spec:
  host: myapp

kubectl get svc gives :

myapp      ClusterIP      10.233.41.178   <none>     8088/TCP    13m

I can curl to my services using ClusterIP address, but when I try curl http://myapp:8088/api, it gives. (ran from my master node)

curl: (7) Failed to connect to myapp port 8088: Connection refused

I use istio service-mesh. What am I missing here?

I had tried following threads :

unable to access services

can not access to exposed services

2 Answers

This is normal. I can achive the same status with kind and 1 deployment of nginx and one service of type clusterIP.

āžœ  github_api k get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
dev-my-nginx    ClusterIP   10.96.235.85    <none>        80/TCP     3d
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP    3d2h
mysql           ClusterIP   10.97.143.150   <none>        3306/TCP   2d23h
prod-my-nginx   ClusterIP   10.101.89.124   <none>        80/TCP     3d
āžœ  github_api docker exec -it 212f25f5f073  bash
root@kind-control-plane:/# curl 10.96.235.85
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@kind-control-plane:/# curl dev-my-nginx:80
curl: (6) Could not resolve host: dev-my-nginx
root@kind-control-plane:/#

the service name is only resolved when inside the cluster because coreDNS(most likely) only works there. If you want to get to that service from each worker node use service of type NodePort.

PS. I am not 100% sure. Will double-check if there is another option and update this comment.

PS2 You can do something like :

k port-forward svc/dev-my-nginx :80

and you can then curl from the node that is port-forwarding

Have you tried using the namespace parameter?

You can try the get services/deployment for a specific namespace using the -n parameter:

kubectl get svc,deploy -n default

or to get services and deployments for all namespaces:

kubectl get svc,deploy -A

Let us know if this works for you. Also you can check in the Google Console online under https://console.cloud.google.com/kubernetes/discovery

Related