单体仓库下通过helm优雅的将微服务部署到k8s

多体仓库、单体仓库介绍

Edison Zhou 介绍了多体仓库、单体仓库

helm subchart

helm介绍就不讲了网上已有很多,helm subchart就是嵌套子级结构。
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 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


访问配置过本地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部署

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

流水线自动部署处理方式就不具体描述,根据实际需求处理。
最后坐等helm支持整体部署后支持更新单个服务。

posted @ 2020-11-05 23:14  ddrsql  阅读(896)  评论(0编辑  收藏  举报