2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【OKE】Serviceで作成されるLBを柔軟に設定する

2
Last updated at Posted at 2025-12-05

やったこと

  • Flexibleロードバランサのシェイプをマニフェストファイルから動的に設定
  • 事前に予約したIPアドレスをLBに付与
  • セキュリティリストではなく、事前作成したNSGを使用

作成したマニフェスト

apiVersion: v1
kind: Service
metadata:
  name: lb
  annotations:
    oci.oraclecloud.com/load-balancer-type: "lb"
    service.beta.kubernetes.io/oci-load-balancer-shape: "flexible"
    service.beta.kubernetes.io/oci-load-balancer-shape-flex-min: "10"
    service.beta.kubernetes.io/oci-load-balancer-shape-flex-max: "100"
    oci.oraclecloud.com/security-rule-management-mode: "None"
    oci.oraclecloud.com/oci-network-security-groups: <NSGのOCID>
spec:
  loadBalancerIP: <予約済みIP>
  selector:
    app: nginx
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: 80

---
# テスト用Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: docker.io/nginx
        ports:
        - containerPort: 80

備考

設定可能なアノテーションはここに書いてある。

フレキシブルロードバランサーの帯域幅指定

フレキシブルロードバランサーを使う場合は、service.beta.kubernetes.io/oci-load-balancer-shape: "flexible"を指定し、最小・最大帯域幅をservice.beta.kubernetes.io/oci-load-balancer-shape-flex-min, service.beta.kubernetes.io/oci-load-balancer-shape-flex-maxで指定する。

予約済みIPアドレスの使用

spec.loadBalancerIPに予約済みのIPアドレスを指定すればOK。
CCMのコード(pkg/cloudprovider/providers/oci/load_balancer_spec.go)を見てみると、

func getLoadBalancerIP(svc *v1.Service) (string, error) {
	// There are no changes here wrt NLB since NLB doesn't support private ip reservation

	ipAddress := svc.Spec.LoadBalancerIP
	if ipAddress == "" {
		return "", nil
	}

	//checks the validity of loadbalancerIP format
	if net.ParseIP(ipAddress) == nil {
		return "", fmt.Errorf("invalid value %q provided for LoadBalancerIP", ipAddress)
	}

	//checks if private loadbalancer is trying to use reservedIP
	isInternal, err := isInternalLB(svc)
	if isInternal {
		return "", fmt.Errorf("invalid service: cannot create a private load balancer with Reserved IP")
	}
	return ipAddress, err
}

このようになっており、spec.loadBalancerIPから取得していることがわかる。

NSGを使用する

CCMに自動的にNSGを作成させる場合は、oci.oraclecloud.com/security-rule-management-mode: "NSG"を指定する。今回は事前作成済みのNSGを使用するため、oci.oraclecloud.com/security-rule-management-mode: "None"とし、CCMの管理下から除いている。

作成するNSGは以下のルールを追加した。

ワーカーノード側

  • 方向: イングレス
    • ソース: lbにアタッチするNSG
    • プロトコル: TCP
    • 宛先ポート: 30000-32767 (NodePortの範囲)
  • 方向: イングレス
    • ソース: lbにアタッチするNSG
    • プロトコル: TCP
    • 宛先ポート: 10256(ヘルスチェック用)

LB側

  • 方向: イングレス
    • ソース: 0.0.0.0/0
    • プロトコル: TCP
    • 宛先ポート: 80
  • 方向: エグレス
    • ソース: ノード側のNSG
    • プロトコル: TCP
    • 宛先ポート: 30000-32767 (NodePortの範囲)
  • 方向: エグレス
    • ソース: ノード側のNSG
    • プロトコル: TCP
    • 宛先ポート: 10256 (ヘルスチェック用)

ロードバランサはヘルスチェックの通らないバックエンドにはトラフィックを転送しないので、ヘルスチェック用のポートを解放していないと疎通しないので注意

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?