Golang + Nuxt.js + Kubernetes でWebサービスを作る - その②

Golang + Nuxt.js + KubernetesWebサービスを作るシリーズ第二弾。 前の記事 で作成したAPIサーバをGKEのKubernetesクラスタにデプロイする。


まずは安くGKEクラスタを作成しなくてはならない。 しかし安すぎると今度は却ってメモリ量が少なく、これからデプロイしようと思っているNuxt.jsのアプリケーションがデプロイできなくなる。 なのでメモリをたくさん積んだn1-standard-1インスタンスで作成しようと思う。 まぁ、実際のサービスを作成するとなるとこの辺はもっと検討しないといけないところなのだが、そこは趣味ということで。 参考にしたのは下記リンク

https://blog.a-know.me/entry/2018/06/17/220222 わかりやすくGKEクラスタを作成する方法が書かれているので要参照。

まずはGKEクラスタを構築するためのインスタンス群を作成する

$ gcloud container clusters create webapp --preemptible --
machine-type n1-standard-1 --num-nodes=3 --disk-size=10

前回の記事で作成した本番環境用のイメージをGoogle Container Registryにアップロードするためにタグ付けを行う

$ docker tag api-prod gcr.io/[プロジェクト名]/[イメージ名]
:[バージョン名]

Google Container Registryに作成したAPIサーバのDockerイメージをアップロードする。

$ docker tag api-prod gcr.io/[プロジェクトID]/apiprod:1.0.0
$ gcloud auth configure-docker
$ docker push gcr.io/impactful-study-219313/apiprod:1.0.0

次に下記ymlファイルを用意してデプロイする

apiVersion: v1
kind: Service
metadata:
  name: webappapi
  labels:
    run: webappapi
spec:
  selector:
    run: webappapi
  ports:
  - name: http
    port: 8080
  type: NodePort

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webappapi
  labels:
    run: webappapi
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: webappapi
    spec:
      containers:
      - name: webappapi
        image: gcr.io/[プロジェクトID]/apiprod:1.0.0
        ports:
        - containerPort: 8080
      dnsPolicy: "None"
      dnsConfig:
        nameservers:
          - 1.1.1.1

ちなみにDockerコンテナからは通常DNSの名前解決ができないがdnsConfignameserversで名前解決の問い合わせ先を指定させることで名前解決を可能にしている。 下記コマンドで上記ymlファイルをKubernetesに反映させる。

$ kubectl apply -f webapp.yml

グローバルIPを割与え、Ingressを使ってAPIサーバをインターネット越しに呼ぶようにする。 ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - http:
      paths:
      - path: /api/*
        backend:
          serviceName: webappapi
          servicePort: 8080

KubernetesIngressの設定を適用する。

kubectl apply -f ingress.yml

これでグローバルIPが割り振られる。 なおIPアドレスが割り振られたあと、しばらく(數十分ほど)待たないとアクセスできないので注意。 Ingressで取得されたグローバルIPアドレスを表示するには下記コマンドを打つこと。

kubectl get ingress

http://[Ingressで割り当てられたIPアドレス]/api/v1/whois/google.com にアクセスしてWhois情報が表示されれば成功。

$ curl http://[IPアドレス]/api/v1/whois/example.com
{"domain":"example.com","result":"   Domain Name: EXAMPLE.COM\r\n   Registry Domain ID: 2336799_DOMAIN_COM-VRSN\r\n   Registrar WHOIS Server: whois.iana.org\r\n   Registrar URL: http://res-dom.iana.org\r\n   Updated Date: 2018-08-14T07:14:12Z\r\n   Creation Date: 1995-08-14T04:00:00Z\r\n   Registry Expiry Date: 2019-08-13T04:00:00Z\r\n   Registrar: RESERVED-Internet Assigned Numbers Authority\r\n   Registrar IANA ID: 376\r\n   Registrar Abuse Contact Email:\r\n   Registrar Abuse Contact Phone:\r\n   Domain Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited\r\n   Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited\r\n   Domain Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited\r\n   Name Server: A.IANA-SERVERS.NET\r\n   Name Server: B.IANA-SERVERS.NET\r\n   DNSSEC: signedDelegation\r\n   DNSSEC DS Data: 31589 8 1 3490A6806D47F17A34C29E2CE80E8A999FFBE4BE\r\n   DNSSEC DS Data: 31589 8 2 CDE0D742D6998AA554A92D890F8184C698CFAC8A26FA59875A990C03E576343C\r\n   DNSSEC DS Data: 43547 8 1 B6225AB2CC613E0DCA7962BDC2342EA4F1B56083\r\n   DNSSEC DS Data: 43547 8 2 615A64233543F66F44D68933625B17497C89A70E858ED76A2145997EDF96A918\r\n   DNSSEC DS Data: 31406 8 1 189968811E6EBA862DD6C209F75623D8D9ED9142\r\n   DNSSEC DS Data: 31406 8 2 F78CF3344F72137235098ECBBD08947C2C9001C7F6A085A17F518B5D8F6B916D\r\n   URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/\r\n\u003e\u003e\u003e Last update of whois database: 2019-08-04T01:55:52Z \u003c\u003c\u003c\r\n\r\nFor more information on Whois status codes, please visit https://icann.org/epp\r\n\r\nNOTICE: The expiration date displayed in this record is the date the\r\nregistrar's sponsorship of the domain name registration in the registry is\r\ncurrently set to expire. This date does not necessarily reflect the expiration\r\ndate of the domain name registrant's agreement with the sponsoring\r\nregistrar.  Users may consult the sponsoring registrar's Whois database to\r\nview the registrar's reported date of expiration for this registration.\r\n\r\nTERMS OF USE: You are not authorized to access or query our Whois\r\ndatabase through the use of electronic processes that are high-volume and\r\nautomated except as reasonably necessary to register domain names or\r\nmodify existing registrations; the Data in VeriSign Global Registry\r\nServices' (\"VeriSign\") Whois database is provided by VeriSign for\r\ninformation purposes only, and to assist persons in obtaining information\r\nabout or related to a domain name registration record. VeriSign does not\r\nguarantee its accuracy. By submitting a Whois query, you agree to abide\r\nby the following terms of use: You agree that you may use this Data only\r\nfor lawful purposes and that under no circumstances will you use this Data\r\nto: (1) allow, enable, or otherwise support the transmission of mass\r\nunsolicited, commercial advertising or solicitations via e-mail, telephone,\r\nor facsimile; or (2) enable high volume, automated, electronic processes\r\nthat apply to VeriSign (or its computer systems). The compilation,\r\nrepackaging, dissemination or other use of this Data is expressly\r\nprohibited without the prior written consent of VeriSign. You agree not to\r\nuse electronic processes that are automated and high-volume to access or\r\nquery the Whois database except as reasonably necessary to register\r\ndomain names or modify existing registrations. VeriSign reserves the right\r\nto restrict your access to the Whois database in its sole discretion to ensure\r\noperational stability.  VeriSign may restrict or terminate your access to the\r\nWhois database for failure to abide by these terms of use. VeriSign\r\nreserves the right to modify these terms at any time.\r\n\r\nThe Registry database contains ONLY .COM, .NET, .EDU domains and\r\nRegistrars.\r\n"}