Friday 31 July 2020

Kubernetes: Ingress hello world

Using Ingress we can easily expose services by DNS Provided urls.

 

Is Ingress capable of doing Load Balancing?

Yes

 

Is Ingress capable of exposing both http and https routes?

Yes

 

To use Ingress, you need to install the Ingress controller. There are many ingress controllers available. Some of them given below.

a.   Ingress-nginx

b.   Traefik

c.    Contour

d.   Haproxy

How to enable Ingress addon in minikube?

Execute below command

minikube addon enable ingress

 

Let’s develop an application using ingress.

 

Step 1: Enable ingress controller.

 

Get all the available addons by executing the below command.

minikube addons list

$minikube addons list
|-----------------------------|----------|--------------|
|         ADDON NAME          | PROFILE  |    STATUS    |
|-----------------------------|----------|--------------|
| dashboard                   | minikube | enabled ✅   |
| default-storageclass        | minikube | enabled ✅   |
| efk                         | minikube | disabled     |
| freshpod                    | minikube | disabled     |
| gvisor                      | minikube | disabled     |
| helm-tiller                 | minikube | disabled     |
| ingress                     | minikube | disabled     |
| ingress-dns                 | minikube | disabled     |
| istio                       | minikube | disabled     |
| istio-provisioner           | minikube | disabled     |
| logviewer                   | minikube | disabled     |
| metrics-server              | minikube | disabled     |
| nvidia-driver-installer     | minikube | disabled     |
| nvidia-gpu-device-plugin    | minikube | disabled     |
| registry                    | minikube | disabled     |
| registry-aliases            | minikube | disabled     |
| registry-creds              | minikube | disabled     |
| storage-provisioner         | minikube | enabled ✅   |
| storage-provisioner-gluster | minikube | disabled     |
		

Let’s enable ingress addon by executing the below command.

minikube addons enable ingress

$minikube addons enable ingress
🌟  The 'ingress' addon is enabled

Step 2: Create a deployment.

 

empService.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 1
  selector:
    matchLabels:
      app: employee-service

Create deployment by executing the below command.

kubectl create -f  empService.yml

$kubectl create -f  empService.yml
deployment.apps/employee-service-deployment created

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   1/1     1            1           2m33s

Step 3: let's expose the service to this deployment by executing the below command.

 

kubectl expose deployment employee-service-deployment --type=NodePort --port=8080

$kubectl expose deployment employee-service-deployment --type=NodePort --port=8080
service/employee-service-deployment exposed

Query services.

$kubectl get svc
NAME                          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
employee-service-deployment   NodePort    10.99.151.96   <none>        8080:30497/TCP   22s
kubernetes                    ClusterIP   10.96.0.1      <none>        443/TCP          31d

Now the service is exposed on minikube cluster port 30497.

 

Let’s run the command 'minikube ip' to retrieve the IP address of the running cluster.

$minikube ip
192.168.99.100

Open the url ‘http://192.168.99.100:30497/’ in browser, you will see Wildfly server home page.

Step 4: Update /etc/hosts file by adding below setting.

192.168.99.100 empservice.example.com

 

Once you update /etc/hosts file, you can able to access the Wildfly server using the DNS name ‘empservice.example.com’.

 

Open below url in brower.

http://empservice.example.com:30497/

 

You will see Wildfly server home page.


Step 5: Lets create ingress object.

ingressDemo.yml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: emp-service-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host:
     empservice.example.com
    http:
      paths:
      - path: /emp-service
        pathType: Prefix
        backend:
          serviceName: employee-service-deployment
          servicePort: 8080

Create an ingress object by executing the below command.

kubectl create -f ingressDemo.yml

$kubectl get ingress
NAME                  CLASS    HOSTS                    ADDRESS          PORTS   AGE
emp-service-ingress   <none>   empservice.example.com   192.168.99.100   80      42s

$kubectl create -f ingressDemo.yml 
ingress.networking.k8s.io/emp-service-ingress created

Query ingress objects.

$kubectl get ingress
NAME                  CLASS    HOSTS                    ADDRESS          PORTS   AGE
emp-service-ingress   <none>   empservice.example.com   192.168.99.100   80      2m47s

As you ingressDemo.yml file, I configured Path with the string ‘/emp-service’.

 

Let’s do a curl request on the path ‘http://empservice.example.com/emp-service’.

$curl http://empservice.example.com/emp-service
<!DOCTYPE html>

<html>
<head>
    <!-- proper charset -->
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

    <title>Welcome to WildFly</title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="StyleSheet" href="wildfly.css" type="text/css">
</head>

<body>
<div class="wrapper">
    <div class="content">
        <div class="logo">
                <img src="wildfly_logo.png" alt="WildFly" border="0" />
        </div>
        <h1>Welcome to WildFly</h1>

        <h3>Your WildFly instance is running.</h3>

        <p><a href="https://docs.wildfly.org">Documentation</a> | <a href="https://github.com/wildfly/quickstart">Quickstarts</a> | <a href="/console">Administration
            Console</a> </p>

        <p><a href="https://wildfly.org">WildFly Project</a> |
            <a href="https://community.jboss.org/en/wildfly">User Forum</a> |
            <a href="https://issues.jboss.org/browse/WFLY">Report an issue</a></p>
        <p class="logos"><a href="https://www.jboss.org"><img src="jbosscommunity_logo_hori_white.png" alt="JBoss and JBoss Community" width=
                "195" height="37" border="0"></a></p>

        <p class="note">To replace this page simply deploy your own war with / as its context path.<br />
            To disable it, remove the "welcome-content" handler for location / in the undertow subsystem.</p>
    </div>
</div>
</body>
</html>

You can even open the url ‘http://empservice.example.com/emp-service’ in web browser to see wildfly server homepage.

 

Let’s cleanup by deleting deployment, service and ingress.

$kubectl delete deployment employee-service-deployment
deployment.apps "employee-service-deployment" deleted
$
$kubectl delete service employee-service-deployment
service "employee-service-deployment" deleted
$
$kubectl delete ingress emp-service-ingress
ingress.extensions "emp-service-ingress" deleted


Previous                                                    Next                                                    Home

No comments:

Post a Comment