|
| 1 | +--- |
| 2 | +min-kubernetes-server-version: v1.29 |
| 3 | +title: Service IPの範囲を拡張する |
| 4 | +content_type: task |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- overview --> |
| 8 | +{{< feature-state feature_gate_name="MultiCIDRServiceAllocator" >}} |
| 9 | + |
| 10 | +このドキュメントはクラスターに割り当てられている既存のService IPの範囲を拡張する方法を説明します。 |
| 11 | + |
| 12 | +## {{% heading "prerequisites" %}} |
| 13 | + |
| 14 | +{{< include "task-tutorial-prereqs.md" >}} |
| 15 | + |
| 16 | +{{< version-check >}} |
| 17 | + |
| 18 | +<!-- steps --> |
| 19 | + |
| 20 | +## API |
| 21 | + |
| 22 | +APIサーバーで`MultiCIDRServiceAllocator`[フィーチャーゲート](/docs/reference/command-line-tools-reference/feature-gates/)を有効にし、`networking.k8s.io/v1beta1`APIグループをアクティブにしているKubernetesクラスターは、`kubernetes`という名前の特別なServiceCIDRオブジェクトを作成します。このオブジェクトには、APIサーバーのコマンドライン引数`--service-cluster-ip-range`の値に基づいたIPアドレス範囲が指定されます。 |
| 23 | + |
| 24 | +```sh |
| 25 | +kubectl get servicecidr |
| 26 | +``` |
| 27 | + |
| 28 | +``` |
| 29 | +NAME CIDRS AGE |
| 30 | +kubernetes 10.96.0.0/28 17d |
| 31 | +``` |
| 32 | + |
| 33 | +APIサーバーのエンドポイントをPodに公開する`kubernetes`という特別なServiceは、デフォルトのServiceCIDRの範囲の最初のIPアドレスを算出し、そのIPアドレスをCluster IPとして使用します。 |
| 34 | + |
| 35 | +```sh |
| 36 | +kubectl get service kubernetes |
| 37 | +``` |
| 38 | + |
| 39 | +``` |
| 40 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 41 | +kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17d |
| 42 | +``` |
| 43 | + |
| 44 | +この例では、デフォルトのServiceはClusterIPとして10.96.0.1を使用しており、対応するIPAddressオブジェクトがあります。 |
| 45 | + |
| 46 | +```sh |
| 47 | +kubectl get ipaddress 10.96.0.1 |
| 48 | +``` |
| 49 | + |
| 50 | +``` |
| 51 | +NAME PARENTREF |
| 52 | +10.96.0.1 services/default/kubernetes |
| 53 | +``` |
| 54 | + |
| 55 | +ServiceCIDRは{{<glossary_tooltip text="ファイナライザー" term_id="finalizer">}}によって保護されており、ServiceのClusterIPが孤立することを防ぎます。ファイナライザーが削除されるのは、既存の全IPAddressを含む別のサブネットがある場合またはサブネットに属するIPAddressがない場合のみです。 |
| 56 | + |
| 57 | +## Serviceに使用できるIPアドレスの個数を拡張する |
| 58 | + |
| 59 | +ユーザーはServiceに使用できるアドレスの個数を増やしたい場合がありますが、従来はServiceの範囲を拡張することは破壊的な操作であり、データ損失につながる可能性もありました。この新しい機能を使用することで、ユーザーは新しいServiceCIDRを追加するだけで使用可能なアドレスを増やすことができます。 |
| 60 | + |
| 61 | +### 新しいServiceCIDRを追加する |
| 62 | + |
| 63 | +Service用として10.96.0.0/28の範囲が設定されたクラスターでは、2^(32-28) - 2 = 14個のIPアドレスしか使用できません。`kubernetes.default`Serviceは常に作成されるため、この例では最大13個しかServiceを作れません。 |
| 64 | + |
| 65 | +```sh |
| 66 | +for i in $(seq 1 13); do kubectl create service clusterip "test-$i" --tcp 80 -o json | jq -r .spec.clusterIP; done |
| 67 | +``` |
| 68 | + |
| 69 | +``` |
| 70 | +10.96.0.11 |
| 71 | +10.96.0.5 |
| 72 | +10.96.0.12 |
| 73 | +10.96.0.13 |
| 74 | +10.96.0.14 |
| 75 | +10.96.0.2 |
| 76 | +10.96.0.3 |
| 77 | +10.96.0.4 |
| 78 | +10.96.0.6 |
| 79 | +10.96.0.7 |
| 80 | +10.96.0.8 |
| 81 | +10.96.0.9 |
| 82 | +error: failed to create ClusterIP service: Internal error occurred: failed to allocate a serviceIP: range is full |
| 83 | +``` |
| 84 | + |
| 85 | +IPアドレス範囲を拡張または追加する新しいServiceCIDRを作成することで、Serviceに使用できるIPアドレスの個数を増やせます。 |
| 86 | + |
| 87 | +```sh |
| 88 | +cat <EOF | kubectl apply -f - |
| 89 | +apiVersion: networking.k8s.io/v1beta1 |
| 90 | +kind: ServiceCIDR |
| 91 | +metadata: |
| 92 | + name: newcidr1 |
| 93 | +spec: |
| 94 | + cidrs: |
| 95 | + - 10.96.0.0/24 |
| 96 | +EOF |
| 97 | +``` |
| 98 | + |
| 99 | +``` |
| 100 | +servicecidr.networking.k8s.io/newcidr1 created |
| 101 | +``` |
| 102 | + |
| 103 | +これにより、新しいServiceを作成できるようになり、新しい範囲からClusterIPが割り当てられます。 |
| 104 | + |
| 105 | +```sh |
| 106 | +for i in $(seq 13 16); do kubectl create service clusterip "test-$i" --tcp 80 -o json | jq -r .spec.clusterIP; done |
| 107 | +``` |
| 108 | + |
| 109 | +``` |
| 110 | +10.96.0.48 |
| 111 | +10.96.0.200 |
| 112 | +10.96.0.121 |
| 113 | +10.96.0.144 |
| 114 | +``` |
| 115 | + |
| 116 | +### ServiceCIDRの削除 |
| 117 | + |
| 118 | +あるServiceCIDRに依存しているIPAddressが存在する場合、そのServiceCIDRは削除できません。 |
| 119 | + |
| 120 | +```sh |
| 121 | +kubectl delete servicecidr newcidr1 |
| 122 | +``` |
| 123 | + |
| 124 | +``` |
| 125 | +servicecidr.networking.k8s.io "newcidr1" deleted |
| 126 | +``` |
| 127 | + |
| 128 | +KubernetesはServiceCIDRのファイナライザーを使ってこの依存関係を追跡します。 |
| 129 | + |
| 130 | +```sh |
| 131 | +kubectl get servicecidr newcidr1 -o yaml |
| 132 | +``` |
| 133 | + |
| 134 | +```yaml |
| 135 | +apiVersion: networking.k8s.io/v1beta1 |
| 136 | +kind: ServiceCIDR |
| 137 | +metadata: |
| 138 | + creationTimestamp: "2023-10-12T15:11:07Z" |
| 139 | + deletionGracePeriodSeconds: 0 |
| 140 | + deletionTimestamp: "2023-10-12T15:12:45Z" |
| 141 | + finalizers: |
| 142 | + - networking.k8s.io/service-cidr-finalizer |
| 143 | + name: newcidr1 |
| 144 | + resourceVersion: "1133" |
| 145 | + uid: 5ffd8afe-c78f-4e60-ae76-cec448a8af40 |
| 146 | +spec: |
| 147 | + cidrs: |
| 148 | + - 10.96.0.0/24 |
| 149 | +status: |
| 150 | + conditions: |
| 151 | + - lastTransitionTime: "2023-10-12T15:12:45Z" |
| 152 | + message: There are still IPAddresses referencing the ServiceCIDR, please remove |
| 153 | + them or create a new ServiceCIDR |
| 154 | + reason: OrphanIPAddress |
| 155 | + status: "False" |
| 156 | + type: Ready |
| 157 | +``` |
| 158 | +
|
| 159 | +ServiceCIDRの削除を止めているIPAddressを含むServiceを削除すると |
| 160 | +
|
| 161 | +```sh |
| 162 | +for i in $(seq 13 16); do kubectl delete service "test-$i" ; done |
| 163 | +``` |
| 164 | + |
| 165 | +``` |
| 166 | +service "test-13" deleted |
| 167 | +service "test-14" deleted |
| 168 | +service "test-15" deleted |
| 169 | +service "test-16" deleted |
| 170 | +``` |
| 171 | + |
| 172 | +コントロールプレーンがそれを検知します。そしてコントロールプレーンはファイナライザを削除し、削除が保留されているServiceCIDRが実際に削除されるようにします。 |
| 173 | + |
| 174 | +```sh |
| 175 | +kubectl get servicecidr newcidr1 |
| 176 | +``` |
| 177 | + |
| 178 | +``` |
| 179 | +Error from server (NotFound): servicecidrs.networking.k8s.io "newcidr1" not found |
| 180 | +``` |
0 commit comments