タスク

タスク
クラスターの管理
Access Clusters Using the Kubernetes API (EN)
Access Services Running on Clusters (EN)
Advertise Extended Resources for a Node (EN)
Autoscale the DNS Service in a Cluster (EN)
Change the default StorageClass (EN)
Change the Reclaim Policy of a PersistentVolume (EN)
Cluster Management (EN)
Configure Multiple Schedulers (EN)
Configure Out of Resource Handling (EN)
Configure Quotas for API Objects (EN)
Control CPU Management Policies on the Node (EN)
Control Topology Management Policies on a node (EN)
Customizing DNS Service (EN)
Debugging DNS Resolution (EN)
Declare Network Policy (EN)
Enabling EndpointSlices (EN)
Enabling Service Topology (EN)
Encrypting Secret Data at Rest (EN)
Guaranteed Scheduling For Critical Add-On Pods (EN)
IP Masquerade Agent User Guide (EN)
Kubernetesクラウドコントローラーマネージャー
Limit Storage Consumption (EN)
Namespaces Walkthrough (EN)
Operating etcd clusters for Kubernetes (EN)
Reconfigure a Node's Kubelet in a Live Cluster (EN)
Reserve Compute Resources for System Daemons (EN)
Safely Drain a Node while Respecting the PodDisruptionBudget (EN)
Securing a Cluster (EN)
Set Kubelet parameters via a config file (EN)
Set up High-Availability Kubernetes Masters (EN)
Share a Cluster with Namespaces (EN)
Using a KMS provider for data encryption (EN)
Using CoreDNS for Service Discovery (EN)
Using NodeLocal DNSCache in Kubernetes clusters (EN)
Using sysctls in a Kubernetes Cluster (EN)
クラウドコントローラーマネージャーの開発
Extend kubectl with plugins (EN)
Manage HugePages (EN)
Schedule GPUs (EN)

Edit This Page

Serviceを利用したクラスター内のアプリケーションへのアクセス

ここでは、クラスター内で稼働しているアプリケーションに外部からアクセスするために、KubernetesのServiceオブジェクトを作成する方法を紹介します。 例として、2つのインスタンスから成るアプリケーションへのロードバランシングを扱います。

目標

  • 2つのHellow Worldアプリケーションを稼働させる。
  • Nodeのポートを公開するServiceオブジェクトを作成する。
  • 稼働しているアプリケーションにアクセスするためにServiceオブジェクトを使用する。

始める前に

Kubernetesクラスターが必要、かつそのクラスターと通信するためにkubectlコマンドラインツールが設定されている必要があります。 まだクラスターがない場合、Minikubeを使って作成するか、 以下のいずれかのKubernetesプレイグラウンドも使用できます:

バージョンを確認するには次のコマンドを実行してください: kubectl version.

2つのPodから成るアプリケーションのServiceを作成

  1. クラスタでHello Worldアプリケーションを稼働させます:

    kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080

    このコマンドは Deployment オブジェクトとそれに紐付く ReplicaSet オブジェクトを作成します。ReplicaSetは、Hello Worldアプリケーションが稼働している2つの Pod から構成されます。

  2. Deploymentの情報を表示します:

    kubectl get deployments hello-world
    kubectl describe deployments hello-world
  3. ReplicaSetオブジェクトの情報を表示します:

    kubectl get replicasets
    kubectl describe replicasets
  4. Deploymentを公開するServiceオブジェクトを作成します:

    kubectl expose deployment hello-world --type=NodePort --name=example-service
  5. Serviceに関する情報を表示します:

    kubectl describe services example-service

    出力例は以下の通りです:

    Name:                   example-service
    Namespace:              default
    Labels:                 run=load-balancer-example
    Annotations:            <none>
    Selector:               run=load-balancer-example
    Type:                   NodePort
    IP:                     10.32.0.16
    Port:                   <unset> 8080/TCP
    TargetPort:             8080/TCP
    NodePort:               <unset> 31496/TCP
    Endpoints:              10.200.1.4:8080,10.200.2.5:8080
    Session Affinity:       None
    Events:                 <none>

    NodePortの値を記録しておきます。上記の例では、31496です。

  6. Hello Worldアプリーションが稼働しているPodを表示します:

    kubectl get pods --selector="run=load-balancer-example" --output=wide

    出力例は以下の通りです:

    NAME                           READY   STATUS    ...  IP           NODE
    hello-world-2895499144-bsbk5   1/1     Running   ...  10.200.1.4   worker1
    hello-world-2895499144-m1pwt   1/1     Running   ...  10.200.2.5   worker2
  7. Hello World podが稼働するNodeのうち、いずれか1つのパブリックIPアドレスを確認します。 確認方法は、使用している環境により異なります。 例として、Minikubeの場合はkubectl cluster-info、Google Compute Engineの場合はgcloud compute instances listによって確認できます。

  8. 選択したノード上で、NodePortの値でのTCP通信を許可するファイヤーウォールを作成します。 NodePortの値が31568の場合、31568番のポートを利用したTCP通信を許可するファイヤーウォールを作成します。 クラウドプロバイダーによって設定方法が異なります。

  9. Hello World applicationにアクセスするために、Nodeのアドレスとポート番号を使用します:

    curl http://<public-node-ip>:<node-port>

    ここで <public-node-ip> はNodeのパブリックIPアドレス、 <node-port> はNodePort Serviceのポート番号の値を表しています。 リクエストが成功すると、下記のメッセージが表示されます:

    Hello Kubernetes!

service configuration fileの利用

kubectl exposeコマンドの代わりに、 service configuration file を使用してServiceを作成することもできます。

クリーンアップ

Serviceを削除するには、以下のコマンドを実行します:

kubectl delete services example-service

Hello Worldアプリケーションが稼働しているDeployment、ReplicaSet、Podを削除するには、以下のコマンドを実行します:

kubectl delete deployment hello-world

次の項目

詳細は serviceを利用してアプリケーションと接続する を確認してください。

フィードバック