Game Tech Tutorial
Published in

Game Tech Tutorial

How to Make Application Server for High Availability?

If you want to keep your services online without any downtime, you must pay attention to the availability of your server.

WebRTC, for example, you should carefully check the availability of TURN Server, because TURN Server plays an important role to help two parties to connect to each other with Video or Audio streaming in different NAT networks.

In common practice, you can keep to telnet TURN Server at PORT 3478 or 3479 to check it is alive or not in a cron job.

In this post, we will show how to automatically monitor your application server and restart it during the downtime by a cron job. You can apply this technique to any cloud service like AWS or GCP.

  1. Install pexpect lib in Python
sudo pip install pexpect --upgrade
  1. Edit MonitorStun.py
  • Telnet your application server at opening port to check your application is alive or not.
  • If it goes down, ssh to your server and restarts it
# MonitorStun.py
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
from pexpect import pxssh
# ssh to Application server and restart it
def connect_server():
s = pxssh.pxssh()
if not s.login ('Application Server IP', 'SERVER PORT', 'ACCOUNT', 'PASSWORD'):
print "SSH session failed on login."
print str(s)
else:
print "SSH session login successful"
s.sendline ('sudo <your app>')
s.prompt() # match the prompt
print s.before # print everything before the prompt.
s.logout()
# If your application server is online at port 8081, 8082
# Clear the screen
subprocess.call('clear', shell=True)
# Ask for input
remoteServer = 'SERVER IP'
remoteServerIP = socket.gethostbyname(remoteServer)
# Print a nice banner with information on which host we are about to scan
print "-" * 60
print "Please wait, scanning remote host", remoteServerIP
print "-" * 60
# Check what time the scan started
t1 = datetime.now()
# Using the range function to specify ports (here it will scans all ports between 1 and 1024)# We also put in some error handling for catching errorstry:
for port in range(8081, 8082):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print "Port {}: Open".format(port)
else:
print "Server is down"
connect_server()
print "restart Server OK"
sock.close()
except KeyboardInterrupt:
print "You pressed Ctrl+C"
sys.exit()
except socket.error:
print "Couldn't connect to server"
sys.exit()
  1. Add MonitorStun.py to cron job to check TURN Server in every 1 min.
*/1 * * * * /your_path/monitorStun.py

Summary

You have learned this technique to monitor any application at specific port by telnet, so as you can ensure high availability of your application server.

Of course, this technique can be applied to any services such as WebRTC TURN Server with port 3478, SIP Proxy with port 5060, Apache with port 80, or Tomcat with port 8080.

--

--

--

High-quality technical tutorials: AWS, GCP, Unity, SDK, Security, and more.

Recommended from Medium

PHP Interview Questions and Answers || Coding Tag

Bring your app to Microsoft 365 in 3 steps

Lesson 2: Value Types in Solidity

StreamElements Supercharges Trovo with Robust Suite of Tools

Constants in Ruby

What is the Game of Life?

GitHub Student Developer Pack

Installing Ubuntu Server 20.04 on Raspberry Pi 4

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eric Wei

Eric Wei

Senior Full Stack Engineer & Solution architecture | AWS, GCP | Cloud, Unity Game Development, SDK, DevOps, and more.

More from Medium

Adding SSL/TLS to a Web Application using AWS Application Load Balancer

Odoo Logging to Cloudwatch

Deploying Spring Boot application on AWS ECS

Micro-service with AWS ElasticBeanStalk | Part1