In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use the Exec Websocket interface to copy files between Pod in K8S. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Demand
Think about how to solve the following problems?
Created a new Pod, want to copy the files in another Pod to the new Pod for analysis, how to achieve it?
How to copy files between Pod like kubectl cp copy files in a project?
The new Pod shares pvc with the instance Pod? Or encapsulate a kubectl execution command line with authentication context?
Brief introduction
Process description
First, the signal channel is initialized, which is used for signal notification between co-programs, and the co-programs that receive the signal perform operations such as pausing / exiting the loop / closing the channel.
Initialize the data channel srcStdOutCh, of type byte array [] byte, which is used to put the standard output of the source Pod into the channel from which the data sent to the standard input of the destination Pod is read
Access address of splicing exec interface (cluster connection, token), tar compression command, standard input / output, tty, pod name, container name and other parameters. Tar czf-/ var/log/xxx.log means to compress the file tree structure into a data stream
Call the Dialer method of websocket to establish a websocket connection with the source Pod container, and open the cooperation program to write standard output to the data channel srcStdOutCh.
Refer to the source pod exec interface to splice the destination Pod exec access connection. Tar xzf-C / tmp means to read the data stream from the standard input and decompress it into a file tree structure (note: include the file directory tree structure after decompression)
Establish a wss connection with the destination Pod, open the cooperative program to read the standard output of the source Pod from the data channel srcStdOutCh, and write to the standard input of the destination Pod. If the read timeout from the data channel indicates that the data has been transferred, the input of data to the destination container is stopped, and a notification signal is sent to inform the master program that the wss connection of the source Pod can be exited.
Matters needing attention
When wesocket is connected to the source Pod, the standard output will output null data, tar command output and other interference data, so when receiving the data, you need to pass in a filter callback function for data filtering.
When sending data to the destination container, the first byte received by the source container needs to be deleted, usually 1, indicating the standard output ID. This byte is not required to send it to the destination container.
When sending data, you need to set the first byte to 0, indicating that it is sent to standard input
Reference code
Cp.go
/ * Summary: 1. Unbuffered channels need to read before writing. The 2.websocket ReadMessage method blocks the read. If you want to interrupt the read, close the connection. Catch the error * / package cpFilePod2Pod import ("crypto/tls"errors"fmt"log"net/url"regexp"strings"sync"time"github.com/gorilla/websocket") / / define the filter callback function type filterCallback func (input string) bool / / Websocket connection object type WsConn struct {Conn * websocket with mutex .Conn mu sync.Mutex} / / send string Automatically add the newline character func (self * WsConn) Send (sender string, str string) {self.mu.Lock () defer self.mu.Unlock () / / when sending data using the k8s exec websocket interface, the first byte needs to be set to 0, indicating that the data is sent to the standard input data: = [] byte {0} data = append (data, [] byte (str+ "\ n").) Err: = self.Conn.WriteMessage (websocket.BinaryMessage, data) / / send binary data type if err! = nil {log.Printf ("send error,% s", err.Error ())} log.Printf ("% s, data:% s, bytes:% + v", sender, str, [] byte (str+ "\ n"))} / / send string, no newline character is added, byte filtering is done internally Wait for operation func (self * WsConn) SendWithFilter (sender string, str string) {self.mu.Lock () defer self.mu.Unlock () / / log.Printf ("send data to destination container:% s", str) str = strings.ReplaceAll (str, "\ r\ n", "\ n") / / rq13, / nq10, windows newline to Linux newline / / remove the first byte (standard output 1) Byte: [0 1...]), because standard output 1 is identified by the first bit of the output from the source container, and the flag needs to be removed when sending bytes to the destination container / / when WebSocket establishes a connection When sending data, the first byte of Buffer needs to be set to stdin (buf [0] = 0), while when accepting data, you need to judge the first byte, stdout (buf [0] = 1) or stderr (buf [0] = 2) strByte: = append ([] byte (str) [: 0], [] byte (str) [1:]...) Data: = [] byte {0} data = append (data, strByte...) Err: = self.Conn.WriteMessage (websocket.BinaryMessage, data) log.Printf ("send data to destination container standard input:\ n% s, bytes:% d, bytes:% + v", string (data), len (data), data) if err! = nil {log.Printf ("send error,% s", err.Error ()}} / / get the data stream from the connection and write it to the byte array channel Internal execution filter (callback function) func (self * WsConn) Receive (receiver string, ch chan [] byte, filter filterCallback) error {self.mu.Lock () defer self.mu.Unlock () msgType, msgByte, err: = self.Conn.ReadMessage () / / blocking read, type 2 for binary data, 1 for text,-1 for connection closed: websocket: close 1000 (normal) log.Printf ("% s, read data:% s Type:% d, number of bytes:% d, bytes:% + v ", receiver, string (msgByte), msgType, len (msgByte), msgByte) if err! = nil {log.Printf ("% s, read error,% s ", receiver, err.Error ()) return err} if filter (string (msgByte)) & & len (msgByte) > 1 {ch
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.