编程知识 cdmana.com

It's so easy to develop k8s application with tye (1)

It happened to be happening recently Newbe.Claptrap Development of new version , Which USES Tye To assist k8s Application development . In this series, let's take a brief look at its usage .

Newbe.Claptrap It's a distributed development framework for dealing with concurrency problems easily . If you are reading this series for the first time . It is suggested to start with the introductory article at the end of this article .

install Tye

First , Make sure it's installed correctly netcore 2.1 Or above dotnet SDK.

Tye It's still in the development stage , therefore , At present, you can only install the preview version to use . You can search for the latest version through the following link , And copy... On the interface CLI install .

https://www.nuget.org/packages/Microsoft.Tye/

dotnet tool install --global Microsoft.Tye --version 0.6.0-alpha.21070.5

After installation , Run in the console tye, You can see the following results :

PS C:\tools\Cmder> tye
tye:
  Developer tools and publishing for microservices.

Usage:
  tye [options] [command]

Options:
  --no-default      Disable default options from environment variables
  -?, -h, --help    Show help and usage information
  --version         Show version information

Commands:
  init <path>        create a yaml manifest
  run <path>         run the application
  build <path>       build containers for the application
  push <path>        build and push application containers to registry
  deploy <path>      deploy the application
  undeploy <path>    delete deployed application

Create and run a test project

Next we create a netcore Application to test the deployment . Select a suitable location and run the following command to create a test project :

dotnet new sln -n TyeTest
dotnet new webapi -n TyeTest
dotnet sln .\TyeTest.sln add .\TyeTest\TyeTest.csproj

such , We've got a test solution and WebApi project . We can start the service locally by running the following command :

dotnet run --project .\TyeTest\TyeTest.csproj

After starting , Can be opened in browser  https://localhost:5001/swagger/index.html  Let's see what's up swagger Interface .

Use tye Run the app locally

Next , Let's close the app that's running , Change to use tye Start the test application locally .

In the solutions Directory , Use the console to run the following command :

tye run

After running , The following results may be obtained :

PS C:\Repos\TyeTest> tye run
Loading Application Details...
Launching Tye Host...

[12:11:30 INF] Executing application from C:\Repos\TyeTest\TyeTest.sln
[12:11:30 INF] Dashboard running on http://127.0.0.1:8000
[12:11:30 INF] Building projects
[12:11:32 INF] Launching service tyetest_9dd91ae4-f: C:\Repos\TyeTest\TyeTest\bin\Debug\net5.0\TyeTest.exe
[12:11:32 INF] tyetest_9dd91ae4-f running on process id 24552 bound to http://localhost:14099, https://localhost:14100
[12:11:32 INF] Replica tyetest_9dd91ae4-f is moving to a ready state
[12:11:32 INF] Selected process 24552.
[12:11:33 INF] Listening for event pipe events for tyetest_9dd91ae4-f on process id 24552

Follow the tips above , stay  http://127.0.0.1:8000  Successfully started tye dashboard. Open with browser dashboard You can view the list of deployed applications . As shown in the figure below :

tye dashboard

adopt dashboard , You can see that the test program has started , And bound  http://localhost:14099  and  https://localhost:14100. Actually in self-test , These two ports are randomly selected , So it's going to be different .

We're going through the open https Binding open swagger You can see and use  dotnet run  Same effect :https://localhost:14100/swagger

Deploy one locally k8s

Next , We will use Tye Deploy the application to k8s among . So in order to achieve this effect , First you need to prepare a k8s .

Deploy... On the development machine k8s There are many ways to do it , This experiment uses Docker Desktop + k8s The plan , The reason is nothing else , It's because there are more or less problems in the process of using other solutions . Specific developers can choose .

Docker Desktop + k8s The following links are very clear , It is suggested that developers can refer to :

《Docker Desktop start-up Kubernetes》https://www.cnblogs.com/weschen/p/12658839.html

In addition to k8s Outside the noumenon , It also needs to be installed nginx ingress and helm , You can also refer to the above article for installation .

Deploy the application to k8s in

but k8s Once configured , We can use tye Quickly publish the app to k8s To see .

Sign in docker registry

First , It needs to be for local docker To configure docker registry. Because in use tye The project will be packaged during the release process docker image And push it to a docker registry in .

Developers can choose a variety of ways to get their own docker registry:

  • Nexus OSS Repository
  • Alibaba cloud 、 Tencent cloud 、DaoCloud And so on, there are free quotas docker registry
  • docker hub, If the Internet is good

Use  docker login  Log in to your docker registry.

tye init establish tye.yml

In the solution catalog , Run the following command to create tye.yml The configuration file :

tye init

After running , You will get the following files in the solution folder :

name: tyetest
services:
  - name: tyetest
    project: TyeTest/TyeTest.csproj

This is one of the simplest tye.yml file .

modify tye.yml

We are tye.yml Add a line about docker registry Configuration of , To specify where the constructed image will be pushed :

name: tyetest
registry: registry.cn-hangzhou.aliyuncs.com/newbe36524
services:
  - name: tyetest
    project: TyeTest/TyeTest.csproj

for example , Here I use the... Of Alibaba cloud Hangzhou node docker registry, The namespace is newbe36524. So a line was added  registry: registry.cn-hangzhou.aliyuncs.com/newbe36524.

This is equivalent to , If you build , Will build a tag by  registry.cn-hangzhou.aliyuncs.com/newbe36524/tyetest:1.0.0  And push it to Alibaba cloud .

Download ahead of time netcore base image

Because what we're releasing this time is netcore Program , They will be built netcore Mirror image , therefore , In order to build more smoothly , It is recommended to download the basic image in advance by using the acceleration tool .

for example , I use in this use net5 TFM Applications for , therefore , It needs to be done locally first  mcr.microsoft.com/dotnet/aspnet:5.0  As a basic mirror .

Because now netcore The source of the underlying image has been removed from docker hub Migrate to mcr.microsoft.com. so , It is recommended to use  Newbe.McrMirror  Do an accelerated download .

For detailed usage, please refer to :https://github.com/newbe36524/Newbe.McrMirror

If developers don't know what basic image they need to pull at present , You can also try the next step to publish directly , View the underlying image content used in the process , And then pull .

Use tye deploy

Everything is ready , Now? , Continue to run the following command in the solution directory , Then you can publish :

tye deploy

The following results may be obtained :

PS C:\Repos\TyeTest> tye deploy
Loading Application Details...
Verifying kubectl installation...
Verifying kubectl connection to cluster...
Processing Service 'tyetest'...
    Applying container defaults...
    Compiling Services...
    Publishing Project...
    Building Docker Image...
            #1 [internal] load build definition from Dockerfile
            #1 sha256:a3872c76e0ccfd4bade43ecac3349907e0d110092c3ca8c61f1d360689bad7e2
            #1 transferring dockerfile: 144B done
            #1 DONE 0.0s

            #2 [internal] load .dockerignore
            #2 sha256:9e3b70115b86134ab4be5a3ce629a55cd6060936130c89b906677d1958215910
            #2 transferring context: 2B done
            #2 DONE 0.0s

            #3 [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0
            #3 sha256:3b35130338ebb888f84ec0aa58f64d182f10a676a625072200f5903996d93690
            #3 DONE 0.0s

            #7 [1/3] FROM mcr.microsoft.com/dotnet/aspnet:5.0
            #7 sha256:31acc33a1535ed7869167d21032ed94a0e9b41bbf02055dc5f04524507860176
            #7 DONE 0.0s

            #5 [internal] load build context
            #5 sha256:2a74f859befdf852c0e7cf66b6b7e71ec4ddeedd37d3bb6e4840dd441d712a20
            #5 transferring context: 3.87MB 0.0s done
            #5 DONE 0.1s

            #4 [2/3] WORKDIR /app
            #4 sha256:56abde746b4f39a24525b2b730b2dfb6d9688bcf704d367c86a4753aefff33f6
            #4 CACHED

            #6 [3/3] COPY . /app
            #6 sha256:4a3b76a4eea70c858830bad519b2d8faf5b6969a820b7e38994c2116d3bacab2
            #6 DONE 0.0s

            #8 exporting to image
            #8 sha256:e8c613e07b0b7ff33893b694f7759a10d42e180f2b4dc349fb57dc6b71dcab00
            #8 exporting layers 0.0s done
            #8 writing image sha256:8867f4e2ed6ccddb509e9c39e86c736188a78f348d6487d6d2e7a1b5919c1fdb
            #8 writing image sha256:8867f4e2ed6ccddb509e9c39e86c736188a78f348d6487d6d2e7a1b5919c1fdb done
            #8 naming to registry.cn-hangzhou.aliyuncs.com/newbe36524/tyetest:1.0.0 done
            #8 DONE 0.1s
        Created Docker Image: 'registry.cn-hangzhou.aliyuncs.com/newbe36524/tyetest:1.0.0'
    Pushing Docker Image...
        Pushed docker image: 'registry.cn-hangzhou.aliyuncs.com/newbe36524/tyetest:1.0.0'

    Validating Secrets...
    Generating Manifests...
Deploying Application Manifests...
    Applying Kubernetes Manifests...
        Verifying kubectl installation...
        Verifying kubectl connection to cluster...
        Writing output to 'C:\Users\Administrator\AppData\Local\Temp\tmp2BC2.tmp'.
        Deployed application 'tyetest'.
Time Elapsed: 00:00:12:99

From the output log , We can see that , The app has been released successfully . And use k8s dashboard perhaps k9s, We can all see that the application has been successfully deployed , And start and finish .

tyetest-674865dcc4-mxkd5    ●●  1/1   Δ            0 Running   Δ 10.1.0.73     docker-desktop     3m46s

It is worth noting that , There are several prerequisites to ensure that this step works properly :

  • We need to make sure that local kubectl Has been properly configured . Generally speaking , If using docker desktop, So it's configured
  • Need to ensure docker login Have succeeded . Developers can run the deployment before , Test whether the image can be pushed manually
  • MCR If the download speed of the image is not ideal , Remember to use Newbe.McrMirror Accelerate

Create and use ingress

To this step , We've finished releasing the app . however , Because there is no configuration nginx ingress, Although the service is already available in k8s It's running inside , But there was no external access . in other words , Using the browser on the computer is still in the state of being unable to open . so , We also need to configure for the service ingress. Not yet for k8s install ingress Friend, , It is recommended to check the previous installation k8s Relevant sections of .

here , We turn on tye.yml add to ingress Related configuration :

name: tyetest
registry: registry.cn-hangzhou.aliyuncs.com/newbe36524
services:
  - name: tyetest
    project: TyeTest/TyeTest.csproj
ingress:
  - name: tyetest-ingress
    bindings:
      - name: https
        protocol: https
    rules:
      - host: www.yueluo.pro
        service: tyetest

We added a ingress To configure , So that when traffic flows from ingress Get into , And the domain name is  www.yueluo.pro  When , It will be forwarded to tyetest service . This enables access from the outside k8s The purpose of internal services .

First , Use  tye run  You can check this effect locally . After running the command, it may be in dashboard See the following in :

tye dashboard2

among ,https://localhost:8310  Namely ingress Entry address . Because we use domain name binding , So you can use two ways to access to verify the effect :

  • stay hosts Add  www.yueluo.pro -> 127.0.0.1 The mapping relation of
  • Use http Request file direct access .

Here we use http Request file direct access :

GET https://localhost:8310/WeatherForecast
Host: www.yueluo.pro

such , We successfully verified the binding result .

Be careful , The port is not configured as a fixed port , So every time you run it, developers should pay attention to the changes that happen .

take ingress Deploy to k8s in

Next , stop it  tye run, function  tye deploy  take ingress And applications to k8s in .

Be careful ,ingress It can take tens of seconds to deploy , So we need to wait .

After deployment , You can go through k8s dashboard perhaps k9s View the results of the deployment .

also , You can use the following http Request to verify the results of the deployment :

GET https://localhost/WeatherForecast
Host: www.yueluo.pro

The result is the same as before .

from k8s Uninstall the application in

Uninstall app , It's simple ,tye undeploy.

PS C:\Repos\TyeTest> tye undeploy
Loading Application Details...
Found 3 resource(s).
Deleting 'Service' 'tyetest' ...
Deleting 'Deployment' 'tyetest' ...
Deleting 'Ingress' 'tyetest-ingress' ...
Time Elapsed: 00:00:02:87

Summary

This article , We briefly introduced how to use tye Simple steps to run or deploy an application . In the actual process, there are many options that can be extended and customized . Interested friends can check  https://github.com/dotnet/tye  Learning from the content of .

Next , We will deploy some more complex multi instance applications in the future .

Last but not least !

If the reader is interested in the content , Welcome to forward 、 Comment on 、 Collect articles and projects .

Recently, the author is building to Reaction formula Actor Pattern and Event source A set of server-side development framework for theoretical basis . Hope to provide developers with the ability to easily develop “ Distributed ”、“ Scalable horizontally ”、“ High testability ” Application system ——Newbe.Claptrap

This article is a technical selection of the framework , It's part of the technology .

Contact information :

You can also refer to other selections in this series :

Introduction to theory

  1. Newbe.Claptrap - A set of “ Event source ” and “Actor Pattern ” As the basic theory of the server development framework

Introduction to terminology

  1. Actor Pattern
  2. Event source (Event Sourcing)
  3. Claptrap
  4. Minion
  5. event (Event)
  6. state (State)
  7. State snapshot (State Snapshot)
  8. Claptrap The design (Claptrap Design)
  9. Claptrap factory (Claptrap Factory)
  10. Claptrap Identity
  11. Claptrap Box
  12. Claptrap Life cycle (Claptrap Lifetime Scope)
  13. serialize (Serialization)

Introduction to implementation

  1. Newbe.Claptrap Introduction to framework , First step —— Create project , Simple shopping cart
  2. Newbe.Claptrap Introduction to framework , The second step —— Simple business , empty cart
  3. Newbe.Claptrap Introduction to framework , The third step —— Definition Claptrap, Manage inventory of goods
  4. Newbe.Claptrap Introduction to framework , Step four —— utilize Minion, Order goods

Example practice

  1. Build a simple train ticketing system ,Newbe.Claptrap Framework use cases , First step —— Business analysis
  2. Online experience of train ticketing system

Other stories

  1. On the application of reactive programming in the server , Database operation optimization , from 20 Seconds to 0.5 second
  2. On the application of reactive programming in the server , Database operation optimization , To speed up Upsert
  3. 100000 simultaneous online users , How much memory is needed ?——Newbe.Claptrap Frame horizontal extension experiment
  4. docker-mcr Download at full speed dotnet Mirror image
  5. More than a dozen global technology experts , For nearly ten hours .Net Introduction to microservice
  6. Young woodcutter , You dropped this for free 8 nucleus 4G Public network server , It's still available at any time Docker The experiment platform ?
  7. How to use dotTrace To diagnose netcore Application performance issues
  8. Just ten steps , You can apply expression trees to optimize dynamic calls
  9. Use Tye Auxiliary development k8s The application is so simple ( One )

GitHub Project address :https://github.com/newbe36524/Newbe.Claptrap

Gitee Project address :https://gitee.com/yks/Newbe.Claptrap

What you are looking at is published in  www.newbe.pro  Blog post on , The actual development document iterates with the version . To view the latest development documents , Need to move  claptrap.newbe.pro.

Newbe.Claptrap

------ In this paper, the end ------
  • The author of this article : newbe36524
  • Link to this article : https://www.newbe.pro/Newbe.Claptrap/Try-Tye-1/
  • Copyright notice :  All articles in this blog except special statement , All adopt  BY-NC-SA  license agreement . Reprint please indicate the source !

版权声明
本文为[Newbe36524]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/01/20210131193430915p.html

Scroll to Top