博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Kubernetes中的Ingress
阅读量:5742 次
发布时间:2019-06-18

本文共 4907 字,大约阅读时间需要 16 分钟。

Ingress是什么

Ingress :简单理解就是个规则定义;比如说某个域名对应某个 service,即当某个域名的请求进来时转发给某个 service;这个规则将与 Ingress Controller 结合,然后 Ingress Controller 将其动态写入到负载均衡器配置中,从而实现整体的服务发现和负载均衡

Ingress Controller

实质上可以理解为是个监视器,Ingress Controller 通过不断地跟 kubernetes API 打交道,实时的感知后端 service、pod 等变化,比如新增和减少 pod,service 增加与减少等;当得到这些变化信息后,Ingress Controller 再结合Ingress 生成配置,然后更新反向代理负载均衡器,并刷新其配置,达到服务发现的作用

1406056-20181024180229091-1262296683.jpg

安装Ingress

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml #安装ingress-Controller
  • 创建一后端pod service:

    [root@master ingress]# kubectl apply -f deploy-demo.yaml[root@master ingress]# cat deploy-demo.yaml apiVersion: v1kind: Servicemetadata:  name: myapp  namespace: defaultspec:  selector:    app: myapp    release: canary  ports:  - name: http    targetPort: 80    port: 80---apiVersion: apps/v1kind: Deploymentmetadata:  name: myapp-deploy  namespace: defaultspec:  replicas: 3  selector:    matchLabels:      app: myapp      release: canary  template:    metadata:      labels:        app: myapp        release: canary    spec:      containers:      - name: myapp        image: ikubernetes/myapp:v2        ports:        - name: http          containerPort: 80
  • 创建一个用于暴露端口的service

    [root@master baremetal]# kubectl apply -f service-nodeport.yaml[root@master baremetal]# cat service-nodeport.yaml apiVersion: v1kind: Servicemetadata:  name: ingress-nginx  namespace: ingress-nginx  labels:    app.kubernetes.io/name: ingress-nginx    app.kubernetes.io/part-of: ingress-nginxspec:  type: NodePort  ports:    - name: http      port: 80      targetPort: 80      protocol: TCP      nodePort: 30080    - name: https      port: 443      targetPort: 443      protocol: TCP      nodePort: 30443  selector:    app.kubernetes.io/name: ingress-nginx
  • 创建Ingress文件

    [root@master ingress]# kubectl apply -f ingress-myapp.yaml[root@master ingress]# cat ingress-myapp.yaml apiVersion: extensions/v1beta1kind: Ingressmetadata:  name: ingress-myapp  namespace: default  annotations:    kubernetes.io/ingress.class: "nginx"spec:  rules:  - host: myapp.template.com    http:      paths:      - path:        backend:          serviceName: myapp          servicePort: 80
  • 查看信息

    [root@master ingress]# kubectl get ingressNAME                 HOSTS                 ADDRESS   PORTS     AGEingress-myapp        myapp.template.com              80        5h55[root@master ingress]# kubectl get svcNAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGEmyapp        ClusterIP   10.98.30.144     
    80/TCP 4h7m[root@master ingress]# kubectl get podsNAME READY STATUS RESTARTS AGEmyapp-deploy-7b64976db9-lfnlv 1/1 Running 0 6h30mmyapp-deploy-7b64976db9-nrfgs 1/1 Running 0 6h30mmyapp-deploy-7b64976db9-pbqvh 1/1 Running 0 6h30m#访问[root@master ingress]# curl myapp.template.com:30080Hello MyApp | Version: v2 |
    Pod Name

Ingress使用ssl

[root@master ingress]# cat tomcat-deploy.yaml apiVersion: v1kind: Servicemetadata:  name: tomcat  namespace: defaultspec:  selector:    app: tomcat    release: canary  ports:  - name: http    targetPort: 8080    port: 8080  - name: ajp    targetPort: 8009    port: 8009    ---apiVersion: apps/v1kind: Deploymentmetadata:  name: tomcat-deploy  namespace: defaultspec:  replicas: 3  selector:    matchLabels:      app: tomcat      release: canary  template:    metadata:      labels:        app: tomcat        release: canary    spec:      containers:      - name: tomcat        image: tomcat:8.5-alpine        ports:        - name: http          containerPort: 8080        - name: ajp          containerPort: 8009[root@master ingress]# kubectl apply -f  tomcat-deploy.yaml [root@master ingress]# openssl genrsa -out tls.key 2048[root@master ingress]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Beijing/L=Beijing/O=DevOps/CN=tomcat.template.com[root@master ingress]# kubectl create secret tls tomcat-ingress-secret --cert=tls.crt --key=tls.key[root@master ingress]# kubectl get secretNAME                    TYPE                                  DATA   AGEdefault-token-962mh     kubernetes.io/service-account-token   3      32htomcat-ingress-secret   kubernetes.io/tls                     2      66m[root@master ingress]# cat ingress-tomcat-tls.yaml apiVersion: extensions/v1beta1kind: Ingressmetadata:  name: ingress-tomcat-tls  namespace: default  annotations:    kubernetes.io/ingress.class: "nginx"spec:  tls:  - hosts:      - tomcat.template.com    secretName: tomcat-ingress-secret  rules:  - host: tomcat.template.com    http:      paths:      - path:        backend:          serviceName: tomcat          servicePort: 8080[root@master ingress]# kubectl apply -f ingress-tomcat-tls.yaml[root@master ingress]# curl -k https://tomcat.template.com:30443 #测试访问

转载于:https://www.cnblogs.com/Template/p/9845025.html

你可能感兴趣的文章
设计模式 ( 十八 ):State状态模式 -- 行为型
查看>>
OracleLinux安装说明
查看>>
nova分析(7)—— nova-scheduler
查看>>
python Gunicorn
查看>>
Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
查看>>
Spring.net 学习笔记之ASP.NET底层架构
查看>>
I.MX6 wpa_cli 使用
查看>>
OpenMediaVault 搭建git,ssh无法连接问题
查看>>
[WPF]使用WindowChrome自定义Window Style
查看>>
java多线程之:Java中的ReentrantLock和synchronized两种锁定机制的对比 (转载)
查看>>
mysql性能优化学习笔记-参数介绍及优化建议
查看>>
[Everyday Mathematics]20150105
查看>>
166.3. 容器
查看>>
1.6. Network
查看>>
【Web动画】SVG 实现复杂线条动画
查看>>
主流手机分辨率 尺寸 操作系统
查看>>
Office版本差别引发的语法问题
查看>>
使用Wireshark捕捉USB通信数据
查看>>
iOS - KVC 键值编码
查看>>
《树莓派渗透测试实战》——1.1 购买树莓派
查看>>