分类:
容器编排
.netcore验证k8s内部访问与获取环境变量
创建k8s-name项目
项目结构如下
实验过程
环境设置
安装 Flurl 相关包
dotnet add package Flurl --version 3.0.0-pre3
dotnet add package Flurl.Http --version 3.0.0-pre3
内容如下
INameService.cs
public interface INameService
{
Task<string> GetName();
}
NameService.cs
public class NameService:INameService
{
private readonly string _nameUrl = "http://name-api/name";
public async Task<string> GetName()
{
return await _nameUrl.GetStringAsync();
}
}
Startup.cs—>ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<INameService,NameService>();
}
ValuesController.cs
[ApiController]
[Route("[controller]")]
public class ValuesController : ControllerBase
{
private readonly INameService _nameService;
public ValuesController(INameService nameService){
_nameService = nameService;
}
[HttpGet]
public async Task<ActionResult<string>> Get(){
return "hello," + await _nameService.GetName();
}
}
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . k8s-demo/
RUN pwd
RUN ls
RUN dotnet restore "k8s-demo/k8s-demo.csproj"
COPY . .
WORKDIR "/src/k8s-demo"
RUN pwd
RUN ls
RUN mkdir -p /app/build
RUN mkdir -p /app/publish
RUN dotnet build "k8s-demo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "k8s-demo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "k8s-demo.dll"]
Deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-demo
namespace: netcore
labels:
name: k8s-demo
spec:
replicas: 2
selector:
matchLabels:
name: k8s-demo
template:
metadata:
labels:
name: k8s-demo
spec:
containers:
- name: k8s-demo
image: aidasi/k8sdemoapi:latest
ports:
- containerPort: 80
imagePullPolicy: Always
---
kind: Service
apiVersion: v1
metadata:
name: k8s-demo
namespace: netcore
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
name: k8s-demo
创建name-api项目
项目结构如下
实验过程
内容如下
NameController.cs
环境变量参考:https://kubernetes.io/zh/docs/concepts/services-networking/service/
[ApiController]
[Route("[controller]")]
public class NameController : ControllerBase
{
[HttpGet]
public ActionResult<string> Get(){
var host = Environment.GetEnvironmentVariable("NAME_API_SERVICE_HOST");
if(string.IsNullOrEmpty(host))
return "empty";
return host;
}
}
Dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . name-api/
RUN dotnet restore "name-api/name-api.csproj"
COPY . .
WORKDIR "/src/name-api"
RUN mkdir -p /app/build
RUN mkdir -p /app/publish
RUN dotnet build "name-api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "name-api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "name-api.dll"]
Deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-api
namespace: netcore
labels:
name: name-api
spec:
replicas: 2
selector:
matchLabels:
name: name-api
template:
metadata:
labels:
name: name-api
spec:
containers:
- name: name-api
image: aidasi/name-api:v1
ports:
- containerPort: 80
imagePullPolicy: Always
---
kind: Service
apiVersion: v1
metadata:
name: name-api
namespace: netcore
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
name: name-api
发布k8s-name与name-api项目
发布k8s-name项目到 aidasi/k8sdemoapi:v2
- 通过 Xftp 上传项目
- 到指定目录下生成项目
docker build -t aidasi/k8sdemoapi:v2 .
发布name-api项目到 aidasi/name-api:v1
同理
docker build -t aidasi/name-api:v1 .
查看所有镜像结果
上传所有镜像
docker push aidasi/name-api:v1
docker push aidasi/k8sdemoapi:v2
部署项目
部署name-api项目到k8s中
kubectl apply -f deploy.yaml
接下来把k8s-demo从v1更新到v2
kubectl apply -f deploy.yaml
测试一下
kubectl proxy
为什么这里是empty呢?
我当时写代码的时候把环境变量 Name_API_SERVICE_HOST 写错成 Name_API_SERVICE_HOST 了
但我已经在代码中做好修改了
所以大家访问的结果应该是
hello,(Pod ip)Hub已经更新了,请大家放心
扩展进入pod指令
kubectl -n netcore exec -it name-api-d47996547-hcpdj -c name-api -- sh
靓仔,给个关注嘛!!!
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
2
文章
621
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 :
好是好,这个对效率影响大不大哇,效率高不高
一个bug让程序员走上法庭 索赔金额达400亿日元
剑轩 : 有点可怕
ASP.NET Core 服务注册生命周期
剑轩 :
http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术