nginx image behaves differently in kubectl deployment and pods - nginx

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?

# 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.
Have not defined COMMAND and ARGS in Kubernetes, use Dockerfile's config.
Have defined COMMAND (no effect if ARGS defined or not) in Kubernetes, use Kubernetes COMMAND.
Have not defined COMMAND but ARGS in Kubernetes, use ENTRYPOINT in Dockerfile and ARGS in Kubernetes.
In summary, Kubernetes has higher priority.

/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.

Related

Deploy docker image to k8s cluster issue

I am trying to deploy docker image to kubernetes but hitting a strange issue. Below is the command i am using in jenkinsfile
stage('Deploy to k8s') {
steps{
sshagent(['kops-machine']) {
sh "scp -o StrictHostKeyChecking=no deployment.yml ubuntu#<ip>:/home/ubuntu/"
sh "ssh ubuntu#<ip> kubectl apply -f ."
sh 'kubectl set image deployment/nginx-deployment nginx=account/repo:${BUILD_NUMBER}'
}
}
I am getting this error message
kubectl set image deployment/nginx-deployment nginx=account/repo:69
error: the server doesn't have a resource type "deployment"
Strange thing is if i copy and paste this command and execute on the cluster, the image gets updated
kubectl set image deployment/nginx-deployment nginx=account/repo:69
Can somebody please help, image builds and pushes to docker hub successfully, its just that i am stuck with pulling and deploying to kubernetes cluster, if you have anyother alternatives please let me know, the deployment.yml file which gets copied to the server is as follows
spec:
containers:
- name: nginx
image: account/repo:3
ports:
- containerPort: 80
Ok so if found the work around. if i change this line in my docker file
sh "ssh ubuntu#<ip> kubectl apply -f ." to
sh "ssh ubuntu#<ip> kubectl set image deployment/nginx-deployment
nginx=account/repo:${BUILD_NUMBER}"
It works, but if there is no deployment created at all, then i have to add these two line to make it work
sh "ssh ubuntu#<ip> kubectl apply -f ."
sh "ssh ubuntu#<ip> kubectl set image deployment/nginx-deployment
nginx=account/repo:${BUILD_NUMBER}"

podman ports connection refused after stopping and starting/restarting pod

I'm new to using podman and am trying to follow along with Richard Walker's tutorial for containerizing a django app (https://www.richardwalker.dev/django-podman.html)
It works fine until I try to stop and restart the pod. Then my host machine can no longer connect to ports (which were exposed when building the images and mapped when the pod was created).
The docs & guides suggest that it is a simple as issuing
podman pod stop ...
podman pod start ...
but this does not seem to work.
Would appreciate your help if you can see that I am missing something.
$ podman pod create -p 8000 -p 7000 -p 5432 -n cardpod
8553ad8fc0b14a849598a51c4ffcbffa9d6d094b96b542f0e432fc0d6dfd22ff
$ podman run --name deckofcards-prod-ctr --pod cardpod -d richardwalker.dev/deckofcards-prod-img
3dbf6f9ad043fe65492f0e15be642af92916ad9e09d941e1f96315343a8d2fae
$ curl http://127.0.0.1:7000/deck/
[{"suit":"clubs","face":"queen","value":10},{"suit":"spades","face":"four","value":4},{"suit":"hearts","face":"king","value":10},{"suit":"diamonds","face":"six","value":6},{"suit":"hearts","face":"two","value":2},{"suit":"diamonds","face":"ace","value":1},{"suit":"hearts","face":"eight","value":8},{"suit":"clubs","face":"three","value":3},{"suit":"spades","face":"five","value":5},{"suit":"clubs","face":"nine","value":9},{"suit":"spades","face":"nine","value":9},{"suit":"diamonds","face":"five","value":5},{"suit":"hearts","face":"nine","value":9},{"suit":"diamonds","face":"two","value":2},{"suit":"clubs","face":"king","value":10},{"suit":"diamonds","face":"eight","value":8},{"suit":"clubs","face":"ace","value":1},{"suit":"hearts","face":"three","value":3},{"suit":"spades","face":"jack","value":10},{"suit":"hearts","face":"ten","value":10},{"suit":"spades","face":"king","value":10},{"suit":"spades","face":"ace","value":1},{"suit":"spades","face":"ten","value":10},{"suit":"hearts","face":"five","value":5},{"suit":"hearts","face":"ace","value":1},{"suit":"clubs","face":"eight","value":8},{"suit":"hearts","face":"jack","value":10},{"suit":"diamonds","face":"queen","value":10},{"suit":"clubs","face":"ten","value":10},{"suit":"diamonds","face":"nine","value":9},{"suit":"clubs","face":"five","value":5},{"suit":"clubs","face":"jack","value":10},{"suit":"diamonds","face":"ten","value":10},{"suit":"hearts","face":"queen","value":10},{"suit":"diamonds","face":"seven","value":7},{"suit":"hearts","face":"seven","value":7},{"suit":"hearts","face":"six","value":6},{"suit":"spades","face":"two","value":2},{"suit":"clubs","face":"two","value":2},{"suit":"clubs","face":"seven","value":7},{"suit":"spades","face":"seven","value":7},{"suit":"clubs","face":"four","value":4},{"suit":"spades","face":"queen","value":10},{"suit":"diamonds","face":"king","value":10},{"suit":"spades","face":"six","value":6},{"suit":"diamonds","face":"jack","value":10},{"suit":"diamonds","face":"four","value":4},{"suit":"hearts","face":"four","value":4},{"suit":"clubs","face":"six","value":6},{"suit":"diamonds","face":"three","value":3},{"suit":"spades","face":"three","value":3},{"suit":"spades","face":"eight","value":8}]
$ podman pod stop cardpod
8553ad8fc0b14a849598a51c4ffcbffa9d6d094b96b542f0e432fc0d6dfd22ff
$ podman pod start cardpod
8553ad8fc0b14a849598a51c4ffcbffa9d6d094b96b542f0e432fc0d6dfd22ff
$ curl http://127.0.0.1:7000/deck/
curl: (7) Failed to connect to 127.0.0.1 port 7000: Connection refused
More Info:
I can see that the django servers are running by inspecting "podman logs <container_id>" and the mapped ports are still available from viewing "podman port <pod_id>"
dockerfile as per tutorial:
# FROM directive instructing base image to build upon
FROM python:3.7-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Create and change to working dir
RUN mkdir /code
WORKDIR /code
# Copy code
COPY /release/ /code/
# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt
# EXPOSE port 7000 to allow communication to/from server
EXPOSE 7000
# CMD specifies the command to execute to start the server running.
CMD python3 manage.py runserver 0.0.0.0:7000
podman: version 2.0.2
distro: ubuntu 18.04

Docker Container Networking with Docker-in-Docker

I would like to network with a child docker container from a parent docker container, with a docker-in-docker setup.
Let's say I'm trying to connect to a simple Apache httpd server. When I run the httpd container on my host machine, everything works fine:
asnyder:~$ docker run -d -p 8080:80 httpd:alpine
asnyder:~$ curl localhost:8080
<html><body><h1>It works!</h1></body></html>
But when I do the same from a docker-in-docker setup, I get a Connection refused error:
asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
I have tried a couple alterations without luck. Specifying the 0.0.0.0 interface:
asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 0.0.0.0:8080:80 httpd:alpine
/ # curl 0.0.0.0:8080
curl: (7) Failed to connect to 0.0.0.0 port 8080: Connection refused
Using the host network:
asnyder:~$ docker run -d --name mydind --privileged docker:dind
asnyder:~$ docker run -it --link mydind:docker docker:latest sh
/ # docker run -d --network host httpd:alpine
/ # curl localhost:80
curl: (7) Failed to connect to localhost port 80: Connection refused
Surprisingly, I was unable to find any existing articles on this. Does anyone here have some insight?
Thanks!
There are pros and cons for both DinD and bind mounting the Docker socket and there are certainly use cases for both. As an example, check out this set of blog posts, which does a good job of explaining one of the use cases.
Given your example docker-in-docker setup above, you can access Apache httpd server in one of two ways:
1) From inside the docker:dind container, it will be available on localhost:8080.
2) From inside the docker:latest container, where you were trying to access it originally, it will be available on whatever hostname is set for the docker:dind container. In this case, you used --name mydind, therefore curl mydind:8080 would give you the standard Apache <html><body><h1>It works!</h1></body></html>.
Hope it makes sense!
Building upon Yuriy's answer:
2) From inside the docker:latest container, [...] it will be available on whatever hostname is set for the docker:dind container. In this case, you used --name mydind, therefore curl mydind:8080 [...]
In the Gitlab CI config, you can address the DinD container by the name of its image (in addition to the name of its container, which is auto-generated):
Accessing the services
Let’s say that you need a Wordpress instance to test some API integration with your application.
You can then use for example the tutum/wordpress image in your .gitlab-ci.yml:
services:
- tutum/wordpress:latest
If you don’t specify a service alias, when the job is run, tutum/wordpress will be started and you will have access to it from your build container under two hostnames to choose from:
tutum-wordpress
tutum__wordpress
Using
service:
- docker:dind
will allow you to access that container as docker:8080:
script:
- docker run -d -p 8080:80 httpd:alpine
- curl docker:8080
Edit: If you'd prefer a more explicit host name, you can, as the documentation states, use an alias:
services:
- name: docker:dind
alias: dind-service
and then
script:
- docker run -d -p 8080:80 httpd:alpine
- curl dind-service:8080
Hth,
dtk
I am very convinced that #Yuriy Znatokov's answer is what I want, but I have understood it for a long time. In order to make it easier for later people to understand, I have exported the complete steps.
1) From inside the docker:dind container
docker run -d --name mydind --privileged docker:dind
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl localhost:8080
<html><body><h1>It works!</h1></body></html>
2) From inside the docker:latest container
docker run -d --name mydind --privileged docker:dind
docker run -it --link mydind:docker docker:latest sh
/ # docker run -d -p 8080:80 httpd:alpine
/ # curl mydind:8080
<html><body><h1>It works!</h1></body></html>

How to properly start nginx in Docker

I want nginx in a Docker container to host a simple static hello world html website. I want to simply start it with "docker run imagename". In order to do that I added the run parameters to the Dockerfile. The reason I want to do that is that I would like to host the application on Cloud Foundry in a next step. Unfortunately I get the following error when doing it like this.
Dockerfile
FROM nginx:alpine
COPY . /usr/share/nginx/html
EXPOSE 5000
CMD ["nginx -d -p 5000:5000"]
Error
Error starting userland proxy: Bind for 0.0.0.0:5000: unexpected error Permission denied.
From ::
https://docs.docker.com/engine/reference/builder/#expose
EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number
CMD ["nginx -d -p 5000:5000"]
You add your dockerfile
FROM nginx:alpine
its already starts nginx.
after you build from your dockerfile
you should use this on
docker run -d -p 5000:5000 <your_image>
Edit:
If you want to use docker port 80 -> machine port 5000
docker run -d -p 5000:80 <your_image>

Unable to connect to Docker Nginx build

I am trying to host a simple static site using the Docker Nginx Image from Dockerhub: https://registry.hub.docker.com/_/nginx/
A note on my setup, I am using boot2docker on OSX.
I have followed the instructions and even I cannot connect to the running container:
MacBook-Pro:LifeIT-war-games-frontend ryan$ docker build -t wargames-front-end .
Sending build context to Docker daemon 813.6 kB
Sending build context to Docker daemon
Step 0 : FROM nginx
---> 42a3cf88f3f0
Step 1 : COPY app /usr/share/nginx/html
---> Using cache
---> 61402e6eb300
Successfully built 61402e6eb300
MacBook-Pro:LifeIT-war-games-frontend ryan$ docker run --name wargames-front-end -d -p 8080:8080 wargames-front-end
9f7daa48a25bdc09e4398fed5d846dd0eb4ee234bcfe89744268bee3e5706e54
MacBook-Pro:LifeIT-war-games-frontend ryan$ curl localhost:8080
curl: (52) Empty reply from server
MacBook-Pro:LifeIT-war-games-frontend ryan$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f7daa48a25b wargames-front-end:latest "nginx -g 'daemon of 3 minutes ago Up 3 minutes 80/tcp, 0.0.0.0:8080->8080/tcp, 443/tcp wargames-front-end
Instead of localhost, use boot2docker ip. First do boot2docker ip and use that ip:
<your-b2d-ip>:8080. Also you need to make sure you forwarded your port 8080 in VirtualBox for boot2docker.
Here is the way to connect nginx docker container service:
docker ps # confirm nginx is running, which you have done.
docker port wargames-front-end # get the ports, for example: 80/tcp, 0.0.0.0:8080->8080/tcp, 443/tcp
boot2docker ip # get the IP address, for example: 192.168.59.103
So now, you should be fine to connect to:
http://192.168.59.103:8080
https://192.168.59.103:8080
Here's how I got it to work.
docker kill wargames-front-end
docker rm wargames-front-end
docker run --name wargames-front-end -d -p 8080:80 wargames-front-end
Then I went to my virtualbox and setup these settings:

Resources