In our last post we talked about an important Kubernetes networking construct – the service.  Services provide a means for pods running within the cluster to find other pods and also provide rudimentary load balancing capabilities.  We saw that services can create DNS entries within Kube-DNS which makes the service accessible by name as well.  So now that we know how you can use services to access pods within the cluster it seems prudent to talk about how things outside of the cluster can access these same services.  It might make sense to use the same service construct to provide this functionality, but recall that the services are assigned IP addresses that are only known to the cluster.  In reality, the service CIDR isnt actually routed anywhere but the Kubernetes nodes know how to interact with service IPs because of the netfilter rules programmed by the kube-proxy.  The service network just needs to be unique so that the containers running in the pod will follow their default route out to the host where the netfilter rules will come into play.  So really the service network is sort of non-existent from a routing perspective as it’s only locally significant to each host.  This means that it can’t really be used by external clients since they wont know how to route to it either.  That being said, we have a few other options we can use most of which still rely on the service construct.  Let’s look at them one at a time…

ExternalIP

In this mode – you are essentially just assigning a service an external IP address.  This IP address can be anything you want it to be but the catch is that you’re on the hook to make sure that the external network knows to send that traffic to the Kubernetes cluster nodes.  In other words – you have to ensure that traffic destined to the assigned external IP makes it to a Kubernetes node.  From there, the service construct will take care of getting it where it needs to be.  To demonstrate this, let’s take a look at our lab where we left it after our last post

We had two different pod deployments running in the cluster in addition to the ‘net-test’ deployment but we wont need that for this example.  We had also defined a service called ‘svc-test-1’ that is currently targeting the pods of the ‘deploy-test-2’ deployment matching the selectors app=web-front-end and version=v2.  As we did when we changed the service selector, let’s once again edit the service and add another parameter.  To edit the service use this command…

In the editor, add the ‘externalIPs:’ list parameter followed by the IP address of 169.10.10.1 as shown in the highlighted field below…

When done, save the service definition by closing the file as you typically would (ESC, :wq, ENTER). You should get confirmation that the service was edited when you return to the console. What we just did was told Kubernetes to answer on the IP address of 169.10.10.1 for the service ‘svc-test-1’. To test this out, let’s point a route on our gateway for 169.10.10.0/24 at the host ubuntu-2. Note that ubuntu-2 is the only host that is not currently running a pod that will match the service selector…

This makes ubuntu-2 a strange choice to point the route at but highlights how the external traffic gets handled within the cluster.  With our route in place, let’s try to access the 169.10.10.1 IP address from a remote host…

Awesome, so it works!  Let’s now dig into how it works.  We can assume that since services use netfilter rules, and that the IP was assigned as part of the service, that the externalIP configuration likely also uses it.  So let’s start there.  For the sake of easily pointing out how the exteralIPs were implemented, I took a ‘iptables-save’ before and after the modification of the service.  Afterwards, I diffed the files and these three lines were added to the iptables-save output after the externalIPs were implemented in the service…

So what do these rules do? The first rule is looking for traffic that is TCP and destined to the IP address of 169.10.10.1 on port 80. This has a target of jump and points to a chain called ‘KUBE-MARK-MASQ’. This chain has the following rules…

This rule matches all traffic and has a target of ‘MARK’ which is a non-terminating target. The traffic in this case will be marked with a value of ‘0x4000/0x400’. So what do I mean by marked? In this case ‘–set-xmarl’ is setting a marking on the packet that is internal to the host. That is – this marking is only locally significant. Since the MARK target is non-terminating we jump back to the KUBE-SERVICES chain after the marking has occurred. The next line is looking for traffic that is…

  • Destined to 169.10.10.1
  • Has a protocol of TCP
  • Has a destination port of 80
  • Has not entered through a bridge interface (! –physdev-is-in)
  • Source of the traffic is not a local interface (! –src-type LOCAL)

The last two rules ensure that this is not traffic that is originated from a POD or the host itself destined to a service.

If the last two rules are foreign to you I suggest you take a look at the MAN page for the IPTables extensions.  It’s definitely worth bookmarking.  

Since the second rule is a match for our external traffic we follow that JUMP target into the KUBE-SVC-SWP62QIEGFZNLQE7 chain.  At that point – the load balancing works just like an internal service.  It’s worth pointing out that the masquerade rule is crucial to all of this working.  Let’s look at an example of what this might look like if we didn’t have the masquerade rule…

Let’s walk through what will happen without the masquerade rule shown above…

  • An external user, in this case 192.168.127.50, makes a request to the external IP of 169.10.10.1.
  • The request reaches the gateway, does a route lookup, and sees that there’s a route for 169.10.10.0/24 pointing at ubuntu-2 (10.20.30.72)
  • The request reaches the host ubuntu-2 and hits the above mentioned IPTables rules.  Without the masquerade rule, the only rule that gets hit is the one for passing the traffic to the service chain KUBE-SVC-SWP62QIEGFZNLQE7.  Normal service processing occurs as explained in our last post and a pod out of the service gets selected, in this case the pod 10.100.3.8 on host ubuntu-5.
  • Traffic is destination NAT’d to 10.100.3.8 and makes it way to the host ubuntu-5.
  • The pod receives the traffic and attempts to reply to the TCP connection based on the source IP address of the request.  The source in this case is unchanged and the host ubuntu-5 attempts to reply directly to the user at 192.168.127.50.
  • The user making the request receives the reply from 10.100.3.8 and drops the packet since it hasn’t initiated a session to that IP address.

So as you can see – this just wont work.  This is why we need the masquerade rule.  When we use the rule, the processing looks like this…

This looks much better.  In this example the flows specify the correct source and destination since the host ubuntu-2 is now hiding it’s request to the pod behind it’s own interface.  This ensures that the reply from the pod 10.100.3.8 will come back to the hose ubuntu-2.  This is an important step because this is the host which performed the initial service DNAT.   If the request does not come back to this host, the DNAT to the pod can not be reversed.  Reversing the DNAT in this case means changing the source of the packet back to the original pre-DNAT source of 169.10.10.1.   So as you can see – the masquerade rule is quite important to ensuring that the externalIP construct works.

NodePort

If you’re not interested in dealing with routing a new subnet to the hosts your other option would be what’s referred to as nodeport.  Nodeport works a lot like the original Docker bridge mode for publishing ports.  It makes a service available externally on the nodes through a different port.  That port is by default in the range of 30000-32767 but can be modified by changing the ‘–service-node-port-range’ configuration flag on the API server.  To change out service to nodeport we simply delete the externalips definition we inserted during the previous example and change the service type to nodeport as shown below…

After we save the change, we can view the services again with kubectl…

Notice that our port column for the service now lists more than one port. The first port is that of the internal service. The second port (30487) is the nodeport, or the port that we can use externally to reach the service. One point about nodeport that I’d like to mention is that it’s an overlay on top of a typical clusterip service.  In the externalip example above, notice that we didnt change the type of the service, we just added the externalips to the spec.  In the case of nodeport, you need to change the service type.  If you’re using a service within the cluster you might be concerned that making this change would remove the clusterip configuration and prevent pods from accessing the service.  That is not the case.  Nodeport works ontop of the clusterip service configuration so you will always have a clusterip when you configure nodeport.

At this point, we should be able to reach the service by connecting to any Kubernetes node on that given port…

Let’s now do a similar stare and compare with the iptables rules on each host.  Once again, I’ll compare the rules in place after the configuration to a copy of the rules I had before we started our work.  These are the only lines that were added to get the nodeport functionality working…

These lines are pretty straight forward and perform similar tasks to what we saw above with the externalip functionality. The first line is looking for traffic destined to port 30487 which it will then pass to the KUBE-MARK-MASQ chain so that the traffic will be masqueraded. We have to do this for the same reason we explained above. The second line is also looking for traffic destined to port 30487 and when matched will pass the traffic to the specific chain for the service to handle the load balancing. But how do we get to this chain? If we look at the KUBE-SERVICES chain we will see this entry at the bottom of the chain…

This rule has always been present in the ruleset the chain it references (KUBE-NODEPORTS) just never existed.

Nodeport offers a couple of advantages over externalip. Namely, we can have more than one load balancing target. With externalip, all of the traffic is headed to the same IP address. While you could certainly route that IP to more than one host, letting the network load balance for you, you’d need to worry about how to update the routing when the host failed. With nodeport, it’s reasonable to think about using an external load balancer that referenced a back-end pool of all of the Kubernetes nodes or minions…

The pool could reference a specific port for each service which would be front-ended on the load balancer by a single VIP.  In this model, if a node went away the load balancer could have the intelligence (in the form of a health check) to automatically pull that node from the pool.  Keep in mind that the destination the load balancer is sending traffic to will not necessarily host the pod that is answering the client request.  However – that’s just the nature of Kubernetes services so that’s pretty much table stakes at this point.

Lastly – if it’s more convenient, you can also specify manually the nodeport you wish to use.  In this instance, I edited the spec to specify a nodeport of 30000…

Every once and a great while there is a need to simulate bad network behavior.  Simulating things like packet loss and high latency are often harder to do than you’d expect.  However – if you’re dealing with a Linux host – there’s a simple solution.  The ‘tc’ command which comes along with the ‘iproute2’ toolset can help you simulate symptoms of a bad network quite easily.

The tc command offers a lot of functionality but in this post we’re just going to walk through a couple of quick examples of using it in conjunction with the netem (network emulator) included in the Linux kernal .  To do this, we’ll use just use two hosts…

To start with – let’s make sure that ‘tc’ is installed and that it’s working…

So what did we just do here? Well we used the tc command to return the current qdisc configuration for our servers physical network interface named ‘ens32’.  So what’s a qdisc?  Qdisc is shorthand for ‘Queue discipline’ and defines the queuing method assigned to the interface. In our case we can see that this interface is using the ‘pfifo_fast’ queuing methodology. This is a default configuration and you should see something similar on your Linux host.

Side Note: Im doing all of this in a lab.  Make sure you know what you’re changing before you change anything.  For instance, configuring a high rate of packet loss on an interface you’re using to SSH into the host is likely not a great idea.  Common sense rules apply.

Let’s first start a constant ping on our host ubuntu-5 heading toward ubuntu-1. Now let’s head back to ubuntu-1 and configure the following command…

So let’s break this command down. We’re telling tc that we want to work with the interface ens32 and add a delay of 200ms to the root qdisc. The root qdisc is an egress queue and where all the packets will inherently get queued by default. If we go and check the ping on the other server we should see the latency has increased…

Great! Now let’s modify the rule with this command…

This command tells the rule to include a random deviation of up to 50ms. Once the rule is in, you should start seeing latency number in between 150ms and 250ms…

Perfect! Having that random deviation helps make it look like ‘real’ latency. Now let’s delete the rule like so…

Its important to keep track of the ‘add|change|del’ keywords. If you try to change a rule that doesnt exist or something similar you’re going to start getting weird errors when you try to work with the rules.

Next up – let’s introduce some packet loss!

The command is similar to the latency commands but now we’re specifying ‘loss’ instead of ‘delay’. If we send 10 ICMP pings to ubuntu-1 from ubuntu-5 we should see some packet loss…

Yep, so 20% packet loss as we expected. When you’re done testing, dont forget to delete the rule…

One interesting thought to point out here is that you could quite easily build a server that acted as a sort of ‘WAN simulator’ to use in your lab.  In that case, if the server was inline with two interface you could enact policy in both flow directions by applying specific policy to each interface.  Lots of possibilities there!

This is literally just the tip of the iceberg. There are so many more things you can do with tc and netem.  If you’re looking for more info here’s a good starting point.  Be aware that the in-document hyperlinks don’t appear to work but you can just scroll down to see all of the examples.

Update: Heres another link with better info about TC and NetEm.  Thanks John!

In our last post we talked about how Kubernetes handles pod networking.  Pods are an important networking construct in Kubernetes but by themselves they have certain limitations.  Consider for instance how pods are allocated.  The cluster takes care of running the pods on nodes – but how do we know which nodes it chose?  Put another way – if I want to consume a service in a pod, how do I know how to get to it?  We saw at the very end of the last post that the pods themselves could be reached directly by their allocated pod IP address (an anti-pattern for sure but it still works) but what happens when you have 3 or 4 replicas?  Services aim to solve these problems for us by providing a means to talk to one or more pods grouped by labels.  Let’s dive right in…

To start with, let’s look at our lab where we left at the end of our last post

 

If you’ve been following along with me there are some pods currently running.  Let’s clear the slate and delete the two existing test deployments we had out there…

So now that we’ve cleaned out the existing deployments let’s define a new one…

This is pretty straight forward with the exception of two things. I’m now using much smaller container images that are based off of an excellent post I read on making a small GO web server using the tiny pause container as the base image.  My previous test images were huge so this is the first step I’m taking toward rightsizing them.  Secondly – you’ll notice that in our spec we define two labels.  One to define the application (in this case ‘web-front-end’) and another to define the version (in this case ‘v1’).  So let’s create a YAML file on our master called ‘deploy-test-1.yaml’ and load this deployment…

Above we do a couple of things. We first load the definition with kubectl. We then verify that the deployment is defined and that the pods have loaded. In this case we can see that the pod has been deployed on the host ubuntu-5 and the pod has an IP address of 10.100.3.7. At this point – the only way to access the pod is directly by it’s pod IP address. If you noted in the above deployment definition we said that the containers port was 8080. By doing a curl to the pod IP on port 8080 we can see that we can reach the service.

This by itself is not very interesting and only really describes normal pod networking behavior.  If another pod in this cluster wanted to reach the service in this pod you’d have to provide it the pod IP address.  That’s not very dynamic and considering that pods may die and be restarted its rather prone to failure.  To solve this Kubernetes uses the service construct.  Let’s look at a service definition…

The main thing a service defines is a selector.  That is – what the service should be used for.  In this case, that selector is ‘app: web-front-end’.  If you’ll recall – our deployment listed this label as part of it’s specification.  Next – we need to define the ports and protocols the service should use.  In this case we’re using TCP and the port definition specifies the port the service will listen on, in this case 80.  At this point I think it’s easier to think of the service as a load balancer.  The ‘port’ definition defines the port that the front end virtual IP address will listen on.  The ‘targetPort’ specific what the back-end hosts are listening on or what the traffic should be load balanced to.  In this case, the back-end is any pod that matches our selector.  Interestingly enough – instead of specify a numeric port here you can specify a port name.  Recall from our deployment specification that we gave the port a name as part of port definition, in this case ‘web-port’.  Let’s use that with our service definition rather than the numerical definition of 8080.

Let’s define this file as ‘svc-test-1.yaml’ on our Kubernetes master and load it into the cluster…

Once loaded we check to make sure that the cluster sees the service.  Notice that the service has been assigned an IP address out of the ‘service_network_cidr’ we defined when we built this cluster using Ansible.  Going back to our load balancer analogy – this is our VIP IP address.  So now let’s head over to one of the worker nodes and try to access the service…

Excellent!  So the host can access the service IP directly. But what about other pods? To test this let’s fire up a quick pod with a single container in it that we can use as a testing point…

In the above output we used the kubectl ‘run’ sub-command to start a pod with a single container using the image ‘jonlangemak/net_tools’. This image is quite large since it is using Ubuntu as it’s base image but its serves as a nice testing endpoint.  Once the pod is running we can use the kubectl ‘exec’ sub-command to run commands directly from within the container much like you would locally by using ‘docker exec’. In this case, we curl to the IP address assigned to the server and get the response we’re looking for. Great!

So while this is a win – we’re sort of back in the same boat as before.  Any client looking to access the service running in the pod now needs to know the service’s IP address.  That’s not much different than needing to know the pods IP address is it?  The fix for this is Kube-DNS…

As you can see above – services can be resolved by name so long as you are running the Kube-DNS cluster add on.  When you register a service the master will take care of inserting a service record for it in Kube-DNS.  The containers can then resolve the service directly by name so long as the kubelet process has the correct DNS information (the ‘cluster-domain’ parameter as part of it’s service definition).  If it’s configured correctly it will configure the containers resolv.conf file to include the appropriate DNS server (which also happens to be a service itself) and search domains…

So now we know what services can do, but we don’t know how they do it. Let’s now dig into the mechanics of how this all works.  To do that, let’s start by doing some packet captures.  Our topology currently looks like this…

 

As we’ve seen already the net-test pod can access the deploy-test-1 pod both via it’s pod IP address as well as through the service.  Let’s start by doing a packet capture as close to the source container (net-test) as possible.  In that case, that would be on the VETH interface that connects the container to the cbr0 bridge on the host ubuntu-4.  To do that we need to find the VETH interface name that’s associated with the pause container which the net-test container is connected to.

Note: If you arent sure what a pause container is take a look at my last post.

In my case, it’s easy to tell since there’s only one pod running on this host.  If there are more, you can trace the name down by matching up interfaces as follows…

First we get the container ID so that we can ‘exec’ into the container and look at it’s interfaces. We see that it’s eth0 interface (really that of the pause containers but same network namespace) is matched up with interface 11. Then on the host we see that the VETH interface with name veth75b33c5c is interface 11. So that’s the interface we want to capture on…

The capture above was taken while SSH’d directly into the ubuntu-4 host.  To generate the traffic I used the kubectl ‘exec’ sub-command on ubuntu-1 to execute a curl command on the net-test container as shown below…

The capture above is interesting as it shows the container communicating with both the DNS service (10.11.12.254) and the service we created as svc-test-1 (10.11.12.125).  In both cases, the container believes it is communicating directly with the service.  That is the service IP is used as the destination in outgoing packets and seen as the the source in the reply packets.  So now that we know what the container sees lets move up a hop in the networking stack and see what traffic is traversing the hosts physical network interface…

Now this is interesting.  Here we see the same traffic but as it leaves the minion or node.  Notice anything different?  The traffic has the same source address (10.100.2.9) but now reflects the ‘real’ destination.  The highlighted lines above show the HTTP request we made with the curl command as it leaves the ubuntu-4 host.  Notice that not only does the destination now reflect the pod 10.100.3.7, but the destination port is now also 8080.  If we continue to think of the Kubernetes service as a load balancer, the first capture (depicted in red below) would be the client to VIP traffic and the second capture (depicted in blue below) would show the load balancer to back-end traffic.  It looks something like this…

As it turns out – services are actually implemented with iptables rules.  The Kubernetes host is performing a simple destination NAT after which normal IP routing takes over and does it’s job. Let’s now dig into the iptables configuration to see exactly how this is implemented.

Side note: I’ve been trying to refer to ‘netfilter rules’ as ‘iptables rules’ since the netfilter term sometimes throws people off.  Netfilter is the actual kernel framework used to implement packet filtering.  IPtables is just a popular tool used to interact with netfilter.  Despite this – netfilter is often referred to as iptables and vice versa.  So if I use both terms, just know Im talking about the same thing.  

If we take a quick look at the iptables configuration of one of our hosts you’ll see quite a few iptables rules already in place…

Side note: I prefer to look at the iptables configuration rather than the iptables command output when tracing the chains. You could also use a command like ‘sudo iptables -nvL -t nat’ to look at the NAT entries we’ll be looking at above. This is useful when looking for things like hits on certain policies but be advised that this wont help you with the current implementation of kube-proxy. The iptables policy is constantly refreshed clearing any counters for given rules. That issue is discussed here as part of another problem.

These rules are implemented on the nodes by the kube-proxy service. The service is responsible for getting updates from the master about new services and then programming the appropriate iptables rules to make the service reachable for pods running on the host. If we look at the logs for the kube-proxy service we can see it picking up some of these service creation events…

Previous versions of the kube-proxy service actually handled the traffic directly rather than relying on netfilter rules for processing. This is still an option ,and configureable as part of the kube-proxy service defintion, but it’s considerably slower than using netfilter. The difference being that the kube-proxy service runs in user space whereas the netfilter rules are being processed in the Linux kernel.

Looking at the above output of the iptables rules it can be hard to sort out what we’re looking for so let’s trim it down slightly and call out how the process works to access the svc-test-1 service…

Note: I know that’s tiny so if you can’t make it out click on the image to open it in a new window.

Since the container is generating what the host will consider forward traffic (does not originate or terminate on one of the devices IP interfaces) we only need to concern ourselves with the PREROUTING and POSTROUTING chains of the NAT table. It’s important to also note here that the same iptables configuration will be made on each host.  This is because any host could possibly have a pod that wants to talk to a service.

Looking at the above image we can see the path a packet would take as it traverses the NAT PREROUTING table.  The red arrows indicate a miss and the green arrows indicate a match occurring along with the associated action.  In most cases, the action (called a target in netfilter speak) is to ‘jump’ to another chain.  If we start at the top black arrow we can see that there are 4 targets that we match on…

  • The first match occurs at the bottom of the PREROUTING chain.  There is no match criteria specified so all traffic that reaches this point will match this rule.  The rule specifies a jump target pointing at the KUBE-SERVICES chain.
  • When we get to the KUBE-SERVICES chain we don’t match until the second to last rule which is looking for traffic that is destined to 10.11.12.125 (the IP of our service), is TCP, and has a destination port of 80.  The target for this rule is another jump pointing at the KUBE-SVC-SWP62QIEGFZNLQE7 chain.
  • There’s only one rule in the KUBE-SVC-SWP62QIEGFZNLQE7 chain and it once again lists no matching criteria, only a jump target pointing at the KUBE-SEP-OA6FICRP4YS6R3CE chain
  • When we get to the KUBE-SEP-OA6FICRP4YS6R3CE chain we don’t match on the first rule so we roll down to the second.  The second rule is looking for traffic that is TCP and specifies a target of DNAT.  The DNAT specifies to change the destination of the traffic to 10.100.3.7 on port 8080.  DNAT is considered a terminating target so processing of the PREROUTING chain ends with this match.

When a DNAT is performed netfilter takes care of making sure that any return traffic is also NAT’d back to the original IP.  This is why the container only see it’s communication occurring with the service IP address.

This was a pretty straight forward example of a service so lets now look at what happens when we have more than one pod that matches the services label selector.  Let’s test that out to see…

Above we can see that we’ve now scaled our deployment from 1 pod to 3. This means we should now have 3 pods that match the service definition. Let’s take a look at our iptables rule set now…

The above depicts the ruleset in place for the PREROUTING chain on one of the minions.  I’ve removed all of the rules that didn’t result in a target being hit to make it easier to see whats happening.  This looks a lot like the output we saw above with the exception of the KUBE-SVC-SWP62QIEGFZNLQE7 chain.  Notice that some of the rules are using the statistic module and appear to be using it to calculate probability.  This is allows the service construct to act as a sort of load balancer.  The idea is that each of the rules in the KUBE-SVC-SWP62QIEGFZNLQE7 chain will get hit 1/3 of the time.  This means that traffic to the service IP will be distributed relatively equally across all of the pods that match the service selector label.

Looking at the numbers used to specify probability you might be confused as to how this would provide equal load balancing to all three pods.  But if you think about it some more, you’ll see that these numbers actually lead to almost a perfect 1/3 spit between all back end pods.  I find it helps to think of the probability in terms of flow…

If we process the rules sequentially the first rule in the chain will get hit about 1/3 (0.33332999982) of the time.  This means that about 2/3 (0.66667000018) of the time the first rule will not be hit and processing will flow to the second rule.  The second rule has a 1/2 (.5) probability of being hit.  However – the second rule is only receiving 2/3 of the traffic since the first rule is getting hit 1/3 of the time.  One half of two thirds is one third.  That means that if the second rule misses half of the time, then 1/3 will end up at the last rule of the chain which will always get hit since it doesn’t have a probability statement.  So what we end up with is a pretty equal distribution between the pods that are a part of the service.  At this point, our service now looks like this with connections toward the service having the possibility of hitting any of the three available back-end pods…

It’s important to call out here that this is providing relatively simple load balancing.  While it works well – it relies on the pods providing fungible services.  That is – each back-end pod should provide the same service and not be dependent on any sort of state with the client.  Since the netfilter rules are processed per flow, there’s no guarantee that we’ll end up on the same back-end pod the next time we talk to the service.  In fact there’s a good chance we wont.

Now that we know how services work – let’s talk about some other interesting things you can do with them.  You’ll recall above that we defined the service by using a target port name rather than a numerical port.  This allows us some flexibility in terms of what the service can use as endpoints.  An example that’s often given is one where you’re application changes the port it’s using.  For instance, our pods are currently using the port 8080.  But perhaps a new version of our pods uses 9090 instead.  This is where using port names rather than port numbers comes in handy.  So long as our pod definition uses the same name, the numbers can be totally different.  For instance, let’s define this deployment file on our Kubernetes master as deploy-test-2.yaml…

Notice that the container port is 9090 but we use the same name for the port.  Now create the deployment…

After deploying it check to make sure the pod is running. Once it comes into a running status try to curl to the service URL (http://svc-test-1) again from your net-test container. Im going to do it through kubectl ‘exec’ sub-command on the master but you could also do it directly on the host with ‘docker exec’…

Notice how the service is picking up the new pod? That’s because the pods both share the ‘app=web-front-end’ label that the service is looking for. We can confirm this by showing all of the pods that mach that label…

If we wanted to migrate between the old and new versions of the pods, we could first scale up the new pod…

Then we can use the kubectl ‘edit’ sub-command to edit the service. This is done with the ‘kubectl edit service/svc-test-1’ command which will bring up a VI like text editor for you to make changes to the service. In this case, we want the service to be more specific so we tell it to look for an additional label. Specifically, the ‘version=v2’ label…

Notice the highlighted line above where we added the new selector. Once edited, save the file like you normally would (ESC, :wq, ENTER). The changes will be made to the service immediately. We can see this by viewing the service and then searching for pods that match the new selector…

And if we execute our test again – we should see only responses from the version 2 pod…

I hinted at this earlier but it’s worth calling out as well. When the kube-proxy service defines rules for the service to work for the pods, it also defines rules for the services to be accessible from the hosts themselves. We saw this at the beginning of the post when the host ubuntu-2 was able to access the service directly by it’s assigned service IP address. In this case,  since the server itself is originating the traffic, different chains are processed. Specifically, the OUTPUT chain is processed which has this rule facilitating getting the traffic to the KUBE-SERVICES chain…

From that point, the processing is largely similar to what we saw from the pod perspective. One thing to point out though is that since the hosts are not configured to use Kube-DNS they can not, by default, resolve the services by name.

In the next post we’ll talk about how you can use services to provide external access into your Kubernetes cluster.  Stay tuned!

« Older entries § Newer entries »