My Devops Journey

Menu
  • AWS
  • Terraform
  • kubernetes
Menu

Kubernetes deployment, service and ingress how they are inter linked with simple example

Posted on October 9, 2019 by taragurung

We must understand that deployment, service and ingress are inter-related so we need to understand all the concepts and how are they related to each other. Here we will setup all of them with simple example and understand all of them.

Creating a simple Kubernetes Deployment:

Here, we will write a manifest for deployment which will run a simple nginx pods(container).

nginx-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mynginx

spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Everything is crystal clear, here we created a deployment with 2 replicas for the container with label app:nginx and will run the pods with nginx image at port 80.

kubernetes deployment listing that has successfully run nginx pods
listing the deployment created with above manifest

The next step will be creating a service to expose the service but remember we will making it of type ClusterIp not NodePort because our ultimate goal is to create a ingress controller for the service.

Creating a kubernetes service for the deployment created:

We have a deployment ready now time to make a service for it. As stated before we will be creating a service of type ClusterIp. The most important things to consider when creating a service is the selector and label. As we have a deployment with label app:nginx, we need to create a service with selector app:nginx to make the service associated with the deployment. In nutshell the label belongs to deployment and the selector belong to service and we use them to match them.

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service

spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: nginx
kubernetes service created for the deployment with label app:nginx
service for the deployment created

The service is ready now the moment has come to create a ingress controller.

Creating a Kubernetes ingress controller:

The service is ready and is of type ClusterIP which means we can’t access the service from the internet. To make it accessible from internet we need here is create a Ingress Controller. Though there are other options too but here we will be going with Ingress. Consider ingress as a way to allow external traffic to reach the cluster.

myingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
    - host: www.mynginx.com
      http:
        paths:
          - backend:
              serviceName: nginx-service
              servicePort: 80

Now we will be able to access the cluster service from internet using the host name www.mynginx.com. Note: add the domain to the /etc/hosts as it is not the real domain. Need to add the following to the /etc/hosts

ip-address-of-minikube. www.mynginx.com

To know the ip address of minikube you can use the following command

minikube ip

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Creating a AWS VPC and subnet using terraform with simple example
  • Kubernetes deployment, service and ingress how they are inter linked with simple example
  • Creating a Private and Public subnet network in AWS
©2021 My Devops Journey | Built using WordPress and Responsive Blogily theme by Superb