Professional Documents
Culture Documents
Networking in Kubernetes-2-2-3
Networking in Kubernetes-2-2-3
1. Introduction
2. Understanding the kubernetes networking
3. Understanding different types of services
4. Implementing the services in a practical way
5. Accessing the applications on our browser
6. Question/answers
Networking in Kubernetes
The Kubernetes network model specifies:
Every pod gets its own IP address
Containers within a pod share the pod IP address and
can communicate freely with each other.
Pods can communicate with all other pods in the
cluster using pod IP addresses.
Isolation (restricting what each pod can communicate
with) is defined using network policies
Networking in a single node
10.40.0.1 10.40.0.2
10.40.0.0
ETH0 = Ethernet
192.168.122.224
BR0 = Bridge
VETH0 = Virtual Ethernet
Networking in Multiple nodeS
10.40.0.0 10.36.0.0
192.168.122.128 192.168.122.129
192.168.122.127
Master Node
ETH0 = Ethernet
BR0 = Bridge
VETH0 = Virtual Ethernet
Concept of pod networking
For example::
Our cluster consist of total 3 nodes (1 master & 2 worker nodes), The default network
interface eth0 on Worker Node 1 have the following IP Address: 192.168.122.224
But for the Pods it creates a Bridge with the IP Address : 10.40.0.0 and through that
IP Address it creates a network within the network
Networking in Multiple nodes
So when we schedule 3 Pods on the Worker 1 node it gets the following IP Range:
10.40.0.1-3 since they're within the same network the pods can now communicate
with each other. The is also ping-able and accessible from within the Cluster but if the
pod crashes or restart for some reason the IP Address of the Pod will eventually
change.
Now open the svc.yaml file and add the labels of your pods in the selector field
that your want to expose. Apply the yaml file using the following command:
"kubectl apply -f svc.yaml" OR "kubectl create -f svc.yaml"
Creating CLUSTER-IP SERVICE
Using the Kubectl Expose Command
Kubectl expose command lets you expose your resources using a single command.
The command looks something like this:
kubectl expose <resource-type> <resource-name> --name=<service-name> --port=80 --target-port=80
So after writing "kubectl expose" you have to define the resource-type and the name
of the resource which you want to expose. And after defining the resource you can
define the name of the service which could be anything but if you don't define a
name it's automatically going to give it a name randomly. Now the main part, The
service port and the targeted container port.
For Example:
Creating CLUSTER-IP SERVICE
Example of Cluster-IP Service YAML File.
nODE-PORT SERVICE
How does Node-Port Service works ?
The NodePort type is an extension of the ClusterIP
type. Node-Port Service Exposes the Service on
each Node's IP at a static port (the NodePort).
The port range of nodeport service is:
30000-32767 (TCP/UDP Both)
After creating the service you can edit the service to add the selectors using the
"kubectl edit service <service-name>" command.
Creating nodeport SERVICE
Using the Kubectl Expose Command
Kubectl expose command lets you expose your resources using a single command.
The command looks something like this:
kubectl expose pod nginx --name=httpd-svc --port=80 --target-port=80 --type=NodePort
The only extra thing that we've added is the service type in the command mentioned
above, because the default service type is ClusterIP and to expose a NodePort
service we have to define the service type using the --type flag.
Note: Exposing a service using this way will automatically assign the Node Port, To
change the port you can edit the service manually.
Testing the Service
Creating nodeport SERVICE
Example of NodePort Service YAML File.
LOADBALANCER SERVICE
How does Load-Balancer Service works ?
This ServiceType exposes the Services externally using
the cloud provider's load balancer. Traffic from the
external load balancer is directed to the backend Pods.
The cloud provider decides how it is load-balanced.
Special Thanks to