LoginSignup
4
5

More than 3 years have passed since last update.

KubernetesのIngressで使うSSL自己署名証明書をPowerShellで作る

Last updated at Posted at 2019-12-30

... 自己署名証明書を使うほうが最早難しい時代だよねメモその1。

EDIT: secretに秘密鍵と公開鍵を同じファイルで入れても動くけど、同じファイルなのは微妙に紛らわしいので注記。

証明書の作成

適当にPowerShellを開いて、 New-SelfSignedCertificate コマンドで作る。

New-SelfSignedCertificate -DnsName "flow.local" -NotAfter (Get-Date).AddYears(50) `
 -CertStoreLocation "cert:\CurrentUser\My" -KeyLength 4096 -Type SSLServerAuthentication `
-TextExtension @("2.5.29.19 = {critical} {text}ca=1&pathlength=0") `
-KeyUsage CertSign,KeyEncipherment,DigitalSignature

KeyLength みたいなパラメタはお好みで良いけど、難しいのは TextExtensionKeyUsage パラメタで、それぞれ:

これで作成した証明書は、ファイル名を指定して実行 → certmgr.msc で見つけられる。

SnapCrab_NoName_2019-12-31_3-48-9_No-00.png

管理ツール上の証明書は特に用途が無いので、インストールが終わったら消してしまって良い。

秘密鍵のインストール

管理ツールでの証明書を右クリックしてエクスポート...から秘密鍵をエクスポート、エクスポートされた .pfx ファイルに openssl コマンドを掛けて鍵データを .pem 形式で取り出す:

openssl pkcs12 -in flowlocal.pfx -nodes -out key.pem

(Windowsでは秘密鍵のエクスポート時にパスワードが必須になっている)

取り出した .pemkubectl で secret としてインストールする:

kubectl create secret tls flowlocal-tls --cert=key.pem --key=key.pem

certkey は同じファイルでOK(1つのファイルに両方を入れておける)。ここでは flowlocal-tls という名前で作成している。 EDIT: この方法でTLS鍵として機能するが、公開鍵ファイルの中に秘密鍵も一緒に入ることになって微妙に気味が悪いので注意 。 気になるなら openssl コマンドに -nocerts とか -nokeys を与えて別々のファイルにする。

秘密鍵のIngressでの使用

これ以降は普通のTLS鍵と違うところはない。単に TLS のsecretとして指定すれば、Ingressのトラフィックは自動的にその鍵で暗号化される:

    tls:
        - hosts:
            - flow.local
          secretName: flowlocal-tls

ただし、ここの hostsNew-SelfSignedCertificate に与えた DnsName は一致している必要がある。

4
5
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
4
5