Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply WebSocket in containerized Management platform

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to apply WebSocket in containerization management platform. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Introduction

Websocket is a persistence protocol, as opposed to a non-persistent protocol such as HTTP. It realizes full-duplex communication between browser and server, which can better save server resources and bandwidth and achieve the purpose of real-time communication. Like HTTP, it transmits data based on the established TCP connection. But what is different from the HTTP protocol is that WebSocket is a two-way communication protocol. After establishing a connection, both the WebSocket server and the client can actively send or receive data to each other, just like Socket; like TCP, WebSocket needs to establish a connection before it can communicate with each other.

Operation mechanism

In the traditional HTTP client and server request response model, there are only one or more requests and one or more corresponding responses in a life cycle. As follows:

In the response mode between client and server in WebSocket mode, once the WebSocket connection is established, the data is transmitted in frame sequence before the client or server interrupts the connection, and there is no need to restart the client or server to re-initiate the connection request.

The client initiates a request

Data transfer after Request Method:GETStatus Code:101 Switching Protocols # 101adopts Websocket protocol Connection:UpgradeSec-WebSocket-Extensions:permessage-deflate;Sec-WebSocket-Key:dfQAy9mlQ0EjD5MmvvsZaQ== # verifies that Sec-WebSocket-Version:13Upgrade:websocket # client supports websocket protocol and uses WebSocket protocol if the server also supports it

The server parses and returns the handshake information to establish a connection

Connection:upgradesec-websocket-accept:+mKC4huknIfMGJFOzclP9fO058g= # verify upgrade:websocket

Transfer data (bidirectional)

Disconnect

Application scenario

WebSocket is a real-time push technology in the field of Web, which aims to enable users to update their browsers in real time without refreshing their browsers. There are mainly two types of application scenarios in the containerized management platform, one is the real-time message class, which pushes the real-time log, business monitoring and other related information to the client. For example, if an application is deployed in the containerized management platform, how can the log, operation log and monitoring information of the application deployment be pushed to the front end in time? the other is the interactive message class, which operates the container in the Web interface to save time for manually logging in to the server to enter the container and improve efficiency.

Example 1: real-time messages

Take the cloud gang as an example, what needs to be pushed in real time is the log information and business monitoring data of each application. So when we deal with real-time messages, we use the combination of WebSocket and ZMQ to quickly push the application log information to the web. The simplified model is as follows: the client and server use WebSocket protocol to transmit relevant data in real time. ZMQ is used in the relevant data. Cloud gang mainly uses the Publisher-Subscriber model in the ZeroMQ message queue to summarize all the relevant information on one node and push it to other nodes. That is, the client (SUB) subscribes the message to the server (PUB), and the server pushes the message to all subscribed messages to the client, similar to a broadcast. The reason for adopting ZMQ instead of Socket is that the former supports the connection of N M, while the latter can only connect at 1:1.

Parameters related to specific application scenarios:

# docker-compose.ymlmpush: container_name: mpush environment:-ZMQ_SUB_FROM=tcp://ip:9242 image: hub.goodrain.com/dc-deploy/mpush log_driver: json-file log_opt: max-size: 50m ports:-0.0.0.0 URLWEBSOCKET_URL: always#config.py definition real-time message URLWEBSOCKET_URL = {'cloudbang':' ws://ip:6060/websocket' } # dalaran_dockerZMQ_BIND_SUB=tcp://0.0.0.0:9241ZMQ_BIND_PUB=tcp://0.0.0.0:9242ZMQ_BIND_PUB2=tcp://0.0.0.0:9243#setting'docker_sub': {'address': [' tcp://ip:9242'], 'storage':' / data/docker_logs'}, example 2: interactive message

GoTTY is a tool developed in GE language, which can share the command character terminals of the operating system into ordinary web applications. In mac installation, choose one depending on the situation.

# brewbrew tap yudai/gottybrew install gotty#gogo get github.com/yudai/gotty

Use:

Gotty-a 0.0.0.0-p '808080'-w tmux

So in the cloud gang, how do we solve this problem? The cloud help web operation container is a WebSocket shared character terminal customized based on gotty. Create a websocket instance and connect to the server for foreground and background interaction and related event handling

# frontend implementation code var openWs = function () {if (is_community = = "True") {host_name = _ window.location.hostname+ ": 8188";} var url = tmp_url.replace ("{{DOCKER_WSS_URL}}", host_name) console.log (url) var ws = new WebSocket (url); var term; var pingTimer Ws.onopen = function (event) {ws.send (JSON.stringify ({T_id: t_id, S_id: s_id, C_id: c_id, Md5: md5,}); pingTimer = setInterval (sendPing, 30 * 1000, ws); hterm.defaultStorage = new lib.Storage.Local (); hterm.defaultStorage.clear (); term = new hterm.Terminal () Term.getPrefs (). Set ("send-encoding", "raw"); term.onTerminalReady = function () {var io = term.io.push (); io.onVTKeystroke = function (str) {ws.send ("0" + str);}; io.sendString = io.onVTKeystroke Io.onTerminalResize = function (columns, rows) {ws.send ("2" + JSON.stringify ({columns: columns, rows: rows) })} Term.installKeyboard ();}; term.decorate (document.getElementById ("terminal"));}

The back-end program is developed based on Gotty.

On how to carry out WebSocket in the containerized management platform to share the application here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report