grpc系列2-针对k8s vip超时设置,自定义服务端和客户端镜像和配置ipvs超时时间

202 篇文章 14 订阅
97 篇文章 1 订阅

问题

https://github.com/PaddlePaddle/Serving/issues/1829

参考

直接看第一手资料

docker指令官网:https://docs.docker.com/engine/reference/commandline/docker/

在Docker中运行特权容器很危险:https://www.4hou.com/posts/4YP2

方案介绍

默认情况下容器中的root用户只是host主机的一个普通用户,但如果docker run --privileged=true 就真正的给这个普通用户赋予了和host主机root用户的特权。

docker --privileged=true 参数作用
大约在0.6版,privileged被引入docker。
使用该参数,container内的root拥有真正的root权限。
否则,container内的root只是外部的一个普通用户权限。
privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。
甚至允许你在docker容器中启动docker容器。

docker run --name mysql-test --privileged=true -e MYSQL_ROOT_PASSWORD=123456 -p 9092:3306  -d daocloud.io/library/mysql:5.7.6

成功运行命令

查询配置

cat /etc/sysctl.conf

构建镜像知识步骤

1:在本地登录到远程仓库:

docker login --username=[用户名] -p=[密码]  远程ip:端口

2:给本地镜像打标签, [远程ip:端口/自定义路径/*]:[版本号] 就是:[标签名:版本号]

docker tag [镜像id] [远程ip:端口/自定义路径/*]:[版本号]

3:推送

docker push [远程ip:端口/自定义路径/*]:[版本号]
nvidia-docker build -t image-server-milvus-grpc:1.0.0.0730 .

#################解决方案1,系统上缩小本身tcp的超时时间

2 服务端实际流程


    nvidia-docker build -t image-server-milvus-grpc:1.0.0.0730 .

    nvidia-docker run -it --privileged=true --entrypoint=/bin/bash image-server-milvus-grpc:1.0.0.0730

	cat /etc/sysctl.conf
	
    echo 'net.ipv4.tcp_keepalive_time=600'  >> /etc/sysctl.conf
    echo 'net.ipv4.tcp_keepalive_intvl=30'  >> /etc/sysctl.conf
    echo 'net.ipv4.tcp_keepalive_probes=9' >> /etc/sysctl.conf
    sysctl -p

    docker commit 4f4af16d3eaa  image-server-milvus-grpc-new:1.0.0.0730

    docker tag image-server-milvus-grpc-new:1.0.0.0730 xx/image-server-milvus-grpc-new:1.0.0.0730   

    docker login -u xx -p xx xx.net

    docker push xx/image-server-milvus-grpc-new:1.0.0.0730


3 客户端实际流程

    cd /data/mart/12345/deploy/docker_server

    nvidia-docker build -t image-client-milvus-grpc:1.0.0.0730 .

    nvidia-docker run -it --privileged=true --entrypoint=/bin/bash image-client-milvus-grpc:1.0.0.0730

	cat /etc/sysctl.conf

    echo 'net.ipv4.tcp_keepalive_time=600'  >> /etc/sysctl.conf
    echo 'net.ipv4.tcp_keepalive_intvl=30'  >> /etc/sysctl.conf
    echo 'net.ipv4.tcp_keepalive_probes=9' >> /etc/sysctl.conf
    sysctl -p

    docker commit ad37fc5edb9c image-client-milvus-grpc-new:1.0.0.0730

    docker tag image-client-milvus-grpc-new:1.0.0.0730 xx:1.0.0.0730   

    docker login -u xx -p xx xx

    docker push xx/image-client-milvus-grpc-new:1.0.0.0730

3.1 是是否配置成功,必须用以下指令查看

sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.tcp_keepalive_intvl

4 继承自建镜像重新构建镜像,解决超时vip主动中断的问题

服务端(特别注意,经验证服务端不需要更改配置,否则会导致服务不可用)

FROM xx/image-server-milvus-grpc-new:1.0.0.0730


COPY . /deploy
WORKDIR /deploy

RUN pip3.7 install -r requirements.txt \
    && python3.7 paddle_model.py

ENTRYPOINT python3.7 web_service.py

客户端

FROM xx/image-client-milvus-grpc-new:1.0.0.0730

COPY . /deploy
WORKDIR /deploy

RUN pip3.7 install -r requirements.txt

ENTRYPOINT ["gunicorn", "-c", "gunicorn_cfg.py", "main_api:app"]

5 继承自建镜像报错,解决方案

解决方案参考:https://blog.csdn.net/qq_15821487/article/details/125885828?spm=1001.2014.3001.5502

6 以上方案存在问题

1、容器重启后又自动恢复,不好维护
2、开通了root权限,暴露了端口风险
3、服务端无需也不能配置,否则导致服务不可用

#################解决方案2,系统上放大k8s,ipvs tcp超时时间

7900 > 7200 + 9 *75
这个是linux物理机系统自带的超时配置,ipv4是网络通信协议

sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.tcp_keepalive_intvl

net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

这个是linux物理机上安装k8s组件后,配置组件虚拟机的超时时间配置

ipvsadm -l --timeout

Timeout (tcp tcpfin udp): 900 120 300

ipvsadm --set 7900 120 300
ipvsadm -l --timeout

Timeout (tcp tcpfin udp): 7900 120 300

以上三个值依次为TCP会话的超时时间,在接收到FIN报文后的TCP会话超时时间,以及UDP报文的超时时间。
当其中某个值为0时,表示不设置此值,其将保留上一次设置的数值。
需要注意的是,这三个timeout值是针对ipvs网络命名空间的全局数值,而不只是针对某个虚拟服务。

1 以上方案存在问题

统一全局更改了相关的配置,可能会影响整体系统的稳定性和其他应用

#################解决方案3,通过底层软件配置,缩小本身tcp的超时时间

grpc配置文档:https://grpc.github.io/grpc/python/grpc_asyncio.html#create-channel

grpc,channel_arguments配置参数列表:https://github.com/grpc/grpc/blob/v1.37.1/include/grpc/impl/codegen/grpc_types.hh

3.0、搜索关键词keepalive

milvus也是rpc的架构,底层都会去统一设置时间
在这里插入图片描述

在这里插入图片描述

3.1、更改虚拟环境对应配置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.2、更改成如下配置

/** After a duration of this time the client/server pings its peer to see if the
    transport is still alive. Int valued, milliseconds. */
#define GRPC_ARG_KEEPALIVE_TIME_MS "grpc.keepalive_time_ms"
/** Is it permissible to send keepalive pings without any outstanding streams.
    Int valued, 0(false)/1(true). */
#define GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS \
  "grpc.keepalive_permit_without_calls"
/** How many pings can we send before needing to send a
   data/header frame? (0 indicates that an infinite number of
   pings can be sent without sending a data frame or header frame) */
#define GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA \
  "grpc.http2.max_pings_without_data"

在这里插入图片描述

设置时间<900-9x75=225,可以设置为200s=200000ms
在这里插入图片描述

3.3、更改成如下配置、更改配置后拷贝到docker

查看linux的python安装包的位置

root@4e8ec42345a0:/# pip3.7 show paddle-serving-client
Name: paddle-serving-client
Version: 0.8.3
Summary: Paddle Serving Package for saved model with PaddlePaddle
Home-page: https://github.com/PaddlePaddle/Serving
Author: PaddlePaddle Author
Author-email: guru4elephant@gmail.com
License: Apache 2.0
Location: /usr/local/lib/python3.7/site-packages
Requires: grpcio, grpcio-tools, numpy, protobuf, requests, six
Required-by: 

root@4e8ec42345a0:/# pip3.7 show paddle-serving-server-gpu
Name: paddle-serving-server-gpu
Version: 0.8.3.post101
Summary: Paddle Serving Package for saved model with PaddlePaddle
Home-page: https://github.com/PaddlePaddle/Serving
Author: PaddlePaddle Author
Author-email: guru4elephant@gmail.com
License: Apache 2.0
Location: /usr/local/lib/python3.7/site-packages
Requires: click, flask, func-timeout, grpcio, grpcio-tools, itsdangerous, Jinja2, MarkupSafe, protobuf, pytest, pyyaml, six, Werkzeug
Required-by: 

3.3.1、服务端基础镜像配置

    cd /data/mart/basic-image/image-server-milvus-grpc

    nvidia-docker build -t image-server-milvus-grpc-code:1.0.0.0730 .

    nvidia-docker run -it --entrypoint=/bin/bash image-server-milvus-grpc-code:1.0.0.0730

    docker cp /data/mart/basic-image/paddle_serving_server/pipeline/pipeline_client.py 4e8ec42345a0:/usr/local/lib/python3.7/site-packages/paddle_serving_server/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_server/pipeline/pipeline_server.py 4e8ec42345a0:/usr/local/lib/python3.7/site-packages/paddle_serving_server/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_client/pipeline/pipeline_client.py 4e8ec42345a0:/usr/local/lib/python3.7/site-packages/paddle_serving_client/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_client/pipeline/pipeline_server.py 4e8ec42345a0:/usr/local/lib/python3.7/site-packages/paddle_serving_client/pipeline/

    docker commit 4e8ec42345a0 image-server-milvus-grpc-code-new:1.0.0.0730
    
    docker tag image-server-milvus-grpc-code-new:1.0.0.0730 xx/image-server-milvus-grpc-code-new:1.0.0.0730   

    docker login -u xx -p xx xx

    docker push xx/image-server-milvus-grpc-code-new:1.0.0.0730

3.3.2、客户端基础镜像配置

    cd /data/mart/basic-image/image-client-milvus-grpc

    nvidia-docker build -t image-client-milvus-grpc-code:1.0.0.0730 .

    nvidia-docker run -it --entrypoint=/bin/bash image-client-milvus-grpc-code:1.0.0.0730

    docker cp /data/mart/basic-image/paddle_serving_server/pipeline/pipeline_client.py 76a9dcb6adeb:/usr/local/lib/python3.7/site-packages/paddle_serving_server/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_server/pipeline/pipeline_server.py 76a9dcb6adeb:/usr/local/lib/python3.7/site-packages/paddle_serving_server/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_client/pipeline/pipeline_client.py 76a9dcb6adeb:/usr/local/lib/python3.7/site-packages/paddle_serving_client/pipeline/

    docker cp /data/mart/basic-image/paddle_serving_client/pipeline/pipeline_server.py 76a9dcb6adeb:/usr/local/lib/python3.7/site-packages/paddle_serving_client/pipeline/

    docker commit 76a9dcb6adeb image-client-milvus-grpc-code-new:1.0.0.0730

    docker tag image-client-milvus-grpc-code-new:1.0.0.0730 xx/image-client-milvus-grpc-code-new:1.0.0.0730   

    docker login -u xx -p xx xx

    docker push xx/image-client-milvus-grpc-code-new:1.0.0.0730


3.3.3、以定制grpc超时时间配置作为基础镜像,重新打镜像部署

备注 ipvs介绍

ipvs 指令介绍:https://blog.51cto.com/u_10978134/2122118

pvsadm是LVS在应用层的管理命令,我们可以通过这个命令去管理LVS的配置。需要使用yum单独安装。
ipvsadm是一个工具,同时它也是一条命令,用于管理LVS的策略规则。
ipvs称之为IP虚拟服务器(IP Virtual Server,简写为IPVS)。是运行在LVS下的提供负载平衡功能的一种技
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。

如何在 kubernetes 中开启 ipvs 模式:https://blog.51cto.com/u_15077560/2585344

ipvs (IP Virtual Server) 实现了传输层负载均衡,也就是我们常说的4层 LAN交换,作为 Linux 内核的一部分。 ipvs运行在主机上,在真实服务器集群前充当负载均衡器。 ipvs可以将基于 TCP和 UDP的服务请求转发到真实服务器上,并使真实服务器的服务在单个 IP 地址上显示为虚拟服务。我们知道 kube-proxy支持 iptables 和 ipvs 两种模式,

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用CMake利用gRPC库生成一个通信案例(包含服务端客户端)的步骤: 1. 安装gRPC库 首先需要在本地安装gRPC库,可以参考官方文档进行安装。 2. 创建CMake项目 使用CMake创建一个新的项目,并在项目中添加以下内容: a. CMakeLists.txt文件 在项目根目录下创建一个CMakeLists.txt文件,内容如下: ```cmake cmake_minimum_required(VERSION 3.10) project(grpc_example) set(CMAKE_CXX_STANDARD 11) # Find gRPC find_package(gRPC REQUIRED) # Find Protobuf find_package(Protobuf REQUIRED) # Include generated *.pb.h files include_directories(${CMAKE_CURRENT_BINARY_DIR}) # Generate *.pb.cc and *.pb.h files protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS proto/example.proto ) # Define server source files set(SERVER_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/server.cpp ${PROTO_SRCS} ) # Define client source files set(CLIENT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/client.cpp ${PROTO_SRCS} ) # Define server executable add_executable(server ${SERVER_SOURCES}) target_link_libraries(server gRPC::grpc++ gRPC::grpc++_reflection protobuf::libprotobuf) # Define client executable add_executable(client ${CLIENT_SOURCES}) target_link_libraries(client gRPC::grpc++ gRPC::grpc++_reflection protobuf::libprotobuf) ``` b. example.proto文件 在项目的proto目录下创建一个example.proto文件,内容如下: ```protobuf syntax = "proto3"; package example; // Define request message message Request { string message = 1; } // Define response message message Response { string message = 1; } // Define service service ExampleService { rpc SendMessage(Request) returns (Response) {} } ``` c. server.cpp文件和client.cpp文件 在项目的src目录下分别创建一个server.cpp文件和client.cpp文件,内容如下: server.cpp: ```cpp #include <iostream> #include <memory> #include <string> #include <grpcpp/grpcpp.h> #include "example.grpc.pb.h" using grpc::Server; using grpc::ServerBuilder; using grpc::ServerContext; using grpc::Status; using example::Request; using example::Response; using example::ExampleService; // Server implementation class ExampleServiceImpl final : public ExampleService::Service { public: Status SendMessage(ServerContext* context, const Request* request, Response* response) override { std::string message = "Server received: " + request->message(); std::cout << message << std::endl; response->set_message(message); return Status::OK; } }; // Main function int main() { std::string server_address("0.0.0.0:50051"); ExampleServiceImpl service; // Build server ServerBuilder builder; builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); builder.RegisterService(&service); std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; // Wait for server to shutdown server->Wait(); return 0; } ``` client.cpp: ```cpp #include <iostream> #include <memory> #include <string> #include <grpcpp/grpcpp.h> #include "example.grpc.pb.h" using grpc::Channel; using grpc::ClientContext; using grpc::Status; using example::Request; using example::Response; using example::ExampleService; // Client implementation class ExampleClient { public: ExampleClient(std::shared_ptr<Channel> channel) : stub_(ExampleService::NewStub(channel)) {} std::string SendMessage(const std::string& message) { Request request; request.set_message(message); Response response; ClientContext context; // Call server Status status = stub_->SendMessage(&context, request, &response); if (status.ok()) { return response.message(); } else { return "RPC failed"; } } private: std::unique_ptr<ExampleService::Stub> stub_; }; // Main function int main() { std::string server_address("0.0.0.0:50051"); // Create client ExampleClient client(grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials())); // Call server method std::string response = client.SendMessage("Hello from client!"); std::cout << "Server responded with: " << response << std::endl; return 0; } ``` 3. 构建项目 在项目根目录下执行以下命令构建项目: ```bash mkdir build cd build cmake .. make ``` 4. 运行项目 在build目录下执行以下命令启动服务端: ```bash ./server ``` 在另一个终端窗口下执行以下命令运行客户端: ```bash ./client ``` 客户端将会向服务端发送一条消息,并在控制台上输出服务端的回复消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值