Creating a Service for a Deployment with an external IP

Kubernetes Deployment is the ideal resource type for a Service. For Pods supervised by the ReplicaSet and Deployment, the Kubernetes system has a controller manager to look over the their life cycles. It is also helpful for updating the version or state of the program by binding the existing Services to another Deployment. For the following commands, we create a Deployment first, and attach a Service with an external IP:

// using subcommand "run" and assign 2 replicas
$ kubectl run nginx-deployment --image=nginx --port=80 --replicas=2 --labels="env=dev,project=My-Happy-Web,role=frontend"
deployment.apps "nginx-deployment" created
// explicitly indicate the selector of Service by tag "--selector", and assign the Service an external IP by tag "--external-ip"
// the IP 192.168.122.102 demonstrated here is the IP of one of the Kubernetes node in system
$ kubectl expose deployment nginx-deployment --port=8080 --target-port=80 --name="another-nginx-service" --selector="project=My-Happy-Web,role=frontend" --external-ip="192.168.122.102"
service "another-nginx-service" exposed

Let's go ahead and check the details of the newly created Service, another-nginx-service:

$ kubectl describe svc another-nginx-service
Name: another-nginx-service
Namespace: default
Labels: env=dev
project=My-Happy-Web
role=frontend
Annotations: <none>
Selector: project=My-Happy-Web,role=frontend
Type: ClusterIP
IP: 10.100.109.230
External IPs: 192.168.122.102
Port: <unset> 8080/TCP
TargetPort: 80/TCP
Endpoints: 192.168.79.15:80,192.168.79.21:80,192.168.79.24:80
Session Affinity: None
Events: <none>

Apart from the Service IP (in the case of the preceding command, 10.100.109.230), which can be accessed within the Kubernetes system, the Service can now be connected through an external one (192.168.122.102, for example) beyond the Kubernetes system. While the Kubernetes master is able to communicate with every node, in this case, we can fire a request to the Service such as the following command:

$ curl 192.168.122.102:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.22.160