A service is an abstraction for pods,
providing a stable, virtual IP (VIP) address. While pods may come and go,
services allow clients to reliably connect to the containers running in the
pods, using the VIP. The virtual in VIP means it’s not an actual
IP address connected to a network interface but its purpose is purely to
forward traffic to one or more pods. Keeping the mapping between the VIP and
the pods up-to-date is the job of kube-proxy, a process that
runs on every node, which queries the API server to learn about new services in
the cluster.
kubectl create -f https://raw.githubusercontent.com/mhausenblas/kbe/master/specs/services/rc.yaml
kubectl create -f
https://raw.githubusercontent.com/mhausenblas/kbe/master/specs/services/svc.yaml
Now we have the supervised pod running:
kubectl get pods
-l app=sise
kubectl describe pod rcsise-
6nq3k
You can, from within the cluster, access the pod directly
via its assigned IP XX.XX.X.X:
curl
172.17.
0.3:
9876/info
This is however, as mentioned above, not advisable since
the IPs assigned to pods may change. Hence, enter the simpleservice we’ve created:
kubectl get svc
kubectl describe svc simpleservice
The service keeps track of the pods it forwards traffic to through the
label, in our case
app=sise
.
From within the cluster we can now access
simpleservice
like so:
curl 172.30.228.255:80/info
What makes the VIP
172.30.228.255
forward
the traffic to the pod? The answer is: IPtables, which is essentially a long list of
rules that tells the Linux kernel what to do with a certain IP package.
Looking at the rules that concern our service (executed on a cluster
node) yields:
sudo iptables-save | grep simpleservice
Above you can see the four rules that
kube-proxy
has thankfully added to the routing
table, essentially stating that TCP traffic to 172.30.228.255:80
should be forwarded
to 172.17.0.3:9876
,
which is our pod.
Let’s now add a second pod by scaling up the RC supervising it:
kubectl scale --replicas=2 rc/rcsise
kubectl get pods -l app=sise
To delete apps.
kubectl delete svc simpleservice
kubectl delete rc rcsise
No comments:
Post a Comment