基于K8S搭建HLS直播/点播云服务

13 篇文章 0 订阅
9 篇文章 0 订阅
相关连接
  1. Kubernetes 核心概念 POD及网络
  2. Kubernetes 核心概念Label、RC、HA、Deployment
  3. Kubernetes 核心概念 StatefulSet、Service
  4. Kubernetes存储
  5. 通过MINIKUBE安装K8S测试环境(国内安装)
  6. 通过kubeadm安装k8s并配置集群
  7. 基于K8S搭建HLS直播/点播云服务
  8. 基于K8S搭建VR直播/点播云服务

基于K8S搭建HLS直播/点播云服务

Nginx本身是一个非常出色的HTTP服务器,FFMPEG是非常好的音视频解决方案.这两个东西通过一个nginx的模块nginx-rtmp-module,
组合在一起即可以搭建一个功能相对比较完善的流媒体服务器。同时利用K8S集群管理,快速完成直播点播云服务。

nginx 配置参考HLS-搭建Nginx流媒体点播服务(SaaS docker)

Pod模块架构图

直播架构

模块讲解

待续

容器编排

管理容器代码省略

#Nginx点播服务
#nginx-hls.conf
worker_processes  auto;
#error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
	sendfile off;
	tcp_nopush on;
	directio 512;
	# aio on;
	
	# HTTP server required to serve the player and HLS fragments
	server {
		listen 8080;
		
		# Serve HLS fragments
		location /hls {
			types {
				application/vnd.apple.mpegurl m3u8;
				video/mp2t ts;
			}
			root /mnt; #资源目录
            add_header Cache-Control no-cache; # Disable cache	
			# CORS setup
			add_header 'Access-Control-Allow-Origin' '*' always;
			add_header 'Access-Control-Expose-Headers' 'Content-Length';
            
			# allow CORS preflight requests
			if ($request_method = 'OPTIONS') {
				add_header 'Access-Control-Allow-Origin' '*';
				add_header 'Access-Control-Max-Age' 1728000;
				add_header 'Content-Type' 'text/plain charset=UTF-8';
				add_header 'Content-Length' 0;
				return 204;
			}
		}
        # Serve DASH fragments
        location /dash {
            types {
                application/dash+xml mpd;
                video/mp4 mp4;
            }
			root /mnt;
			add_header Cache-Control no-cache; # Disable cache
            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # Allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        }
	}
}
#Nginx直播服务
#nginx-rtmp.conf
worker_processes  auto;
#error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
	sendfile off;
	tcp_nopush on;
	directio 512;
	# aio on;
	
	# HTTP server required to serve the player and HLS fragments
	server {
		listen 8080;		
		# This URL provides RTMP statistics in XML
		location /stat {
			rtmp_stat all;
			rtmp_stat_stylesheet stat.xsl; # Use stat.xsl stylesheet 
		}
		location /stat.xsl {
			# XML stylesheet to view RTMP stats.
			root /usr/local/nginx/html;
		}
		location /players {
			root /usr/local/nginx/html;    
		}
	}
}

rtmp {
    server {
		listen 1935; # Listen on standard RTMP port
		chunk_size 4000; 
		# ping 30s;
		# notify_method get;

		# This application is to accept incoming stream
		application live {
			live on; # Allows live input

			# for each received stream, transcode for adaptive streaming
			# This single ffmpeg command takes the input and transforms
			# the source into 4 different streams with different bitrates
			# and qualities. # these settings respect the aspect ratio.
			exec_push  /usr/local/bin/ffmpeg -i rtmp://localhost:1935/$app/$name -async 1 -vsync -1
						-c:v libx264 -c:a aac -b:v 256k  -b:a 64k  -vf "scale=480:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_low
						-c:v libx264 -c:a aac -b:v 768k  -b:a 128k -vf "scale=720:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_mid
						-c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2"  -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_high
						-c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset superfast -crf 23 -f flv rtmp://localhost:1935/show/$name_hd720
						-c copy -f flv rtmp://localhost:1935/show/$name_src;
		}

		# This is the HLS application
		application show {
			live on; # Allows live input from above application
			deny play all; # disable consuming the stream from nginx as rtmp
			
			hls on; # Enable HTTP Live Streaming
			hls_fragment 3;
			hls_playlist_length 20;
			hls_path /mnt/hls/;  # hls fragments path
			# Instruct clients to adjust resolution according to bandwidth
			hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution
			hls_variant _hd720 BANDWIDTH=2048000; # High bitrate, HD 720p resolution
			hls_variant _high BANDWIDTH=1152000; # High bitrate, higher-than-SD resolution
			hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
			hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
			
			# MPEG-DASH
            dash on;
            dash_path /mnt/dash/;  # dash fragments path
			dash_fragment 3;
			dash_playlist_length 20;			
		}
	}
}

#创建configMap
kubectl create configmap nginx-rtmp-hls --from-file=nginx-rtmp.conf=./nginx-rtmp.conf --from-file=nginx-rtmp.conf=./nginx-rtmp.conf
#NFS存储模块
#pv-pvc-nfs.yml
#----------------创建pv---------------------
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-pv01              #创建的pv名称可创建多个.
  namespace: mt-math             #属于的命名空间
  labels:
    pv: pv-nfs-01                #定义pv标签,后续通过pvc绑定特定的pv标签。通常如果不写标签则默认通过访问方式和storage大小进行批量绑定。(重要)
spec:
  capacity:
    storage: 1Gi                 #创建的pv-nfs-pv01大小为1G
  accessModes:
  - ReadWriteMany
  nfs:                           #创建的pv数据来源
    path: /NFS/pv01              #数据源目录
    server: 192.168.0.14         #数据源ip

---

#-----------------创建pvc------------------
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-data-pvc             #创建的pvc名称
  namespace: mt-math             #属于的命名空间 
spec:
  accessModes:
    - ReadWriteMany                       
  resources:
    requests:
      storage: 100Gi             #请求大小为1G
  selector:                      #定义标签选择器,此时k8s会根据标签,storage,访问方式 进行匹配。三者同时满足才会绑定。如果不定义,则系统会根据storage的大小和访问方式进行匹配绑定.
    matchLabels:
      pv: pv-nfs-01              #定义请求标签为pv-nfs-pv01的pv且大小为1G
#nginx直播模块
#nginx-rtm-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-rtmp
  namespace: mt-math            #属于的命名空间
  labels:
    app: nginx-rtmp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-rtmp-pod
  template:
    metadata:
      labels:
        app: nginx-rtmp-pod
    spec:
      containers:
      - name: nginx-rtmp
        image: alqutami/rtmp-hls:latest-alpine
        imagePullPolicy: IfNotPresent
        ports:
          - name: http
            containerPort: 8080
          - name: rtmp
            containerPort: 1935
        volumeMounts:
        - name: nfs-media-data
          mountPath: /mnt/hls
        - name: nginx-config                #挂载数据节点名称
          mountPath: /etc/nginx/nginx.conf  #挂载此目录
          subPath: nginx.conf
    volumes:
    - name: nfs-media-data 
      persistentVolumeClaim:
       claimName: nfs-data-pvc #NFS的ip地址
    - name: nginx-config
      configMap:
       name: nginx-rtmp-hls       #指定创建configMap的名称
       items:
        - key:  nginx-rtmp.conf    #key为文件名称
          path: nginx-rtmp.conf    #文件路径内容
    #imagePullSecrets:
    #- name: xxx # harbor
    
#nginx HLS 点播模块
#nginx-hls-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-hls
  namespace: mt-math            #属于的命名空间
  labels:
    app: nginx-hls
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-hls-pod
  template:
    metadata:
      labels:
        app: nginx-hls-pod
    spec:
      containers:
      - name: nginx-hls
        image: alqutami/rtmp-hls:latest-alpine
        imagePullPolicy: IfNotPresent
        ports:
          - name: http
            containerPort: 8080
        volumeMounts:
        - name: nfs-media-data
          mountPath: /mnt/hls
        - name: nginx-config                #挂载数据节点名称
          mountPath: /etc/nginx/nginx.conf  #挂载此目录
          subPath: nginx.conf
    volumes:
    - name: nfs-media-data 
      persistentVolumeClaim:
       claimName: nfs-data-pvc #NFS的ip地址
    - name: nginx-config
      configMap:
       name: nginx-rtmp-hls         #指定创建configMap的名称
       items:
        - key:  nginx-hls.conf      #key为文件名称
          path: nginx-hls.conf      #文件路径内容
    #imagePullSecrets:
    #- name: xxx # harbor
    
#对外服务模式
#service.yml
apiVersion: v1
kind: Service
metadata:
  name: nginx-rtmp-service             
  namespace: mt-math             #属于的命名空间
spec:
  selector:
    app: nginx-rtmp              #针对标签为wyl-nginx的标签进行负载
  type: NodePort                 #正对Node节点进行端口暴露
  ports:                       
    - protocol: TCP              #使用端口的协议
      port: 1935                 #供内网访问暴露的端口
      targetPort: 1935           #目标pod的端口
      nodePort: 29995            #node节点暴露的端口
      
    #- protocol: HTTP             #使用端口的协议
    #  port: 8080                 #供内网访问暴露的端口
    #  targetPort: 8080           #目标pod的端口
    #  nodePort: 29995            #node节点暴露的端口
      
---

apiVersion: v1
kind: Service
metadata:
  name: nginx-hls-service             
  namespace: mt-math             #属于的命名空间
spec:
  selector:
    app: nginx-hls               #针对标签为wyl-nginx的标签进行负载
  type: NodePort                 #正对Node节点进行端口暴露
  ports:                       
    - protocol: TCP              #使用端口的协议
      port: 8080                 #供内网访问暴露的端口
      targetPort: 8080           #目标pod的端口
      nodePort: 29994            #node节点暴露的端口

该模式缺点

由于通过读取ts文件的方式进行流媒体播放,如果用于直播,则有一定的时间延时。当配置ts时间间隔为1时,延时最小,测试发现大概在5~10秒左右的时间,但是对于一般的直播用户都可以接受。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一款免费的流媒体直播软件,主要用于流媒体直播,视频直播,视频点播,视频录制等应用,与FMS、WOWZA、RED5一道,作为用户流媒体直播应用的可选方案。 1、支持Windows/Linux等多种操作系统; 2、采用开发的流媒体协议,而非私有协议,可以与主流系统对接,至少支持RTMP推流,RTMP拉流,RTSP拉流三种方式获取直播流,支持对获取的直播流再按照TS组播或者单播,RTMP,RTSP,HLS,TS OVER HTTP,FLV OVER HTTP等标准流媒体协议的方式进行对外直播转发; 3、支持录制,可定时录制或手动录制,能录制MP4或者FLV格式文件,录制时支持生成新文件或者追加文件两种方式,支持录制超过4G的文件; 4、单台服务器可支持1000个以上并发用户,直播延时小于2秒; 5、支持RTMP转发功能,能够把AMS上的直播流转发给其它AMS服务器或者第三方的RTMP服务器,例如RED5,FMS,WOWZA流媒体服务器; 6、 支持PC/手机/平板电脑等多终端访问,无论是windows,linux,或者andriod以及IOS系统访问收看直播时都无需下载插件,直接观看; 7、响应点播时间控制在100ms以内,支持暂停、拖动等特技操作; 8、支持文件直播,可把硬盘上存在的FLV文件编目后,按设定好的任务和顺序进行直播; 9、内置Web应用系统,无需复杂配置,可直接部署到用户服务器使用,也可以选配更复杂的媒资管理系统,完成类似优酷土豆的应用模式; 10、开放的系统架构,提供二次开发接口,可轻松的融入到用户已有的平台或网站平台中、或在此基础上进行二次开放等 ------------------------------------------------------------------------- 使用说明: 1、关于安装运行:软件解压到硬盘上后,执行AokuMServiceManager,即可完成AMS服务的安装,安装完成后,在Windows系统服务中会出现一个AokuMService服务; 2、关于端口:AMS默认的管理端口是9001,可通过浏览器登陆http://127.0.0.1:9001/main.html进行管理;AMS默认的RTMP端口是1935,默认的rtsp端口是5554,默认的flv over http端口是7000,默认的ts over http端口是8008,默认的hls端口也是9001; 3、关于推流:使用直播你需要有一个支持rtmp推流的编码器,建议用奥酷全接口高清编码器,当然你也可以通过FME进行软编码来实现; 4、关于接收直播:AMS正常启动并发布上直播流后,你可以在网络内的任一电脑上打开http://ip:9001,通过浏览器观看直播,若开启了hls功能,通过iphone或者ipad登陆http://ip:9001即可观看直播。 5、若开启了TS组播,请确认防火墙是否允许组播,另外您网络内的路由器是否支持组播,接收组播是需要用VLC播放器来完成; 6、接收flv over http也是通过VLC播放器或者支持类似协议的播放器来测试。 7、若使用中出现问题,你可以查看logs目录下的rtmpserver.log文件,里面有详细的错误日志描述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值