Home > other >  minimal connectivity test for a pod exposed on service not working in minikube
minimal connectivity test for a pod exposed on service not working in minikube

Time:01-28

I'm using minikube version: v1.25.1, win10, k8s version 1.22

There is 1 node, 2 pods on it : main, front, 1 service - svc-main.

I'm trying to exec into front and call main thru service and see some msg confirming connection is ok.

main.yaml:

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: main
  name: main
  namespace: default
spec:
  containers:
    - name: main
      image: nginx
      command: ["/bin/sh","-c"]
      args: ["while true; do echo date; sleep 2; done"]

front.yaml:

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: front
  name: front
spec:
  containers:
  - image: nginx
    name: front
    command:
    - /bin/sh
    - -c
    - while true; echo date; sleep 2; done

service is created like this:

k expose pod ngin --name=svc-main --type=ClusterIP --port=80 --target-port=80

k get svc
NAME         TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc-main     ClusterIP 10.104.26.249   <none>        80:31775/TCP   11m

when i try to curl from inside front it says "Could not resolve host: svc-main"

 k exec front -it -- sh
 curl svc-main:80

or this

curl http://svc-main:80

curl 10.104.26.249:80

i tried the port 31775, same result, what am i doing wrong?!

CodePudding user response:

The problem is when you are creating a Kubernetes pod using your yaml file, you are overwriting the default Entrypoint and Cmd defined in nginx docker image with your custom command and args:

command: ["/bin/sh","-c"]
args: ["while true; do echo date; sleep 2; done"]

That's why nginx web server doesn´t work in created pods. You should remove these lines, delete running pods, and create new pods. After that, you will be able to reach the nginx web page by running

# curl svc-main

within your front pod.

You can read more info about defining a command and arguments for a container in a Pod here

And there is a good article about Docker CMD and Entrypoint here

  •  Tags:  
  • Related