Docker-Compose運行Nginx+Redis+NetCoreAPI
1、準備Docker-Compose
Docker
開始安裝Docker-compose以前你須要先確認已經安裝了Docker
Docker安裝能夠參考我以前的【Docker安裝教程】:https://blog.go99.top/2019/04/09/docker-install/
也能夠參考官網教程安裝:https://docs.docker.com/install/
安裝Docker-compose
- 參考官網:https://docs.docker.com/compose/install/
1. 安裝
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2. 修改`docker-compose`權限,確保當前用戶有執行權限
sudo chmod +x /usr/local/bin/docker-compose
3. 測試是否安裝成功
$ docker-compose --version
docker-compose version 1.24.0, build 0aa59064
4. 若是沒有成功,能夠嘗試下面操做,`docker-compose`創建軟鏈接到`/usr/bin`目錄
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
2、準備NetCoreAPI項目
1. 建立api項目
dotnet new webpai -n composeAPI --no-https
2. 演示項目中咱們涉及了`Redis`操做,因此先引入`Redis`程序包
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
3. 修改`appsettings.json`,加入`redis`鏈接字符串配置(注意這裏有密碼,須要後續修改redis配置文件的訪問密碼)
{
"ConnectionStrings": {
// 這裏須要注意,server的名稱須要與docker-compose.yml文件中的services名稱一致
"Redis": "redis-service:6379,password=test123"
}
}
4. 修改`StartUp.cs`注入`Redis`服務
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddStackExchangeRedisCache(options => {
options.Configuration = Configuration.GetConnectionString("Redis");
});
services.AddHttpContextAccessor();
}
5. 修改`ValuesController`內容,到時候在使用nginx訪問網站的時候區分放到的是哪一個服務
[HttpGet("{name}")]
public ActionResult<string> Get(string name)
{
var connection = _httpContextAccessor.HttpContext.Connection;
var ipv4 = connection.LocalIpAddress.MapToIPv4().ToString();
var cacheKey = $"test:{name}";
_distributedCache.SetString(cacheKey, $"{ipv4} {name}");
var message = _distributedCache.GetString(cacheKey);
return message;
}
6. 建立`Dockerfile`(打包api項目鏡像使用)
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build /src/out .
ENTRYPOINT [ "dotnet", "composeAPI.dll" ]
7. 建立`docker-compose.yml`配置文件
- 查看`version`地址:https://docs.docker.com/compose/compose-file/compose-versioning/ 裏面有docker與compose對應關係
- `docker-compose`配置能夠參考:https://docs.docker.com/compose/compose-file/
version: "3.7"
services:
web-service:
container_name: composeapi
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "10001:80"
volumes:
- ./appsettings.json:/app/appsettings.json
web-service-2:
container_name: composeapi-2
depends_on:
- web-service
image: composeapi_web-service
restart: always
ports:
- "10002:80"
volumes:
- ./appsettings.json:/app/appsettings.json
web-service-3:
container_name: composeapi-3
depends_on:
- web-service
image: composeapi_web-service
restart: always
ports:
- "10003:80"
volumes:
- ./appsettings.json:/app/appsettings.json
nginx-service:
container_name: composeapi-nginx
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./conf/nginx.conf:/etc/nginx/conf.d/default.conf
redis-service:
container_name: composeapi-redis
image: redis
restart: always
ports:
- "6379:80"
volumes:
- ./conf/redis.conf:/etc/redis/redis.conf
上面的配置文件中一共包含了5個服務:
- 3個webapi服務(因爲webapi服務所依賴的鏡像都是同樣的,因此後面2個我直接使用第一個服務建立出來的鏡像,`docker-compose`建立出來的鏡像名稱格式`project_service`)
- 1個nginx服務,直接使用nginx鏡像(反向代理3個webapi服務,實現負載均衡)
8. 建立`nginx`配置文件:
- 在上一步`docker-compose.yml`文件中咱們看到,須要將`./conf/nginx.conf`文件映射到容器的nginx默認配置文件,方便後續維護nginx配置,否則每次修改配置文件都須要進入docker容器再修改比較麻煩。
新建文件
mkdir conf && cd conf
touch nginx.conf
`nginx.conf`添加以下配置內容:
upstream composeapi {
# 這裏須要注意,server的名稱須要與docker-compose.yml文件中的services名稱一致
server web-service:80;
server web-service-2:80;
server web-service-3:80;
}
server {
listen 80;
location / {
proxy_pass http://composeapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
9. `redis`配置文件:
直接到官網拉取基本配置文件:
wget http://download.redis.io/redis-stable/redis.conf
查找`requirepass`配置文件,刪除註釋,密碼修改爲與`webapi`項目中的一直,我這邊是`test123`
10. OK,全部準備工做都已經完畢,使用`docker-compose`跑起來
docker-compose up -d
# 若是提示沒有權限,加上sudo
sudo docker-compose up -d
# 運行結果
Creating composeapi-nginx ... done
Creating composeapi-redis ... done
Creating composeapi ... done
Creating composeapi-2 ... done
Creating composeapi-3 ... done
3、查看運行結果
1. `docker-compose`狀態
sudo docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------
composeapi dotnet composeAPI.dll Up 0.0.0.0:10001->80/tcp
composeapi-2 dotnet composeAPI.dll Up 0.0.0.0:10002->80/tcp
composeapi-3 dotnet composeAPI.dll Up 0.0.0.0:10003->80/tcp
composeapi-nginx nginx -g daemon off; Up 0.0.0.0:80->80/tcp
composeapi-redis docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6379->80/tcp
2. 網站訪問狀況(直接使用瀏覽器打開,我這裏的地址是`http://172.16.102.111/api/values/hello`)
172.18.0.3 hello
172.18.0.5 hello
172.18.0.6 hello
總結
整體來講使用docker-compose部署多容器應用仍是比較簡單的,看完文檔本身動手去實踐一下會理解的更加深入。
本次測試的代碼我已經上傳到`Github`地址:https://github.com/yasewang987/Hz.DonetDemo/tree/master/composeAPI