微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx reverse proxy in Kubernetes

Dynamic reverse proxy using nginx in Kubernetes

1. create a configmap.yaml  

apiVersion: v1
kind: ConfigMap
Metadata:
  name: confNginx
data:
  Nginx.conf: |
    user  Nginx;
    worker_processes  1;
    error_log  /var/log/Nginx/error.log warn;
    pid        /var/run/Nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
      include       /etc/Nginx/mime.types;
      default_type  application/octet-stream;
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
      access_log  /var/log/Nginx/access.log  main;
      sendfile        on;
      keepalive_timeout  65;
      server {
        listen 80;

        server_name ~^(?<subdomain>.*?)\.;
        resolver kube-dns.kube-system.svc.cluster.local valid=5s;

        location /healthz {
          return 200;
        }

        location / {
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "Upgrade";
          proxy_pass http://$subdomain.msce0.svc.cluster.local;
          proxy_set_header Host $host;
          proxy_http_version 1.1;
        }
      }
    }

 run

kubectl apply -f configmap.yaml

 

2. create a service.yaml

apiVersion: apps/v1
kind: Deployment
Metadata:
  name: Nginx
  labels:
    app: Nginx
spec:
  selector:
    matchLabels:
      app: Nginx
  replicas: 1
  template:
    Metadata:
      labels:
        app: Nginx
    spec:
      containers:
        - name: Nginx
          image: Nginx:alpine
          ports:
          - containerPort: 80
          volumeMounts:
            - name: Nginx-config
              mountPath: /etc/Nginx/Nginx.conf
              subPath: Nginx.conf
      volumes:
        - name: Nginx-config
          configMap:
            name: confNginx

run

kubectl apply -f service.yaml

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐