Ingress-Nginx

发现好文:

收藏 https://www.cnblogs.com/crazymagic/p/11267303.html

注意:

本文只用的ingress-nginx版本为0.24.1,可在官方网站自行下载
https://github.com/kubernetes/ingress-nginx

一、Ingress-Nginx工作原理

  1. ingress controller通过和kubernetes api交互,动态的去感知集群中ingress规则变化
  2. 然后读取它,按照自定义的规则,规则就是写明了哪个域名对应哪个service,生成一段nginx配置
  3. 再写到nginx-ingress-control的pod里,这个Ingress controller的pod里运行着一个Nginx服务,控制器会把生成的nginx配置写入/etc/nginx.conf文件中, 4.然后reload一下使配置生效。以此达到域名分配置和动态更新的问题。

二、搭建

kubnet ~]$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.24.1/deploy/mandatory.yaml
kubnet ~]$ kubectl get pods -n ingress-nginx
kubnet ~]$ mkdir ingress
kubnet ingress]$ vim deploy-demo.yml
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: stable
  ports:
  - name: myapp
    port: 80
    targetPort: 80
---
            
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    matchLabels:
      app: myapp
      release: stable
   replicas: 3
   template:
     metadata:
       labels:
         app: myapp
         release: stable
      spec:
        containers:
        - name: myapp
          image: fangwenkai/kubernetes:v1.15
          imagePullPolicy: IfNotPresent
        ports:
        - name: myapp
      containerPort: 80
kubnet ingress]$ kubectl apply -f deploy-demo.yml
kubnet ingress]$ kubectl get svc
kubnet ingress]$ kubectl get deploy
kubnet ingress]$ kubectl get pods
kubnet ingress]$ wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.24.1/deploy/provider/baremetal/service-nodeport.yaml
kubnet ingress]$ vim service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
 name: ingress-nginx
 namespace: ingress-nginx
 labels:
   app.kubernetes.io/name: ingress-nginx
   app.kubernetes.io/part-of: ingress-nginx
spec:
  type: NodePort
  ports:
  - name: http
   port: 80
   targetPort: 80
   nodePort: 30001                                 # 手动指定服务器端口,不指定就是默认
   protocol: TCP
  - name: https
    port: 443
   targetPort: 443
   nodePort: 30443                                 # 手动指定服务器端口,不指定就是默认
    protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
kubnet ingress]$ kubectl get svc -n ingress-nginx
kubnet ingress]$ curl 192.168.23.101:30001    # 访问本机地址,此时结果应该是404
kubnet ingress]$ vim ingress-myapp.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
 name: ingress-myapp
 namespace: default
 annotations:
   kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: myapp.kaikai.com
    http:
     paths:
     - path:
       backend:
        serviceName: myapp
        servicePort: 80
kubnet ingress]$ kubectl get ingress
kubnet ingress]$ curl myapp.kaikai.com:30001       # 能看到后端Pod结果,或者浏览器直接访问地址
posted @ 2019-07-11 11:53  k-free  阅读(1409)  评论(0编辑  收藏  举报