.net core api docker 部署到Linux Debian 11 Nginx 负载配置

Debian 11安装docker

参考文档

https://blog.csdn.net/cukw6666/article/details/107984154

1、首先,更新您现有的软件包列表

sudo apt update

2、接下来安装这让一些必备软件包apt使用的包装通过HTTPS

sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

3、然后将官方Docker存储库的GPG密钥添加到您的系统中

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

4、将Docker存储库添加到APT源

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

5、接下来,使用新添加的存储库中的Docker软件包更新软件包数据库

sudo apt update

6、确保要从Docker存储库而不是默认Debian存储库进行安装

apt-cache policy docker-ce

7、安装Docker

sudo apt install docker-ce

中间如果网络断了在重新执行 sudo apt install docker-ce 就好了

最后

sudo docker images

代表安装成功

API部署打包

1、上传部署包到Liunx系统文档下面

2、添加Dockerfile 文件到执行目录 我这边内容是 是文件系统发布的

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

#引入镜像,低版本 docker 去掉  AS base
#换成别人做的阿里镜像
#FROM registry.cn-hangzhou.aliyuncs.com/newbe36524
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
#配置工作目录 相当于cd
WORKDIR /app
#暴露容器端口,此端口与程序运行路径一致,可
EXPOSE 5000
#文件夹发布不需要 编译
#FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
#WORKDIR /src
#COPY ["FAST.API.Core/FAST.API.Core.csproj", "FAST.API.Core/"]
#COPY ["FAST.API.CommentHelper/FAST.API.CommentHelper.csproj", "FAST.API.CommentHelper/"]
#COPY ["FAST.API.Models/FAST.API.Models.csproj", "FAST.API.Models/"]
#COPY ["FAST.API.DAL/FAST.API.DAL.csproj", "FAST.API.DAL/"]
#RUN dotnet restore "FAST.API.Core/FAST.API.Core.csproj"

COPY . .
#WORKDIR "/src/FAST.API.Core"
#RUN dotnet build "FAST.API.Core.csproj" -c Release -o /app/build

#FROM build AS publish
#RUN dotnet publish "FAST.API.Core.csproj" -c Release -o /app/publish

#FROM base AS final
#WORKDIR /app
#COPY --from=publish /app/publish .

#ENV :配置系统环境变量,比如程序环境环境等在这里配置(开发、预发、线上环境)
#这里是配置程序运行端口,如果程序不使用默认的80端口这里一定要设置(程序运行端口)
ENV ASPNETCORE_URLS http://+:5000

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENTRYPOINT ["dotnet", "FAST.API.Core.dll"]

3、执行构建命令镜像

sudo docker build -t fastapitgythimage .

  (注意,注意,注意后面有一个点“.”,看清楚了,这个点表示当前目录上下文)

构建完成查看镜像

docker images

运行镜像

sudo docker run --name fastapitgythimage -d -p 8989:5000 --restart always fastapitgythimage

4、验证是否正常

我这里验证码没有显示出来

 我们进入容器看报什么错:

sudo docker exec -it 容器id /bin/bash

查看日志:

root@e29f0112723f:/app/Log/Error/20211203# cat 1.log
System.TypeInitializationException: The type initializer for 'System.DrawingCore.GDIPlus' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'gdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgdiplus: cannot open shared object file: No such file or directory
   at System.DrawingCore.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output)
   at System.DrawingCore.GDIPlus..cctor()
   --- End of inner exception stack trace ---
   at System.DrawingCore.GDIPlus.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, PixelFormat format, IntPtr scan0, IntPtr& bmp)
   at System.DrawingCore.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
   at System.DrawingCore.Bitmap..ctor(Int32 width, Int32 height)
   at FAST.API.Utils.VerificationCode.YZM1.Create(String& code, Int32 numbers)
   at FAST.API.Core.Controllers.YZMController.ValidateCode1() in D:\GitSourceCode\tgyth3\SourceCode\Declare\TYB\FAST_API\FAST.API.Core\Controllers\YZMController.cs:line 26
   at lambda_method(Closure , Object , Object[] )

发现是少包问题(之前遇到过):

在容器里面执行

#在容器里面执行 先更新
apt-get update 
#安装组件
apt-get install libgdiplus

在测试 验证码可以


docker 常见命令
#停止容器
sudo docker stop (容器id)
#移除容器
sudo docker rm (容器id)
#删除镜像
sudo docker rmi (镜像ID)
#查看运行的容器
sudo docker ps
#进入 docker容器内部
docker exec -it 容器id /bin/bash
#退出容器
Ctrl + D 或 exit
#保存本地tar备份
docker save -o ~/xxxx.tar 容器名称
#恢复docker备份
docker load -i docker-alibaba-server.tar
#重启容器
sudo docker restart(容器id)
#导出镜像
docker export
例如:sudo docker export -o /home/hwp/文档/tgyth3/tgyth3_api/tgythv1.tar fastapitgythimage
#导入
docker import tgythv1.tar tgyth5001
 

打包容器

docker commit :从容器创建一个新的镜像

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

OPTIONS说明:

  • -a :提交的镜像作者;
  • -c :使用Dockerfile指令来创建镜像;
  • -m :提交时的说明文字;
  • -p :在commit时,将容器暂停。
例1
docker commit -a "runoob.com" -m "my apache" a404c6c174a2  mymysql:v1 

例2

docker commit -m  ""   -a  ""   [CONTAINER ID]  [给新的镜像命名]
docker commit -m  ""   -a  "" aa myelasticsearch:1.0

打包一个镜像

docker save [OPTIONS] IMAGE [IMAGE...]
docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3

镜像远程拷贝

使用scp传输至其他服务器:

从本地复制到远程:

复制代码
scp local_file remote_username@remote_ip:remote_folder 
或者 
scp local_file remote_username@remote_ip:remote_file 
或者 
scp local_file remote_ip:remote_folder 
或者 
scp local_file remote_ip:remote_file 
复制代码

从远程复制到本地

scp root@www.runoob.com:/home/root/others/music /home/space/music/1.mp3 
scp -r www.runoob.com:/home/root/others/ /home/space/music/

新服务器载入镜像

docker load [OPTIONS]

OPTIONS 说明:

  • --input , -i : 指定导入的文件,代替 STDIN

  • --quiet , -q : 精简输出信息。

如:
$ docker load --input fedora.tar

Nginx 负载配置

执行把VUE前端部署到Nginx  参考

https://www.cnblogs.com/piaoxuewuming/p/15576568.html

创建多个容器运行:

hwp@debian:~/文档/tgyth3/tgyth3_api/publishLinux$ sudo docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED             STATUS          PORTS                                       NAMES
1c9ff02413a9   fastapitgythimage   "dotnet FAST.API.Cor…"   10 seconds ago      Up 9 seconds    0.0.0.0:8990->5000/tcp, :::8990->5000/tcp   fastapitgythimage8990
e29f0112723f   fastapitgythimage   "dotnet FAST.API.Cor…"   About an hour ago   Up 21 minutes   0.0.0.0:8989->5000/tcp, :::8989->5000/tcp   fastapitgythimage
hwp@debian:~/文档/tgyth3/tgyth3_api/publishLinux$ sudo docker run --name fastapitgythimage8991 -d -p 8991:5000 --restart always fastapitgythimage
454f17208dd86642c37dfb666fc27697e3591a5239f38d9a3a4f41dcf9428513
hwp@debian:~/文档/tgyth3/tgyth3_api/publishLinux$ sudo docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED              STATUS              PORTS                                       NAMES
454f17208dd8   fastapitgythimage   "dotnet FAST.API.Cor…"   2 seconds ago        Up 1 second         0.0.0.0:8991->5000/tcp, :::8991->5000/tcp   fastapitgythimage8991
1c9ff02413a9   fastapitgythimage   "dotnet FAST.API.Cor…"   About a minute ago   Up About a minute   0.0.0.0:8990->5000/tcp, :::8990->5000/tcp   fastapitgythimage8990
e29f0112723f   fastapitgythimage   "dotnet FAST.API.Cor…"   About an hour ago    Up 22 minutes       0.0.0.0:8989->5000/tcp, :::8989->5000/tcp   fastapitgythimage

 #修改nginx配置

我这直接给文件授权 所有权限 方便修改 双击文件修改之后直接保存

hwp@debian:/etc/nginx$ sudo chmod 777 nginx.conf


添加配置

upstream upstream_name{
server 192.168.189.129:8989 weight=2;
server 192.168.189.129:8990 weight=3;
server 192.168.189.129:8991 weight=1;
}


server
{
listen 8081;
server_name localhost;


location / {
proxy_pass http://upstream_name;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

 

刷新 nginx 配置

 sudo systemctl reload nginx

把vue前端配置改成新端口8081

再次测试成功

nginx 负载参数参考这个文章:

https://www.cnblogs.com/lave/p/10477375.html

nginx负载均衡策略

nginx的负载均衡策略有4种:

轮询(默认)

最基本的配置方法,它是upstream的默认策略,每个请求会按时间顺序逐一分配到不同的后端服务器。

参数有:

参数描述
fail_timeout 与max_fails结合使用
max_fails 设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了
fail_time 服务器会被认为停机的时间长度,默认为10s。
backup 标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里。
down 标记服务器永久停机了。

注意:

  • 在轮询中,如果服务器down掉了,会自动剔除该服务器。
  • 缺省配置就是轮询策略。
  • 此策略适合服务器配置相当,无状态且短平快的服务使用。

权重

在轮询策略的基础上制定沦陷的几率。例如

upstream foo {
    server localhost:8001 weight=2;
    server localhost:8002;
    server localhost:8003 backup;
    server localhost:8004 max_fails=3 fail_timeout=20s;
}

这里例子中,weight参数用于制定轮询的几率,weight默认值为1;weight的数值和被访问的几率成正比。

注意:

  • 权重越高分配到需要处理的请求越多。
  • 此策略可以与least_conn和ip_hash结合使用。
  • 此策略比较适合服务器的硬件配置差别比较大的情况。

ip_hash

负载均衡器按照客户端IP地址的分配方式,可以确保相同客户端的请求一直发送到相同的服务器。这样每个访客都固定访问一个后端服务器。

upstream foo {
    ip_hash;
    server localhost:8001 weight=2;
    server localhost:8002;
    server localhost:8003;
    server localhost:8004 max_fails=3 fail_timeout=20s;
}

注意:

  • 在nginx版本1.3.1之前,不能在ip_hash中使用权重(weight)。
  • ip_hash不能与backup同时使用。
  • 此策略适合有状态服务,比如session。
  • 当有服务器需要剔除,必须手动down掉。

least_conn 最小连接

把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果

upstream foo {
    least_conn;
    server localhost:8001 weight=2;
    server localhost:8002;
    server localhost:8003 backup;
    server localhost:8004 max_fails=3 fail_timeout=20s;
}

注意:

  • 此负载均衡策略适合请求处理时间长短不一造成服务器过载的情况。

除了上面这些调度策略之后,还有一些第三方的调度策略可以集成到nginx中。

原文地址:https://www.cnblogs.com/piaoxuewuming/p/15629856.html