Https Bestdotnettraining - Azureedge.net Documents Kubernetes 5 Kubernetes Services 5 Kubernetes Services and Ingress
Https Bestdotnettraining - Azureedge.net Documents Kubernetes 5 Kubernetes Services 5 Kubernetes Services and Ingress
Kubernetes Services
The Problem:
• Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance
across them.
• In Kubernetes, if you use a Deployment to run your app, it can create and destroy Pods dynamically. Each
Pod gets its own IP address, however in a Deployment, the set of Pods running in one moment in time could
be different from the set of Pods running that application a moment later.
• This can lead to a problem: if some set of Pods (call them “backends”) provides functionality to other Pods
(call them “frontends”) inside our cluster, how do the frontends find out and keep track of which IP address
to connect to, so that the frontend can use the backend part of the workload?
This is where Services can be helpful.
Service Resources:
• Kubenetes Service is an abstract way to expose an application running on a logical set of Pods and a policy
by which to access them.
• Services enable a loose coupling between dependent Pods. Frontend Pods don’t need to know the direct IP
address of the Backend Pods.
Page 1 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
---
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
selector:
app: mynginx
ports:
- protocol: TCP
port: 8080
targetPort: 80
Page 2 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
• This specification creates a new Service object named “my-service”, which targets TCP port 80 on any Pod
with the app=mynginx label.
• Kubernetes assigns this Service an IP address (sometimes called the "cluster IP").
• The controller for the Service selector continuously scans for Pods that match its selector, and then POSTs
any updates to an Endpoint object also named “my-service”.
Multi-Port Services
For some Services, you need to expose more than one port. Kubernetes lets you configure multiple port
definitions on a Service object. When using multiple ports for a Service, you must give all of your ports names
so that these are unambiguous. For example:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
selector:
app: MyApp
ports:
- name: http-port
protocol: TCP
port: 8080
targetPort: 80
- name: https-port
protocol: TCP
port: 44433
targetPort: 443
ServiceTypes:
• Pod IP address are not exposed outside the cluster without a Service. Services allow our applications to
receive traffic from outside the cluster.
• For some parts of our application (for example, frontends) we may want to expose a Service on to an
external IP address, i.e. outside of our cluster.
• Kubernetes ServiceTypes allow us to specify what kind of Service we want.
1. ClusterIP
2. LoadBalancer
3. NodePort
4. ExternalName
Page 3 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
CLUSTER-IP SERVICE
• 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.
• ClusterIP is the preferred option for internal/backend service access and uses an internal/private IP address
to access the service.
• Some examples of where ClusterIP might be the best option include service debugging during development
and testing and internal traffic.
Page 4 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
Benefit: Now even if the POD is destroyed and recreated, we can still access the application using service url.
Page 5 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
NODEPORT SERVICE:
• Exposes the Service on each Node’s IP at a static port (the NodePort [Hi Range Port]). High range port (30000
to 32767) used for exposing NodePort.
• A ClusterIP Service, to which the NodePort Service routes, is automatically created.
• We will be able to contact the NodePort Service, from outside the cluster, by requesting
<NodeIP>:<NodePort>.
• A NodePort is primarily used for exposing services in a non-production environment. As an example, a
NodePort would be used to expose a single pod functionality (with no load-balancing requirements for
multiple services).
• Same NodePort will be used across all the nodes in a multi node cluster. Also, same service (single instance)
can be used to access container in any Pod or any Node in the cluster.
Page 6 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
We can access the service through localhost without getting into the cluster. This works only in Docker and not
in minicube.
4. kubectl get svc myservice-np -o yaml
Note: 3 ports are involved
a) 30145 (High Port) is of NodePort Service
b) 8080 is of ClusterIP
c) 80 is Application port in container
apiVersion: v1
kind: Service
metadata:
name: my-service-np
spec:
type: NodePort
selector:
app: mynginx
ports:
- protocol: TCP
nodePort: 30377 #Port or Node
port: 8080 #Port of Service
targetPort: 80 #Port of Container
ExternalName Service
Maps the Service to the contents of the externalName field (e.g. abc.example.com), by returning a CNAME
record with its value. No proxying of any kind is needed. Services of type ExternalName map a Service to a DNS
name, not to a typical selector.
apiVersion: v1
kind: Service
metadata:
name: my-service-ext
Page 7 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
spec:
type: ExternalName
externalName: my.database.com
When looking up the host my-service-ext.prod.svc.cluster.local, the cluster DNS Service returns
a CNAME record with the value my.database.com.
Accessing my-service works in the same way as other Services but with the crucial difference that redirection
happens at the DNS level rather than via proxying or forwarding. Should you later decide to move your
database into your cluster, you can start its Pods, add appropriate selectors or endpoints, and change the
Service's type.
• Built-in service discovery makes it easier for applications to find and communicate with each other on
Kubernetes clusters, even when pods and services are being created, deleted, and shifted between nodes.
• By default most Kubernetes clusters automatically configure an internal DNS service to provide a
lightweight mechanism for service discovery.
• Every Service defined in the cluster (including the DNS server itself) is assigned a DNS name.
• By default, a client Pod's DNS search list will include the Pod's own namespace and the cluster's default
domain.
Services:
• The full DNS A record of a Kubernetes service will look like myservice.namespace.svc.cluster.local. This
resolves to the cluster IP of the Service.
Page 8 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
• Assume a Service named foo in the Kubernetes namespace n1. A Pod running in namespace n1 can look
up this service by simply doing a DNS query for foo. A Pod running in namespace namespace n2 can look
up this service by doing a DNS query for n1.bar.
foo.n1.svc.cluster.local
Pods:
• A pod would have a record in this format, reflecting the actual IP address of the pod :
pod-ip-address.namespace.pod.cluster.local
pod-ip-address.namespace.pod (eg: 10-1-1-100.default.pod)
Ingress Controller
• Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic
routing is controlled by rules defined on the Ingress resource.
• An Ingress may be configured to give Services externally-reachable URLs, load balance traffic, terminate SSL
/ TLS, and offer name-based virtual hosting.
• Ingress controllers work at layer 7 (unlike Load Balancer which works at layer 4), and can use more intelligent
rules to distribute application traffic.
In order for the Ingress resource to work, the cluster must have an ingress controller running.
(List of Ingress Controllers: https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/)
Ingress with NGINX Ingress Controller as a reverse proxy and load balancer
(https://kubernetes.github.io/ingress-nginx/deploy/).
Step1: Install NGINX Ingress controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-
v1.1.0/deploy/static/provider/cloud/deploy.yaml
Page 9 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
app: mynginx
name: mynginx
spec:
replicas: 1
selector:
matchLabels:
app: mynginx
template:
metadata:
labels:
app: mynginx
spec:
containers:
- image: nginx
name: mynginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mynginx
name: mynginx-cip
spec:
ports:
- port: 8090
protocol: TCP
targetPort: 80
selector:
app: mynginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myhttpd
Page 10 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
name: myhttpd
spec:
replicas: 1
selector:
matchLabels:
app: myhttpd
template:
metadata:
labels:
app: myhttpd
spec:
containers:
- image: httpd
name: myhttpd
---
apiVersion: v1
kind: Service
metadata:
labels:
app: myhttpd
name: myhttpd-cip
spec:
ports:
- port: 8091
protocol: TCP
targetPort: 80
selector:
app: myhttpd
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
Page 11 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
ingressClassName: nginx
defaultBackend:
service:
name: mynginx-cip
port:
number: 8090
rules:
- host: demo.mydomain.com
http:
paths:
- path: /nginx
pathType: Prefix #Exact
backend:
service:
name: mynginx-cip
port:
number: 8090
- path: /httpd
pathType: Prefix
backend:
service:
name: myhttpd-cip
port:
number: 8091
Note: Stop IIS or Apache or Tomcat or any other service running on your machine.
Step3: Edit as administrator: c:\windows\system32\drivers\etc\hosts (Mac/Linux: /etc/hosts)
127.0.0.1 demo.mydomain.com
Step4: Open in browser following URL's (may require to wait for couple of minutes). Also stop other WebService
Services running on Port 80.
1) http://demo.mydomain.com
2) http://demo.mydomain.com/nginx
3) http://demo.mydomain.com/httpd
To View the Logs and the POD to which the traffic is forward:
kubectl get pods -n ingress-nginx
kubectl logs -n ingress-nginx pod/ingress-nginx-controller-XXXXXXX-xxxx
Page 12 of 13
Develop Intelligence - Kubernetes Service, Ingress and Networking
Page 13 of 13