kuberneteslog流式數(shù)據(jù)處理

PS: 最近在重構公司的業(yè)務容器化平臺,記錄一塊。關于容器日志的, kubernetes python API本身提供了日志流式數(shù)據(jù),在以前的版本是不會輸出新數(shù)據(jù)的,后續(xù)版本進行了改進。

創(chuàng)新互聯(lián)公司是一家集網站建設,鼓樓企業(yè)網站建設,鼓樓品牌網站建設,網站定制,鼓樓網站建設報價,網絡營銷,網絡優(yōu)化,鼓樓網站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網站。

直接上代碼

Flask 前端路由塊
# Router
"""獲取項目pod的日志"""
@api_cluster_pod.route('/<env>/<cluster_name>/pod/<pod_name>/log')
@env_rules
def api_cluster_pod_log(env, cluster_name, pod_name):
    """查看pod的log"""

    tail_lines = request.values.get("tail_lines", 1000)
    namespace = request.values.get("namespace", "")

    # 生成Config Object
    try:
        cluster_config = ClusterConfig(
            env=env,
            cluster_name=cluster_name,
            namespace=namespace
        )
    except Exception as e:
        return jsonify(dict(
            code=5000,
            message='獲取集群接口時未找到對應條目, 信息:{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 后端代碼塊
# 后臺功能
class Pod:
    ...
       def get_pod_log(self, pod_name, tail_lines=100):
        """
        獲取pod的日志
        :param tail_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("獲取日志時,未找到此pod: {0}".format(pod_name))
            if e.status == 400:
                raise PodNotFund("容器并未創(chuàng)建成功,請聯(lián)系運維人員進行排查。")
            raise e
        except Exception as e:
            logger.exception("Get Log Fail: {0}".format(str(e)))
            raise e
HTML
<!DOCTYPE>
<html>
<head>
    <title>Flushed ajax test</title>
    <meta charset="UTF-8" />
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>

    <style>
        #log-container {
            height: 800px;
            /*width: 800px;*/
            overflow-x: scroll;
            padding: 10px;
        }
        .logs {
            background-color: black;
            color: aliceblue;
            font-size: 18px;
        }
    </style>
</head>
<body>
<div id="log-container">
    <pre class="logs">
    </pre>
</div>

<script type="text/javascript">
    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);
                // 接收服務端的實時日志并添加到HTML頁面中
                $("#log-container pre").append(this_response);
                // 滾動條滾動到最低部
                $("#log-container").scrollTop($("#log-container pre").height() - $("#log-container").height() + 10);
            }
        }
    })
        .done(function(data)
        {

            console.log('Complete response = ' + data);
        })
        .fail(function(data)
        {
            console.log('Error: ', data);
        });
    console.log('Request Sent');
</script>
</body>
</html>

其它

我們應用是前后端分離的,把html里面的核心代碼放置VUE里面就可以了。

效果圖

日志是流式的,如果Container有日志,則窗口會運態(tài)更新。
kubernetes log 流式數(shù)據(jù)處理

當前題目:kuberneteslog流式數(shù)據(jù)處理
分享URL:http://muchs.cn/article18/ppjsdp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、品牌網站建設、電子商務網站排名、App設計網頁設計公司

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化