Use a Service to Access an Application in a Cluster
This page shows how to create a Kubernetes Service object that external clients can use to access an application running in a cluster. The Service provides load balancing for an application that has two running instances.
शुरू करने से पहले
आपको कुबरनेट्स क्लस्टर की ज़रूरत पड़ेगी और क्यूब सीटीएल कमांड लाइन साधन को समनुरूप करना होगा ताकि वो आपके क्लस्टर के साथ संवाद कर सकें। हमारी सलाह है की इस टुटोरिअल को क्लस्टर में रन करने के लिए कम से कम दो नोड का इस्तेमाल करे जो कि कंट्रोल प्लेन होस्ट के तरह ना एक्ट करे। अगर आपके पास पहले से क्लस्टर नही है, आप minikube की मदद से वह बना सकते है या आप नीचे दिए हुए इन दो कुबरनेट्स प्लेग्राउंड का इस्तेमाल कर सकते हैं:
उद्देश्य
- Run two instances of a Hello World application.
- Create a Service object that exposes a node port.
- Use the Service object to access the running application.
Creating a service for an application running in two pods
Here is the configuration file for the application Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
selector:
matchLabels:
run: load-balancer-example
replicas: 2
template:
metadata:
labels:
run: load-balancer-example
spec:
containers:
- name: hello-world
image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0
ports:
- containerPort: 8080
protocol: TCP
-
Run a Hello World application in your cluster: Create the application Deployment using the file above:
kubectl apply -f https://k8s.io/examples/service/access/hello-application.yaml
The preceding command creates a Deployment and an associated ReplicaSet. The ReplicaSet has two Pods each of which runs the Hello World application.
-
Display information about the Deployment:
kubectl get deployments hello-world kubectl describe deployments hello-world
-
Display information about your ReplicaSet objects:
kubectl get replicasets kubectl describe replicasets
-
Create a Service object that exposes the deployment:
kubectl expose deployment hello-world --type=NodePort --name=example-service
-
Display information about the Service:
kubectl describe services example-service
The output is similar to this:
Name: example-service Namespace: default Labels: run=load-balancer-example Annotations: <none> Selector: run=load-balancer-example Type: NodePort IP: 10.32.0.16 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 31496/TCP Endpoints: 10.200.1.4:8080,10.200.2.5:8080 Session Affinity: None Events: <none>
Make a note of the NodePort value for the Service. For example, in the preceding output, the NodePort value is 31496.
-
हैलो वर्ल्ड एप्लिकेशन चला रहे पॉड्स की सूची देखें:
kubectl get pods --selector="run=load-balancer-example" --output=wide
आउटपुट इस तरह का होगा:
NAME READY STATUS ... IP NODE hello-world-2895499144-bsbk5 1/1 Running ... 10.200.1.4 worker1 hello-world-2895499144-m1pwt 1/1 Running ... 10.200.2.5 worker2
-
अपने किसी एक नोड का पब्लिक IP पता प्राप्त करें जो हैलो वर्ल्ड पॉड चला रहा है। यह पता कैसे प्राप्त करें यह इस बात पर निर्भर करता है कि आपने अपना क्लस्टर कैसे सेट अप किया है। उदाहरण के लिए, यदि आप Minikube का उपयोग कर रहे हैं, तो आप
kubectl cluster-info
चलाकर नोड का पता देख सकते हैं। यदि आप Google Compute Engine इंस्टेंस का उपयोग कर रहे हैं, तो आप अपने नोड्स के पब्लिक पते देखने के लिएgcloud compute instances list
कमांड का उपयोग कर सकते हैं। -
अपने चुने हुए नोड पर, अपने नोड पोर्ट पर TCP ट्रैफ़िक की अनुमति देने वाला फ़ायरवॉल नियम बनाएं। उदाहरण के लिए, यदि आपकी सर्विस का NodePort मान 31568 है, तो पोर्ट 31568 पर TCP ट्रैफ़िक की अनुमति देने वाला फ़ायरवॉल नियम बनाएं। विभिन्न क्लाउड प्रोवाइडर्स फ़ायरवॉल नियमों को कॉन्फ़िगर करने के अलग-अलग तरीके प्रदान करते हैं।
-
हैलो वर्ल्ड एप्लिकेशन तक पहुंचने के लिए नोड पता और नोड पोर्ट का उपयोग करें:
curl http://<public-node-ip>:<node-port>
जहाँ
<public-node-ip>
आपके नोड का पब्लिक IP पता है, और<node-port>
आपकी सर्विस का NodePort मान है। सफल अनुरोध का जवाब एक हैलो संदेश है:Hello, world! Version: 2.0.0 Hostname: hello-world-cdd4458f4-m47c8
सर्विस कॉन्फ़िगरेशन फ़ाइल का उपयोग करना
kubectl expose
का उपयोग करने के विकल्प के रूप में, आप सर्विस बनाने के लिए
सर्विस कॉन्फ़िगरेशन फ़ाइल का उपयोग कर सकते हैं।
सफाई करना
सर्विस को हटाने के लिए, यह कमांड दर्ज करें:
kubectl delete services example-service
डिप्लॉयमेंट, रेप्लिकासेट और हैलो वर्ल्ड एप्लिकेशन चला रहे पॉड्स को हटाने के लिए, यह कमांड दर्ज करें:
kubectl delete deployment hello-world
आगे क्या है
सर्विसेज के साथ एप्लिकेशन कनेक्ट करना ट्यूटोरियल का पालन करें।