Kubernetes Services
1. Kubernetes Services
So, what is a Service in Kubernetes? A Service is a logical abstraction that defines a set of Pods and a single IP address for accessing them. A Service is how the outside world accesses the cluster. Think of it like a GKE doorman or bouncer, keeping out unwanted visitors! Services are used to provide a stable IP address and name for a Pod, because these can change frequently. With Services, these details can remain the same through updates, upgrades, scalability changes, and Pod failures. This stability allows applications to connect to each other and be found by external clients, even when Pods are updated or replaced. Pods have a different lifecycle compared to virtual machines. While VMs are typically designed to be durable and persist through application updates and upgrades, Pods are usually terminated and replaced with newer Pods after application updates. Pod IP addresses are ephemeral, which means that they’re temporary, and as a result of the new Pod deployment, the updated version of the application gets a new IP address. Also, if a Pod deployment is rescheduled for any reason, then the Pod gets assigned a new IP address. Please note, if a Pod's IP address changes unexpectedly, it can cause significant Service disruptions. All of this is to say that locating an application running in your cluster by IP address can be difficult, so this is where a Service comes in. The dynamic collection of IP addresses created by a Kubernetes Service is called an endpoint. Endpoints belong to Pods that match the Service’s label selector. When a Service is created, it’s issued a static virtual IP address from the pool of IP addresses that the cluster reserves for Services. Unlike the Pod’s IP address, the Virtual IP is durable. It’s published to all the nodes in the cluster and it doesn't change, even if all of the Pods behind it change. In GKE, this range is automatically managed, and by default contains over 4,000 addresses per cluster. There are multiple ways to search for and locate a Service in GKE, including searching by environment variables, DNS, or by Service type. Let’s explore what each means, starting with environment variables. When a new Pod starts running on a node, kubelet adds a set of environment variables for each active service in the same namespace as the Pod. This allows the Pod to access the Service by using the environment variables. However, this isn’t the most robust mechanism for discovery, as changes made to a Service after Pods have been started will not be visible to the Pods that are already running. Let’s explore an example of the environment variables for a Service named “demo”, where several environment variables have been defined to hold commonly used values such as the host IP, port address, and tcp port. A better practice to locate a Service in GKE is by using a DNS server. DNS comes pre-installed in Google Kubernetes Engine, and the DNS server watches the API server to identify when new Services get created. When a new Service is created, kube-dns, which is a lightweight DNS server, automatically creates a set of DNS records. This allows all the Pods in the cluster to resolve Kubernetes Service names automatically. By default, a client Pod’s DNS search list will include the Pod’s own namespace and the cluster’s default domain. The final way to find a Service in GKE is by changing the service type, which we’ll explore next.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.