Exposing your App: Creating a Service and Ingress

KubeSphere has built a global load balancer into each project (namespace), Ingress Controller, which is responsible for fulfilling the Ingress. Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.

Kubernetes-ingress provides such an example: As for website https://cafe.example.com, if users access the URL https://cafe.example.com/coffee then it will return “Coffee Ordering System”. Similarly, when access the URL https://cafe.example.com/tea then it will return “Tea Ordering System”.

To elaborate this process, we will create a Deployment, Service and Ingress to expose an application in this tutorial.

Exposing your APP using Ingress - 图1

Prerequisites

You’ve completed all steps in Getting Started with Multi-tenant Management.

Estimated Time

About 20 minutes.

Hands-on Lab

Create Deployments

To get started, we’ll create a tea deployment and a coffee deployment respectively.

Step 1: Create a Tea

1.1. Sign in with project-regular, then enter into demo-project. Choose Workloads → Deployments and click Create Deployment.

Exposing your APP using Ingress - 图2

1.2. Name it as tea, click Next.

1.3. Set the replicas to 2, and click Add Container. Then fill in the Pod Template according to the following hints.

  • Image: nginxdemos/hello:plain-text
  • Container Name: tea
  • Service Settings: Name it as port, fill in 80 with the TCP protocol.

Exposing your APP using Ingress - 图3

Exposing your APP using Ingress - 图4

1.4. Click Save and then click Next → Create to complete tea deployment creation.

Exposing your APP using Ingress - 图5

Step 2: Create a Coffee

2.1. Similarly, click Create button to create a “Coffee Ordering System” deployment.

2.2. Name it as coffee and click Next, then set the replicas to 2, and click Add Container. Then fill in the Pod Template according to the following hints.

  • Image: nginxdemos/hello:plain-text

  • Container Name: coffee

  • Service Settings: Name it as port, fill in 80 with the TCP protocol.

    2.3. Click Save and then click Next → Create to complete coffee deployment creation.

Exposing your APP using Ingress - 图6

Create Services

In this section, we’ll create a tea service and a coffee service for their deployments respectively. Choose Network & Services → Services, then click Create Service.

Exposing your APP using Ingress - 图7

Step 3: Create a tea-svc

3.1. Name it as tea-svc, click Next.

3.2. Choose the first item Virtual IP: Access the service through the internal IP of the cluster, then fill in the Service Settings page according to the following hints.

  • Click Specify Workload and select tea, click Save.

  • Ports:

    • Name it as port
    • Service Port: 80
    • Container Port: 80

Exposing your APP using Ingress - 图8

3.3. Then click Next → Create to complete the creation.

Step 4: Create a coffee-svc

4.1. Similarly, click Create button to create a Service, name it as coffee-svc.

4.2. Then Specify the workload to coffee, the other options are the same with tea-svc. Finally, click Create after other blanks are filled. At this point, two services have been successfully created.

Exposing your APP using Ingress - 图9

Step 5: Create a TLS Certificate

Since the domain name bound in the route is the HTTPS protocol, we need to create the TLS certificate as a Secret.

5.1. Choose Configuration Center → Secrets, then click Create.

Exposing your APP using Ingress - 图10

5.2. Name it as cafe-secret, click Next. Select the TLS as the Type, then copy and paste Credential and Private Key as following, click Create when you’ve done.

  1. #Credential
  2. -----BEGIN CERTIFICATE-----
  3. MIIDLjCCAhYCCQDAOF9tLsaXWjANBgkqhkiG9w0BAQsFADBaMQswCQYDVQQGEwJV
  4. UzELMAkGA1UECAwCQ0ExITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0
  5. ZDEbMBkGA1UEAwwSY2FmZS5leGFtcGxlLmNvbSAgMB4XDTE4MDkxMjE2MTUzNVoX
  6. DTIzMDkxMTE2MTUzNVowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMSEwHwYD
  7. VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxGTAXBgNVBAMMEGNhZmUuZXhh
  8. bXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCp6Kn7sy81
  9. p0juJ/cyk+vCAmlsfjtFM2muZNK0KtecqG2fjWQb55xQ1YFA2XOSwHAYvSdwI2jZ
  10. ruW8qXXCL2rb4CZCFxwpVECrcxdjm3teViRXVsYImmJHPPSyQgpiobs9x7DlLc6I
  11. BA0ZjUOyl0PqG9SJexMV73WIIa5rDVSF2r4kSkbAj4Dcj7LXeFlVXH2I5XwXCptC
  12. n67JCg42f+k8wgzcRVp8XZkZWZVjwq9RUKDXmFB2YyN1XEWdZ0ewRuKYUJlsm692
  13. skOrKQj0vkoPn41EE/+TaVEpqLTRoUY3rzg7DkdzfdBizFO2dsPNFx2CW0jXkNLv
  14. Ko25CZrOhXAHAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAKHFCcyOjZvoHswUBMdL
  15. RdHIb383pWFynZq/LuUovsVA58B0Cg7BEfy5vWVVrq5RIkv4lZ81N29x21d1JH6r
  16. jSnQx+DXCO/TJEV5lSCUpIGzEUYaUPgRyjsM/NUdCJ8uHVhZJ+S6FA+CnOD9rn2i
  17. ZBePCI5rHwEXwnnl8ywij3vvQ5zHIuyBglWr/Qyui9fjPpwWUvUm4nv5SMG9zCV7
  18. PpuwvuatqjO1208BjfE/cZHIg8Hw9mvW9x9C+IQMIMDE7b/g6OcK7LGTLwlFxvA8
  19. 7WjEequnayIphMhKRXVf1N349eN98Ez38fOTHTPbdJjFA/PcC+Gyme+iGt5OQdFh
  20. yRE=
  21. -----END CERTIFICATE-----
  22. #Private Key
  23. -----BEGIN RSA PRIVATE KEY-----
  24. MIIEowIBAAKCAQEAqeip+7MvNadI7if3MpPrwgJpbH47RTNprmTStCrXnKhtn41k
  25. G+ecUNWBQNlzksBwGL0ncCNo2a7lvKl1wi9q2+AmQhccKVRAq3MXY5t7XlYkV1bG
  26. CJpiRzz0skIKYqG7Pcew5S3OiAQNGY1DspdD6hvUiXsTFe91iCGuaw1Uhdq+JEpG
  27. wI+A3I+y13hZVVx9iOV8FwqbQp+uyQoONn/pPMIM3EVafF2ZGVmVY8KvUVCg15hQ
  28. dmMjdVxFnWdHsEbimFCZbJuvdrJDqykI9L5KD5+NRBP/k2lRKai00aFGN684Ow5H
  29. c33QYsxTtnbDzRcdgltI15DS7yqNuQmazoVwBwIDAQABAoIBAQCPSdSYnQtSPyql
  30. FfVFpTOsoOYRhf8sI+ibFxIOuRauWehhJxdm5RORpAzmCLyL5VhjtJme223gLrw2
  31. N99EjUKb/VOmZuDsBc6oCF6QNR58dz8cnORTewcotsJR1pn1hhlnR5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ZLO7ltB0KIkO9ZRcmUjo8Q
  48. cpLlHMAqbLJ8WYGJCkhiWxyal6hYTyWY4cVkC0xtTl/hUE9IeNKo
  49. -----END RSA PRIVATE KEY-----

Exposing your APP using Ingress - 图11

Step 6: Create a cafe-ingress

6.1. Choose Network & Services → Routes, and click Create Route button.

Exposing your APP using Ingress - 图12

6.2. Name it as cafe-ingress, then click Add Route Rule.

6.3. Choose Specify Domain and fill in the table as following:

  • HostName: cafe.example.com

  • Protocol: Choose https

  • Secret Name: Choose cafe-secret

  • Paths:

    • Input /coffee, then choose coffee-svc as the service and select 80 as the port
    • Click Add Path, input /tea, then choose tea-svc as the service and select 80 as the port

Exposing your APP using Ingress - 图13

6.4. Click Save after you’ve done, then skip to the final step to click Create, we can see the cafe-ingress has been created successfully.

Exposing your APP using Ingress - 图14

Step 7: Access the Application via Route

So far, we have exposed two different application via route and its rules. We can access the tea and coffee application through different path.

For example, when we visit https://cafe.example.com:{$HTTPS_PORT}/coffee, any one of the coffee Pod should respond to the request. As following demo, the Server name and Server address is corresponding to the Pod coffee-5db79467d6-ghm95.

  1. $ curl --resolve cafe.example.com:30972:192.168.0.88 https://cafe.example.com:30972/coffee --insecure
  2. Server address: 10.233.87.215:80
  3. Server name: coffee-5db79467d6-ghm95
  4. Date: 16/Jul/2019:09:24:33 +0000
  5. URI: /coffee
  6. Request ID: d396d300af9df6d31e0c1edd50d5da54
  7. $ kubectl get pod -n demo-project -o wide
  8. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  9. coffee-5db79467d6-ghm95 1/1 Running 0 93m 10.233.87.215 ks-allinone <none> <none>

Similarly, when we visit https://cafe.example.com:{$HTTPS_PORT}/tea, any one of the tea Pod should respond to the request. As following demo, the Server name and Server address is corresponding to the Pod tea-5bf6c889c4-vlv69.

  1. $ curl --resolve cafe.example.com:30972:192.168.0.88 https://cafe.example.com:30972/tea --insecure
  2. Server address: 10.233.87.174:80
  3. Server name: tea-5bf6c889c4-vlv69
  4. Date: 16/Jul/2019:09:31:01 +0000
  5. URI: /tea
  6. Request ID: 3f047c0461640da52c6d152039d016e1
  7. $ kubectl get pod -n demo-project -o wide
  8. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  9. tea-5bf6c889c4-vlv69 1/1 Running 0 106m 10.233.87.174 ks-allinone <none> <none>

Conclusion

According to above instruction, it indicates that the route has successfully forwarded different requests to the corresponding back-end service, and the service redirects that traffic to one of the Service’s backend Pods.