Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use K8S Technology to Control K8S Resources

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about how to use K8S technology to control K8S resources. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

Private Cloud K8S Resource Control principle

Based on the details of the previous series of articles, we can manually start the Docker image of the CentOS 7.4 operating system from the command line of the K8S cluster, and then users can log in to the CentOS container through SSH. But in practice, it is impossible for different users to manually start a CentOS image on the command line every time, and then use the command to check the IP address and port of the container, and then connect through ssh. We'd better encapsulate the startup, query and deletion of the Docker container, and then provide it to the user through the WEB page, which meets the needs of user behavior!

In our practice, the Docker container is managed in a kubernetes cluster, so the resources involved in resource control here are mainly kubernetes resources, such as Pod, RC, Service and so on.

The Master node in kubernetes runs the kube-apiserver module, which provides restful interface. We can program based on this interface to complete the control and management of cluster resources, such as Pod, RC, Service and other tasks such as application, query, deletion and so on.

To CRUD a kubernetes resource, such as a pod, you first need to know the interface that interacts with the kubernetes cluster. The kubectl client program is installed on the master node of kubernetes, which provides a command line interface for users to interact with the kubernetes cluster. Kubectl transmits user commands to apiserver through restful interface, so as to add, delete and modify resources. The process can be illustrated as follows:

Now that the principle is over, let's talk about how to implement the code level.

K8S resource control client

To sum up the above, it is not difficult to see that the CRUD control of K8s related resources can be realized through the restful interface. To use Kubernetes REST API to write applications, most languages can easily implement HTTP requests to operate the restful interface of kubernetes to control and query resources, but this paper mainly uses the existing kubernetes client to realize the resource control of kubernetes more elegantly. You do not need to write your own API to call, request / response and other types, you can directly use off-the-shelf client libraries to achieve.

K8s has a lot of client-side open source projects, official support and a lot of community maintenance.

Officially supported Kubernetes client libraries are limited, including the following:

Community-maintained client libraries are richer and are supported by basic popular programming languages. The following Kubernetes API client libraries are maintained by community creators and are not supported and maintained by the Kubernetes team:

This practice chooses k8s-client of Fabric8, which is implemented based on Java.

Let's talk about the code-level implementation based on this Clinet.

An example of K8S resource control code implementation

The following illustrates the k8s resource control operations commonly used in Fabric8 with the code:

Create a K8S client

String namespace = "default"; / / namespace name String master = "http://XXXX/"; / / configured as the master node address of your k8s cluster Config config = new ConfigBuilder (). WithMasterUrl (master). Build (); KubernetesClient client = new DefaultKubernetesClient (config)

Then the control of K8S resources, query and other operations are basically completed by the client!

Create Pod

We create the Pod resource from a pod.yaml file:

List resources = client.load (new FileInputStream (fileName)). Get (); / / fileName is an external pod.yaml file if (resources.isEmpty ()) {System.err.println ("No resources loaded from file:" + fileName); return;} HasMetadata resource = resources.get (0); if (resource instanceof Pod) {Pod pod = (Pod) resource; System.out.println ("Creating pod in namespace" + namespace) NonNamespaceOperation pods = client.pods () .inNamespace (namespace); Pod result = pods.create (pod); / / create a pod resource here! System.out.println ("Created pod" + result.getMetadata (). GetName ());} else {System.err.println ("Loaded resource is not a Pod!" + resource);}

Delete Pod

Delete is very simple, as long as you know the namespace and name where Pod is located, you can do it with a single line of code.

Client.pods (). InNamespace ("namespace name"). WithName ("pod name"). Delete ()

Query Pod

Query the log log of Pod and print it on the console:

Client.pods () .inNamespace ("namespace name") .withName ("pod name") .tailingLines (10) .watchLog (System.out))

For some additional information about Pod, you can refer to the code in the step of creating a Pod:

Pod result = pods.create (pod); / / create a pod resource here! System.out.println ("Created pod" + result.getMetadata (). GetName ()); / / similar to here, you can get a lot of similar information in addition to the name of Pod.

Modify Pod configuration

Pod updatedPod = client.pods (). InNamespace ("namespace name"). WithName ("pod name"). Edit () .editMetavigation () .addToLabels ("server2", "nginx2") .and () .done (); log ("Replaced testPod:", updatedPod)

The above from the creation of a K8S client to start a relatively complete introduction of a Pod resource CRUD operation, limited to space, other resources, such as RC, Service, Deployment and so on, the add, delete, change and search operations are similar, but the name is different, its interface a little analogy Pod can write the corresponding resource control code, here is no longer repeated, it is very important.

After reading the above, have you mastered how to use K8S technology to control K8S resources? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report