clusterIP

It exposes the service on a cluster-internal IP.

When we expose a pod using kubectl expose command , we are creating a service object in API.

Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType.

We can see the Service spec using --dry-run & --output=yaml

NodePort

When would you use this?

There are a few scenarios where you would use the Kubernetes proxy to access your services.

Debugging your services, or connecting to them directly from your laptop for some reason Allowing internal traffic, displaying internal dashboards, etc. Because this method requires you to run kubectl as an authenticated user, you should NOT use this to expose your service to the internet or use it for production services.

Lets create a deployment

piVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-ctr
        image: nginx:1.15.4
        ports:
        - containerPort: 80

Deploy it

$ kubectl apply -f deploy.yaml 

Create a service for the deployment

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  labels:
    app: nginx
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: nginx

Deploy the service

$ kubectl apply -f clusterip-svc.yaml

Check if services are running fine

$ kubectl get svc 

Verify the DNS service via nslookup

  • Run another nginx deployment and use dnslookup by service name
$ kubectl run demo --it --rm --image=nginx:latest /bin/sh
  • Inside the pod run below
$ apt-get update; apt-get install dnsutils; nslookup nginx-svc

If everything works then you should see a response.

Delete deployment and service

$ kubectl delete svc nginx-svc
$ kubectl delete deploy nginx-deployment