Types of Services:
ClusterIP: Exposes the Service on a cluster-internal IP. This is the default type. we can communicate inside the cluster only
NodePort: Exposes the Service on each Node's IP at a static port.
LoadBalancer: Exposes the Service externally using a cloud provider's load balancer.
ExternalName: Maps the Service to the contents of the externalName field.
Headless:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
By default node port service will support with the port range of 30000 to 32000
Yes we can can modify the port range by doing the below way.
Update the file.
cd /etc/kubernetes/manifests/kube-apiserver.yml in this change the --service-node-port-range into the as per requirement like 20000 to 22767
#Expose port as a service
#kubectl expose deployment deploymentname --port=8000 --target-port=80 --type=NodePort
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort:31217
type: NodePort
** nodePort:31217
Kubectl apply -f svc.ymlPS D:\Cloud-DevOps\minikube> kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-deployment-8d545c96d-8zh96 1/1 Running 0 51s 172.17.0.7 minikube <none> <none>
pod/nginx-deployment-8d545c96d-r5hzf 1/1 Running 0 51s 172.17.0.6 minikube <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/flask-service NodePort 10.106.235.50 <none> 80:32422/TCP 23h app=flask-app
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 431d <none>
service/nginx-service NodePort 10.107.219.206 <none> 80:31645/TCP 51s app=nginx
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx-deployment 2/2 2 2 51s nginx nginx:latest app=nginx
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
replicaset.apps/nginx-deployment-8d545c96d 2 2 2 51s nginx nginx:latest app=nginx,pod
access browser:
Ex: http://192.168.59.103:31645/
Note: if you are deployed in aws you must enable security group inbound for the ec2 machine
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort:31217
type: LoadBalancer
type: LoaBalancer
When we mentioned over loadlancer as type by default it will use the classic load balancer
to use Network loadbalancer
apiVersion: v1
kind: Service
metadata:
name: nginx-service
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: external
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort:31217
type: LoadBalancer
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: external
Ref: https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.3/guide/service/annotations/#lb-type
Max of 50 elb per region in aws
External name:
The ExternalName service type in Kubernetes is used to map a service to a DNS name, rather than selecting and load balancing to pods. This service type is often used when you want to provide a stable DNS name for an external service, allowing your applications within the Kubernetes cluster to access it by name rather than IP address.
Here's a use case for ExternalName:
Use Case: Accessing an External Database
Let's say you have a Kubernetes cluster running applications that need to interact with an external database hosted outside the cluster. The database has a stable DNS name, and you want your applications within the cluster to connect to the database using this DNS name rather than hardcoding an IP address.
external.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app-image:latest
env:
- name: DATABASE_HOST
value: external-db-service
---
apiVersion: v1
kind: Service
metadata:
name: external-db-service
spec:
type: ExternalName
externalName: my-database.example.com
ports:
- protocol: TCP
port: 3306
targetPort: 3306
kubectl apply -f external-db-service.yaml
if the service is outside but if you want connect via ip insted of dns name then we can use the endpont
apiVersion: v1
kind: Endpoints
metadata:
name: external-db-service
subsets:
- addresses:
- ip: 192.168.1.100 # Replace with the actual IP address of your external service
ports:
- port: 3306
Headless service
clusterIP: None indicates that this is a headless service. Headless services do not allocate a virtual IP address.
selector specifies the pods that should be included in the service. Pods with the label app: database will be part of this service.
ports define the ports that the service will use. It exposes port 3306 internally, matching the targetPort of the pods.
With a headless service, you won't get a Cluster IP, and DNS resolution for the service will directly return the IP addresses of the pods backing the service.
You can apply this service definition to your cluster using
apiVersion: v1
kind: Service
metadata:
name: headless-db-service
spec:
clusterIP: None
selector:
app: database
ports:
- protocol: TCP
port: 3306
targetPort: 3306
For headless service you must specify clusterip as none
clusterIP: None
will get more information when will discuss about statefulset
"It will give the all pods ipaddress instead of loadbalancing like nodeport and load balancer it will loadbalance the pods which part of deployment behind the which part of headless service"
0 Comments