Skip to content

Latest commit

 

History

History
161 lines (129 loc) · 5.85 KB

K8s-Service-App.md

File metadata and controls

161 lines (129 loc) · 5.85 KB

LAB: K8s Service Implementations (ClusterIp, NodePort and LoadBalancer)

This scenario shows how to create Services (ClusterIp, NodePort and LoadBalancer). It goes following:

  • Create Deployments for frontend and backend.
  • Create ClusterIP Service to reach backend pods.
  • Create NodePort Service to reach frontend pods from Internet.
  • Create Loadbalancer Service on the cloud K8s cluster to reach frontend pods from Internet.

image (Ref: Udemy Course: Kubernetes-Temelleri)

Steps

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  labels:
    team: development
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  labels:
    team: development
spec:
  replicas: 3
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - name: backend
        image: ozgurozturknet/k8s:backend
        ports:
        - containerPort: 5000

image

  • Run on the terminal: "kubectl get pods -w" (on Linux/WSL2: "watch kubectl get pods")

image

apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  type: ClusterIP
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000

image

  • ClusterIP Service created. If any resource in the cluster sends a request to the ClusterIP and Port 5000, this request will reach to one of the pod behind the ClusterIP Service.
  • We can show it from frontend pods.
  • Connect one of the front-end pods (list: "kubectl get pods", connect: "kubectl exec -it frontend-5966c698b4-b664t -- bash")
  • In the K8s, there is DNS server (core dns based) that provide us to query ip/name of service.
  • When running nslookup (backend), we can reach the complete name and IP of this service (serviceName.namespace.svc.cluster_domain, e.g. backend.default.svc.cluster.local).
  • When running curl to the one of the backend pods with port 5000, service provides us to make connection with one of the backend pods.

image

apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  type: NodePort
  selector:
    app: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

image

  • With NodePort Service (you can see the image below), frontend pods can be reachable from the opening port (32098). In other words, someone can reach frontend pods via WorkerNodeIP:32098. NodePort service listens all of the worker nodes' port (in this example: port 32098).
  • While working with minikube, it is only possible with minikube tunnelling. Minikube simulates the reaching of the NodeIP:Port with tunneling feature.

image

  • On the other terminal, if we run the curl command, we can reach the frontend pods.

image

apiVersion: v1
kind: Service
metadata:
  name: frontendlb
spec:
  type: LoadBalancer
  selector:
    app: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

image

  • If you run on the cloud, you'll see the external-ip of the loadbalancer service.

image

image

  • In addition, it can be possible service with Imperative way (with command).
  • kubectl expose deployment --type= --name=

image

References