Skip to content

Instantly share code, notes, and snippets.

@kubosuke
Last active November 27, 2019 14:07
Show Gist options
  • Save kubosuke/192f1d0e1143d1c7069a282e56d71975 to your computer and use it in GitHub Desktop.
Save kubosuke/192f1d0e1143d1c7069a282e56d71975 to your computer and use it in GitHub Desktop.

下記のように、set_real_ip_fromにLBやIngressのIPを記載し、real_ip_recursiveを設定することで、remote_addr変数にクライアントの ソース元IPを入れることができるようです。

       ##                                                                                                                                                                                          
        # Logging Settings                                                                                                                                                                          
        ##                                                                                                                                                                                          
        real_ip_header X-Forwarded-For;
        set_real_ip_from 130.211.0.0/22;
        set_real_ip_from 35.191.0.0/16;
        set_real_ip_from 107.178.252.189;
        real_ip_header    X-Forwarded-For;
        real_ip_recursive on;

log_format  main  'remote_addr is [$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;

以前のトライアル

  • resqueポッドにnginxコンテナを追加(イメージ:https://hub.docker.com/_/nginx)
  • resqueコンテナを8080アクセスに、nginxコンテナを80アクセスとし、resqueコンテナへのアクセスポートを80に変更
  • バックエンドへのアクセス

TODO 8/6

  • set_real_ip_fromにIngressなどのIPを記載
  • remote_addrにクライアントのGIPが記載されることを確認
  • location項にて、allow, denyによりIPフィルタリングを実現
@kubosuke
Copy link
Author

kubosuke commented Aug 6, 2019

時間がかかった部分

  • ポッド内のコンテナを変更(=レジストリにイメージをPUSH)した時、反映に時間がかかる & なかなか反映されない?
  • 誤った設定を追加した際にヘルスチェックが効いてしまい、502応答がなされてしまい、サーバ側への疎通ができない(=ログによる設定ミスの確認ができない)

@kubosuke
Copy link
Author

kubosuke commented Aug 6, 2019

Codes

defoult.conf

log_format  test  'remote_addr is [$remote_addr] - $remote_user "$http_x_forwarded_for"';

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  test;

    #ip_filtering setting
    real_ip_header X-Forwarded-For;
    set_real_ip_from 35.201.89.164;
    set_real_ip_from 10.0.0.0/8;
    real_ip_recursive on;

    location / {
    	allow 118.8.21.173;
	deny all;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /overview {
        allow 118.8.21.173 10.0.0.0/8;
	deny all;
    	proxy_pass http://127.0.0.1:8080;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • Dockerfile
FROM nginx

COPY ./default.conf /etc/nginx/conf.d/default.conf
  • deployment.integration.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: airfunding-resque
  labels:
    app: airfunding-resque
    version: '2.0.0'
    tier: frontend
    role: web
    access: external
    clientFacing: 'no'
spec:
  selector:
    matchLabels:
      app: airfunding-resque
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: airfunding-resque
        version: '2.0.0'
        tier: frontend
        role: web
        access: external
        clientFacing: 'no'
    spec:
      containers:
      - name: resque-web
        args:
        - resque-web
        - -p
        - '8080'
        - /resque.rb
        - --foreground
        env:
        - name: RAILS_ENV
          value: integration
        image: gcr.io/kiheitai.co.jp/api-project-784973659234/airfunding-resque:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: 64Mi
            cpu: 10m
        readinessProbe:
          httpGet:
            path: /overview
            port: 8080
          initialDelaySeconds: 5
        livenessProbe:
          httpGet:
            path: /overview
            port: 8080
          initialDelaySeconds: 5
      - name: resque-nginx-web
        image: gcr.io/kiheitai.co.jp/api-project-784973659234/airfunding-resque-nginx
        imagePullPolicy: Always
        ports:
          - containerPort: 80
        resources:
          requests:
            memory: 64Mi
            cpu: 10m
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 5
  • service.yaml
apiVersion: v1
kind: Service
metadata:
  name: airfunding-resque
spec:
  ports:
  - name: http
    port: 80
  selector:
    app: airfunding-resque
  type: NodePort

@kubosuke
Copy link
Author

kubosuke commented Aug 6, 2019

現在いくつか編集を加えたので、環境が乱れています、、元に戻す場合は上記ファイルにてPUSHお願いします。

残務は次の通りです。

  • ヘルスチェック用のエンドポイントを作成
  • それ以外の全てのLOCATIONについて、バックエンドへプロキシする設定を構築
  • ヘルスチェック先を上述のエンドポイントに変更

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment