Dockerが動作しているホストとPodを表示してくれるnginxコンテナ

投稿者: | 5月 30, 2022

Horizontal Pod Autoscalerが面白くて、誰が応答しているのか知りたくなった。そういうものがないかなぁと思ったら、作っている人がいた。

https://qiita.com/yasthon/items/6a4627f249bb7fa52eb9

これを参考にして、作り直してみることに。作り直す目的は、セキュリティがうるさくなってきたので最新イメージを使う。

Dockerのイメージを作って、動作確認して、コンテナで動かすというのが全体の流れ。

必要な環境

  • Docker端末
  • Private Registry(なければDocker hub)
  • Loadbalancer付きのKubernetesクラスタ(もしLoadbalancerがない場合は、Serviceはいらない。)

オンプレ志向なので、全部オンプレのものを使う。ロードバランサーの動きもみたいので、ロードバランサー環境に最終的に作る。

1. Dockerfileの作成

Debianの最新をベースに作る。

cat << 'EOF' > Dockerfile
FROM debian:bullseye-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
EOF

2. nginxのdefault.confの作成

cat << 'EOF' > 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;
}
}
EOF

3. index.shの作成

オリジナルは、ノードの名前を表示するが、podの名前も出してみた。

cat << 'EOF' > index.sh
#!/bin/sh

host_name=$(cat hostname)
pod_name=$(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 "POD HOSTNAME : ${pod_name}"
echo "</body></html>"
EOF

4. ビルドと動作確認

以下でビルドしてみる、

docker image build -t nginx-display-hostname .

動作確認

80番を使うと何かとバッティングしそうなので、18082を利用

docker container run -d -v /etc/hostname:/usr/share/nginx/html/hostname -p 18082:80 nginx-display-hostname
curl http://localhost:18082/index.sh

5. Private Registryにアップロード

Private Registry 192.168.16.2:5000にアップロードする。Dockerやcontainerdの設定を事前にしておく。

docker tag nginx-display-hostname:latest 192.168.16.2:5000/nginx-display-hostname:latest
docker push 192.168.16.2:5000/nginx-display-hostname:latest
docker rmi nginx-display-hostname

6. Kubernetesで動かしてみる。

Private Registryの設定が終わっていることが前提で書いています。イメージのアドレス(192.168.16.2:5000〜は適宜書き換えて)

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
name: nginx-prod
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx-prod
spec:
selector:
matchLabels:
app: nginx-pod
replicas: 2
template:
metadata:
name: nginx-pod
namespace: nginx-prod
labels:
app: nginx-pod
spec:
containers:
- name: nginx-container
image: 192.168.16.2:5000/nginx-display-hostname:latest
env:
- name: nginx-container
ports:
- containerPort: 80
volumeMounts:
- name: file-hostname
mountPath: /usr/share/nginx/html/hostname
volumes:
- name: file-hostname
hostPath:
path: /etc/hostname
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-pod
name: nginxsvc
namespace: nginx-prod
spec:
ports:
- port: 80
targetPort: 80
selector:
app: nginx-pod
type: LoadBalancer
EOF

ロードバランサーのIPを調べる

kubectl -n nginx-prod get service nginxsvc

アクセスをしてみる。
http:/<EXTERNAL IP>/index.sh

ちょっとしたエクソサイズでした。

コメントを残す