Concepts
For some parts of your application (e.g. frontends) you may want to expose a Service onto an external (outside of your cluster) IP address.
Kubernetes ServiceTypes allow you to specify what kind of service you want. The default is ClusterIP.
Type values and their behaviors are:
- ClusterIP: Exposes the service on a cluster-internal IP. Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType.
- NodePort: Exposes the service on each Node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service will route, is automatically created. You’ll be able to contact the NodePort service, from outside the cluster, by requesting :.
- LoadBalancer: Exposes the service externally using a cloud provider’s load balancer. NodePort and ClusterIP services, to which the external load balancer will route, are automatically created.
- ExternalName: Maps the service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up. This requires version 1.7 or higher of kube-dns.
Hands on Practice
- Create an External Load Balancer to Access an Application in a Cluster
[test@demotest nginx_kube_example]$ kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
deployment.apps "hello-world" created
[test@demotest nginx_kube_example]$ kubectl get pods --selector="run=load-balancer-example"
NAME READY STATUS RESTARTS AGE
hello-world-58f9949f8-hchpz 1/1 Running 0 5s
hello-world-58f9949f8-wfdcf 1/1 Running 0 5s
[test@demotest nginx_kube_example]$ kubectl expose deployment hello-world --type=LoadBalancer --name=example-service
service "example-service" exposed
[test@demotest nginx_kube_example]$ kubectl get services example-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-service LoadBalancer 10.96.52.33 <pending> 8080:30166/TCP 5s
[test@demotest nginx_kube_example]$ kubectl get services example-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-service LoadBalancer 10.96.52.33 129.146.88.108 8080:30166/TCP 34s
[test@demotest nginx_kube_example]$ curl http://129.146.88.108:8080
Hello Kubernetes!
[test@demotest nginx_kube_example]$ kubectl describe svc example-service-lb
Name: example-service-lb
Namespace: default
Labels: run=load-balancer-example
Annotations: <none>
Selector: run=load-balancer-example
Type: LoadBalancer
IP: 10.96.81.181
LoadBalancer Ingress: 129.146.155.148
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32507/TCP
Endpoints: 10.244.0.165:8080,10.244.1.30:8080
Session Affinity: None
External Traffic Policy: Cluster
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal EnsuringLoadBalancer 1m (x2 over 2m) service-controller Ensuring load balancer
Normal EnsuredLoadBalancer 1m (x2 over 1m) service-controller Ensured load balancer
[test@demotest nginx_kube_example]$ kubectl delete svc example-service-lb
service "example-service-lb" deleted
[test@demotest nginx_kube_example]$ kubectl delete deployment hello-world
deployment.extensions "hello-world" deleted
- Use NodePort to Access an Application in a Cluster
[test@demotest nginx_kube_example]$ kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
deployment.apps "hello-world" created
[test@demotest nginx_kube_example]$ kubectl get deployments hello-world
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-world 2 2 2 2 7s
[test@demotest nginx_kube_example]$ kubectl describe deployments hello-world
Name: hello-world
Namespace: jx
CreationTimestamp: Thu, 28 Jun 2018 01:56:48 +0000
Labels: run=load-balancer-example
Annotations: deployment.kubernetes.io/revision=1
Selector: run=load-balancer-example
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=load-balancer-example
Containers:
hello-world:
Image: gcr.io/google-samples/node-hello:1.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: hello-world-58f9949f8 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 14s deployment-controller Scaled up replica set hello-world-58f9949f8 to 2
[test@demotest nginx_kube_example]$ kubectl get replicasets
NAME DESIRED CURRENT READY AGE
hello-world-58f9949f8 2 2 2 24s
[test@demotest nginx_kube_example]$ kubectl describe replicasets
Name: hello-world-58f9949f8
Namespace: jx
Selector: pod-template-hash=149550594,run=load-balancer-example
Labels: pod-template-hash=149550594
run=load-balancer-example
Annotations: deployment.kubernetes.io/desired-replicas=2
deployment.kubernetes.io/max-replicas=3
deployment.kubernetes.io/revision=1
Controlled By: Deployment/hello-world
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=149550594
run=load-balancer-example
Containers:
hello-world:
Image: gcr.io/google-samples/node-hello:1.0
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 29s replicaset-controller Created pod: hello-world-58f9949f8-bg7pk
Normal SuccessfulCreate 29s replicaset-controller Created pod: hello-world-58f9949f8-sbrzx
[test@demotest nginx_kube_example]$ kubectl expose deployment hello-world --type=NodePort --name=example-service
service "example-service" exposed
[test@demotest nginx_kube_example]$ kubectl describe services example-service
Name: example-service
Namespace: jx
Labels: run=load-balancer-example
Annotations: <none>
Selector: run=load-balancer-example
Type: NodePort
IP: 10.96.70.43
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32322/TCP
Endpoints: 10.244.0.151:8080,10.244.1.16:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[test@demotest nginx_kube_example]$ kubectl get pods --selector="run=load-balancer-example" --output=wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-world-58f9949f8-bg7pk 1/1 Running 0 1m 10.244.0.151 129.146.112.202
hello-world-58f9949f8-sbrzx 1/1 Running 0 1m 10.244.1.16 129.146.135.107
[test@demotest nginx_kube_example]$ curl http://129.146.112.202:32322
Hello Kubernetes!
[test@demotest nginx_kube_example]$ curl http://129.146.135.107:32322
Hello Kubernetes!
Reference
https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types