LoginSignup
9
6

More than 5 years have passed since last update.

IngressでBasic認証を使いたい時のメモ

Posted at

IngressでBasic認証を使うとき用のメモ。
ほぼ、以下ページのトレースです。

Basic Authentication - kubernetes/ingress-nginx - github
Basic Authentication - NGINX Ingress Controller

設定の流れ

htpasswdでパスワード生成

$ htpasswd -c auth foo
New password:
Re-type new password:
Adding password for user foo

secretの作成

$ kubectl create secret generic basic-auth --from-file=auth -n $namespace
secret/basic-auth created

作ったsecretの確認

$ kubectl get secret basic-auth -o yaml -n $namespace
apiVersion: v1
data:
  auth: Zm9vOiRhcHIxJExQdVB2NmpTJEFObWV4NVloUmIwT1JGYXE0TjVYaS8K
kind: Secret
metadata:
  name: basic-auth
  namespace: $namespace
type: Opaque

マニフェストの作成

ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-with-auth
  annotations:
    # 認証タイプを指定
    nginx.ingress.kubernetes.io/auth-type: basic
    # 作成したsecret名を指定
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
    # 認証時のメッセージ。なくても良さげ
    nginx.ingress.kubernetes.io/auth-realm: "Authentication Required - foo"
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /
        backend:
          serviceName: http-svc
          servicePort: 80

デプロイ

kubectl create -f ingress.yaml

以上で、Basic認証が有効になっているはずです。

参考ページ

Basic Authentication - kubernetes/ingress-nginx - github
Basic Authentication - NGINX Ingress Controller

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6