このトピックでは、Alibaba Cloud Container Service for Kubernetes により提供される Ingress 機能を用いて最新バージョンのサービスのリリース方法を解説します。
始める前に
- Kubernetes クラスターが作成されている必要があります。 詳しくは、「Kubernetes クラスターの作成」をご参照ください。
- kubectl を利用して Kubernetes クラスターへ接続されている必要があります。 詳しくは、「kubectl を利用した Kubernetes クラスターへの接続」をご参照ください。
手順
- Container Service コンソール にログインします。
- Kubernetes で、左側のナビゲーションウィンドウから [アプリケーション] > [デプロイ] をクリックします。
- 右上角の [テンプレートによる作成] をクリックします。
-
対象となるクラスターおよび名前空間を選択し、サンプルテンプレートを選択するか、またはテンプレートをカスタマイズします。その後、[デプロイ] をクリックします。
必要となるデプロイ、対象となるサービスおよび Ingress を含んだ、最新バージョンの Nginx アプリケーションをデプロイします。 デプロイおよびサービスを含んだオーケストレーションテンプレートは以下のようになります。apiVersion: extensions/v1beta1 kind: Deployment metadata: name: new-nginx spec: replicas: 1 selector: matchLabels: run: new-nginx template: metadata: labels: run: new-nginx spec: containers: - image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx imagePullPolicy: Always name: new-nginx ports: - containerPort: 80 protocol: TCP restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: new-nginx spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: new-nginx sessionAffinity: None type: NodePort
異なるアノテーション設定の Ingress オーケストレーションテンプレートは以下のようになります。注 Ingress テンプレートのアノテーションフィールドにおいて、service-match または service-weight を設定しない場合、Ingress コントローラーはランダムな方式で、最新バージョンおよび以前のバージョンのサービスに均等にクライアントリクエストを転送します。-
最新バージョンのサービスへの転送に正規表現 "foo=bar" の要件を満たすクライアントリクエストのみを指定するために使用される Ingress テンプレート
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: gray-release annotations: nginx.ingress.kubernetes.io/service-match: | # Only if the request header of a request meets the requirements of the regular expression foo=bar, can the request be routed to the new-nginx service. new-nginx: header("foo", /^bar$/) spec: rules: - host: www.example.com http: paths: # Earlier version of the service - path: / backend: serviceName: old-nginx servicePort: 80 # Latest version of the service - path: / backend: serviceName: new-nginx servicePort: 80
-
最新バージョンのサービスへ転送するリクエストの割合を指定するために使用される Ingress テンプレート注 この例では、最新バージョンのサービスおよび以前のバージョンのサービスはそれぞれ 50% に重み付けされています。
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: gray-release annotations: nginx.ingress.kubernetes.io/service-weight: | # Set 50% of traffic to be routed to the new-nginx service. new-nginx: 50, old-nginx: 50 spec: rules: - host: www.example.com http: paths: # Earlier version of the service - path: / backend: serviceName: old-nginx servicePort: 80 # Latest version of the service - path: / backend: serviceName: new-nginx servicePort: 80
-
"foo=bar" の要件を満たすクライアントリクエストのトラフィックの 50% のみを最新バージョンのサービスへ転送することを指定するために使用される Ingress テンプレート
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: gray-release annotations: nginx.ingress.kubernetes.io/service-match: | # Only if the request header of a request meets the requirements of the regular expression foo=bar, can the request be routed to the new-nginx service. new-nginx: header("foo", /^bar$/) nginx.ingress.kubernetes.io/service-weight: | # Only 50% of the client request traffic that meets the requirements of the preceding matching rule can be routed to the new-nginx service. new-nginx: 50, old-nginx: 50 spec: rules: - host: www.example.com http: paths: # Earlier version of the service - path: / backend: serviceName: old-nginx servicePort: 80 # Latest version of the service - path: / backend: serviceName: new-nginx servicePort: 80
-
- 左側のナビゲーションウィンドウから、[アプリケーション] > [Ingress] をクリックします。
"old-nginx" を指す仮想ホスト名が確認できます。
- マスターノードにログインします。"curl" コマンドを実行し、以下のような設定の Ingress アクセスを表示します。
-
正規表現 "foo=bar" 要件を満たすクライアントリクエストのみ、最新バージョンのサービスへ転送されます。
# curl -H "Host: www.example.com" -H "foo: bar" http://<EXTERNAL_IP>
-
指定した割合のリクエストが最新バージョンのサービスへ転送されます。
# curl -H "Host: www.example.com" http://<EXTERNAL_IP>
-
正規表現 "foo=bar" 要件を満たすクライアントリクエストのトライフィックの 50% のみ最新バージョンのサービスへ転送されます。
# curl -H "Host: www.example.com" -H "foo: bar" http://<EXTERNAL_IP>
-