Get the App
SLTechnology News&Howtos  ›  Servers  › 

Kubernetes log streaming data processing

Shulou Source: shulou.com Published: 2022-06-03 08:02:59 09月21日 Update

PS: recently refactoring the company's business containerization platform, recording a piece. With regard to container logs, kubernetes python API itself provides log streaming data, which will not output new data in previous versions, which has been improved in subsequent versions.

Directly go to the code Flask front-end route block # Router "get the project pod log" @ api_cluster_pod.route ('/ pod//log') @ env_rulesdef api_cluster_pod_log (env, cluster_name, pod_name): "" View pod's log "" tail_lines = request.values.get ("tail_lines", 1000) namespace = request.values.get ("namespace") "") # generate Config Object try: cluster_config = ClusterConfig (env=env, cluster_name=cluster_name, namespace=namespace) except Exception as e: return jsonify (dict (code=5000, message=') did not find the corresponding entry when obtaining the cluster interface Information: {0} '.format (str (e) try: poder = Pod (cluster_config) resp = Response (stream_with_context (poder.get_pod_log (pod_name, tail_lines)), mimetype= "text/plain") return resp except Exception as e: return jsonify (dict (code=7000) Message=str (e)) Flask backend code block # background function class Pod:. Def get_pod_log (self, pod_name, tail_lines=100): "get the log of pod: param tail_lines: # shows the last number of lines: return:" try: # stream pod log streams = self.cluster.api.read_namespaced_pod_log (pod_name Self.cluster_config.namespace, follow=True, _ preload_content=False Tail_lines=tail_lines) .stream () return streams except ApiException as e: if e.status = = 404: logger.exception ("Get Log not fund Podname: {0}" .format (pod_name)) raise PodNotFund ("when getting logs This pod: {0} ".format (pod_name)) if e.status = = 400: raise PodNotFund was not found (" the container has not been created successfully, please contact the operator for troubleshooting. ") Raise e except Exception as e: logger.exception ("Get Log Fail: {0}" .format (str (e)) raise eHTML Flushed ajax test # log-container {height: 800px; / * width: 800pxdomestic / overflow-x: scroll; padding: 10px } .logs {background-color: black; color: aliceblue; font-size: 18px;} var last_response_len = false; var logs = $("# log-container") $.ajax ('http://localhost/api/pre/ops-test/pod/ops-test-1211763235-jfbst/log?tail_lines=100', {xhrFields: {onprogress: function (e) {var this_response, response = e.currentTarget.response; if (last_response_len = false) {this_response = response) Last_response_len = response.length;} else {this_response = response.substring (last_response_len); last_response_len = response.length;} / / console.log (this_response) / / receive the real-time log from the server and add it to the HTML page $("# log-container pre") .append (this_response); / / scroll to the lowest $("# log-container") .scrollTop ($("# log-container pre"). Height ()-$("# log-container") .append () + 10) ) .done (function (data) {console.log ('Complete response =' + data);}) .fail (function (data) {console.log ('Error:', data);}); console.log ('Request Sent'); other

Our application is separated from the front and back ends, so just put the core code in html into VUE.

Effect picture

The log is streaming, and if Container has a log, the window will be updated.

Tags: Log code container data version minimum success business personnel information company front end function background real-time platform interface effect effect diagram entry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Xiaomi vpn Shulou Technology Huawei