Services are used to access a Pod inside and outside a cluster.
Service has a static IP, DNS, Port and load balances requests to the Pods.
Service uses Label selector to choose Pods.
Service Discovery
Options:
- DNS based
- Environment variables
Using the ReplicationController
we built here: kubectl: create pod
we’ll make an app visible to the outside world.
Checking it’s not visible currently:
kubectl describe rc
kubectl describe pod/hello-rc-tp5vf
shows:
1 2 |
IP: 172.17.0.9 Port: 8080/TCP |
and
1 2 |
curl 172.17.0.9:8080 curl: (7) Failed to connect to 172.17.0.9 port 8080: Connection refused |
Expose port with:
1 2 |
kubectl expose rc hello-rc --name=hello-svc --target-port 8080 --type=NodePort service/hello-svc exposed |
then
kubectl describe svc hello-svc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Name: hello-svc Namespace: default Labels: app=hello-world Annotations: <none> Selector: app=hello-world Type: NodePort IP: 10.111.196.170 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 31094/TCP Endpoints: 172.17.0.10:8080,172.17.0.11:8080,172.17.0.12:8080 + 17 more... Session Affinity: None External Traffic Policy: Cluster Events: <none> |
to show the service.
To delete pods, replicationcontroller or service use:
kubectl delete -f pod.yml
kubectl delete -f rc.yml
kubectl delete svc hello-svc
Service Types
ClusterIP: Stable internal cluster IP
NodePort: exposes the app outside of the cluster by adding a cluster-wide port on top of ClusterIP
LoadBalancer: integrates NodePort with cloud-based load balancers
Creating the Service above with a yml file looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
apiVersion: v1 kind: Service metadata: name: hello-svc labels: app: hello-world spec: type: NodePort ports: - port: 8080 protocol: TCP selector: app: hello-world |
Note: port
is what the app is using inside the container. This then gets mapped to NodePort.