Kubernetes-??? ?? ASP.NET 9-?? ??????????

Kubernetes-??? ?? ASP.NET 9-?? ??????????

????????

????????? ???-???????????????? ??????????????? ?? ??????????? ?????????? ???? ????????. Kubernetes ???? ??????? ????????????? ????????????? ?????????, ??????? ?????????? ???????? ???????? ???????????, ??????? ?? ????????????? ???????????????? ???????????.

ASP.NET 9, ?????? ???? ?????? .NET ?????????? ?????? (??????????), ????????? cloud-native ?????? ?? ?????? ???????????? ??????????? ????????????? ??????????????. Microsoft-?? ????????????? ????????, .NET 9 ????????????? ?????????? ?????????? ??????????? ???? ???????????? ????????????? – ??? ?????, Kubernetes-?? ???????? ???????? ?????????????.

????? ???? Kubernetes ?? ASP.NET 9 ????? ???????????

  • ????????????????: Kubernetes ??????????? ????? ?? ???????? ?????? ?????????? ??????????? ?????????? ????????? ????????.
  • ?????????: ?????????? ???????? ??????????? ????????????? ?????????? ?????? ????????????????.
  • ???????????: ??????????????? ?????????? ???????? ????????? ???????? ????????? ????????? ???????? - ?????????, ???????? ?????????? ?? ????????? ????????????.
  • ????????? ??????: ????????? ?????????? ?????????? ???????????, ???????????? ???????? ?? ???????? ?????????? ????? ???????????.
  • ???????????? ????????????: Kubernetes ?????????? YAML-???????? ?????? ?????????? ???????? ????????? ?????????????? ?????? ????.

???????? ASP.NET 9 ?????????? ????????????????.

ASP.NET 9 ???? ??????????????? Docker-??

??????????????? ?????????? ??????????? ?? ???? ????? ??????????????? ???????? ??? ???????? ??????. ASP.NET 9 ?????????? ???????????, ?? ??????? ??????? ????????????? ?????, ??????? ???????, ?????????????? ?? ????????????? ??? ??????????? ????????.

ASP.NET 9 ???????? ?????????

?????? ?????, ??????? ???????? ???? ASP.NET 9 ???????. ?? ??? ?? ?????, ????????? ??????? ????? ??????? .NET CLI-?? ???????????:

# ???????? ????? ASP.NET 9 ??? ?????????
dotnet new webapp -n MyAspNetApp

# ??????? ???????? ????????????
cd MyAspNetApp        

Dockerfile-?? ??????

ASP.NET 9 ?????????? ???????????????????? ???? ???????? Dockerfile. ???-???? ????????????? ???????? Multi-stage build-?? ??????????, ??? ?????????? ???????? ??????????? ?????????? ???????? ????? ??????? ?? ???????? ????????????, ????? ????????? ??????? ?????? ?????.

???????? ????? Dockerfile ???????? ??????? ???????????? ??????? ?????????:

# ???????? 1: Build ?????? (.NET SDK)
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build-env
WORKDIR /app

# ???????? ???????? ?????????? ?? ???????
COPY *.csproj ./
RUN dotnet restore

# ????? ????? ?????????? ?? ???????????? Release ???????
COPY . ./
RUN dotnet publish -c Release -o /app/out

# ???????? 2: Runtime ?????? (.NET ASP.NET Runtime)
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app

# ?????????????? ???????????? ?????????? build ????????
COPY --from=build-env /app/out ./

# ??????? ????????? ??????????
ENV ASPNETCORE_URLS=https://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production

# ?????? ???????????
EXPOSE 8080

# ??????????? ????????
ENTRYPOINT ["dotnet", "MyAspNetApp.dll"]        

?? Dockerfile-?? ??? ?????? ????????????:

  1. ??????? ????? (build-env): ??????? .NET 9 SDK ?????, ????? ????? ???? ????? ?????????? ?? ???????????? Release ??????? /app/out ????????????.
  2. ????? ?????: ??????? ?????? ASP.NET 9 Runtime ?????, ????? ?????? ?????????????? ??????? ??????????? ??????? ????????.

????? ???????? ??????? ??????????? ????? ???? ?????? ?? ?????????????? ????????????.

?????? ?????? ?? ????????? ???????

Dockerfile-?? ??????? ??????, ??????? Docker ????? ?? ???????? ????????? ??????????????:

# ?????? ??????
docker build -t myaspnetapp:1.0 .

# ??????????? ???????
docker run -d -p 8080:8080 --name myaspnetapp myaspnetapp:1.0        

?? ????????? ?????????? ????????, ?????? ASP.NET 9 ????????? ????????????? ?????? ????????? https://localhost:8080 ??????????.

?????? ????????? ????????????

Kubernetes-?? ??????????, ??????? ?????? Docker ????? ????????????? ???? ????????????? ??????????? (?? ???????? ???? Docker Hub, Azure Container Registry, Google Container Registry ?? ????????? ????????).

Docker Hub-?? ?????? ????????:

# ?????? ???????? Docker Hub ???????????? ???????
docker tag myaspnetapp:1.0 YourDockerHubUsername/myaspnetapp:1.0

# Docker Hub-?? ???????????
docker login

# ?????? ????????
docker push YourDockerHubUsername/myaspnetapp:1.0        

???? ???? ?????????? ASP.NET 9 ????????? Kubernetes-?? ??????????????.

Kubernetes ????????? ??????????

ASP.NET 9 ?????????? ??????????? ??????? ??????, ????????? ????????? ??? Kubernetes ????????? ????????????. ????????? ???? ???????? ????????? Kubernetes ???????, ???????? ????????????? ?? ????? ???? ??????? ?? ??????? ????? ?????????.

????????????

  • ?????? Kubernetes ????????? (???????? ???? ???????? Minikube, Kind, ?? cloud-??????????? ????????)
  • ??????????????? ?? ??????????????? kubectl ????????
  • ????????????? ????????? ????????? ASP.NET 9 ?????????? ?????

Deployment YAML

?????? ?????, ???????? Deployment ???????, ??????? ???????????? ????? ?????????? ??????? ???????? ????????????. ???? ???? ??????? ????? ???????????? ??????? Kubernetes-??, ??????? ??????? ??? ?? ????????? ??????????.

???????? ????? aspnetapp-deployment.yaml ??????? ?????????:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnet9-app-deployment
  labels:
    app: aspnet9-app
spec:
  replicas: 3  # ???????? 3 ??????? ?????? ????????????????????
  selector:
    matchLabels:
      app: aspnet9-app
  template:
    metadata:
      labels:
        app: aspnet9-app
    spec:
      containers:
      - name: aspnet9-app
        image: YourDockerHubUsername/myaspnetapp:1.0
        ports:
        - containerPort: 8080
        env:
        - name: ASPNETCORE_ENVIRONMENT
          value: "Production"
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "500m"
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 15
          periodSeconds: 20        

?? YAML-??:

  • replicas: 3 - ?????? ?????????? ??? ??????????
  • image - ???????? ?????? ????????? ?????? ?????????
  • resources - ????????????? ????????? (requests) ?? ?????????? (limits) ?????????
  • readinessProbe ?? livenessProbe - ????????? ?????????? ????????????

????????: ??????????, ??? ?????? ASP.NET 9 ?????????? ???? /health ?????????. ?? ?? ?????, ????????? ?????????? ?????????? ???????????? ?????????? ????????? Program.cs ?????? ????????????:
builder.Services.AddHealthChecks();
// ...
app.MapHealthChecks("/health");        

Service YAML

??????, ???????? Service, ??????? ???????????? ???????? ??????? ????? ???????. Service-? ???? ??????? IP ????????? ?? DNS ??????, ??? ?????????? ???????? ?????????????? ?????????? ????????, ?????????? ?????, ?? ??? ????? ?????????.

???????? ????? aspnetapp-service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: aspnet9-service
spec:
  type: ClusterIP  # ????????? ???? ???????
  selector:
    app: aspnet9-app
  ports:
  - port: 80        # ??????? ?????? 80 ??????
    targetPort: 8080 # ??????????????? ??????? 8080 ??????        

Ingress YAML

?? ??????, ??? ????? ????????? ????????????? ???? ????????? ???????, ????????? ??????????? Ingress - Kubernetes-?? ???????, ??????? ??????? ???? HTTP/HTTPS ???????.

???????? ????? aspnetapp-ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: aspnet9-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: aspnet9.example.com  # ???????? ?????? ??????? ???????
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: aspnet9-service
            port:
              number: 80        
????????: Ingress-?? ???????????????, ????????? ???? ???? ????????????? Ingress Controller (???., Nginx Ingress Controller). ?????, ?????? ???? ??????????? ?????? ????????? ?????? IP-??.

?????????? ???????????

???????? YAML ???????? Kubernetes ????????? ?????????????? ?????????? kubectl ????????:

# Deployment-?? ???????????
kubectl apply -f aspnetapp-deployment.yaml

# Service-?? ???????????
kubectl apply -f aspnetapp-service.yaml

# Ingress-?? ??????????? (?? ??????????)
kubectl apply -f aspnetapp-ingress.yaml        

???????????? ?????????

???????????? ?????????? ???????? ????????????? ?????????? ??????? ??????????:

# ??????? ???????? ?????????
kubectl get pods -l app=aspnet9-app

# ???????? ?????????
kubectl get service aspnet9-service

# Ingress-?? ????????? (?? ??????????)
kubectl get ingress aspnet9-ingress        

?? ????????? ?????????? ????????, ?????? ?????? ??????? ??????? "Running" ?? ?????? ????????? ????????????? ??????.

??????????? ?? ????????????

Kubernetes-?? ????????? ASP.NET 9 ?????????? ???????? ??????????? ?????????????? ????????????? ?? ?????????? ????????????? ???????????? ????????.

Horizontal Pod Autoscaler (HPA)

HPA ?????????? ???????? ??????????? ????????? ?? ???????? ?????? ?????????? ????????. ?? ????????????, ??? ?????????? ?????? ??????????? ???????? ?????? ?? ??????????? ??????? ?????????, ???? ????????? ????????.

???????? ????? aspnetapp-hpa.yaml:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: aspnet9-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: aspnet9-app-deployment
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70        

?? HPA ??????? ??????? ?????????? 3-??? 10-???, ???? CPU-? ?????????? ????????? 70%-?, ?? ??????????, ???? ????????? ????????.

???????????:

kubectl apply -f aspnetapp-hpa.yaml        

Prometheus ?? Grafana ????????????????

???????? ????????????????, ?????????????? Prometheus-??? ?? Grafana-? ??????????. Prometheus ???????? ?????????, ???? Grafana ???????????? ?????????????.

ASP.NET 9 ??????????? Prometheus ?????????? ????????

  1. ???????? ?????????? NuGet ????????:

dotnet add package prometheus-net.AspNetCore        

2. ????????? Program.cs:

using Prometheus;

var builder = WebApplication.CreateBuilder(args);

// ?????????? ????????...

var app = builder.Build();

// ...

// Prometheus ?????????? ?????????? ????????
app.UseMetricServer();
app.UseHttpMetrics();

app.Run();        

3. ????????? ?? ??????? ???????????? ?????? ??????????? ?????.

Prometheus ?? Grafana-? ??????????? Helm-?? ???????????

Kubernetes-?? Prometheus-??? ?? Grafana-? ??????? ??????? ???? Helm chart-???? ??????????:

# Helm-?? ???????? (?? ?? ?????)
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# Prometheus-?? ???????????? ????????
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

# Prometheus Stack-?? ??????????? (Prometheus + Grafana)
helm install prometheus prometheus-community/kube-prometheus-stack        

???? ??????, Grafana-?? ????????? ??????? ?????????? ?????? ASP.NET 9 ?????????? ????????????????. ???????? ??? ???????????, ???????? ??????????? .NET ?????????????????? ????????.

??????????? ?? ????????????

ASP.NET 9 ?????????? Kubernetes-?? ?????????????, ?????????????? ????????????? ?? ????????????? ?????????? ??????????????.

ConfigMap ?? Secret ?????????????????

Kubernetes-?? ????????? ????????? ?????????? ????????????? ???????? ????????. ???????? ??????????? ConfigMap ?? Secret ?????????.

ConfigMap ???-?????????? ????????????????

???????? ????? aspnetapp-configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: aspnet9-config
data:
  appsettings.json: |
    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "ApiSettings": {
        "BaseUrl": "https://api.example.com"
      }
    }        

Secret ?????????? ???????????????

???????? ????? aspnetapp-secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: aspnet9-secrets
type: Opaque
data:
  ConnectionStrings__DefaultConnection: BASE64_ENCODED_CONNECTION_STRING
  ApiKey: BASE64_ENCODED_API_KEY        
????????: Secret-?? ????????????? ???? ???? Base64-?? ????????????. ?????????: echo -n "your-secret-value" | base64

???? ?????????? Deployment-??

????????? ?????? Deployment YAML, ??? ?????????? ?? ??????????????:

spec:
  containers:
  - name: aspnet9-app
    # ...
    env:
    - name: ConnectionStrings__DefaultConnection
      valueFrom:
        secretKeyRef:
          name: aspnet9-secrets
          key: ConnectionStrings__DefaultConnection
    - name: ApiKey
      valueFrom:
        secretKeyRef:
          name: aspnet9-secrets
          key: ApiKey
    volumeMounts:
    - name: config-volume
      mountPath: /app/appsettings.json
      subPath: appsettings.json
  volumes:
  - name: config-volume
    configMap:
      name: aspnet9-config        

RBAC (Role-Based Access Control)

Kubernetes-?? ???????????? ????????????? ???????? RBAC - ??????? ??????????? ??????? ????????. ?? ?????????? ???????? ????????????, ?? ??? ?? ???????? ???? ?????????.

?? ?????? ASP.NET 9 ????????? ?????????????? Kubernetes API-????, ??????? ????????? ??????? ServiceAccount, Role ?? RoleBinding:

# ServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
  name: aspnet9-sa
  namespace: default

---
# Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: default
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]

---
# RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
- kind: ServiceAccount
  name: aspnet9-sa
  namespace: default
roleRef:
  kind: Role
  name: pod-reader
  apiGroup: rbac.authorization.k8s.io        

Network Policy ????????????????

Network Policy ?????????? ???????? ?????????????, ????? ?????? ???????? ??????????? ???????????. ?? ???????? ?????????? ???????? ?????????.

?????????, ?? ??????, ??? ????? ASP.NET 9 ????????? ?????? ????????? ????? ?????? ???????????? ????????????:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: aspnet9-network-policy
spec:
  podSelector:
    matchLabels:
      app: aspnet9-app
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: default
  egress:
  - to:
    - podSelector:
        matchLabels:
          app: database
    ports:
    - protocol: TCP
      port: 5432        

eShopOnContainers - ?????????????? ???????? ???????????

Microsoft-?? ???? ???????? ?????????????? ??????? eShopOnContainers ???? ???????? ?????, ?? ????? ???????? ASP.NET Core ?????????? ??????????????? ?? Kubernetes-?? ???????. ?? ??????????? ??????????, ????? ??????? ???????????, ?????????????? e-commerce ??????? ???????????????? ???????????????.

ASP.NET 9-?? ???????????? ????? eShopOnContainers ??????????, ???? ?????????? ????? ?????????????? ?? ???????????? ?????????? Kubernetes ???????????.

?????????? ???? ??????????????? ?????????????

????? ??????????? ??????????? ????? ?????????? .NET Framework ???????????? ?????????????. ??????? ????????:

  1. ????????? ?????? ???????????????
  2. ???????? ??????????? .NET Framework-???? ??????????? .NET/.NET Core-??
  3. ??????????????? Docker-??
  4. ?????????? Kubernetes ?????????

???-???? ????????? ???????????, ????????? ??????????? ASP.NET ????????? ????????????? "lift-and-shift" ??????? Windows ????????????? ?????????? AKS-?? (Azure Kubernetes Service). ???????????, ????? ???????? ???? .NET Core/ASP.NET-?? ??????? ?? Linux ????????????? ??????????.

?? ?????????????? ??????????? ???????? ???????????????, ????????????? ?? ?????????? ????????????.

Stack Overflow-? ?????????

??????? Q&A ???????? Stack Overflow ?????????? .NET ?????????????? ???????. ???? ?????? ??? ????????????? ????????????? ???????? .NET Framework-??? .NET Core-?? ?? ??????????? ?????????? Windows ???????????? Linux ????????????? Kubernetes-?? ???????????.

???? ?????????? ?????, "Stack Overflow-? .NET Core-?? ???????? ???? ????????? ??????, ?????? ???????????? ????? ???????? Linux ?????????????". ???? Stack Overflow ??????? ????????????? ?? Kubernetes-? ?????? ?????????? ??????????????, ?????????? ?????????? ?????????????, ?????????? ????????????? ?? ?????????? ??????????????.

????????????

????????? 1: ??????? ASP.NET 9 ???? ???????????

???????: ???????? ??????? ASP.NET 9 Web API, ???????? ???? ??????????????? ?? ??????????? Kubernetes-??.

????????:

  1. ???????? ????? ASP.NET 9 Web API ???????
  2. ???????? ???????????? ?????????? ?????????
  3. ???????? Dockerfile
  4. ?????? ?? ???????? Docker ?????
  5. ???????? Kubernetes ??????????? (Deployment, Service)
  6. ???????? ??????????? ??????? ?????????

????????? 2: ???????????? Helm Chart-?? ??????????

???????: ?????????? ???? ?????????? Kubernetes ??????????? Helm Chart-??, ??? ???????????? ????????????? ?? ???????????.

????????:

  1. ???????? Helm Chart ?????????
  2. ?????????? YAML ??????? Helm ??????????
  3. ???????? ???????? values.yaml-??
  4. ???????? ?????????? ??????????? (dev, staging, prod) values ???????
  5. ???????????? ?? ????????????? Chart-?

????????? 3: CI/CD ???????? ??????

???????: ???????? CI/CD ???????, ??????? ??????????? ???????, ????????????? ?? ??????????? ????????? ?????? ASP.NET 9 ?????????? Kubernetes-??.

????????:

  1. ???????? GitHub (?? ???? VCS) ??????????
  2. ????????????? GitHub Actions (?? Jenkins/Azure DevOps/etc)
  3. ???????? CI ???????: build, test, контейнер??ац??
  4. ???????? CD ???????: ??????????? Kubernetes-??
  5. ???????? ??????? ???????? ???????????? ?????????????

???????

?? ????? ?? ????????? ?????????? ??????? ??????? ?? ?????? ??????????? ???-???????????, ???????? ??????????? cloud-native ???????????? ????????? ??????????.


要查看或添加评论,请登录

David Shergilashvili的更多文章