天天看點

單體倉庫下通過helm優雅的将微服務部署到k8s

目錄

  • 多體倉庫、單體倉庫介紹
  • helm subchart
  • 準備一個簡單的鏡像
  • 整體方式部署
  • 更新單個服務
  • 現狀每個服務單獨helm部署

Edison Zhou 介紹了多體倉庫、單體倉庫

helm介紹就不講了網上已有很多,helm subchart就是嵌套子級結構。

模闆項目添加 Middleware 以備後面展示使用,将項目打包成鏡像
namespace Test
{
    public class RequestCultureMiddleware
    {
        private readonly RequestDelegate _next;

        public RequestCultureMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            var cultureQuery = context.Request.Query["culture"];
            if (!string.IsNullOrWhiteSpace(cultureQuery))
            {
                var culture = new CultureInfo(cultureQuery);

                CultureInfo.CurrentCulture = culture;
                CultureInfo.CurrentUICulture = culture;

            }

            await context.Response.WriteAsync($"{context.Request.Host}{context.Request.Path}  {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }
    }

    public static class RequestCultureMiddlewareExtensions
    {
        public static IApplicationBuilder UseRequestCulture(
            this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RequestCultureMiddleware>();
        }
    }
}
           
準備helm
  • 目錄結構
    單體倉庫下通過helm優雅的将微服務部署到k8s

#安裝
helm install myserve --namespace=test --render-subchart-notes myserve
           

執行效果已折疊...

NAME: myserve
LAST DEPLOYED: Thu Nov  5 22:30:55 2020
NAMESPACE: test
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=ordering,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80

1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=marketing,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80

1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=catalog,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80

1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=basket,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80

1. Get the application URL by running these commands:
  http://www.ddrsql.com/api/basket
  http://www.ddrsql.com/api/catalog
  http://www.ddrsql.com/api/identity
  http://www.ddrsql.com/api/locations
  http://www.ddrsql.com/api/marketing
  http://www.ddrsql.com/api/ordering

1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=identity,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80

1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=locations,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80
           
單體倉庫下通過helm優雅的将微服務部署到k8s
單體倉庫下通過helm優雅的将微服務部署到k8s
通路配置過本地host
λ curl http://www.ddrsql.com/api/basket
www.ddrsql.com/api/basket  2020-11-05 22:37:06

λ curl http://www.ddrsql.com/api/catalog
www.ddrsql.com/api/catalog  2020-11-05 22:37:06

λ curl http://www.ddrsql.com/api/identity
www.ddrsql.com/api/identity  2020-11-05 22:37:07

λ curl http://www.ddrsql.com/api/locations
www.ddrsql.com/api/locations  2020-11-05 22:37:07

λ curl http://www.ddrsql.com/api/marketing
www.ddrsql.com/api/marketing  2020-11-05 22:37:07

λ curl http://www.ddrsql.com/api/ordering
www.ddrsql.com/api/ordering  2020-11-05 22:37:10
           
更新、調試相關指令
#整體更新
helm upgrade myserve --namespace=test --render-subchart-notes myserve

#安裝調試
helm install myserve --namespace=test --dry-run --debug --render-subchart-notes myserve
#更新調試
helm upgrade myserve --namespace=test --dry-run --debug --render-subchart-notes myserve
           

#調試更新單個服務
helm upgrade myserve --namespace=test --dry-run --debug myserve/charts/basket-api
           
調試輸出(如下)看上去很正常,隻有單個服務變更記錄輸出

upgrade.go:121: [debug] preparing upgrade for myserve...

upgrade.go:121: [debug] preparing upgrade for myserve
upgrade.go:129: [debug] performing update for myserve
upgrade.go:299: [debug] dry run for myserve
Release "myserve" has been upgraded. Happy Helming!
NAME: myserve
LAST DEPLOYED: Thu Nov  5 22:40:25 2020
NAMESPACE: test
STATUS: pending-upgrade
REVISION: 2
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
...

HOOKS:
---
# Source: basket/templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "myserve-basket-test-connection"
  labels:
    helm.sh/chart: basket-0.1.0
    app.kubernetes.io/name: basket
    app.kubernetes.io/instance: myserve
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
  annotations:
    "helm.sh/hook": test-success
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['myserve-basket:80']
  restartPolicy: Never
MANIFEST:
---
# Source: basket/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: myserve-basket
  labels:
    helm.sh/chart: basket-0.1.0
    app.kubernetes.io/name: basket
    app.kubernetes.io/instance: myserve
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
---
# Source: basket/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: myserve-basket
  labels:
    helm.sh/chart: basket-0.1.0
    app.kubernetes.io/name: basket
    app.kubernetes.io/instance: myserve
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: basket
    app.kubernetes.io/instance: myserve
---
# Source: basket/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myserve-basket
  labels:
    helm.sh/chart: basket-0.1.0
    app.kubernetes.io/name: basket
    app.kubernetes.io/instance: myserve
    app.kubernetes.io/version: "1.16.0"
    app.kubernetes.io/managed-by: Helm
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: basket
      app.kubernetes.io/instance: myserve
  template:
    metadata:
      labels:
        app.kubernetes.io/name: basket
        app.kubernetes.io/instance: myserve
    spec:
      serviceAccountName: myserve-basket
      securityContext:
        {}
      containers:
        - name: basket
          securityContext:
            {}
          image: "test:v1"
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          env:

          lifecycle:
            preStop:
              exec:
                command: ["/bin/sleep","20"]
          resources:
            {}

NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace test -l "app.kubernetes.io/name=basket,app.kubernetes.io/instance=myserve" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace test port-forward $POD_NAME 8080:80
           

但是更新完成後其它的子服務被删除了,被删除了了了...

#應用更新其中一個服務
helm upgrade myserve --namespace=test myserve/charts/basket-api
λ kubectl.exe get pod -n test
NAME                              READY   STATUS    RESTARTS   AGE
myserve-basket-54c5b7774d-cg48p   1/1     Running   0          5m11s
           

網上查了下有人有同樣的問題

已經有人提Issues,整體部署後支援更新單個服務

helm install myserve-basket --namespace=test myserve/charts/basket-api
helm upgrade myserve-basket --namespace=test myserve/charts/basket-api
           

流水線自動部署處理方式就不具體描述,根據實際需求處理。

最後坐等helm支援整體部署後支援更新單個服務。