In the previous article we deployed our first application using K8S. At this point we can use Ingress to make it accessible on the Internet (if you have your own domain name and point to it correctly, of course)

  • Deploy your first Application: Pod, Application, and Service

The following is a picture of the Ingress used by the website to describe its role. If you don’t know anything about it, you can think of it as traditional Nginx, which is used to configure the domain name of your website to be accessible from the Internet.

internet
    |
[ Ingress ]
--|-----|--
[ Services ]
Copy the code

The Ingress contains two components

  • Ingress: Configures forwarding rules, similar to nginx configuration files
  • Ingress Controller: forward, similar to nginx, which readsIngressAnd convert tonginxConfiguration file of

In addition to Nginx, Ingress Controller also has haproxy, Ingress, etc. We choose Nginx as Ingress Controller

Deploy the Nginx Ingress Controller using the helm

We used helm to select the official stable/ nginx-Ingress chart for deployment.

Nginx-ingress will configure a service whose type is LoadBalancer. Therefore, you need to set external-ip to the IP address of the k8S cluster node. In this case, external-IP is set to [172.17.68.39, 172.17.68.40]

We can get the IP address by kubectl get Nodes

Get node internal-IP as external-IP of LoadBalancer$ kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME shanyue Ready master 13D v1.16.0 172.17.68.39 < None > CentOS Linux 7 (Core) 3.10.0-957.21.3.el7.x86_64 Docker ://18.6.2 shuifeng Ready < None > 13d v1.16.0 172.17.68.40 < None > CentOS Linux 7 (Core) 3.10.0-957.21.3.el7.x86_64 Docker: / / 18.6.2Copy the code

In this case, external-IP is set to [172.17.68.39, 172.17.68.40]

Controller. Service. ExternalIPs [0] = 172.17.68.39 controller. Service. ExternalIPs [1] = 172.17.68.40Copy the code
If helm V2 is deployed, release-name is specified with --name
$ helm install nginx-ingress stable/nginx-ingress --set "Controller. Service. ExternalIPs [0] = 172.17.68.39, controller. Service. ExternalIPs [1] = 172.17.68.40"NAME: Nginx-ingress LAST DEPLOYED: 2019-10-18 21:21:44.115902395 +0800 CST M =+1.904554085 NAMESPACE: default STATUS: DEPLOYED deployed NOTES: The nginx-ingress controller has been installed. It may take a few minutesfor the LoadBalancer IP to be available.
You can watch the status by running 'kubectl --namespace default get services -o wide -w nginx-ingress-controller'

An example Ingress that makes use of the controller:

  apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
      kubernetes.io/ingress.class: nginx
    name: example
    namespace: foo
  spec:
    rules:
      - host: www.example.com
        http:
          paths:
            - backend:
                serviceName: exampleService
                servicePort: 80
              path: /
    # This section is only required if TLS is to be enabled for the Ingress
    tls:
        - hosts:
            - www.example.com
          secretName: example-tls

If TLS is enabled for the Ingress, a Secret containing the certificate and key must also be provided:

  apiVersion: v1
  kind: Secret
  metadata:
    name: example-tls
    namespace: foo
  data:
    tls.crt: <base64 encoded cert>
    tls.key: <base64 encoded key>
  type: kubernetes.io/tls
Copy the code

Verify nginx-ingress deployment

$helm LS NAME NAMESPACE REVISION UPDATED STATUS CHART nginx-INGress default 1 2019-10-18 11:21:44.115902395 +0800 CST Deployed nginx - ingress - 1.24.0Nginx-ingress: nginx-ingress
$ kubectl get svc -lApp =nginx-ingress NAME TYPE cluster-ip external-ip PORT(S) AGE nginx-ingress-controller LoadBalancer 10.101.64.64 172.17.68.39, 172.17.68.40 / TCP, 80-30285, 443-31094 / TCP 7 m19s nginx - ingress - default - backend ClusterIP 10.110.76.15 < none > 80/TCP 7m19sCopy the code

Configure the Ingress mapping domain name

The following is a simple configuration file for deploying a blog application with Nginx and Ingress

  1. The extranet accesses applications using the domain name nginx.xiange.tech
  2. Proxy service nginx to do load balancing
  3. Nginx exposed port 80
server {
  listen 80
  server_name nginx.xiange.tech

  location / {
    proxy_pass: http://nginx:80
  }
}
Copy the code

The routing rules for Ingress are as follows

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-service-ingress
spec:
  rules:
  - host: nginx.xiange.tech
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 80
        path: /
Copy the code

Nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang. tech: nginx.xiang

summary

The complete configuration file for deploying an application from Deployment, Service, and Ingress is shown below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

---

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-service-ingress
spec:
  rules:
  - host: nginx.xiange.tech
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 80
        path: /
Copy the code

Pay attention to my

Welcome to follow the public account Shanyuexing. I will regularly share some articles on the front and back end and operation and maintenance, and there will be a daily review and summary of technology and life. Welcome to follow and exchange