【 K8S Technology Stack to build personal private cloud series of articles directory 】

  • Using K8S technology Stack to build personal private Cloud
  • Build personal private cloud using K8S Technology Stack (serial: K8S Cluster Construction)
  • Build personal Private Cloud using K8S Stack (Serial: K8S Environment Understanding and Practice)
  • Build personal Private cloud using K8S Technology Stack (Serial: Basic Image Making and Experiment)
  • Building personal Private Cloud using K8S Technology Stack (Serial: Resource Control Research)
  • Use K8S technology stack to build personal private cloud (Serial: Private Cloud Client Building)

Note: This article was published on My public account CodeSheep. You can subscribe by holding down or scanning the heart below ↓ ↓ ↓


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 to use it. However, it is impossible for different users to manually start a CentOS image in the command line every time, and then use commands to check the IP address and port number of the container, and then connect to the container through SSH. We had better encapsulate a layer of Docker container startup, query, delete, and so on, and then provide users with operations through the WEB page, which can meet the needs of user behavior!

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

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

To CRUD a Kubernetes resource, such as a POD, you first need to know the interface to interact with the Kubernetes cluster. 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 sends user commands to Apiserver through restful interfaces to add, delete, and modify resources. The process can be illustrated as follows:

With the principles behind it, let’s talk about how the code level is implemented.


K8S Resource control client

In summary, it is not difficult to see that THE CRUD control of K8S-related resources can be realized through restful interfaces. To use Kubernetes REST API to write applications, most languages can easily implement HTTP requests to operate Kubernetes restful interface to control and query resources, But this article mainly uses the existing Kubernetes client to realize the resource control of Kubernetes more elegantly. You don’t need to write your own API to do the call, request/response, etc., you can use off-the-shelf client libraries to do it.

K8s has many open source client projects, with official support and community maintenance.

Kubernetes client library (Kubernetes client library)

Kubernetes API client libraries are maintained by the community creator, Kubernetes team will not provide support and maintenance:

This practice uses Fabric8 K8S-Client based on Java.

Here’s a code-level implementation based on Clinet


K8S resource control code implementation example

Fabric8 k8S resource control operations

  • Example Create a K8S client
String namespace = "default"; // Namespace name String master ="http://XXXX/"; Config Config = new ConfigBuilder().withmasterUrl (master).build(); KubernetesClient client = new DefaultKubernetesClient(config);Copy the code

The following K8S resource control, query and other operations are basically completed by the client!

  • Create a Pod

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

List<HasMetadata> resources = client.load(new FileInputStream(fileName)).get(); // fileName is an external pod.yaml fileif (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<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = client.pods().inNamespace(namespace); Pod result = pods.create(pod); // Create pod resource here!! System.out.println("Created pod " + result.getMetadata().getName());
  } else {
	System.err.println("Loaded resource is not a Pod! " + resource);
  }
Copy the code
  • Delete the Pod

Deleting a Pod is as simple as knowing the namespace and name of the Pod in one line of code

client.pods().inNamespace("The namespace name").withName("Pod name").delete();
Copy the code
  • Query the Pod

Query Pod log and print it on console:

client.pods().inNamespace("The namespace name").withName("Pod name").tailingLines(10).watchLog(System.out))
Copy the code

For additional information about a Pod, refer to the code in the Create Pod step:

Pod result = pods.create(pod); // Create pod resource here!! System.out.println("Created pod "+ result.getMetadata().getName()); // Similar to here, you can get a lot of similar information besides the name of PodCopy the code
  • Modifying the Pod Configuration
Pod updatedPod = client.pods().inNamespace("The namespace name").withName("Pod name").edit()
          .editMetadata()
          .addToLabels("server2"."nginx2")
          .and().done();
log("Replaced testPod:", updatedPod);
Copy the code

The CRUD operation of a Pod resource is introduced completely from the creation of a K8S client above. Due to limited space, the add, delete, change and check operations of other resources, such as RC, Service, Deployment, etc., are similar except for their different names. The corresponding resource control code can be written by using a slightly similar interface to Pod. I won’t repeat it here. It’s important to draw inferences.


Afterword.

  • The author’s more original articles are here, welcome to watch

  • My Personal Blog

The author has more SpringBt practice articles here:

  • Spring Boot application monitoring actual combat
  • The SpringBoot application is deployed in an external Tomcat container
  • ElasticSearch in SpringBt practice
  • A preliminary study on Kotlin+SpringBoot joint programming
  • Spring Boot Logging framework practices
  • SpringBoot elegant coding: Lombok plus

If you are interested, take some time to read some of the author’s articles on containerization and microservitization:

  • Use K8S technology stack to create personal private cloud serial articles
  • Nginx server configuration from a detailed configuration list
  • Docker container visual monitoring center was built
  • Use ELK to build Docker containerized application log center
  • RPC framework practice: Apache Thrift
  • RPC framework practice: Google gRPC
  • Establishment of microservice call chain tracking center
  • Docker containers communicate across hosts
  • Preliminary study on Docker Swarm cluster
  • Several guidelines for writing dockerFiles efficiently