Build and use K8s cluster < use ingress to expose spring cloud service >

Keywords: Ruby Nginx Vue Spring

Exposing Spring cloud services with ingress

The k8s cluster has been successfully built and can be deployed, but it is impossible to check the running node and nodePort every time you visit the service. Even if you check the node and nodePort, you can't avoid restarting k8s or Replication Controller, node and nodeport will change again. K8s provides Ingress to solve this problem.

1. Generate a default backend

Generate a default backend and forward it to the default backend page if you encounter an unresolved URL

[root@master ingress]# cat default-backend.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: default-http-backend
  labels:
    k8s-app: default-http-backend
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissable as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: docker.io/cdchen/defaultbackend:1.0 
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
  name: default-http-backend
  namespace: default
  labels:
    k8s-app: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    k8s-app: default-http-backend
[root@master ingress]# kubectl create -f default-backend.yaml 
deployment "default-http-backend" created
service "default-http-backend" created

2. Deployment of Ingress Controller

Click to view the official yaml

[root@master ingress]# cat  nginx-ingress-controller.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-ingress-lb
  labels:
    name: nginx-ingress-lb
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: nginx-ingress-lb
      annotations:
        prometheus.io/port: '10254'
        prometheus.io/scrape: 'true'
    spec:
      terminationGracePeriodSeconds: 60
      hostNetwork: true
      containers:
      - image: docker.io/cdchen/nginx-ingress-controller:0.9.0-beta.12 
        name: nginx-ingress-lb
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 1
        ports:
        - containerPort: 80
          hostPort: 80
        - containerPort: 443
          hostPort: 443
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: KUBERNETES_MASTER
            value: http://192.168.6.150:8080
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
        - --apiserver-host=http://192.168.6.45:8080

Remember to modify - - apiserver-host = http://192.168.6.45:8080

[root@master ingress]# kubectl create -f nginx-ingress-controller.yaml 
replicationcontroller "nginx-ingress-lb" created

3. Configure ingress

Posting an ingress configuration for k8s dashboard

[root@master ingress]# cat k8s-dashboard.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: k8s-dashboard-ingress
  namespace: kube-system
spec:
  rules:
  - host: k8s.webui
    http:
      paths:
      - path: /
        backend:
          serviceName: kubernetes-dashboard
          servicePort: 80
[root@master ingress]# kubectl create -f k8s-dashboard.yaml 
ingress "k8s-dashboard-ingress" created

Understand as follows:

  • Host refers to a virtual domain name. The specific address (which I understand should be the address of the Pod host in Ingress-controller) should be added to / etc/hosts so that all requests to k8s.webui will be sent to nginx.
  • path:/console matching application path
  • Service Port is mainly the port when defining service, not NodePort.
  • Path:/ Match the path of dashboard Application after

In the node node, dockers check whether ingress Controller exists, and then add the corresponding ip to the local host.
Access in the browser http://k8s.webui/
The following picture:

Post an ingress configuration file for multi-application deployment described in a previous blog post

[root@master ingress]# cat cloud-vue.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cloud-vue-api
  namespace: default
spec:
  rules:
  - host: cloud.vue.api
    http:
      paths:
      - path: /console
        backend:
          serviceName: helloworldsvc 
          servicePort: 7001
      - path: /
        backend:
          serviceName: cloud-simple-server
          servicePort: 8081


  - host: cloud.vue.eureka
    http:
      paths:
      - path: /
        backend:
          serviceName: cloud-eureka-server
          servicePort: 8888


  - host: cloud.vue.config
    http:
      paths:
      - path: /
        backend:
          serviceName: cloud-config-server
          servicePort: 1111 
  - host: cloud.vue.zipkin
    http:
      paths:
      - path: /
        backend:
          serviceName: cloud-zipkin-server
          servicePort: 9012

Note: Since ingress has been used to expose services, the type of service can be selected internal ly when redeploying, and no external is required. And https configuration, please refer to http://www.cnblogs.com/ericnie/p/6965091.html

Posted by abigail on Thu, 03 Jan 2019 19:57:09 -0800