When I was working on Kubernetes, I created the container connected by the browser because I was wondering which worker it was running on. I use it to stop the worker on purpose and check the connection destination.
It has been uploaded to https://hub.docker.com/r/yasthon/nginx-display-hostname.
Here, it is executed in the environment with the host name "dockerhost.example.jp".
$ docker pull yasthon/nginx-display-hostname
$ docker container run -d -v /etc/hostname:/usr/share/nginx/html/hostname -p 80:80 yasthon/nginx-display-hostname
$ curl http://localhost/index.sh
<html><head>
<title>dockerhost.example.jp</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head><body>
HOSTNAME : dockerhost.example.jp
</body></html>
Also check from your browser.
//[IP address of Docker host]/index.Access sh.
HOSTNAME : dockerhost.example.jp
## motion
I am mounting ``` / etc / hostname``` when starting the container.
I'm running sh from Nginx and reading the hostname.
Dockerfile
This file is used when building locally.
#### **`Dockerfile`**
```dockerfile
FROM debian:buster-slim
COPY default.conf /etc/nginx/conf.d/
COPY index.sh /usr/share/nginx/html/
RUN apt-get update && \
apt-get install -y --no-install-recommends nginx fcgiwrap && \
apt-get autoclean && \
rm -rf /var/lib/apt/lists/* && \
echo "daemon off;" >> /etc/nginx/nginx.conf && \
rm -f /etc/nginx/sites-enabled/default && \
chmod 744 /usr/share/nginx/html/index.sh && \
chown www-data:www-data /usr/share/nginx/html/index.sh
CMD /etc/init.d/fcgiwrap start && nginx
When I installed it on the OS normally, it worked easily, but when I ran it in a container, I had a problem.
--If you thought that the default root of Nginx was `/ usr / share / nginx / html /`
, you were referring to `/ var / www / html```. Since the use of the container is limited, I solved it by deleting
/ etc / nginx / sites-enabled / default``` without investigating. --There was no ``` fcgiwrap.socket```. I added ``
/etc/init.d/fcgiwrap start`` to CMD and created it when the container started. --There was a permission error. Added ``
chmod`` and ``
chown```.
--The Nginx Official image pulled from Docker Hub didn't work as expected. I installed it with a package and made it work.
default.conf
Nginx configuration file.
default.conf
server {
listen 80;
server_name localhost;
location ~ \.sh$ {
root /usr/share/nginx/html/;
include /etc/nginx/fastcgi_params;
fastcgi_index index.sh;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
}
No problem compared to Dockerfile.
index.sh
Reads and displays the mounted hostname file.
index.sh
#!/bin/sh
host_name=$(cat hostname)
echo "Content-type:text/html"
echo ""
echo "<html><head>"
echo "<title>${host_name}</title>"
echo '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">'
echo "</head><body>"
echo "HOSTNAME : ${host_name}"
echo "</body></html>"
Similarly, without any problems.
The image name is "nginx-display-hostname". Put the three files in the same directory and run `` `docker build```.
$ docker image build -t nginx-display-hostname .
$ docker container run -d -v /etc/hostname:/usr/share/nginx/html/hostname -p 80:80 nginx-display-hostname
$ curl http://localhost/index.sh
<html><head>
<title>dockerhost.example.jp</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head><body>
HOSTNAME : dockerhost.example.jp
</body></html>
Also check from your browser.
//[IP address of Docker host]/index.Access sh.
HOSTNAME : dockerhost.example.jp
## Working on Kubernetes
You need to mount `` `/ etc / hostname``` using volumeMounts and volumes.
#### **`sample.yaml`**
```yaml
apiVersion: v1
kind: Namespace
metadata:
name: nginx-prod #namespace name
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment #Deployment name(This is also the name of the replicaset)
namespace: nginx-prod #namespace name
spec:
selector:
matchLabels: #Creating a replicaset for pods with matching labels
app: nginx-pod
replicas: 2
template: #Pod template
metadata:
name: nginx-pod #Pod name
namespace: nginx-prod #The name of the pod's namespace
labels: #Pod label
app: nginx-pod
spec:
containers: #Container settings
- name: nginx-container #The name of the container
image: yasthon/nginx-display-hostname #Image name
env:
- name: nginx-container
ports:
- containerPort: 80 #Container port
volumeMounts:
- name: file-hostname
mountPath: /usr/share/nginx/html/hostname
volumes:
- name: file-hostname
hostPath:
path: /etc/hostname
Start the pod.
$ kubectl apply -f sample.yaml
namespace/nginx-prod created
deployment.apps/nginx-deployment created
Check the name of the pod.
$ kubectl get all -n nginx-prod
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-7ff4cc65cd-6r4qv 1/1 Running 0 3m52s
pod/nginx-deployment-7ff4cc65cd-djm9r 1/1 Running 0 3m52s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 2/2 2 2 3m53s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-7ff4cc65cd 2 2 2 3m53s
Check the IP in the pod details.
$ kubectl describe pod/nginx-deployment-7ff4cc65cd-djm9r -n nginx-prod | grep ^IP:
IP: 10.244.1.5
Access the confirmed IP from the Worker.
$ curl http://10.244.1.5/index.sh
<html><head>
<title>worker.example.jp</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head><body>
HOSTNAME : worker.example.jp
</body></html>
It was confirmed that Pod (nginx-deployment-7ff4cc65cd-6r4qv) was started by host worker.example.jp. Service is required for browser access from outside the cluster.
I thought I could make it soon, but it took an unexpected amount of time. I hope it will be useful to those who need it.
Recommended Posts