jueves, 19 de septiembre de 2024

Expose a nginx pod on ec2, just for testing

Deploy the nginx pod:

kubectl run nginx --image=nginx

Expose pod port with a service:

kubectl expose pod/nginx --port 80 --type NodePort


Check the services:

kubectl get services


NAME                TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE

kubernetes          ClusterIP      10.96.0.1       <none>        443/TCP          2d2h

nginx               NodePort       10.102.83.199   <none>        80:31545/TCP     8s




Forward public traffic to minikube cluster

We’ll be forwarding traffic using iptables tool as follows 
-Identify instance private network interface. Its IP address should correlate with the one shown from the instance main page (the default should be called enX0.
-Identify the minikube bridge network interface using ifconfig command, looking for an interface starting with br-, and copy its IP address aside.
-Identify the nginx service port generated by service creation.
kubectl get services



Then add the rules to forward the traffic of enX0 to the Minikube bridge

sudo iptables -A PREROUTING -t nat -i enX0 -p tcp --dport 31545 -j DNAT --to-destination 192.168.49.2:31545


sudo iptables -A FORWARD -p tcp -d 192.168.49.2 --dport 31545 -j ACCEPT



Add the rules on the ec2 security group to accept the traffic coming with destination por 31545.

Then test the app, something like this:

http://ec2-18-207-220-253.compute-1.amazonaws.com:31545/


Then if you want clean things from the cluster:

kubectl delete service nginx

service "nginx" deleted


kubectl delete pod nginx

pod "nginx" deleted


No hay comentarios:

Publicar un comentario