In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to analyze the types of Kubernetes services. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
In Kubernetes, a service can always make its network access to one or a group of Pod. The service will select the Pod based on the label and when networking these services, it will select all the Pod in the cluster that match the selector of the service, select one of them, and forward the network request to it.
Kubernetes Service vs Deployment how should we distinguish between Deployment and service in K8S?
Deployment is mainly responsible for keeping a set of pod running in the cluster.
The service is mainly responsible for enabling network access to a set of pod in the cluster
We can use deployment instead of services, so we can keep several identical Pod running in the K8S cluster. In addition, the size of Deployment can be expanded and reduced, and pod can be replicated. In Kubernetes, a single pod can be accessed directly through a network request, so it is difficult to track the pod.
We can also use a service type without the need for deployment. If we do this, we will create a single pod instead of creating all the pod together as we did in deployment. However, we have another alternative where our services can route network requests to these Pod by choosing based on the tags assigned to them.
How do we find Kubernetes services?
In Kubernetes, there are two ways to discover services:
DNS type. DNS server is added to the cluster to observe that Kubernetes API creates a DNS record set for each new service. When DNS is enabled for the entire cluster, all Pod should be able to resolve service names automatically.
ENV variable. In this discovery method, a pod runs on a node, so kubelet adds environment variables for each active service.
What are ClusterIP, NodePort, and LoadBalancer?
The type attributes in the service specification determine how the service is exposed to the network. For example, ClusterIP, NodePort and LoadBalancer.
ClusterIP- default value. This service can only be accessed from within the Kubernetes cluster.
NodePort- this allows services to be accessed through static ports on each node in the cluster.
LoadBalancer- services can be accessed externally through the cloud provider's load balancer function. Aliyun, AWS, and Azure all provide this feature.
How to create a service
With the help of deployment kind, a simple example in the form of "Hello World" App will help you better understand how to create a service.
Our procedure is that when we see that the application has been deployed and running in a up state, we will create a service (Cluster IP) to access the application in Kubernetes.
Now, let's create a running deployment
Kubectl run hello-world-replicas=3-labels= "run=load-balancer-example"-image=gcr.io/google-samples/node-hello:1.0-port=8080 ".
Here, this command creates a deployment with two copies of the application in Kubernetes.
Next,
Run "kubectl get deployment hello-world" so see that the deployment is running.Now we can check the replicaset and pods that the deployment created.$ kubectl get deployments hello-worldNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEhello-world 3 3 3 76s
The application is now running. If you want to access the newly created application, we need to create a service of type ClusterIP:
Create a YAML manifest for the service and apply it, or
Using the kubectl expose command, this is a simpler option. Because this command can create a service without creating a YAML file.
$kubectl expose deployment hello-world-type=ClusterIP-name=example-serviceservice "example-service" exposed
Here, we will create a service called example-service with the type ClusterIP.
So, now we will visit our application:
Run "kubectl get service example-service" to get our port number.
Then we need to execute the port-forward command. Because our service type is ClusterIP, it can only be accessed within the cluster, so we must forward the port to the local port in the cluster to access our application.
We can use other types, such as LoadBalancer, which will create a LB in AWS or GCP, and then we can access the application using the DNS address given to LB and our port number.
$kubectl get service example-serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEexample-service ClusterIP 100.20.167.76 8080/TCP 1h $kubectl port-forward service/example-service 8080:8080Forwarding from 127.0.0.1 8080/TCP 8080-> 8080
Now we can browse http://localhost:8080 from the workstation, and we should see:
Hello Kubernetes service NodePort YAML example
This sample YAML creates a service that can be used for external network requests. Here, we mention NodePort with Value, so the service is mapped to the port on each node in the cluster.
The following is an example of yaml, which shows how we can use NodePort service types in Kubernetes.
Kind: Service apiVersion: v1 metadata: name: hostname-service spec: # Expose the service on a static port on each node # so that we can access the service from outside the cluster type: NodePort# When the node receives a request on the static port (30163) # "select pods with the label 'app' set to' echo-hostname'" # and forward the request to one of them selector: app: echo-hostnameports: # Three types of ports for a service # nodePort-a static port assigned on each The node # port-port exposed internally in the cluster # targetPort-the container port to send requests to-nodePort: 30163 port: 8080 targetPort: 80 this is the end of the analysis on how to implement Kubernetes service types I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.