現在表示しているのは、次のバージョン向けのドキュメントです。Kubernetesバージョン: v1.25
Kubernetes v1.25 のドキュメントは積極的にメンテナンスされていません。現在表示されているバージョンはスナップショットです。最新のドキュメントはこちらです: 最新バージョン
クラスター内のアプリケーションにアクセスするために外部IPアドレスを公開する
このページでは、外部IPアドレスを公開するKubernetesのServiceオブジェクトを作成する方法を示します。
始める前に
- kubectlをインストールしてください。 
- Kubernetesクラスターを作成する際に、Google Kubernetes EngineやAmazon Web Servicesのようなクラウドプロバイダーを使用します。このチュートリアルでは、クラウドプロバイダーを必要とする外部ロードバランサーを作成します。 
- Kubernetes APIサーバーと通信するために、 - kubectlを設定してください。手順については、各クラウドプロバイダーのドキュメントを参照してください。
目標
- 5つのインスタンスで実際のアプリケーションを起動します。
- 外部IPアドレスを公開するServiceオブジェクトを作成します。
- 起動中のアプリケーションにアクセスするためにServiceオブジェクトを使用します。
5つのPodで起動しているアプリケーションへのServiceの作成
- クラスターにてHello Worldアプリケーションを実行してください。
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: load-balancer-example
  name: hello-world
spec:
  replicas: 5
  selector:
    matchLabels:
      app.kubernetes.io/name: load-balancer-example
  template:
    metadata:
      labels:
        app.kubernetes.io/name: load-balancer-example
    spec:
      containers:
      - image: gcr.io/google-samples/node-hello:1.0
        name: hello-world
        ports:
        - containerPort: 8080
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
上記のコマンドにより、 Deploymentを作成し、ReplicaSetを関連づけます。ReplicaSetには5つのPodがあり、それぞれHello Worldアプリケーションが起動しています。
- Deploymentに関する情報を表示します: - kubectl get deployments hello-world kubectl describe deployments hello-world
- ReplicaSetオブジェクトに関する情報を表示します: - kubectl get replicasets kubectl describe replicasets
- Deploymentを公開するServiceオブジェクトを作成します。 - kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
- Serviceに関する情報を表示します: - kubectl get services my-service- 出力は次のようになります: - NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54s備考:- type=LoadBalancerのServiceは外部のクラウドプロバイダーによってサポートされており、ここでは扱いません。詳細はこちらのページを参照してください。備考: 外部IPアドレスが<pending>と表示されている場合は、しばらく待ってから同じコマンドを実行してください。
- Serviceに関する詳細な情報を表示します: - kubectl describe services my-service- 出力は次のようになります: - Name: my-service Namespace: default Labels: app.kubernetes.io/name=load-balancer-example Annotations: <none> Selector: app.kubernetes.io/name=load-balancer-example Type: LoadBalancer IP: 10.3.245.137 LoadBalancer Ingress: 104.198.205.71 Port: <unset> 8080/TCP NodePort: <unset> 32377/TCP Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more... Session Affinity: None Events: <none>- Serviceによって公開された外部IPアドレス( - LoadBalancer Ingress)を記録しておいてください。 この例では、外部IPアドレスは104.198.205.71です。 また、- Portおよび- NodePortの値も控えてください。 この例では、- Portは8080、- NodePortは32377です。
- 先ほどの出力にて、Serviceにはいくつかのエンドポイントがあることを確認できます: 10.0.0.6:8080、 10.0.1.6:8080、10.0.1.7:8080、その他2つです。 これらはHello Worldアプリケーションが動作しているPodの内部IPアドレスです。 これらのPodのアドレスを確認するには、次のコマンドを実行します: - kubectl get pods --output=wide- 出力は次のようになります: - NAME ... IP NODE hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
- Hello Worldアプリケーションにアクセスするために、外部IPアドレス( - LoadBalancer Ingress)を使用します:- curl http://<external-ip>:<port>- ここで、 - <external-ip>はServiceの外部IPアドレス(- LoadBalancer Ingress)で、- <port>はServiceの詳細出力における- Portです。minikubeを使用している場合、- minikube service my-serviceを実行することでHello Worldアプリケーションをブラウザで自動的に 開かれます。- 正常なリクエストに対するレスポンスは、helloメッセージです: - Hello Kubernetes!
クリーンアップ
Serviceを削除する場合、次のコマンドを実行します:
kubectl delete services my-service
Deployment、ReplicaSet、およびHello Worldアプリケーションが動作しているPodを削除する場合、次のコマンドを実行します:
kubectl delete deployment hello-world
次の項目
connecting applications with servicesにて詳細を学ぶことができます。