In order to facilitate everyone to learn Kubernetes system, I organized a Kubernetes learning series of articles, covering the basic knowledge of Kubernetes, installation steps and the related content of the whole Kubernetes system, I believe we read this series, To have a deeper understanding of Kubernetes.

There are three ways to expose a service in Kubernetes

  • Loadbalancer is usually supported by cloud vendors or local F5 devices
  • NodePort The caller accesses the service through NodeIP:NodePort, which cannot handle Pod migration scenarios
  • Ingress Ingress is a resource in Kubernetes that provides external access to internal services through a Pod plus NodePort. Ingress allows us to define our own load balancing. There are a variety of Ingress support, this article focuses on Nginx solutions.

Both the Kubernetes community and the Nginx Corporation have released a Controller called Nginx-Ingress, Nginx-ingress Controller Nginx-Ingress Controller The version is 1.3.0.

Kubernetes Community program

The installation

[root@devops-101 ~]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
namespace/ingress-nginx created
configmap/nginx-configuration created
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.extensions/nginx-ingress-controller created
[root@devops-101 ~]# kubectl get pods --all-namespaces -l app.kubernetes.io/name=ingress-nginx --watchNAMESPACE NAME READY STATUS RESTARTS AGE ingress-nginx nginx-ingress-controller-664f488479-pr87w 0/1 ContainerCreating 0  5s ingress-nginx nginx-ingress-controller-664f488479-pr87w 0/1 Running 0 11s ingress-nginx nginx-ingress-controller-664f488479-pr87w 1/1 Running 0 16s [root@devops-101 ~]# kubectl get all -n ingress-nginx
NAME                                            READY     STATUS    RESTARTS   AGE
pod/nginx-ingress-controller-664f488479-pr87w   1/1       Running   0          2m

NAME                                       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-ingress-controller   1         1         1            1           2m

NAME                                                  DESIRED   CURRENT   READY     AGE
replicaset.apps/nginx-ingress-controller-664f488479   1         1         1         2m
Copy the code

The hostNetwork attribute must be added to the mandatory installation file. Otherwise, problems occur during access.

Configure Tomcat and HTTPD

Download the tomcat-deploy\httpd-deploy\tomcat-service\httpd-service files from my Github and deploy them separately.

Then download the tomcat-ingress.yaml file and deploy it.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
# namespace: nginx-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: ingressweb.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080
      - path: /httpd
        backend:
          serviceName: httpd-service
          servicePort: 80
Copy the code

validation

In the example above, I set the domain name as ingressweb.com, modify host locally to point to the node where ingress-Controller runs, and visit ingressweb.com through the browser to see the Tomcat interface. See HTTPD’s It Works at ingressweb.com/httpd! Interface.

Nginxinc solution

Take a look at the Nginx Ingress Controller schematic

Nginx Ingress Controller

  • Services are exposed in one of two ways, using different host names (e.g. ServiceA.com, serviceB.com) or different urls (e.g. /serviceA and /serviceB).
  • Configuring SSL Support
  • TCP Load Balancing
  • The Url rewrite

The installation

First, you need to have a working Kubernetes environment. If you don’t have one, you can install the Kubernetes V1.11.1 cluster by referring to my installation steps kubeadm

[root@devops-101 ~]# kubectl apply -f ingress-install.yaml 
namespace/nginx-ingress created
serviceaccount/nginx-ingress created
secret/default-server-secret created
configmap/nginx-config created
clusterrole.rbac.authorization.k8s.io/nginx-ingress configured
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress configured
deployment.extensions/nginx-ingress created
[root@devops-101 ~]# kubectl get pods -n nginx-ingress
NAME                             READY     STATUS    RESTARTS   AGE
nginx-ingress-767cc6477f-flj2f   1/1       Running   5          4m
Copy the code

The instance

Without rule configuration, 404 status is returned by default for all requests.

[root@devops-101 ~]# kubectl apply -f cafe-example.yaml 
deployment.extensions/coffee created
service/coffee-svc created
deployment.extensions/tea created
service/tea-svc created
secret/cafe-secret created
ingress.extensions/cafe-ingress created
[root@devops-101 ~]# IC_IP = 192.168.0.102
[root@devops-101 ~]# IC_HTTPS_PORT=31586
[root@devops-101 ~]# curl --resolve cafe.example.com:$IC_HTTPS_PORT:$IC_IP https://cafe.example.com:$IC_HTTPS_PORT/tea --insecure
Server address: 172.16.1.138:80
Server name: tea-7d57856c44-jxpvt
Date: 22/Oct/2018:11:37:47 +0000
URI: /tea
Request ID: 796e79e0280a27743cb682b8e893d6e9
Copy the code

The resources

  1. Kubernetes ingress controller
  2. Github nginx ingress
  3. Difference between two nginx-ingress
  4. Kubernetes load balancing problem
  5. Kubernetes exposes the service type of Nginx Ingress
  6. Kubernetes Nginx Ingress
  7. Kubernetes Nginx Ingress