k8s部署srs服务
- 项目需要把srs纳入k8s进行管理,需要通过k8s来部署srs服务
- 然后原本的srs可以支持rtmp与webrtc两种,官网查了部署方式,k8s只有最基本的部署方式
- 于是开始研究k8s部署能够正常推拉流的webrtc版本的srs
-
首先肯定是去官网查有无相关资料
https://ossrs.net/lts/zh-cn/docs/v5/doc/getting-started-k8s
-
选择通用k8s的配置,点开srs.yaml的配置
apiVersion: v1
kind: ConfigMap
metadata:
name: srs-config
data:
srs.conf: |-
listen 1935;
max_connections 1000;
daemon off;
http_api {
enabled on;
listen 1985;
}
http_server {
enabled on;
listen 8080;
}
vhost __defaultVhost__ {
http_remux {
enabled on;
}
hls {
enabled on;
hls_dispose 30;
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: srs-deployment
labels:
app: srs
spec:
replicas: 1
selector:
matchLabels:
app: srs
template:
metadata:
labels:
app: srs
spec:
volumes:
- name: config-volume
configMap:
name: srs-config
containers:
- name: srs
image: ossrs/srs:4
ports:
- containerPort: 1935
- containerPort: 1985
- containerPort: 8080
volumeMounts:
- name: config-volume
mountPath: /usr/local/srs/conf
---
apiVersion: v1
kind: Service
metadata:
name: srs-service
spec:
type: LoadBalancer
selector:
app: srs
ports:
- name: srs-service-1935-1935
port: 1935
protocol: TCP
targetPort: 1935
- name: srs-service-1985-1985
port: 1985
protocol: TCP
targetPort: 1985
- name: srs-service-8080-8080
port: 8080
protocol: TCP
targetPort: 8080
- 可以看到包含三个基础配置ConfigMap,srs-deployment,srs-service
- 但如果只是使用rtmp传输,自然是毫无问题,但是如果还需要使用webrtc功能,那只开这些端口是不够的,因为webrtc还需要有8000的udp端口,然后还有webrtc需要的https,配置文件也需要改成webrtc的
端口需要增加,版本也切换为srs5.0
spec:
containers:
- image: 'ossrs/srs:5'
imagePullPolicy: IfNotPresent
name: srs
ports:
- containerPort: 1935
protocol: TCP
- containerPort: 1985
protocol: TCP
- containerPort: 8080
protocol: TCP
- containerPort: 8088
protocol: TCP
- containerPort: 8000
protocol: UDP
- containerPort: 1990
protocol: TCP
然后service的端口也需要增加,然后再按照对外映射的端口来连通就可以了
ports:
- name: srs-service-1935-1935
nodePort: 31935
port: 1935
protocol: TCP
targetPort: 1935
- name: srs-service-1985-1985
nodePort: 31985
port: 1985
protocol: TCP
targetPort: 1985
- name: srs-service-8080-8080
nodePort: 31080
port: 8080
protocol: TCP
targetPort: 8080
- name: srs-service-1990-1990
nodePort: 31990
port: 1990
protocol: TCP
targetPort: 1990
- name: srs-service-8000-8000
nodePort: 31800
port: 8000
protocol: UDP
targetPort: 8000
- name: srs-service-8088-8088
nodePort: 31808
port: 8088
protocol: TCP
targetPort: 8088
selector:
app: srs
sessionAffinity: None
type: LoadBalancer
-
启动完毕后,就可以通过31808端口打开页面
-
贴一下yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: srs-deployment
namespace: wzh
labels:
app: srs
spec:
replicas: 1
selector:
matchLabels:
app: srs
template:
metadata:
labels:
app: srs
spec:
volumes:
- name: config-volume
configMap:
name: srs-config
- name: ssl-volume
persistentVolumeClaim:
claimName: ssl-pvc
containers:
- name: srs
image: ossrs/srs:5
ports:
- containerPort: 1935
- containerPort: 1985
- containerPort: 8080
- containerPort: 8088
- containerPort: 8000
protocol: UDP
- containerPort: 1990
volumeMounts:
- name: config-volume
mountPath: /usr/local/srs/conf
- name: ssl-volume
mountPath: /home
nodeSelector:
wzh: master
apiVersion: v1
kind: Service
metadata:
name: srs-service
namespace: wzh
spec:
type: LoadBalancer
selector:
app: srs
ports:
- name: srs-service-1935-1935
nodePort: 31935
port: 1935
protocol: TCP
targetPort: 1935
- name: srs-service-1985-1985
nodePort: 31985
port: 1985
protocol: TCP
targetPort: 1985
- name: srs-service-8080-8080
nodePort: 31080
port: 8080
protocol: TCP
targetPort: 8080
- name: srs-service-1990-1990
nodePort: 31990
port: 1990
protocol: TCP
targetPort: 1990
- name: srs-service-8000-8000
nodePort: 31800
port: 8000
protocol: UDP
targetPort: 8000
- name: srs-service-8088-8088
nodePort: 31808
port: 8088
protocol: TCP
targetPort: 8088
- 还有config的配置文件
listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;
http_api {
enabled on;
listen 1985;
https {
enabled on;
listen 1990;
key /home/server.key;
cert /home/server.crt;
}
}
stats {
network 0;
}
http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
https {
enabled on;
listen 8088;
key /home/server.key;
cert /home/server.crt;
}
}
rtc_server {
enabled on;
listen 8000; # UDP port
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate 192.168.30.7:31800;
}
vhost __defaultVhost__ {
hls {
enabled on;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
rtc {
enabled on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp on;
}
}