posts - 177,comments - 14,views - 40万

ingress-nginx的部署方式有多种,本文介绍nodeport方式和hostnetwork方式:

一、nodeport方式

1、下载mandatory.yaml文件:https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

然后修改文件中的镜像:registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:0.25.1

2、编辑service-nodeport.yaml(nodeport方式需要额外使用这个文件)

复制代码
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
      protocol: TCP
      nodePort: 32080  #http
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
      nodePort: 32443  #https
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
View Code
复制代码

3、运行部署上面这两个文件:

$ kubectl apply -f mandatory.yaml
$ kubectl apply -f node-port.yaml

4、部署后端程序

复制代码
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
    release: canary
  ports:
  - name: http
    port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
      release: canary
  template:
    metadata:
      labels:
        app: myapp
        release: canary
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v2
        ports:
        - name: httpd
          containerPort: 80
View Code
复制代码

5、配置后端程序的ingress:

复制代码
$ vim ingress-myapp.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-myapp namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: myapp.cabel.com http: paths: - path: backend: serviceName: myapp servicePort: 80
复制代码

6、然后访问:myapp.cabel.com:NodePort(32080)

7、增加后端服务:

复制代码
$ vim deploy-tomcat.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector:
    app: tomcat
    release: canary
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  - name: ajp
    port: 8009
    targetPort: 8009

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deploy
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat
        image: tomcat:7-alpine
        ports:
        - name: httpd
          containerPort: 8080
        - name: ajp
          containerPort: 8009
View Code
复制代码
复制代码
$ vim ingress-tomcat.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-tomcat
  namespace: default
  annotations:
    kubernets.io/ingress.class: "nginx"
spec:
  rules:
  - host: tomcat.cabel.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080
View Code
复制代码

8、然后访问:tomcat.cabel.com:NodePort(32080)

二、deamonset方式,deamonset方式与上一种方式只有一处不同,只修改mandatory.yaml,而且,service-nodeport.yaml也不用配置,其他不用修改,访问的时候,直接域名访问,不用加nodeport。

 三、外部nginx引入流量到集群内部

比如域名为myapp.cabel.com,在配置了Deamonset模式的ingress-nginx后,外部的nginx配置如下

复制代码
upstream app4core {
        server  192.168.10.55;
        server  192.168.10.56;
}

server {
        listen 80;
        server_name myapp.cabel.com;
        access_log /data/logs/nginx/app4core.dogotsn.access.log;
        error_log /data/logs/nginx/app4core.dogotsn.error.log;

        location / {

                        proxy_set_header X-Forwarded-For $remote_addr;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $host;
                        proxy_redirect off;
                        proxy_read_timeout 600;
                        proxy_connect_timeout 600;
                        proxy_pass http://app4core;
                }

        }
复制代码

此种架构为双层nginx代理,所以配置外层的nginx时,需要在请求报文的时候不去修改请求头

参考文献:https://www.jb51.net/article/122582.htm

posted on   自然洒脱  阅读(8167)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示