20 C
Jaipur
Thursday, October 29, 2020

How to Setup NGINX Ingress Controller in Kubernetes

Must read

How To Use pulseaudio-dlna To Stream Audio From Ubuntu 20.10 To Chromecast Devices

pulseaudio-dlna is a streaming server which allows streaming audio from your Linux computer to a Chromecast or DLNA / UPNP device in the same...

Nokia cuts full-year profit forecast, announces new strategy – Latest News

STOCKHOLM/HELSINKI: Nokia cut its full-year profit forecast even as the telecom network equipment maker's quarterly underlying profit met expectations in its first earnings under...

payments technology: JPMorgan backs UK payments technology company Icon Solutions – Latest News

JPMorgan Chase & Co has made a strategic investment in Icon Solutions, a UK-based company that advises and provides technology to banks to help...

paytm: Parliamentary panel questions Paytm about Chinese investment, storing of data in servers abroad – Latest News

A parliamentary panel on Thursday questioned Paytm representatives about the quantum of Chinese investment in the company and told them that the servers on...

If we re-run “minikube addons list” command, this time we must see the status of ingress is enabled.

Run following kubectl command to verify whether ingress controller’s pod is running or not.

Above output confirms that nginx-controller has been enabled and started its pod successfully under kube-system namespace.

Note: I am assuming Kubernetes cluster is up and running.

Kubectl-Deploy-NGINX-Ingress-Controller-Kubernetes

Run following kubectl command to verify the status of nginx-ingress controller pods,

[email protected]:~$ kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx
NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-v7ftn        0/1     Completed   0          6m12s
ingress-nginx-admission-patch-njdnf         0/1     Completed   0          6m12s
ingress-nginx-controller-75f84dfcd7-p5dct   1/1     Running     0          6m23s
[email protected]:~$

Perfect, above output confirms that NGINX Ingress Controller has been deployed successfully and it’s pod is currently running.

Test Ingress Controller

To test Ingress controller, we will create two applications based on httpd and nginx container and will expose these applications via their respective services and then will create ingress resource which will allow external users to access these applications using their respective urls.

Deploy httpd based deployment and its service with NodePort type listening on the port 80, Create the following yaml file which includes deployment and service section,

[[email protected] ~]# vi httpd-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      run: httpd-deployment
  template:
    metadata:
      labels:
        run: httpd-deployment
    spec:
      containers:
      - image: httpd
        name: httpd-webserver

---
apiVersion: v1
kind: Service
metadata:
  name: httpd-service
spec:
  type: NodePort
  selector:
    run: httpd-deployment
  ports:
    - port: 80

Save and close the file.

Run kubectl command to deploy above httpd based deployment and its service,

[[email protected] ~]# kubectl create -f httpd-deployment.yaml
deployment.apps/httpd-deployment created
service/httpd-service created
[[email protected] ~]#

Deploy our next NGINX based deployment and its service with NodePort as its type and port as 80, Content of yaml file listed below,

[[email protected] ~]# vi nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-deployment
  template:
    metadata:
      labels:
        run: nginx-deployment
    spec:
      containers:
      - image: nginx
        name: nginx-webserver

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    run: nginx-deployment
  ports:
    - port: 80

Save and exit the file

Now run following kubectl command to deploy above nginx based deployment and its service,

[[email protected] ~]# kubectl create -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
service/nginx-service created
[[email protected] ~]#

Run below command to verify the status of both deployments and their services

[[email protected] ~]# kubectl get deployments.apps httpd-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
httpd-deployment   1/1     1            1           19m
[[email protected] ~]#
[[email protected] ~]# kubectl get deployments.apps nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           24m
[[email protected] ~]#
[[email protected] ~]# kubectl get service nginx-service httpd-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service NodePort 10.103.75.91 <none> 80:30042/TCP 78m
httpd-service NodePort 10.98.6.131 <none> 80:31770/TCP 73m
[[email protected] ~]#

Create and Deploy Ingress Resource

Create the following ingress resource yaml file which will route the request to the respective service based url or path. In our example we be using url or fqdn.

[[email protected] ~]# vim myweb-ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: name-based-virtualhost-ingress
spec:
  rules:
  - host: httpd.example.com
    http:
      paths:
      - backend:
          serviceName: httpd-service
          servicePort: 80

  - host: nginx.example.com
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 80

save and close the file.

Execute beneath kubectl command to create above ingress resource,

[[email protected] ~]# kubectl create -f myweb-ingress.yaml
ingress.networking.k8s.io/name-based-virtualhost-ingress created
[[email protected] ~]#

Run following to verify the status of above created ingress resource

[[email protected] ~]# kubectl get ingress name-based-virtualhost-ingress
[[email protected] ~]# kubectl describe ingress name-based-virtualhost-ingress

Perfect, above output confirms that ingress resources have been created successfully.

Before accessing these urls from outside of the cluster please make sure to add the following entries in hosts file of your system from where you intended to access these.

192.168.1.190                httpd.example.com
192.168.1.190                nginx.example.com

Now try to access these URLs from web browser, type

http://httpd.example.com

http://nginx.example.com

Great, above confirms that we have successfully deployed and setup nginx ingress controller in Kubernetes. Please do share your valuable feedback and comments.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How To Use pulseaudio-dlna To Stream Audio From Ubuntu 20.10 To Chromecast Devices

pulseaudio-dlna is a streaming server which allows streaming audio from your Linux computer to a Chromecast or DLNA / UPNP device in the same...

Nokia cuts full-year profit forecast, announces new strategy – Latest News

STOCKHOLM/HELSINKI: Nokia cut its full-year profit forecast even as the telecom network equipment maker's quarterly underlying profit met expectations in its first earnings under...

payments technology: JPMorgan backs UK payments technology company Icon Solutions – Latest News

JPMorgan Chase & Co has made a strategic investment in Icon Solutions, a UK-based company that advises and provides technology to banks to help...

paytm: Parliamentary panel questions Paytm about Chinese investment, storing of data in servers abroad – Latest News

A parliamentary panel on Thursday questioned Paytm representatives about the quantum of Chinese investment in the company and told them that the servers on...

Siemens: Siemens agrees to sell Flender to Carlyle Group in $2.4 billion deal – Latest News

Siemens has agreed to sell Flender to U.S. buyout group Carlyle Group in a deal valuing the mechanical drives business at 2.025 billion euros...