1. 程式人生 > >Docker & Consul & Fabio & ASP.NET Core 2.0 微服務跨平臺實踐

Docker & Consul & Fabio & ASP.NET Core 2.0 微服務跨平臺實踐

相關博文:

閱讀目錄:

  • Docker 執行 Consul 環境
  • Docker 執行 Fabio 環境
  • 使用 Consul 註冊 ASP.NET Core 2.0 服務
  • 使用 Docker 釋出部署 ASP.NET Core 2.0 服務

本篇博文的目的:在 Mac OS 中使用 VS Code 開發 ASP.NET Core 2.0 應用程式,然後在 Ubuntu 伺服器配置 Docker 環境,並使用 Docker 執行 Consul 和 Fabio 環境,最後使用 Docker 執行 ASP.NET Core 2.0 應用程式。

上面配置看起來還蠻簡單,但實際去操作的時候,還是遇到了蠻多的問題,並且花了很多的時間去解決,比如 Docker 執行 Consul 和 Fabio,下面詳細說下過程。

1. Docker 執行 Consul 環境

Consul 是 HashiCorp 公司推出的開源工具,用於實現分散式系統的服務發現與配置。與其他分散式服務註冊與發現的方案,比如 Airbnb 的 SmartStack 等相比,Consul 的方案更“一站式”,內建了服務註冊與發現框 架、分佈一致性協議實現、健康檢查、Key/Value 儲存、多資料中心方案,不再需要依賴其他工具(比如 ZooKeeper 等)。使用起來也較 為簡單。Consul 用 Golang 實現,因此具有天然可移植性(支援 Linux、windows 和 Mac OS X);安裝包僅包含一個可執行檔案,方便部署,與 Docker 等輕量級容器可無縫配合。

配置 Consul 的微服務叢集環境,需要先配置下 Server 服務端(需要獨立伺服器環境),配置命令(沒有使用 Docker):

$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -ui-dir=./dist  -config-dir /etc/consul.d -bind=10.9.10.110

一開始,我想在 Mac OS 環境中使用 Docker 配置 Consul Client 客戶端,但遇到了一些問題,配置命令:

$ docker run -d --net=host --name=consul-client consul agent -bind=10.9.10.190 -client=0.0.0.0 -node=mac-xishuai -retry-join=10.9.10.236

先解析下命令的意思:

  • --net=hosthost網路模式,容器的網路介面和主機一樣,也就是共享一個 IP 地址,如果沒有此命令,預設是bridge網路模式,也就是我們常用的橋接模式,Docker 會分配給容器一個獨立的 IP 地址(埠也是獨立的),並且容器和主機之間可以相互訪問。
  • -bind=:Consul Client 繫結的 IP 地址,一般是內網的私有 IP 地址,需要內網伺服器之前可以相互訪問到,注意並不是127.0.0.1
  • -retry-join=:加入 Consul 叢集中,地址是 Consul Server 的 IP 地址,也可以是-join=,加上retry會不斷進行重試。

一臺伺服器一般會配置一個 Consul Client,所以我們可以直接讓 Consul 容器和主機的 IP 地址一樣(我使用的),但使用了 Docker 之後,一臺伺服器就可以配置多個 Consul Client,我們就可以使用bridge網路模式,一臺伺服器可以完成配置整個 Consul 叢集環境。

這裡需要再重點說下-client=,一開始我沒有理解,先看下官方說明:

If you want to expose the Consul interfaces to other containers via a different network, such as the bridge network, use the-clientoption for Consul.
With this configuration, Consul's client interfaces will be bound to the bridge IP and available to other containers on that network, but not on the host network. Note that we still keep the cluster address out on the host network for performance. Consul will also accept the-client=0.0.0.0option to bind to all interfaces.

啥意思呢?Consul 服務註冊的時候,一般是通過 HTTP API 介面進行註冊,比如:http://10.9.10.190:8500/v1/agent/service/register,就是往 Consul 叢集中註冊服務,需要注意的是,10.9.10.190一般是 Consul Client 的 IP 地址(也可以是 Consul Server),-client配置的就是此地址,簡單來說,就是用來服務註冊並能訪問到的地址,換句話說,服務註冊可以跨伺服器(服務和 Consul Client 並不需要在同一臺伺服器上),0.0.0.0表示任何本機的相關 IP 地址都可以訪問,推薦此配置。

這裡需要再說明下,Docker 部署 ASP.NET Core 2.0、Consul 和 Fabio 有兩種方式:

  • 使用一個 Docker 容器:很簡單,在一個容器中完成服務部署,並且配置 Consul 和 Fabio 環境,這樣容器就會很臃腫,並且每次釋出的時候都得重新配置 Consul 和 Fabio 環境,如果服務很多的話,想想就覺得恐怖。
  • 分別獨立 Docker 容器:服務部署、配置 Consul 和 Fabio 環境,都是獨立容器實現,互不影響,也可以跨服務實現,簡單靈活。

顯而易見,推薦第二種方式。

回到正題,上面配置命令,在 Mac OS 報如下錯誤:

$ docker logs consul-client
==> Starting Consul agent...
==> Error starting agent: Failed to start Consul client: Failed to start lan serf: Failed to create memberlist: Could not set up network transport: failed to obtain an address: Failed to start TCP listener on "10.9.10.190" port 8301: listen tcp 10.9.10.190:8301: bind: cannot assign requested address

這個問題花了很多時間也沒有解決,奇怪的是不使用 Docker,直接執行 Consul Client 配置命令,卻是可以的,後來沒辦法,我就在 Mac OS 中使用 Ubuntu 虛擬機器了(版本 14.04),使用的 Vagrant 管理工具。

再重新執行配置命令:

$ docker run -d --net=host --name=consul-client consul agent -bind=10.9.10.89 -client=0.0.0.0 -node=vagrant-ubuntu-xishuai -retry-join=10.9.2.236

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9c4988cf475f        consul              "docker-entrypoint..."   2 seconds ago       Up 2 seconds                            consul-client

$ docker logs consul-client
==> Starting Consul agent...
==> Consul agent running!
           Version: 'v1.0.0'
           Node ID: '34e63f0a-d361-f152-3803-b9fda0642e4d'
         Node name: 'vagrant-ubuntu-xishuai'
        Datacenter: 'dc1' (Segment: '')
            Server: false (Bootstrap: false)
       Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, DNS: 8600)
      Cluster Addr: 10.9.10.89 (LAN: 8301, WAN: 8302)
           Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false

==> Log data will now stream in as it occurs:

    2017/11/14 06:40:52 [INFO] serf: EventMemberJoin: vagrant-ubuntu-xishuai 10.9.10.89
    2017/11/14 06:40:52 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp)
    2017/11/14 06:40:52 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp)
    2017/11/14 06:40:52 [INFO] agent: Started HTTP server on [::]:8500 (tcp)
    2017/11/14 06:40:52 [INFO] agent: Retry join LAN is supported for: aws azure gce softlayer
    2017/11/14 06:40:52 [INFO] agent: Joining LAN cluster...
    2017/11/14 06:40:52 [INFO] agent: (LAN) joining: [10.9.2.236]
    2017/11/14 06:40:52 [WARN] manager: No servers available
    2017/11/14 06:40:52 [ERR] agent: failed to sync remote state: No known Consul servers
    2017/11/14 06:40:52 [INFO] serf: EventMemberJoin: agent_1 10.9.2.236
    2017/11/14 06:40:52 [INFO] agent: (LAN) joined: 1 Err: <nil>
    2017/11/14 06:40:52 [INFO] agent: Join LAN completed. Synced with 1 initial agents
    2017/11/14 06:40:52 [INFO] consul: adding server agent_1 (Addr: tcp/10.9.2.236:8300) (DC: dc1)

開啟 Consul UI 介面,就可以看到我們配置的 Consul Client 了:

2. Docker 執行 Fabio 環境

Fabio 是一個快速、現代、zero-conf 負載均衡 HTTP(S) 路由器,用於部署 Consul 管理的微服務。

配置命令:

$ docker run -d --net=host --name=fabio -e 'registry_consul_addr=10.9.10.89:8500' magiconair/fabio

執行日誌:

$ docker logs fabio
2017/11/14 09:43:49 [INFO] Setting log level to INFO
2017/11/14 09:43:49 [INFO] Runtime config
{
    "Proxy": {
        "Strategy": "rnd",
        "Matcher": "prefix",
        "NoRouteStatus": 404,
        "MaxConn": 10000,
        "ShutdownWait": 0,
        "DialTimeout": 30000000000,
        "ResponseHeaderTimeout": 0,
        "KeepAliveTimeout": 0,
        "FlushInterval": 1000000000,
        "LocalIP": "10.0.2.15",
        "ClientIPHeader": "",
        "TLSHeader": "",
        "TLSHeaderValue": "",
        "GZIPContentTypes": null,
        "RequestID": ""
    },
    "Registry": {
        "Backend": "consul",
        "Static": {
            "Routes": ""
        },
        "File": {
            "Path": ""
        },
        "Consul": {
            "Addr": "10.9.10.89:8500",
            "Scheme": "http",
            "Token": "",
            "KVPath": "/fabio/config",
            "TagPrefix": "urlprefix-",
            "Register": true,
            "ServiceAddr": ":9998",
            "ServiceName": "fabio",
            "ServiceTags": null,
            "ServiceStatus": [
                "passing"
            ],
            "CheckInterval": 1000000000,
            "CheckTimeout": 3000000000,
            "CheckScheme": "http",
            "CheckTLSSkipVerify": false
        },
        "Timeout": 10000000000,
        "Retry": 500000000
    },
    "Listen": [
        {
            "Addr": ":9999",
            "Proto": "http",
            "ReadTimeout": 0,
            "WriteTimeout": 0,
            "CertSource": {
                "Name": "",
                "Type": "",
                "CertPath": "",
                "KeyPath": "",
                "ClientCAPath": "",
                "CAUpgradeCN": "",
                "Refresh": 0,
                "Header": null
            },
            "StrictMatch": false,
            "TLSMinVersion": 0,
            "TLSMaxVersion": 0,
            "TLSCiphers": null
        }
    ],
    "Log": {
        "AccessFormat": "common",
        "AccessTarget": "",
        "RoutesFormat": "delta",
        "Level": "INFO"
    },
    "Metrics": {
        "Target": "",
        "Prefix": "{{clean .Hostname}}.{{clean .Exec}}",
        "Names": "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}",
        "Interval": 30000000000,
        "Timeout": 10000000000,
        "Retry": 500000000,
        "GraphiteAddr": "",
        "StatsDAddr": "",
        "Circonus": {
            "APIKey": "",
            "APIApp": "fabio",
            "APIURL": "",
            "CheckID": "",
            "BrokerID": ""
        }
    },
    "UI": {
        "Listen": {
            "Addr": ":9998",
            "Proto": "http",
            "ReadTimeout": 0,
            "WriteTimeout": 0,
            "CertSource": {
                "Name": "",
                "Type": "",
                "CertPath": "",
                "KeyPath": "",
                "ClientCAPath": "",
                "CAUpgradeCN": "",
                "Refresh": 0,
                "Header": null
            },
            "StrictMatch": false,
            "TLSMinVersion": 0,
            "TLSMaxVersion": 0,
            "TLSCiphers": null
        },
        "Color": "light-green",
        "Title": "",
        "Access": "rw"
    },
    "Runtime": {
        "GOGC": 800,
        "GOMAXPROCS": 1
    },
    "ProfileMode": "",
    "ProfilePath": "/tmp"
}

需要注意的兩個屬性值:

  • Proxy.LocalIP:10.0.2.15:繫結本機的 IP 地址,伺服器的 IP 地址是10.9.10.89,所以配置的10.0.2.15是錯誤的,這個 IP 地址內網是訪問不了的。
  • Registry.Consul.Addr:10.9.10.89:8500:繫結 Consul 地址,我們上面已經完成的 Consul Client 地址就是10.9.10.89:8500,所以是正確的。

這個配置命令研究了好久,也沒有解決繫結本機 IP 地址的問題,後來又找到了另外一種方式。

首先,在/etc/fabio/目錄下建立一個fabio.properties檔案(示例配置),然後vim fabio.properties增加下面配置:

registry.consul.register.addr = 10.9.10.89:9998

registry.consul.addr = 10.9.10.89:8500

registry.consul.register.addr繫結 Fabio 地址(本機 IP 地址),registry.consul.addr繫結 Consul 地址。

然後切換到/etc/fabio/目錄,執行配置命令:

$ docker run -d -p 9999:9999 -p 9998:9998 --net=host --name=fabio -v $PWD/fabio.properties:/etc/fabio/fabio.properties magiconair/fabio

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
301fe4a5b40b        magiconair/fabio    "/fabio -cfg /etc/..."   About a minute ago   Up About a minute                       fabio
9c4988cf475f        consul              "docker-entrypoint..."   4 hours ago          Up 4 hours                              consul-client

檢視執行日誌:

$ docker logs fabio
2017/11/14 10:10:58 [INFO] Version 1.5.3 starting
2017/11/14 10:10:58 [INFO] Go runtime is go1.9.2
2017/11/14 10:10:58 [INFO] Metrics disabled
2017/11/14 10:10:58 [INFO] Setting GOGC=800
2017/11/14 10:10:58 [INFO] Setting GOMAXPROCS=1
2017/11/14 10:10:58 [INFO] consul: Connecting to "10.9.10.89:8500" in datacenter "dc1"
2017/11/14 10:10:58 [INFO] Admin server access mode "rw"
2017/11/14 10:10:58 [INFO] Admin server listening on ":9998"
2017/11/14 10:10:58 [INFO] Waiting for first routing table
2017/11/14 10:10:58 [INFO] consul: Using dynamic routes
2017/11/14 10:10:58 [INFO] consul: Using tag prefix "urlprefix-"
2017/11/14 10:10:58 [INFO] consul: Watching KV path "/fabio/config"
2017/11/14 10:10:58 [INFO] consul: Manual config changed to #3723938
2017/11/14 10:10:58 [INFO] HTTP proxy listening on :9999
2017/11/14 10:10:58 [INFO] Access logging disabled
2017/11/14 10:10:58 [INFO] Using routing strategy "rnd"
2017/11/14 10:10:58 [INFO] Using route matching "prefix"
2017/11/14 10:10:58 [INFO] consul: Health changed to #3727339
2017/11/14 10:10:59 [INFO] consul: Registered fabio with id "fabio-vagrant-ubuntu-trusty-9998"
2017/11/14 10:10:59 [INFO] consul: Registered fabio with address "10.9.10.89"
2017/11/14 10:10:59 [INFO] consul: Registered fabio with tags ""
2017/11/14 10:10:59 [INFO] consul: Registered fabio with health check to "http://[10.9.10.89]:9998/health"
2017/11/14 10:11:00 [INFO] Config updates

可以通過 Consul UI,進行檢視 Fabio 是否正常:

3. 使用 Consul 註冊 ASP.NET Core 2.0 服務

在 Mac OS 中使用 VS Code 開發 ASP.NET Core 2.0 應用程式,就像寫 Markdown 一樣方便。

安裝程式包(VS Code 需要使用NuGet Pakcage Manager命令安裝):

> install-package Conusl

然後新增一個RegisterWithConsul擴充套件服務:

using System;
using System.Collections.Generic;
using System.Linq;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;

public static class ApplicationBuilderExtensions
{
    public static IApplicationBuilder RegisterWithConsul(this IApplicationBuilder app, IApplicationLifetime lifetime)
    {
        //var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{Program.IP}:8500"));//如果服務和 Consul 在同一臺伺服器上,使用此程式碼
        var consulClient = new ConsulClient(x => x.Address = new Uri($"http://10.9.10.89:8500"));//請求註冊的 Consul 地址
        var httpCheck = new AgentServiceCheck()
        {
            DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服務啟動多久後註冊
            Interval = TimeSpan.FromSeconds(10),//健康檢查時間間隔,或者稱為心跳間隔
            HTTP = $"http://{Program.IP}:{Program.Port}/health",//健康檢查地址
            Timeout = TimeSpan.FromSeconds(5)
        };

        // Register service with consul
        var registration = new AgentServiceRegistration()
        {
            Checks = new[] { httpCheck },
            ID = Guid.NewGuid().ToString(),
            Name = Program.ServiceName,
            Address = Program.IP,
            Port = Program.Port,
            Tags = new[] { $"urlprefix-/{Program.ServiceName}" }//新增 urlprefix-/servicename 格式的 tag 標籤,以便 Fabio 識別
        };

        consulClient.Agent.ServiceRegister(registration).Wait();//服務啟動時註冊,內部實現其實就是使用 Consul API 進行註冊(HttpClient發起)
        lifetime.ApplicationStopping.Register(() =>
        {
            consulClient.Agent.ServiceDeregister(registration.ID).Wait();//服務停止時取消註冊
        });
        return app;
    }
}

Start.cs配置程式碼:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
              ILoggerFactory loggerFactory, IApplicationLifetime lifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();
    app.RegisterWithConsul(lifetime);//here
}

Program.cs配置程式碼:

public class Program
{
    public static string IP = "";
    public const int Port = 54917;
    public const string ServiceName = "hello-docker";
    public const string Version = "v1";

    public static void Main(string[] args)
    {
        //Program.IP = LocalIPAddress;//使用 Docker 的時候,獲取的是 IP 地址不正確,需要進行完善
        Program.IP = "10.9.10.190";//Docker 容器中的 IP 地址,如果使用 host 網路模式,也是主機的 IP 地址
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls($"http://*:{Program.Port}")
            .UseStartup<Startup>()
            .Build();

    public static string LocalIPAddress
    {
        get
        {
            UnicastIPAddressInformation mostSuitableIp = null;
            var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (var network in networkInterfaces)
            {
                if (network.OperationalStatus != OperationalStatus.Up)
                    continue;
                var properties = network.GetIPProperties();
                if (properties.GatewayAddresses.Count == 0)
                    continue;

                foreach (var address in properties.UnicastAddresses)
                {
                    if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                        continue;
                    if (IPAddress.IsLoopback(address.Address))
                        continue;
                    return address.Address.ToString();
                }
            }
            return mostSuitableIp != null
                ? mostSuitableIp.Address.ToString()
                : "";
        }
    }
}

健康檢查配置程式碼:

[Route("[controller]")]
public class HealthController : Controller
{
    private static readonly HttpClient _httpClient;

    static HealthController()
    {
        _httpClient = new HttpClient();
    }

    [HttpGet]
    public IActionResult Get() => Ok("ok");

    //或者使用fabio進行健康檢查
    //[Route("")]
    //[HttpGet]
    //public async Task<HttpResponseMessage> GetWithFabio() => await _httpClient.GetAsync("http://127.0.0.1:9998/health");
}

4. 使用 Docker 釋出部署 ASP.NET Core 2.0 服務

我們需要在 ASP.NET Core 2.0 應用程式目錄下,新增一個Dockerfile檔案,用來構建自定義映象(命令參考:使用 Dockerfile 定製映象),示例:

FROM microsoft/aspnetcore-build AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# build runtime image
FROM microsoft/aspnetcore
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "HelloDocker.Web.dll"]

簡單來說,就是先使用microsoft/aspnetcore-build映象,還原程式包併發布,然後再使用microsoft/aspnetcore映象,執行 ASP.NET Core 2.0 應用程式。

構建映象命令:

$ docker build -t hello-docker:v1 .
Sending build context to Docker daemon  41.98kB
Step 1/10 : FROM microsoft/aspnetcore-build AS build-env
 ---> d6273f7c44d4
Step 2/10 : WORKDIR /app
 ---> e37f90cd9aaf
Removing intermediate container a4e4db93ea06
Step 3/10 : COPY *.csproj ./
 ---> 56278755f94c
Step 4/10 : RUN dotnet restore
 ---> Running in 3b7e8c5d01f4
  Restoring packages for /app/HelloDocker.Web.csproj...
  Restore completed in 411.99 ms for /app/HelloDocker.Web.csproj.
  Installing System.Security.Principal.Windows 4.4.0-preview1-25305-02.
  Installing Microsoft.NETCore.Platforms 2.0.0-preview1-25305-02.
  Installing System.Security.AccessControl 4.4.0-preview1-25305-02.
  Installing Microsoft.Win32.Registry 4.3.0.
  Installing System.Security.Permissions 4.4.0-preview1-25305-02.
  Installing System.Diagnostics.Process 4.3.0.
  Installing Newtonsoft.Json 10.0.2.
  Installing System.Net.Http.WinHttpHandler 4.0.0.
  Installing CoreCompat.System.Drawing.v2 5.2.0-preview1-r131.
  Installing System.Data.Common 4.3.0.
  Installing System.Security.Cryptography.Pkcs 4.3.0.
  Installing System.Xml.XPath.XmlDocument 4.3.0.
  Installing Pomelo.EntityFrameworkCore.MySql 2.0.0.
  Installing MySqlConnector 0.26.4.
  Installing Pomelo.JsonObject 1.1.1.
  Installing Consul 0.7.2.3.
  Installing EPPlus.Core 1.5.2.
  Generating MSBuild file /app/obj/HelloDocker.Web.csproj.nuget.g.props.
  Generating MSBuild file /app/obj/HelloDocker.Web.csproj.nuget.g.targets.
  Restore completed in 5.71 sec for /app/HelloDocker.Web.csproj.
 ---> cdf6ca65acf6
Removing intermediate container 3b7e8c5d01f4
Step 5/10 : COPY . ./
 ---> fffa81d15ddc
Step 6/10 : RUN dotnet publish -c Release -o out
 ---> Running in 291c8eea750f
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  HelloDocker.Web -> /app/bin/Release/netcoreapp2.0/HelloDocker.Web.dll
  HelloDocker.Web -> /app/out/
 ---> 078311772175
Removing intermediate container 291c8eea750f
Step 7/10 : FROM microsoft/aspnetcore
 ---> b97d3cf55223
Step 8/10 : WORKDIR /app
 ---> b0637e3d706b
Removing intermediate container 7095565fbbca
Step 9/10 : COPY --from=build-env /app/out .
 ---> c3cb8a708c4b
Step 10/10 : ENTRYPOINT dotnet HelloDocker.Web.dll
 ---> Running in d4111dc055f8
 ---> 29121f0eb2b0
Removing intermediate container d4111dc055f8
Successfully built 29121f0eb2b0
Successfully tagged hello-docker:v1

上面構建映象的過程,非常詳細,我們可以得到很多的資訊,這邊就不敘述了,構建完映象之後,我們可以檢視下是否成功:

$ docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
hello-docker                 v1                  29121f0eb2b0        Less than a second ago   284MB
<none>                       <none>              078311772175        Less than a second ago   1.9GB
microsoft/aspnetcore-build   latest              d6273f7c44d4        4 days ago          1.85GB
microsoft/aspnetcore         latest              b97d3cf55223        4 days ago          280MB
consul                       latest              dff07cab6abd        9 days ago          51.8MB
magiconair/fabio             latest              b0d96559369f        10 days ago         11.8MB

hello-docker下面沒命名的映象,是臨時生成的,作用是使用microsoft/aspnetcore-build映象,還原程式包的時候,不需要重新安裝了。

另外,我們可以在 ASP.NET Core 2.0 應用程式目錄下,新增.dockerignore檔案,來減少我們構建的映象檔案大小,示例:

bin/*
obj/*

需要說明下,Consul 和 Fabio 我都是部署在虛擬機器的 Docker 容器中,ASP.NET Core 2.0 應用程式,我打算執行在 Mac OS 系統中,也就是說服務和 Consul 是跨伺服器的。

執行命令:

$ docker run -d -p 54917:54917 --name hello-docker-web hello-docker:v1

檢視下是否執行成功:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED                  STATUS              PORTS                      NAMES
396d42f37185        hello-docker:v1        "dotnet Hello-Dock..."   Less than a second ago   Up 2 seconds        0.0.0.0:54917->54917/tcp   hello-docker-web

$ docker logs hello-docker-web
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:54917
Application started. Press Ctrl+C to shut down.

可以看到,執行是成功的。

我們可以請求驗證下:

$ curl http://10.9.10.190:54917/api/values
["value1","value2"]

或者使用 Fabio 的閘道器請求(Fabio 可以用作負載均衡):

$ curl http://10.9.10.89:9999/hello-docker/api/values
["value1","value2"]

我們可以檢視 Consul UI 中的服務是否執行正常:

或者檢視 Fabio UI 中的服務是否存在(通過健康檢查後會出現):

檢視資料:

相關推薦

Docker & Consul & Fabio & ASP.NET Core 2.0 服務跨平臺實踐

相關博文: 閱讀目錄: Docker 執行 Consul 環境 Docker 執行 Fabio 環境 使用 Consul 註冊 ASP.NET Core 2.0 服務 使用 Docker 釋出部署 ASP.NET Core 2.0 服務 本篇博文的目的:在 Mac OS 中使用 VS Code 開發 A

Docker & ASP.NET Core 2.0 服務跨平臺實踐

本篇博文的目的:在 Mac OS 中使用 VS Code 開發 ASP.NET Core 2.0 應用程式,然後在 Ubuntu 伺服器配置 Docker 環境,並使用 Docker 執行 Consul 和 Fabio 環境,最後使用 Docker 執行 ASP.NET Core 2.0 應用程式。 你

ASP.NET Core 2.0 : 五.服務是如何加載並運行的, Kestrel、配置與環境

log oid 方案 配置 結構 soc windows系統 ogg apps "跨平臺"後的ASP.Net Core是如何接收並處理請求的呢? 它的運行和處理機制和之前有什麽不同? 本章從"宏觀"到"微觀"地看一下它的結構以及不同時期都幹了些什麽. ASP.NET

Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自動化發布和部署

必須 ans http 集成 ase blank ps 命令 local rom 寫在前面 Docker一直很火熱,一直想把原本的Jenkins自動部署工具搬到Docker上面,無奈今年一直忙於各種事情,遲遲未實施這個事情,正好迎來了dotnet core 2.0 的

Centos7 & Docker & Jenkins & ASP.NET Core 2.0 自動化釋出和部署

寫在前面   Docker一直很火熱,一直想把原本的Jenkins自動部署工具搬到Docker上面,無奈今年一直忙於各種事情,遲遲未實施這個事情,正好迎來了dotnet core 2.0 的正式釋出,升級專案的同時,順便直接將Jenkins搬到Docker上。為什麼要寫這篇文章呢?因為找過相關的資料,大多數文

Asp.net Core 2.0連接mysql數據庫一系列錯誤問題

alt sql mysql mysql數據庫 work image sql數據庫 報錯 entity mysql:最新版 一定是最新版,不然各種報錯 Install-Package Pomelo.EntityFrameworkCore.MySql 2.0.0-rtm-100

03、手工創建Asp.Net Core 2.0 web應用程序(探索最小架構)

log bho 啟動 oid img gif method nuget ica 03、手工創建Asp.Net Core 2.0 web應用程序(探索最小架構) 一、創建一個幹凈的控制臺程序 這裏我們使用的開發工具是VS2017 項目只有一個Program.cs類。

一起學ASP.NET Core 2.0學習筆記(一): CentOS下 .net core2 sdk nginx、supervisor、mysql環境搭建

image dev 預覽 def star fig brush rest aspnet 作為.neter,看到.net core 2.0的正式發布,心裏是有點小激動的,迫不及待的體驗了一把,發現速度確實是快了很多,其中也遇到一些小問題,所以整理了一些學習筆記: 閱讀目

一起學ASP.NET Core 2.0學習筆記(二): ef core2.0 及mysql provider 、Fluent API相關配置及遷移

upd order rac option 包管理 rtl code create .net core 不得不說微軟的技術叠代還是很快的,上了微軟的船就得跟著她走下去,前文一起學ASP.NET Core 2.0學習筆記(一): CentOS下 .net core2 s

ASP.NET Core 2.0 源代碼

安裝 nal 支持 介紹 作者 framework 重新啟動 stack 參考資料 ASP.NET Core 2.0 源代碼 在Visual Studio 2017中可以通過符號以及源鏈接,非常方便對 ASP.NET Core 2.0中源代碼進行調試。在這篇文章中,我們將重

從頭編寫 asp.net core 2.0 web api 基礎框架 (2)

req new 定義 問題 ... htm error 期待 實驗 上一篇是: http://www.cnblogs.com/cgzl/p/7637250.html Github源碼地址是: https://github.com/solenovex/Building-asp

從頭編寫 asp.net core 2.0 web api 基礎框架 (3)

cep -- 以及 我們 lease ogg 適合 targe crud 第一部分: http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html Git

ASP.net core 2.0.0asp.net identity 2.0.0 的基本使用(一)

使用 相對路徑 註意 apps 模型視圖 hand getc 技術分享 star 開發環境:vs2017 版本:15.3.5 項目環境:.net framework 4.6.1 模板asp.net core 2.0 Web應用程序(模型視圖控制器) 身份

ASP.net Core 2.0 EF (mysql) 遷移 正確姿勢

com update reat .com ttr 數據庫 pda creat mic 用於進行遷移的 Entity Framework Core NuGet 包 註意:必須通過編輯 .csproj 文件來安裝此包;不能使用 install-package 命令或程序包管理器

asp.net core 2.0 webapi集成signalr

soft onf everyone text hosting tro 不能 .com fault 在博客園也很多年了,一直未曾分享過什麽東西,也沒有寫過博客,但自己也是汲取著博客園的知識成長的; 這兩天想著不能這麽無私,最近.NET CORE貌似挺流行的,

ASP.NET Core 2.0 Cookie Authentication

pen builder class end collect exce enc ati develop using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder

Asp.net core 2.0.1 Razor 的使用學習筆記(一)

提升 完成後 安全 provider razor 官方 one text .cn 環境:vs2017 版本:15.5.6 這裏說明下, Razor頁面模式跟mvc出現了嚴重的不同。正如微軟官方說的一樣“Razor 頁面是 ASP.NET Core MVC 的一

Asp.net core 2.0.1 Razor 的使用學習筆記(三)

post 應用程序 新的 entity gin start 密碼強度 ice httponly ASP.net core 2.0.1 中 asp.net identity 2.0.1 的基本使用(二)—用戶賬戶及cookie配置 修改用戶賬戶及cookie配置

ASP.Net Core 2.0: 二. 開發環境

class 要求 blank 安裝 col ans 裝包 fly code macOS:Install Visual Studio for Mac 系統要求: macOS 10.12 Sierra 及更高版本 其他要求: 可能會要求安裝xcode或android相關環境

Validate Model State automatically in ASP.NET Core 2.0

開始 lte 定義 新一代 get www. true 狀態 ins if (!ModelState.IsValid) { //TODO 模型驗證失敗需要做的事情 } 上面的代碼不管是在傳統的ASP.NET還是新一代ASP.NET Core中都是為了驗證模型的狀