Error while proxying request kubectl proxy - kubernetes

I'm trying to follow this documentation https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/.
After running this command
kubectl proxy --port=8080 &
I get the output
Starting to serve on 127.0.0.1:8080
However when I run "curl http://localhost:8080/api/" to hit the server, I get this response
dial tcp: lookup localhost: no such host
Any idea's why I would get this response?
EDIT:
I'm using a VPN to connect to corporate network. When I disable the VPN I still get the same message for both localhost and 127.0.0.1 (same exact message for both).
I'm not using kubeadm.
When I run host localhost I get this output
localhost has address 127.0.0.1
localhost has IPv6 address ::1
cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section

It is possible you might have http_proxy set on you system , so try curl with noproxy as following :
curl --noproxy '*' http://localhost:8080
If you want to check if the http_proxy is set :
echo $http_proxy

Related

Docker container can't reach the host's Postgresql database

I have a Docker container with Redmine. I run it and it works, except the fact that it doesn't connect to the host's database.
I've searched for all similar questions, and tried what was suggested everytime, but it doesn't work.
Here is how I run the container:
docker run -d --name redmine-4.2.5 -e REDMINE_DB_POSTGRES=172.17.0.1 -e REDMINE_DB_PORT=5432 -e REDMINE_DB_USERNAME=redmine -e REDMINE_DB_PASSWORD=XXX -e REDMINE_DB_DATABASE=redmine -e REDMINE_NO_DB_MIGRATE=1 --add-host=database:172.17.0.1 redmine
When I enter the container and try to connect manually I have this:
root#86a42eae3adf:/usr/src/redmine# psql -U redmine -h database
psql: error: could not connect to server: Connection refused
Is the server running on host "database" (172.17.0.1) and accepting
TCP/IP connections on port 5432?
When I check the open ports, I have this:
root#86a42eae3adf:/usr/src/redmine# nmap 172.17.0.1
Starting Nmap 7.80 ( https://nmap.org ) at 2022-04-02 16:33 UTC
Nmap scan report for database (172.17.0.1)
Host is up (0.000024s latency).
Not shown: 996 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
111/tcp open rpcbind
8080/tcp open http-proxy
MAC Address: XXXX (Unknown)
Nmap done: 1 IP address (1 host up) scanned in 0.25 seconds
So I checked the configuration on my host, and I have everything correct:
pg_hba.conf has this line:
host redmine redmine 172.17.0.0/16 trust
And postgresql.conf has this one:
listen_addresses = '*'
(I know I shouldn't put '*' but I'll limit to 172.17.0.2 when it will work for '*')
I restarted postgresql and reloaded the config, up to no avail.
I have no clue on how to investigate to see what I should modify to have my container be able to contact the host on port 5432. I'm open (on all ports) to any suggestion.
Thanks in advance.

SSH Tunnel for MongoDb Connection Within VPC

I am attempting to tunnel from my localhost (on port 24000) via a Bastion box to my mongo instance (on 27017) that is only available via the VPC private subnet so that I may develop locally whilst connected to the staging db. Using this tunnel command on my OSX box:
ssh -A -L 24000:ip-10-0-11-11.ec2.internal:27017 ec2-3-211-555-333.compute-1.amazonaws.com -N -v
"ip-10-0-11-11.ec2.internal" is the mongo box.
"ec2-3-211-555-333.compute-1.amazonaws.com" is the bastion box.
Aiming to bind local port 24000 to the bastion then from there to the mongo box on 27017.
However upon trying to connect via the tunnel from my local box with:
mongo -u dbUser localhost:24000/db-name
The connection is timing out. Below is the verbose output from ssh tunnel command (presumably from the bastion?).
debug1: channel 3: free: direct-tcpip: listening port 24000 for ip-10-0-11-11.ec2.internal port 27017, connect from 127.0.0.1 port 63451 to 127.0.0.1 port 24000, nchannels 4
channel 4: open failed: connect failed: Connection timed out
Seems to try to be working but it is just not. Any and all help would be appreciated! I do have ssh forwarding enabled on the bastion via the sshd config. I can also connect to the mongo instance while on the bastion no problem.
Circling back... not sure how I got it working or why it wasn't working, but for those looking forward the ssh command to open a tunnel forwarding the keys in your ssh-agent this command is indeed the way todo.
ssh -A -L 24000:ip-10-0-20-141.ec2.internal:27017 ec2-54-165-159-177.compute-1.amazonaws.com -N -v

SSH Tunnel for PostgreSQL - Connection Refused

I am, for the first time, attempting to set up an application on a remote/cloud VPS (I am using Digital Ocean if it matters). I am attempting to create an SSH tunnel from my client to the remote database. As this is not something I have attempted before, I referenced this, this, and this.
After looking over the articles, I ran the following on my client/local machine:
ssh -L 5433:localhost:5432 user#REMOTE_IP
then I tried to connect:
psql -h localhost -p 5433 postgres;
However, I receive the following error:
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5433?
To my knowledge, my pg_hba.conf (on the remote server) is the default:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
I changed "listen_addresses" in postgresql.conf to *
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
I also tried substituting 127.0.0.1 forlocalhost with no success.
Any advice would be appreciated; SSH tunnels and the like are not something I am familiar with.
Thanks.
EDIT:
Per #drdaeman excellent advice, I ran the following:
sudo ssh -N -vvv -L 5433:localhost:5432 user#host
The last few debug lines are as follows:
debug1: Local forwarding listening on 127.0.0.1 port 5433.
debug2: fd 5 setting O_NONBLOCK
debug3: fd 5 is O_NONBLOCK
debug1: channel 1: new [port listener]
debug2: fd 3 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x10
debug1: Requesting no-more-sessions#openssh.com
debug3: send packet: type 80
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 80
debug1: client_input_global_request: rtype hostkeys-00#openssh.com want_reply 0
Output from sudo netstat -ltpn | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 5835/postgres
It stops there, unresponsive to any commands.
Thanks for any direction.
Based on your description, everything looks OK to me - don't see where the problem is, but the commands you're running and your configuration looks correct. Here are the general steps you can take to diagnose the issue:
First, check if your PostgreSQL server is actually listening. On your server, run this:
$ sudo netstat -ltpn | grep 5432
(Or you can use ss -ltpn from iproute2 instead of older netstat)
If you don't see anything, it means no process is listening on tcp/5432. You can try to see if PostgreSQL is listening anywhere at all:
$ sudo netstat -lpn | grep postgre
If it doesn't - check whenever your server is actually running (depends on the OS and distribution, but check ps aux output first) and check your server logs (probably in /var/log) if you see any problems there.
Then, make sure you don't accidentally run psql on your server (when you SSH, it also opens the shell session unless you specify the -N flag). You need to run it on your local machine ;)
Then, you may also consider adding -v (or even -vvv) to your ssh command - it'll spew a lot of useful debug information, e.g. a normal operation looks like this:
debug1: Connection to port 5433 forwarding to localhost port 5432 requested.
debug1: channel 3: new [direct-tcpip]
debug1: channel 3: free: direct-tcpip: listening port 5433 for localhost port 5432, connect from ::1 port 60039 to ::1 port 5433, nchannels 4
If you see something like channel 3: open failed: connect failed: Connection refused instead, this means PostgreSQL had refused the connection - and you need to check its logs for the reasoning - possibly, after enabling log_connections and log_disconnections in the config (don't forget to reload the configuration).

How can I access Postgresql hosted in Vagrant as a guest in Windows?

I am trying to connect to Postgresql using the PGAdmin III client in Windows 8.1. Postgresql is installed in a local copy of Vagrant (Ubuntu 14.04) on my Windows machine. It's up and running on Vagrant:
LISTENING
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 814/postgres
VERIFIED USER/PASS/LOCAL CONNECTION
I can access Postgresql locally in Vagrant via SSH in Windows:
vagrant#precise32:/etc/postgresql/9.1/main$ psql -h localhost testdb myuser
Password for user myuser:
psql (9.1.15)
SSL connection (<removed)
Type "help" for help.
testdb=> \quit
PG_HBA.CONF
I added this to my pg_hba.conf file:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all <my IP> md5
VAGRANT CONFIG
My Vagrant config is set to port forward to 5432:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 5432, host: 15432
POSTGRESQL.CONF
And my postgresql.conf file is set to listen on all IP's:
#listen_addresses = '*' # what IP address(es) to listen on;
PGADMIN ERROR
So, what am I missing here when I try to connect as a guest via PGAdmin to the host and I get the following message, which indicates it sees it but something is not letting me through?:
An error has occurred:
Error connecting to the server: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.
PGADMIN CONNECTION INFO
Host: localhost
Port: 15432
Service: <blank>
Maintenance DB: postgres
Username: Myuser (verified)
Password: ****** (verified)
Store password:
Colour: <blank>
Group: Servers
In your Vagrant config add a IP (if not set) eg:
config.vm.network :forwarded_port, host: 15432, guest: 5432
config.vm.network :private_network, ip: "192.168.111.222"
Now from PGAdmin in Windows connect to host 192.168.111.222, port 5432.
Worked for me although I'm not know why.. :P
I dont know what you mean with in pg_hba.conf but in vagrant enviroment you should use ip like 10.0.2.2/24 instead your machine network address.
I had the same problem in linux, and i think in windows this can happen too. In my postgresql.conf the port variable was defined in two places. First with 5432 value, and second with 5435 value.
Running telnet vm_ip 5435, i was able to connect to the server running in the vm.
In my case my vm was running with public_network option, so i have a external ip. In this case, you don't need to forward a port, once you will access the vm,for example, with 192.168.60.15:5435 address.

Docker - Tomcat and PostgreSQL containers in same host - No Route to host

I have containerized a web application running on Tomcat and started the container using the command,
docker run -d -p 8080:8080 tveuser/tve-repository:tve-services
I am also running a PostgreSQL container on the same host using the following command:
docker run -d -p 80:80 -p 5432:5432 tveuser/tve-repository:tve-postgresql
i verified that the PostgreSQL is running by using phpPgAdmin but could not get tomcat to connect to it. 'docker ps' also tells me that both the containers are up and running.
I connect the web application with the database through tomcat context.xml which has an entry like
<Parameter name="abc.connection.url" value="jdbc:postgresql://1.2.3.4:5432/dbname" />
where 1.2.3.4 is the docker host Ip in which the container is running.But i get the following error when i run the tomcat container:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
...............
Caused by: java.net.NoRouteToHostException: No route to host
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
Any help appreciated.
TL;DR : check the firewall on your host
You need to connect to your host's IP address on the port that you've exposed for postgres on your postgres container, not to the IP address of the docker container.
E.g.,
<Parameter name="abc.connection.url" value="jdbc:postgresql://192.168.x.x:5432/dbname" />
You'll also need to configure postgres inside your postgres docker container to listen for connections from somewhere other than 127.0.0.1, which is the default.
E.g., in pg_hba.conf:
host all all 0.0.0.0/0 md5
This tells postgres to accept incoming connections from any IP address (you'll probably want to lock this down to something like 192.168.0.0/16.
You'll also need to change the value of listen_addresses in postgres.conf:
listen_addresses = '*' # what IP address(es) to listen on;
Once you've made these configuration changes, you can check to see if postgres is listening to all requests from all IPs inside your docker container:
netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 816/postgres
tcp6 0 0 :::5432 :::* LISTEN 816/postgres
And then from your host, make sure you can telnet into port 5432:
telnet 192.168.x.x 5432
Trying 192.168.x.x...
Connected to 192.168.x.x.
Escape character is '^]'.
As Chris McKinnel points out, forwarding ports to the host doesn't work like that. I wanted to give you a second solution as an alternative to his: Links.
https://docs.docker.com/userguide/dockerlinks/
So you could do something like:
docker run -d -P --name tomcat --link postgres:postgres apache/tomcat apachectl start
... which, assuming your PostgreSQL container was named "postgres", would allow you to connect directly from the Tomcat container.
Note that this only works if both containers are on the same machine, but it sounds like they are.