Launching a Live Stream Website on Kubernetes: Step-by-Step Guide

Ayushmaan Srivastav
5 min readOct 22, 2024

--

In today’s digital world, live streaming platforms are gaining immense popularity, and Kubernetes, with its ability to orchestrate and scale services, is an excellent choice for hosting such platforms. This blog will guide you through deploying a live stream website on Kubernetes, using technologies like Nginx, FFmpeg, and a streaming server such as Nginx RTMP or Media Server.

Let’s explore how to set up a live streaming website on Kubernetes step by step.

1. What You’ll Need

Before diving into the setup, ensure you have:

  • A running Kubernetes Cluster (Minikube for local development or a cloud provider like AWS, GCP, or Azure)
  • kubectl installed and configured to manage the cluster
  • Docker installed to build and manage container images
  • FFmpeg installed for streaming support (optional for testing)
  • A Domain (optional but recommended for production)

2. Overview of the Components

To create a live stream platform, we’ll use the following technologies:

  • Nginx RTMP Module: This will serve as the streaming server, handling the incoming RTMP stream and forwarding it to viewers via HTTP.
  • FFmpeg: A powerful tool for handling multimedia data, encoding live streams, and converting formats.
  • Nginx (Web Server): For serving the live stream to users over HTTP.
  • Kubernetes: To manage, deploy, and scale the streaming services.

3. Step-by-Step Guide to Deploying a Live Stream Website

Step 1: Set Up Nginx RTMP Server

We will start by setting up an Nginx RTMP Server to accept incoming RTMP streams and serve them via HTTP.

1.1 Create Dockerfile for Nginx with RTMP Module

Create a custom Dockerfile for Nginx with the RTMP module. This allows Nginx to accept RTMP streams and forward them as HTTP streams.

FROM nginx:alpine

RUN apk add --no-cache ffmpeg nginx-mod-rtmp

COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 1935 8080

1.2 Configure Nginx for RTMP Streaming

In the nginx.conf file, configure the RTMP module and HTTP streaming:

worker_processes auto;

events {
worker_connections 1024;
}

rtmp {
server {
listen 1935;
chunk_size 4096;

application live {
live on;
record off;
}
}
}

http {
include mime.types;
default_type application/octet-stream;

server {
listen 8080;
location / {
root /usr/share/nginx/html;
}

location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/share/nginx/html;
}

location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
root /usr/share/nginx/html;
}
}
}

In this configuration:

  • RTMP listens on port 1935 for incoming streams.
  • HTTP is set to serve content on port 8080, including HLS (HTTP Live Streaming).
  • The location /hls block defines the path for streaming HLS content.

1.3 Build the Docker Image

Build the Docker image for the Nginx RTMP server:

docker build -t nginx-rtmp .

Push it to a Docker registry if you’re using a remote Kubernetes cluster:

docker tag nginx-rtmp <registry>/nginx-rtmp
docker push <registry>/nginx-rtmp

Step 2: Create Kubernetes Deployment and Service

Next, we’ll create the necessary Kubernetes Deployment and Service files to deploy the Nginx RTMP server on Kubernetes.

2.1 Deployment Manifest

Create a Kubernetes deployment manifest (nginx-rtmp-deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-rtmp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx-rtmp
template:
metadata:
labels:
app: nginx-rtmp
spec:
containers:
- name: nginx-rtmp
image: <registry>/nginx-rtmp
ports:
- containerPort: 1935
- containerPort: 8080

2.2 Service Manifest

Create a service to expose the Nginx RTMP server (nginx-rtmp-service.yaml):

apiVersion: v1
kind: Service
metadata:
name: nginx-rtmp-service
spec:
selector:
app: nginx-rtmp
ports:
- protocol: TCP
port: 1935
targetPort: 1935
- protocol: TCP
port: 8080
targetPort: 8080
type: LoadBalancer

2.3 Apply the Kubernetes Manifests

Deploy the Nginx RTMP server and service to your Kubernetes cluster:

kubectl apply -f nginx-rtmp-deployment.yaml
kubectl apply -f nginx-rtmp-service.yaml

Step 3: Stream Video Content

Now that the server is running, let’s push a live stream to it.

3.1 Push a Stream Using FFmpeg

Use FFmpeg to send a live stream to the RTMP server. For example, if you have a video file, use the following command:

ffmpeg -re -i input.mp4 -c:v libx264 -f flv rtmp://<external-ip>:1935/live/stream

Replace <external-ip> with the external IP of the Kubernetes service.

3.2 Access the Live Stream via HLS

You can now access the live stream using an HLS-capable player or a browser. Go to:

http://<external-ip>:8080/hls/stream.m3u8

This URL serves the live stream via HLS (HTTP Live Streaming) protocol, which is supported by most modern browsers and streaming players.

4. Scaling and Managing Your Live Stream Service

Kubernetes allows you to scale and manage your streaming service efficiently:

4.1 Horizontal Pod Autoscaling (HPA)

Enable Horizontal Pod Autoscaling to scale your RTMP server based on traffic:

kubectl autoscale deployment nginx-rtmp-deployment --cpu-percent=50 --min=2 --max=10

4.2 Centralized Logging and Monitoring

For centralized logging, use tools like Elasticsearch, Fluentd, and Kibana (EFK stack) or Prometheus and Grafana for monitoring performance and health metrics.

4.3 Ingress for Domain-Based Access

For production, consider setting up Ingress to allow users to access your live stream service using a domain name:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-rtmp-ingress
spec:
rules:
- host: stream.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-rtmp-service
port:
number: 8080

Apply the Ingress and configure your domain’s DNS to point to the Kubernetes cluster.

5. Advanced Considerations

5.1 Multi-Bitrate Streaming

For better streaming experiences, especially on different devices and network conditions, you can enable multi-bitrate streaming with FFmpeg. FFmpeg can transcode streams into multiple quality levels (e.g., 240p, 360p, 720p) and serve them via HLS.

5.2 Using CDN for Scalability

For high-traffic production environments, you may want to integrate a Content Delivery Network (CDN) like Cloudflare or AWS CloudFront to distribute your live streams to viewers around the world, ensuring low latency and high availability.

5.3 Securing Your Streams

To prevent unauthorized access to your streams, you can:

  • Implement token-based authentication for RTMP.
  • Use HTTPS for secure streaming via HTTP.
  • Control access using Kubernetes Network Policies.

6. Conclusion

Launching a live streaming platform on Kubernetes is a powerful way to handle high traffic, ensure scalability, and provide a smooth user experience. With Nginx RTMP as the streaming server, FFmpeg for stream handling, and Kubernetes for orchestration, you can easily build a reliable and scalable streaming service.

--

--

No responses yet