I have a very simple Ubuntu VM hosted on MS Azure. I have this simple python program running on it:
import http.server
from prometheus_client import start_http_server
from prometheus_client import Counter
REQUESTS = Counter('hello_worlds_total','Hello World requested')
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
REQUESTS.inc()
self.send_response(200)
self.end_headers()
self.wfile.write("Hello world")
if __name__ == "__main__":
start_http_server(8001)
server = http.server.HTTPServer(('localhost',8000), MyHandler)
server.serve_forever()
}}}
when I hit from my PC the url http://VM_AZURE_IP:8001 it replies with Promethues output. When I try http://VM_AZURE_IP:8000 I get connection refused. The newtork rules are ok, if I switch start_http_server(8001)
to start_http_server(8000)
and http.server.HTTPServer(('localhost',8000), MyHandler)
to http.server.HTTPServer(('localhost',8001), MyHandler)
, I get promethues metrics hitting http://VM_AZURE_IP:8000 and connection refused on port 8001
You could change your code like this and make sure you have added the port 8001,8000 in the inbound rule of your NSG associated to the Ubuntu VM.
import http.server
from prometheus_client import start_http_server
from prometheus_client import Counter
REQUESTS = Counter('hello_worlds_total','Hello World requested')
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
REQUESTS.inc()
self.send_response(200)
self.end_headers()
self.wfile.write(b"Hello world") # add b
if __name__ == "__main__":
start_http_server(8000)
server = http.server.HTTPServer(('0.0.0.0',8001), MyHandler) # chang to IP address 0.0.0.0
print("server on!")
server.serve_forever()
It worked on my side.