Followers

Kubernetes-Services

 An abstract way to expose an application running on a set of Pods as a network service.

With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them

CLUSTER IP

ClusterIP is the default kubernetes service. This service is created inside a cluster and can only be accessed by other pods in that cluster. So basically we use this type of service when we want to expose a service to other pods within the same cluster.

Nodeport:

NodePort opens a specific port on your node/VM and when that port gets traffic, that traffic is forwarded directly to the service.

There are a few limitations and hence its not advised to use NodePort

- only one service per port

- You can only use ports 30000-32767


LoadBalancer:

This is the standard way to expose service to the internet. All the traffic on the port is forwarded to the service. It's designed to assign an external IP to act as a load balancer for the service.  There's no filtering, no routing. LoadBalancer uses cloud service

Few limitations with LoadBalancer:

- every service exposed will it's own ip address 

- It gets very expensive 


Lab for Cluster IP service

1. Create a deployment

2. Access the application which is deployed internally using ClusterIP

Deployment YAML file:


# Deployment
# nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - containerPort: 80


kubectl create -f nginx-deploy.yaml

Kubectl get pods -o wide


Create a ClusterIP service

# Service- ClusterIP
# nginx-svc-ci.yaml
apiVersion: v1
kind: Service
metadata:
  name: internal-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80


kubectl create -f nginx-svc-ci.yml

 Run below command and You can see the internal-service in the list with a static IP address.

kubectl get svc  

To see all the End Points (IP addresses of the Pods which are associated with service)

kubectl describe svc internal-service

Remove one of the pod and monitor the Endpoints

To access the application you need to use the IP address of service with port number.

Delete the CI service

 kubectl delete svc internal-service


NodePort Service

Create Node Port service

# Service- NodePort
# nginx-svc-np.yaml
apiVersion: v1
kind: Service
metadata:
  name: external-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: NodePort
  ports:
  - nodePort: 31869
    port: 80
    targetPort: 80



Create NodePort service

kubectl create -f nginx-svc-np.yml

kubectl get svc

kubectl describe svc  external-service

Remove one of the pod and monitor the Endpoints

To access the application you need to use the IP address of service with port number.

You can access the application on browser by providing the IP address of you any node with port number 31869 ...IP:31869

kubectl delete svc external-service


LoadBalancer service


# Service- LoadBalancer
# nginx-svc-lb.yaml
apiVersion: v1
kind: Service
metadata:
  name: external-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: LoadBalancer
  ports:
  - nodePort: 31869
    port: 80
    targetPort: 80


Create LoadBalancer service

kubectl create -f nginx-svc-lb.yml

kubectl get svc

The External IP is to be provided by the load balancer ( more suitable in cloud based environment) if there is not load balancer then the external ip is in the pending state.

kubectl describe svc  external-service

Remove one of the pod and monitor the Endpoints

To access the application you need to use the IP address of service with port number.

You can access the application on browser by providing the IP address of you any node with port number 31869 ...IP:31869

kubectl delete svc external-service


Delete the Deploy

kubectl delete deploy nginx-deployment


Ingress

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.

    internet
        |
   [ Ingress ]
   --|-----|--
   [ Services ]

An Ingress may be configured to give Services externally-reachable URLs, load balance traffic, terminate SSL / TLS, and offer name-based virtual hosting. An Ingress controller is responsible for fulfilling the Ingress, usually with a load balancer, though it may also configure your edge router or additional frontends to help handle the traffic.

Install nginx ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.1/deploy/static/provider/baremetal/deploy.yaml
A new namespace ingress-nginx will be created.
kubectl get ns
Find the services under ingress-nginx namespace
kubectl get svc -n ingress-nginx

Create a pod with image nginx
kubectl run nginx --image=nginx
Create a Cluster IP service nginx which targets nginx pod.
kubectl expose pod nginx --type=ClusterIP --port=80
Create a pod with image tomcat
kubectl run tomcat --image=tomcat
Create another Cluster  IP service which points to tomcat pod
kubectl expose pod tomcat --type=ClusterIP --port=8080

Create a ingress to access the cluster ip services externally.

#ingress.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: minimal-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: ingressClassName: nginx rules: - http: paths: - path: /nginx pathType: Prefix backend: service: name: nginx port: number: 80 - path: /web pathType: Prefix backend: service: name: tomcat port: number: 8080




kubectl create -f ingress.yaml
kubectl get ingress
Find the http port number for the ingress controller's NodePort service.
kubectl get svc -n ingress-nginx

COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Kubernetes-Services
Kubernetes-Services
DevOpsWorld
https://www.devopsworld.co.in/p/kubernetes-services.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/kubernetes-services.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content