In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to build a project with .NET Core+Kubernetes". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to build a project with .NET Core+Kubernetes.
In Kubernetes, controllers such as Deployment, Replicaset and Daemonset of Pod resources are often used to manage stateless applications. The corresponding IP, name, start and stop order of the Pod they manage are random, and there is no relationship between Pod. In fact, when deploying an application cluster, instances may need to be associated with each other (startup sequence, role), such as MySQL and MongoDB, so StatefulSet is a type of resource introduced to run stateful services. StatefulSet maintains a unique and fixed identifier for each Pod, creates a dedicated storage volume for it if necessary, and retains the original identifier and storage volume when the Pod is rebuilt.
A complete StatefulSet usually consists of three parts: StatefulSet, VolumeClaimTemplate, and Headless Service.
StatefulSet is used for the definition and control of Pod resources. In StatefulSet mode, Pod has its own fixed naming rules (StatfulSet name + index where Pod was created). Assuming that the StatefulSet name set is k8sdemothesis replicas is 3, the corresponding Pod names will be k8sdemo-0, k8sdemo-1 and k8sdemo-0, respectively. At the same time, the Pod replica can be scaled up and down by sequence number.
VolumeClaimTemplate is used to define the PVC declaration of storage required by Pod, and PVC binds with PV to provide proprietary fixed storage volumes.
Headless Service (clusterIP: None) is used to generate resolvable DNS domain name records for Pod. Based on the ordered rules of Pod names, Pod domain names will not change (Pod name .serviceName), which also ensures the stability of Pod network identity.
Let's continue to take the beckjin/k8sdemo:1.2.0 image built by the .NET Core project as an example, adding the ability of the interface to access logging. The API access log is recorded by integrating log4net, and the log is output to the / Data/ directory, and each Pod has its own log file (this is only a hypothetical scenario, so don't take it seriously. In fact, log recording generally uses a unified log collection tool).
Define resources
K8sdemo-statefulset.yaml:
ApiVersion: apps/v1
Kind: StatefulSet
Metadata:
Name: k8sdemo
Spec:
ServiceName: "k8sdemo-service" # needs to be consistent with the created service name
Replicas: 3
Selector:
MatchLabels:
Name: k8sdemo
Template:
Metadata:
Labels:
Name: k8sdemo
Spec:
Containers:
-name: k8sdemo
Image: beckjin/k8sdemo:1.2.0
ImagePullPolicy: IfNotPresent
VolumeMounts:
-name: data
MountPath: / app/Data # Mount the Data directory in the container
VolumeClaimTemplates: # define template and automatically create PVC
-metadata:
Name: data
Spec:
AccessModes:
-ReadOnlyMany
Resources:
Requests:
Storage: 100Mi
StorageClassName: "k8sdemo-sc" # will automatically bind to the PV that matches the storageClassName in the cluster
K8sdemo-service.yaml:
ApiVersion: v1
Kind: Service
Metadata:
Name: k8sdemo-service
Spec:
ClusterIP: None
Ports:
-port: 80
TargetPort: 80
Selector:
Name: k8sdemo
The serviceName field needs to be set in StatefulSet mode to tell the StatefulSet controller which service to use to parse the Pod it manages. At the same time, the PVC is defined through the volumeClaimTemplates field, and the StatefulSet controller automatically creates the name of the PVC,PVC corresponding to the Pod as (volumeClaimTemplateName)-(podName), and then the PVC is automatically bound to the PV that meets the requirements. If the PV does not support automatic creation, it can be done manually. In addition, when Pod is deleted, PVC and PV will still be retained, and the corresponding PVC and PV will be re-associated when Pod is rebuilt.
Here, we also use the NFS to create PV to implement storage, and create 3 (data-k8sdemo-pv- [1 / 3]) PV that meet the definition requirements, as follows:
ApiVersion: v1
Kind: PersistentVolume
Metadata:
Name: data-k8sdemo-pv-1
Spec:
Nfs:
Server: 192.168.124.21
Path: / statefulset/data1
AccessModes:
-ReadOnlyMany
Capacity:
Storage: 100Mi
StorageClassName: k8sdemo-sc
Deployment and testing
Create PV and StatefulSet:
Kubectl apply-f k8sdemo-statefulset-pv1.yaml
Kubectl apply-f k8sdemo-statefulset-pv2.yaml
Kubectl apply-f k8sdemo-statefulset-pv3.yaml
Kubectl apply-f k8sdemo-statefulset.yaml
Note: the PV naming order does not represent the binding order of the PVC, and there is no relationship between the two, so there is no need to question the numerical number correspondence of the figure above.
Create a Service:
Kubectl apply-f k8sdemo-service.yaml
Because Service defines Headless mode, you need to enter the Pod for API access testing. For example, kubectl exec-it k8sdemo-0 bash enters the Pod of k8sdemo-0 and accesses it through the domain name Pod. ServiceName, as follows:
Curl k8sdemo-0.k8sdemo-service/weatherforecast
Curl k8sdemo-1.k8sdemo-service/weatherforecast
Curl k8sdemo-2.k8sdemo-service/weatherforecast
View the API access log in the NFS mount directory. The following is the log in Pod k8sdemo-1:
2020-09-20 06 k8sdemo-1 01 http://k8sdemo-1.k8sdemo-service/weatherforecast 17451 [17] INFO [k8sdemo-1]-Request starting HTTP/1.1 GET
2020-09-20 06 INFO [17] INFO [k8sdemo-1]-Executing endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 Route matched with 01 INFO [k8sdemo-1]-Route matched with {action = "Get", controller = "WeatherForecast"}. Executing controller action with signature system. Collection. Generic.IEnumerable`1 [T.K8SDemo.WeatherForecast] Get () on controller T.K8SDemo.Controllers.WeatherForecastController (T.K8SDemo).
2020-09-20 06 INFO [k8sdemo-1]-Executing ObjectResult, writing value of type 'T.K8SDemo.WeatherForecast []'.
2020-09-20 06 Executed action T.K8SDemo.Controllers.WeatherForecastController.Get 01 Executed action T.K8SDemo.Controllers.WeatherForecastController.Get 17460 [17] INFO [k8sdemo-1]-Executed action T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo) in 2.3627ms
2020-09-20 06 Executed endpoint 01V 17460 [17] INFO [k8sdemo-1]-Executed endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 Request finished in 9.9194ms 01 application/json; charset=utf-8 17461 [17] INFO [k8sdemo-1]-200 application/json; charset=utf-8
Execute kubectl delete pod k8sdemo-1 to delete Pod k8sdemo-1, wait for a while, k8sdemo-1 will automatically restore, and then revisit curl k8sdemo-1.k8sdemo-service/weatherforecast, and the log will still be appended to the original file, which means that the original state has been retained.
2020-09-20 06 k8sdemo-1 01 http://k8sdemo-1.k8sdemo-service/weatherforecast 17451 [17] INFO [k8sdemo-1]-Request starting HTTP/1.1 GET
2020-09-20 06 INFO [17] INFO [k8sdemo-1]-Executing endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 Route matched with 01 INFO [k8sdemo-1]-Route matched with {action = "Get", controller = "WeatherForecast"}. Executing controller action with signature system. Collection. Generic.IEnumerable`1 [T.K8SDemo.WeatherForecast] Get () on controller T.K8SDemo.Controllers.WeatherForecastController (T.K8SDemo).
2020-09-20 06 INFO [k8sdemo-1]-Executing ObjectResult, writing value of type 'T.K8SDemo.WeatherForecast []'.
2020-09-20 06 Executed action T.K8SDemo.Controllers.WeatherForecastController.Get 01 Executed action T.K8SDemo.Controllers.WeatherForecastController.Get 17460 [17] INFO [k8sdemo-1]-Executed action T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo) in 2.3627ms
2020-09-20 06 Executed endpoint 01V 17460 [17] INFO [k8sdemo-1]-Executed endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 Request finished in 9.9194ms 01 application/json; charset=utf-8 17461 [17] INFO [k8sdemo-1]-200 application/json; charset=utf-8
2020-09-20 06 k8sdemo-1 17 Request starting HTTP/1.1 GET 06467 [12] INFO [k8sdemo-1]-Request starting HTTP/1.1 GET
2020-09-20 06 INFO [12] INFO [k8sdemo-1]-Executing endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 INFO [12] INFO [k8sdemo-1]-Route matched with {action = "Get", controller = "WeatherForecast"}. Executing controller action with signature system. Collection. Generic.IEnumerable`1 [T.K8SDemo.WeatherForecast] Get () on controller T.K8SDemo.Controllers.WeatherForecastController (T.K8SDemo).
2020-09-20 06 INFO [12] INFO [k8sdemo-1]-Executing ObjectResult, writing value of type 'T.K8SDemo.WeatherForecast []'.
2020-09-20 06 INFO [12] INFO [k8sdemo-1]-Executed action T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo) in 17.1904ms
2020-09-20 06 INFO [12] INFO [k8sdemo-1]-Executed endpoint 'T.K8SDemo.Controllers.WeatherForecastController.Get (T.K8SDemo)'
2020-09-20 06 Request finished in 84.3414ms 1715 06550 [12] INFO [k8sdemo-1]-200 application/json; charset=utf-8
In addition, the effect is the same when scaling a copy of Pod, which maintains the state that Pod has. Of course, the example in this paper is different from the cluster deployment of some components, such as MySQL, where data synchronization is performed among instances to achieve data consistency, and eventually each instance is associated with its own data storage volume.
Thank you for reading, the above is the content of "how to build a project with .NET Core+Kubernetes". After the study of this article, I believe you have a deeper understanding of how to build a project with .NET Core+Kubernetes, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.