使用ingress 暴露springcloud服务

使用ingress 暴露springcloud服务

k8s 集群搭建成功了,也能部署应用了,但是不可能每次去访问服务的时候都要去查一下运行的node和nodePort吧,即使查好node和nodePort ,避免不了重启k8s或者 ReplicationController,node 和 nodeport 又会改变,K8s 提供了 Ingress 来解决这个问题。


  • 使用ingress 暴露springcloud服务
    • 生成一个默认的后端
    • 部署Ingress Controller
    • 配置ingress

1.生成一个默认的后端

生成一个默认的后端,如果遇到解析不到的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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
[root@master ingress]# kubectl create -f default-backend.yaml 
deployment "default-http-backend" created
service "default-http-backend" created
  • 1
  • 2
  • 3

2.部署Ingress Controller

点击查看官方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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

记得修改 - –apiserver-host=http://192.168.6.45:8080

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

3.配置ingress

贴一个k8s dashboard 的 ingress配置

[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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
[root@master ingress]# kubectl create -f k8s-dashboard.yaml 
ingress "k8s-dashboard-ingress" created
  • 1
  • 2

理解如下:

  • host指虚拟出来的域名,具体地址(我理解应该是Ingress-controller那台Pod所在的主机的地址)应该加入/etc/hosts中,这样所有去k8s.webui的请求都会发到nginx
  • path:/console匹配后面的应用路径
  • servicePort主要是定义服务的时候的端口,不是NodePort.
  • path:/ 匹配后面dashboard应用的路径

在 node 节点中docker ps 查看是否存在ingress Controller,然后把对应的ip添加进本机host, 
在浏览器内访问 http://k8s.webui/ 
如下图: 
webui

贴一个前一篇博客介绍的多应用的部署的ingress 配置文件

[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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

:已经使用ingress暴露服务了,再部署的时候service的类型可以选择internal了 ,不再需要external。还有https的配置,请参考 http://www.cnblogs.com/ericnie/p/6965091.html

你可能感兴趣的:(kubernetes)