Closed
Description
K8S uses ConfigMap to store configuration files, for example:
cat <<EOF | kubectl apply -f -
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;
}
}
EOF
ConfigMap will be mounted as a volume and become a configuration file:
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: srs-deploy
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:3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1935
- containerPort: 1985
- containerPort: 8080
volumeMounts:
- name: config-volume
mountPath: /usr/local/srs/conf
EOF
You can view the running pods:
Mac:trunk chengli.ycl$ pod=`kubectl get po|grep srs-deploy|awk '{print $1}'`
Mac:trunk chengli.ycl$ kubectl exec $pod -- cat conf/srs.conf && echo ""
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;
}
}
When ConfigMap changes, you can use fsnotify to receive notifications of file changes, thereby triggering the reload of SRS.
TRANS_BY_GPT3
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
winlinvip commentedon Mar 12, 2020
The paragraph in Aliyun's SLS log service says link is as follows:
Another solution is to listen for changes in the configuration file. However, there is an issue where the fsnotify signal may not be received due to symbolic links: link
Here is an example of Nginx's reload, which is implemented using fsnotify: link
You can see that it also listens for changes in the file directory and then sends the SIGHUP (reload) signal to Nginx.
TRANS_BY_GPT3
winlinvip commentedon Mar 12, 2020
It is implemented using inotify under Linux.
Since inotify returns an fd and can be initialized as non-blocking mode using inotify_init1, reading it using read is an IO operation, so we should be able to use ST to read this fd.
TRANS_BY_GPT3
winlinvip commentedon Mar 12, 2020
SRS has added two configurations, automatically enabling auto reload under Docker, and disabling auto reload outside of Docker.
The added log prints are as follows:
You can see that there is an additional fd=10, a_inode(inotify):
TRANS_BY_GPT3
For #1635, inotify watch ConfigMap for reload. 3.0.130
winlinvip commentedon Mar 12, 2020
If the file is deleted or moved, and then created again (as part of the ConfigMap update logic).
When a file is deleted, including the target of symbolic links being moved or deleted:
When a file is created, including the creation of the target of symbolic links:
TRANS_BY_GPT3
For #1635, inotify watch ConfigMap for reload. 3.0.131
winlinvip commentedon Mar 12, 2020
K8S will have a ..data subdirectory:
All events of K8S are as follows:
The key events are as follows, it appears that K8S executed a command similar to
ln -sf ..2020_03_12_14_17_54.925365984 ..data_tmp && mv ..data_tmp ..data
:inotify
should listen to theconf
directory.conf
directory,IN_MODIFY | IN_CREATE | IN_MOVED_TO
, so that the creation of the..data
subdirectory can also be detected.After receiving the event, it is necessary to determine the file name. Only if it is
srs.conf
or..data
, it is considered a reload, and others are ignored.TRANS_BY_GPT3
For #1635, inotify watch ConfigMap for reload. 3.0.132
For #1635, inotify watch ConfigMap for reload. 3.0.133
For #1635, inotify watch ConfigMap for reload. 3.0.134
For #1635, refine inotify watch for relative path
For #1635, refine inotify watch for relative path