skip to Main Content

I am using nginx image to create pods as follows –

$ kubectl run nginx --image=nginx --port=80 -- /bin/sh -c 'sleep 20000'
$ kubectl create deployment nginx-deploy --image=nginx --port=80 --replicas=1 

These results two pods as follows –

$ kubectl get pods
nginx                           1/1     Running   0          24s
nginx-deploy-7496796997-wkhv8   1/1     Running   0          19s

the curl connects to localhost in the "nginx-deploy" pod whereas in other pod it does not.

$ kubectl  exec -it nginx -- /bin/sh -c 'curl localhost'
curl: (7) Failed to connect to localhost port 80: Connection refused
$ k exec -it nginx-deploy-7496796997-wkhv8 -- /bin/sh -c 'curl localhost'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
....

Any reason nginx image is behaving differently in these two pods?

2

Answers


  1. /bin/sh -c 'sleep 20000'

    Your command has overridden the default CMD/ENTRYPOINT defined in nginx image. As a result, the nginx server will not start. If you kubectl run again without your command, the nginx will run like the deployment.

    Login or Signup to reply.
  2. # Here command = /bin/sh -c 'sleep 20000'
    kubectl run nginx --image=nginx --port=80 -- command
    

    When you create a pod using the command, it will overwrite command in Dockerfile. So that nginx server will not be started in the pod. Please refer to the Dockerfile of nginx.

    1. Have not defined COMMAND and ARGS in Kubernetes, use Dockerfile’s config.
    2. Have defined COMMAND (no effect if ARGS defined or not) in Kubernetes, use Kubernetes COMMAND.
    3. Have not defined COMMAND but ARGS in Kubernetes, use ENTRYPOINT in Dockerfile and ARGS in Kubernetes.

    enter image description here

    In summary, Kubernetes has higher priority.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search