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
/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.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.
In summary, Kubernetes has higher priority.