Skip to content

Latest commit

 

History

History
166 lines (148 loc) · 6.11 KB

lab_ingresscontroller.adoc

File metadata and controls

166 lines (148 loc) · 6.11 KB

Ingress Controller

In this lab, we will deploy Nginx ingress controller per doc and define ingress routes sample apps and to our fortune UI, fortune service, and redis.

Install Nginx Ingress Controller

This generally will be done by cluster administrator and be shared across cluster. We have pre-installed for you.

  1. Pks login as cluster admin

    $ pks login -a {PKS API URL} -k -u username -p *****
  2. Start by creating the “mandatory” resources for Nginx Ingress in your clustes, which includes the deployment:

    $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
  3. Next, create loadbalancer service for ingress controller:

    $ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/cloud-generic.yaml
  4. Check it’s all set up properly

    $ kubectl get all -n ingress-nginx

Test with a sample ingress

  1. First, let’s create two services to demonstrate how the Ingress routes our request. We’ll run two web applications that output a slightly different response. apple.yml and banana.yml

  2. Deploy the two applications:

    $ kubectl apply -f apple.yaml
    $ kubectl apply -f banana.yaml
  3. Now, declare an Ingress to route requests to /apple to the apple service, and requests to /banana to banana service. Check out the Ingress rules field that declares how requests are passed along apple_banana_ingress.yml:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: example-ingress
      annotations:
        ingress.kubernetes.io/rewrite-target: /
    spec:
      rules:
      - http:
          paths:
            - path: /apple
              backend:
                serviceName: apple-service
                servicePort: 5678
            - path: /banana
              backend:
                serviceName: banana-service
                servicePort: 5678
  4. Create the Ingress in the same namespace your apps deployed

    $  kubectl create -f apple_banana_ingress.yml
    ingress.extensions/example-ingress created
  5. Find external IP of the ingress controller load balancer service:

    $ kubectl get svc -n ingress-nginx
    NAME            TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)                      AGE
    ingress-nginx   LoadBalancer   10.100.200.206   35.237.179.46   80:30827/TCP,443:31190/TCP   18h
    
    root@fortune-redis-0:/data# redis-cli
    
    127.0.0.1:6379> KEYS *
    (empty list or set)
  6. Perfect! Let’s check that it’s working.

    $ curl -kL http://35.237.179.46/apple
    apple
    $ curl -kL http://35.237.179.46/banana
    banana
    $ curl -kL http://35.237.179.46/random
    <html>
    <head><title>404 Not Found</title></head>
    <body>
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx/1.15.10</center>
    </body>
    </html>

Create a ingress to our fortune app

  1. If you still have fortune UI app running from our last labs, we can create an ingress to route traffic to it. If not, we have this all-in-one yaml ready for you.

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: fortune-ingress
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      rules:
      - http:
          paths:
            - path: /fortune
              backend:
                serviceName: fortune-deploy-service
                servicePort: 80
  2. Next deploy the ingress:

    $ kubectl apply -f fortune_ingress_allinone.yml
  3. Now we can use the same ingress controller load balancer IP and defined path to access out fortune app

    $ curl -kL https://35.237.179.46/fortune
    <DOCTYPE html>
        <html lang="en">
        <head>
            <title>Fortune Teller</title>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
            <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
            <script src="app.js"></script>
        </head>
        <body onload="loadOne()">
          <div class="jumbotron container">
              <h1>Fortune Teller!</h1>
              <p>Find out what the future holds...</p>
          </div>
          <div class="container">
              <p>| <b>Random Fortune</b> | <a href="all-fortunes.html">All Fortunes</a> |</p>
            <blockquote id="fortune" />
          </div>
        </body>
        </html>