初めに
kubernetesの勉強を始めた初心者です。
参考にしている書籍がクラウドのkubernetesを対象としているものだった為、
EXTERNAL-IPの部分で詰まりました。
調べた所、オンプレ環境でもMetalLBというLoadBalancerを使用すれば
EXTERNAL-IPの払い出しが出来るとのことなのでデプロイしてみます。
せっかくなのでyamlファイルではなく、Helmを使ってみることにしました。
参考URL
環境
- Nutanix CE:2.1
- Kubernetes Management:v2.2.3
- OS Image:ntnx-1.7
- Kubernetes:1.28.5-1
※HWに関しては以下
MetalLBリポジトリを追加
# 確認
$ helm repo list
Error: no repositories to show
# 追加
$ helm repo add metallb https://metallb.github.io/metallb
"metallb" has been added to your repositories
# 確認
$ helm repo list
NAME URL
metallb https://metallb.github.io/metallb
# 更新
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "metallb" chart repository
Update Complete. ⎈Happy Helming!⎈
MetalLBをデプロイ
# namespaceを作成
$ kubectl create namespace metallb-system
namespace/metallb-system created
# metallb-system空間にmetallbとしてデプロイ
$ helm install metallb metallb/metallb -n metallb-system
NAME: metallb
LAST DEPLOYED: Sun Apr 6 15:06:09 2025
NAMESPACE: metallb-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
MetalLB is now running in the cluster.
Now you can configure it via its CRs. Please refer to the metallb official docs
on how to use the CRs.
# 確認
$ kubectl get pod,svc -n metallb-system
NAME READY STATUS RESTARTS AGE
pod/metallb-controller-758987bc5-wcsvh 1/1 Running 0 61s
pod/metallb-speaker-j56l4 4/4 Running 0 61s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/metallb-webhook-service ClusterIP 172.19.217.150 <none> 443/TCP 61s
IPアドレスプールをデプロイ
#アドレスプールを定義
$ cat >./IPAddressPool.yaml <<EOF
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
- 192.168.2.151-192.168.2.170
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: example
namespace: metallb-system
EOF
# デプロイ
$ kubectl apply -f IPAddressPool.yaml
ipaddresspool.metallb.io/first-pool created
l2advertisement.metallb.io/example created
# 確認
$ kubectl get IPAddressPool,L2Advertisement -n metallb-system
NAME AUTO ASSIGN AVOID BUGGY IPS ADDRESSES
ipaddresspool.metallb.io/first-pool true false ["192.168.2.151-192.168.2.170"]
NAME IPADDRESSPOOLS IPADDRESSPOOL SELECTORS INTERFACES
l2advertisement.metallb.io/example
サンプルサイトをデプロイ
# yaml生成
$ cat >./deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:1.17
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
selector:
app: my-nginx
EOF
# デプロイ
$ kubectl apply -f deployment.yaml
deployment.apps/my-nginx created
# 確認
$ kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/my-nginx-f9d8dcb6c-82bn8 1/1 Running 0 23s
pod/my-nginx-f9d8dcb6c-dfscq 1/1 Running 0 23s
pod/my-nginx-f9d8dcb6c-rxzmk 1/1 Running 0 23s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/my-nginx LoadBalancer 172.19.54.50 192.168.2.151 80:31738/TCP 23s
※Helm公式も入れておく
$ helm repo add stable https://charts.helm.sh/stable
"stable" has been added to your repositories
$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "metallb" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!⎈