GeekFactory

int128.hatenablog.com

GKE with HTTPS by kube-lego

GKEでHTTPSを使う場合のメモです。

kube-legoのサンプルに書いてある通りですが、一部注意が必要です。

github.com

まず、kube-legoをデプロイします。

cd lego
vim configmap.yaml
#...snip
data:
  # modify this to specify your address
  lego.email: "int128@example.com"
#...snip
kubectl apply -f 00-namespace.yaml
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml

GCPのコンソールを開き、VPC networkのExternal IP addressesでStatic IPアドレスを取得します。Ephemeral IPのままだと、HTTPとHTTPSで別のIPアドレスが割り当てられてしまうようです。取得したIPアドレスワイルドカードドメイン(例:*.gke.example.com)に割り当てておきます。

続いて、echoserverをデプロイします。

cd echoserver
vim ingress-tls.yaml
#...snip
   annotations:
     kubernetes.io/tls-acme: "true"
     kubernetes.io/ingress.class: "gce"
     kubernetes.io/ingress.global-static-ip-name: "取得したIPアドレスの名前"
 spec:
   tls:
   - hosts:
     - "echoserver.gke.example.com"
     secretName: echoserver-tls
   rules:
   - host: "echoserver.gke.example.com"
     http:
       paths:
       - path: /*
#...snip
kubectl apply -f 00-namespace.yaml
kubectl apply -f service.yaml
kubectl apply -f deployment.yaml
kubectl apply -f ingress-tls.yaml

しばらく待つとHTTPSでアクセスできるようになります。echoserverは下記のようなレスポンスを返すはずです。

CLIENT VALUES:
client_address=('10.138.0.2', 55605) (10.138.0.2)
command=GET
path=/
real path=/
query=
request_version=HTTP/1.1

SERVER VALUES:
server_version=BaseHTTP/0.6
sys_version=Python/3.5.0
protocol_version=HTTP/1.0

HEADERS RECEIVED:
Connection=Keep-Alive
Host=echo.gke.example.com
Via=1.1 google
X-Cloud-Trace-Context=448bd4d1de99765e9f40a87da8f39be2/16499974018548596039
X-Forwarded-For=68.177.129.98, 35.227.235.139
X-Forwarded-Proto=https
accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
accept-encoding=gzip, deflate, br
accept-language=en-US,en;q=0.9
cache-control=max-age=0
upgrade-insecure-requests=1
user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

kube-legoが証明書を取得する過程は下記のログで確認できます。

kubectl get po --namespace kube-lego
kubectl logs -f --namespace kube-lego kube-lego-****-****

Name based virtual host

1つのIngressで複数のドメインを公開してみましょう。ここではnginxを追加します。

kubectl run nginx --image=nginx --port=80 --namespace=echoserver
kubectl expose deployment nginx --target-port=80 --type=NodePort --namespace=echoserver
cd echoserver
vim ingress-tls.yaml
#...snip
spec:
  tls:
  - hosts:
    - echo.gke.example.com
    - nginx.gke.example.com    # 追加
    secretName: echoserver-tls
  rules:
  - host: echo.gke.example.com
    http:
      paths:
      - path: /*
        backend:
          serviceName: echoserver
          servicePort: 80
  - host: nginx.gke.example.com    # 追加
    http:
      paths:
      - path: /*
        backend:
          serviceName: nginx
          servicePort: 80
kubectl apply -f ingress-tls.yaml

10〜15分ぐらい待つと証明書が更新されてアクセスできるようになります。