phpyamlminikubeconfigmapingress-nginx

Pass client IP from my local computer to remote Minikube using Apache proxy instead of Ingress


Does anyone know how to pass client IP from my local computer to a remote VM host (Debian 11, don't have GUI on it and don't want to have it) where Minikube is installed, minikube has MySQL and phpmyadmin services (phpmyadmin pod/container among phpmyadmin has on path var/www/html/my_path/ where php files of my PHP app are) .. Phpmyadmin service I access on local computer via port forwarding from remote VM, I access it through port 80. Ingress is also accessed through port forwarding from a remote VM, I access it through port 443. Why port forwarding? Because NodePort or LoadBalncer, minikube tunnel approach did work, most people on tutorials and the net say expose it through NodePort then use it in the browser.

The result of the minikube service some-service --url command or URL in the browser: minikube_ip:nodeport_service_port - which is ok if minikube is on your local machine, otherwise it doesn't work, so that's why port forwarding and it works.

In index.php (login form - in my minikube based php app) added these lines:

$clientIP = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
echo "<br><br>Client IP Address: " . $clientIP. "<br>";
foreach (getallheaders() as $name => $value) {
    echo "$name: $value <br>";
}
echo "<br><br>*************************************<br><br> " . $clientIP. "<br>";
$clientIP2 = isset($_SERVER['X-ORIGINAL-FORWARDED-FOR']) ? $_SERVER['X-ORIGINAL-FORWARDED-FOR'] : $_SERVER['sdfREMOTE_ADDR'];
echo "<br><br>X-Original-Forwarded-For: " . $clientIP2. "<br>";
echo "<br> php_uname :<br>";
echo php_uname();
var_dump($_SERVER);

I traversed all and tried all with online AI apps and basic net support and made modifications across ingress yaml, nginx.conf in ingress pod/container, regular logs, error logs, after regular (minikube addons enable ingress) install of ingress made helm installation of Ingress, changed nginx.conf and ingress and configmap yaml of Ingress pod that is used with phpmyadmin pod .. to no avail.

Still no success in seeing my real local IP in minikube php app accessed via local web browser instead of:

Client IP Address: 127.0.0.1
X-Real-IP: 127.0.0.1
X-Forwarded-For: 127.0.0.1

thx


Solution

  • Solution :

    Ok, so on remote VM where Minikube is installed I installed Apache and deleted Ingress deployment from Minikube as Ingress behaves as reverse proxy and as I am a beginner in general context of networking and servers I didn’t want any potential conflicts. Anyway deployment deleted doesn’t mean it is gone, it can be brought back with simply reapllying Ingeress Deployment YAML again to Minikube cluster.

    So, first I checkd my Phpmyadmin service Nodeport :

    my_user@my_vm_host /etc/apache2/sites-available $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql-service NodePort 10.97.143.83 3306:30796/TCP 49d phpmyadmin-service NodePort 10.106.171.32 80:30901/TCP 4d1h

    Then in apache .conf file which i dont remember where i first edited but can be found in /etc/apache2/sites-enabled/ or /etc/apache2/sites-available/

    and edited exiting content to this :

    <VirtualHost *:80>

    ServerName my_k8s.my_evidencija.myAD.domain
    
    ProxyPreserveHost On
    ProxyPass /my_evidencija http://172.17.0.3:30901/my_evidencija/
    ProxyPass /phpmy_admin http://172.17.0.3:30901/
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    

    myAD.domain that is your AD domain you can find on Win with Powershell : PS C:\windows\system32> Get-ADDomain | Select-Object -ExpandProperty DNSRoot

    AD is necessary to check as my php app is integrated with it ..

    my_k8s.my_evidencija is my host name which for now has to be added to C:\Windows\System32\drivers\etc\hosts file with according IP and works only on my local Win 10 machine, but if you use host IP in browser then it works on every machine in local network.

    172.17.0.3 is minikube IP you get with "minikube ip" command on remote VM where Minikube is installed. 30901 is Phypmyadmin service NodePort and 80 is Phpmyadmin pod/container port as it was in Docker before i pushed it to Minikube that is using Docker driver.

    The ProxyPass directive forwards requests from http://my_k8s.my_evidencija.myAD.domain/my_evidencija to http://172.17.0.3:30901/my_evidencija

    /my_evidencija/ is dir i created to group my app php files in phpmyadmin pod/container root folder var/www/html/ so when i enter in my local machine browser http://my_k8s.my_evidencija/my_evidencija/ it opens index.php not of phpmyadmin but of my php app and opens my app login form

    If I enter in browser http://my_k8s.my_evidencija/phpmy_admin/ then it leads to Phpmyadmin login form because http://172.17.0.3:30901/ path is phpmyadmin root path where Phpmydmin own index.php is located

    This is still Minikube environment for learning/developing/testing so of course there is no Cloud provider like in production environment..