码迷,mamicode.com
首页 > 其他好文 > 详细

Ingress-Nginx

时间:2019-07-11 12:49:05      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:efault   default   交互   域名   rem   png   ica   load   -o   

注意:

   本文只用的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结果

  或者浏览器直接访问地址

技术图片

 

Ingress-Nginx

标签:efault   default   交互   域名   rem   png   ica   load   -o   

原文地址:https://www.cnblogs.com/k-free-bolg/p/11169111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!