← Back to list

Setting Up a Secure TURN Server (Coturn) for WebRTC — Like the Ones Behind WhatsApp Calls

If you’ve ever built a voice or video calling feature using WebRTC, you’ve probably heard of TURN servers. They’re the unsung heroes that…

Muhammad Abdullah in DevOps.dev · 2025-10-31 11:02 · 0 claps · 3.0 min read
#webrtc #turn #stun #whatsapp #sip
Open on Medium ↗

Setting Up a Secure TURN Server (Coturn) for WebRTC — Like the Ones Behind WhatsApp Calls

If you’ve ever built a voice or video calling feature using WebRTC, you’ve probably heard of TURN servers. They’re the unsung heroes that make your calls connect — even when users are behind strict firewalls, mobile networks, or corporate NATs.

In fact, this same technology is what powers WhatsApp Business calling, Google Meet, Zoom, and many other real-time communication systems. Let’s see how to set it up yourself — securely, privately, and production-ready.

What is a TURN Server and Why You Need It

TURN (Traversal Using Relays around NAT) is part of the WebRTC ICE framework. When two users try to connect, the browser first attempts a direct peer-to-peer link via a STUN server. But when both peers are behind restrictive NATs, this fails — that’s where TURN comes in.

The TURN server relays audio/video traffic between both users, ensuring the call still goes through.

Without a TURN server:

  • Calls may fail for users on 4G/5G or enterprise Wi-Fi.
  • File transfer and data channels may hang or never start.

That’s why apps like WhatsApp Business, Telegram, and Google Meet rely on TURN in the background — it’s the bridge that makes real-time communication reliable.

Step 1: Prepare Your Environment

You’ll need:

  • A Kubernetes cluster (EKS, GKE, or K3s)
  • A DNS record like turn.example.com pointing to your node or load balancer
  • A Kubernetes secret for credentials

Create your secret (this keeps sensitive data out of YAML files):

kubectl create secret generic turn-secret \
  --from-literal=TURN_USERNAME=myturnuser \
  --from-literal=TURN_PASSWORD=supersecurepassword \
  -n namespace

Step 2: Coturn Pod Configuration

Below is a clean, production-ready manifest stripped of all runtime metadata:

apiVersion: v1
kind: Pod
metadata:
  name: coturn
  namespace: rtc
  labels:
    app: coturn
spec:
  hostNetwork: true
  dnsPolicy: ClusterFirstWithHostNet
  containers:
    - name: coturn
      image: coturn/coturn:4.7.0
      imagePullPolicy: Always
      args:
        - -n
        - --log-file=stdout
        - --fingerprint
        - --lt-cred-mech
        - --realm=your-domain.com
        - --user=$(TURN_USERNAME):$(TURN_PASSWORD)
        - --listening-port=3478
        - --min-port=49152
        - --max-port=65535
        - --external-ip=YOUR_PUBLIC_IP/YOUR_INTERNAL_IP
      ports:
        - name: udp-3478
          containerPort: 3478
          hostPort: 3478
          protocol: UDP
      env:
        - name: TURN_USERNAME
          valueFrom:
            secretKeyRef:
              name: turn-secret
              key: TURN_USERNAME
        - name: TURN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: turn-secret
              key: TURN_PASSWORD
  restartPolicy: Always

Apply it:

kubectl apply -f coturn-pod.yaml

Once it’s running, your TURN server is live and ready.

Step 3: Hiding Sensitive Details

You don’t want to expose your domain name, node IP, or credentials publicly — here’s how to keep them private:

  1. Use a Load Balancer or CloudFront proxy to mask your node’s real IP.
  2. Use a neutral subdomain, such as relay.example.net, instead of your app domain.
  3. Disable verbose logs to prevent credentials from showing up in stdout.
  4. Always use Kubernetes Secrets to inject environment variables — never hardcode credentials.

If you’re deploying on AWS, put the Coturn node behind a Network Load Balancer (NLB) or assign an Elastic IP to keep the node private but reachable.

Step 4: Configure Your App Environment

Now set your TURN configuration in your application or .env file:

TURN_URL=turn:relay.example.net:3478
TURN_USERNAME=myturnuser
TURN_PASSWORD=supersecurepassword

Then add it to your WebRTC configuration:

const pc = new RTCPeerConnection({
  iceServers: [
    {
      urls: 'turn:relay.example.net:3478',
      username: TURN_USERNAME,
      credential: TURN_PASSWORD
    }
  ]
});

Your users can now connect through your TURN relay — even behind NATs or corporate firewalls.

Step 5: Optional Hardening and Scaling

For production:

  • Convert this Pod into a Deployment for automatic recovery and scaling.
  • Expose via a Service (NodePort or LoadBalancer).
  • Use TLS (turns:) for encrypted TURN traffic.
  • Restrict allowed ports in your firewall to only what Coturn needs (3478, 49152–65535).
  • Add monitoring (Prometheus or Grafana) to track connections and bandwidth.

How This Compares to WhatsApp Business Calling

When you make a WhatsApp Business voice or video call, it uses the same underlying WebRTC structure — STUN + TURN. TURN ensures calls can traverse complex networks and always find a working route between users.

The main difference? Large-scale systems like WhatsApp use distributed TURN servers across regions and load-balance based on latency.

But the concept — and even much of the configuration — remains the same as the one you just deployed.

Wrapping Up

Setting up a TURN server like Coturn isn’t complicated, but it’s critical. It’s the invisible infrastructure that guarantees connectivity across every kind of network.

By running it on Kubernetes, hiding sensitive data, and securing your deployment, you’re following the same engineering practices that power real-time apps like WhatsApp, Google Meet, and Zoom.

Your users will never know it’s there — but their calls will always connect.


메타데이터
post_id
bdaf6140b97c
slug
setting-up-a-secure-turn-server-coturn-for-webrtc-like-the-ones-behind-whatsapp-calls-bdaf6140b97c
url
https://blog.devops.dev/setting-up-a-secure-turn-server-coturn-for-webrtc-like-the-ones-behind-whatsapp-calls-bdaf6140b97c
canonical_url
https://blog.devops.dev/setting-up-a-secure-turn-server-coturn-for-webrtc-like-the-ones-behind-whatsapp-calls-bdaf6140b97c
author_url
https://medium.com/@abdullah3480
status
ok
fetched_at
2026-06-21 19:25:17