导语:通过yaml中设置env的值 动态更新nginx的端口信息,以便一个镜像可以在不同环境使用。

测试的文件 /etc/nginx/conf.d/nginx.conf.template


upstream backend {
  server ${BACKEND_HOST}:${BACKEND_PORT};
}

upstream backend2 {
  server ${BACKEND_HOST}:${SVRSHELL_PORT};
}
#设定虚拟主机配置
server {
  listen    80;
  server_name  _;
  root /var/html;

  client_max_body_size 8g;
  proxy_send_timeout 600s;
  proxy_read_timeout 600s;
  proxy_redirect off;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header REMOTE-HOST $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  client_body_buffer_size 256k;
  proxy_buffering off;

  location ~ .*globalConfig.js$ {
    add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
  }

  location ~ .*\.(?:js|css)$ {
    expires 30d;
  }

  location ~ .*\.(?:htm|html)$ {
    add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
  }

  location ~ .*?\.(js|css|jpg|png|jpeg|less|sass) {
    root /var/html;
  }

  location ^~ /chrome {
    return 301 ./49chrome.exe;
  }

  location / {
    try_files $uri $uri/ /index.html;
    index index.html;
  }

  location ${FRONT_END_PREFIX}/ {
    proxy_set_header Host ${BACKEND_HOST};

    proxy_pass  http://backend/;
    #client_max_body_size 40m;
  }

  error_page   500 502 503 504 /50x.html;
    location = /50x.html {
  }

  location ~ /.ht {
    deny all;
  }
}

修改配置的脚本run.sh

echo "################################## Run nginx"
# 不加export DOLLAR='$'执行会把$remote_addr这些替换成空。
# 注意DOLLAR='$'的用法,并用${DOLLAR}PATH转义$ PATH以避免将$ PATH替换为来自"错误"主机的值。
export DOLLAR='$'

# 这里如果使用envsubst < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/nginx.conf # /etc/nginx/conf.d/default.conf 会导致nginx自带的参数为空
envsubst '${SVRSHELL_PORT}' < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/nginx.conf # /etc/nginx/conf.d/default.conf
#envsubst '${BACKEND_HOST}','${BACKEND_PORT}','${FRONT_END_PREFIX}' < /etc/nginx/conf.d/nginx.conf.template > /etc/nginx/conf.d/nginx.conf # /etc/nginx/conf.d/default.conf
#envsubst '${VERSION}' < /var/html/index.html.template > /var/html/index.html

echo "Generate nginx.conf"

把上述文件上传到容器中后 设置env

export SVRSHELL_PORT='8123'
./run.sh

脚本执行完成之后查看生成的文件。已经替换对应的参数成功。

关于DOLLAR='$'我理解应该是如下面所说。不存在的参数$Par会变成${DOLLAR}Par,env中能够获取到的的变量会替换进去。

export DOLLAR='$'
export THIS=THAT
echo '${DOLLAR}THIS' | envsubst

部署时helm的yaml env为SVRSHELL_PORT

# svrshell-dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: svrshell-dp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: svrshell
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: svrshell
    spec:
      restartPolicy: Always
#        - command: ['sh', '-c', "until nslookup platform-headless.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for platform; sleep 2; done"]
#          image: 10.10.3.246/cta/busybox-curl:01
#          name: wait-for-platform
      containers:
      - name: svrshell
        image: {{ .Values.images.svrshell }}
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: en_US.UTF-8
        - name: SVRSHELL_PORT
          value: "81"
        volumeMounts:
        - name: dicom-pvc
          mountPath: /data1/inputdata
      volumes:
      - name: dicom-pvc
        hostPath:
          path: /data1/inputdata
          type: DirectoryOrCreate

参考

https://www.cnblogs.com/mailaidedt/p/12527090.html

Logo

开源、云原生的融合云平台

更多推荐