目的
Ingressを少しだけ勉強していたのですが、凍結され新機能はGateway APIに追加されていくということなので試しに使ってみることにしました。
環境・使用したツール
OS | コマンドラインツール | Docker | Kubernetes |
---|---|---|---|
Windows11 | powershell7.4.1 | Docker Desktop | minikube |
また、Gatewayのコントローラが必要となるようなのですが、今回はminikubeへの導入が簡単だったkongを使用します。インストールについては次のページを参照しました。(Helmを使用)
手順
1. クラスタの作成
minikube start
などの方法でクラスタを起動させ、次のコマンドを実行してGateway APIをインストールします。
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.0.0/standard-install.yaml
2. サービスのデプロイ
次のファイルを作成し、kubectl apply -f <ファイル名>
のコマンドを実行して設定を行います。
apiVersion: v1
kind: Pod
metadata:
name: httpd-pod
labels:
name: httpd-label
spec:
containers:
- name: httpd-container
image: httpd
ports:
- containerPort: 80 # pod内にあるhttpd.conf(apache2の設定ファイル)に"Listen 80"という形でサーバーのポート番号が載っている
name: httpd-port
---
apiVersion: v1
kind: Service
metadata:
name: httpd-service
spec:
selector:
name: httpd-label # この行はhttpd-podのlabelsフィールド内と一致させる
ports:
- port: 8000
targetPort: httpd-port # この行はhttpd-podのポート名と一致させる
3. Gatewayの設定
次の二つのファイルを作成し、上と同様にkubectl apply -f <ファイル名>
でクラスタに設定を加えていきます。
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: httpd-gatewayclass
annotations:
konghq.com/gatewayclass-unmanaged: 'true'
spec:
controllerName: konghq.com/kic-gateway-controller
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: httpd-gateway
spec:
gatewayClassName: httpd-gatewayclass
listeners:
- protocol: HTTP
port: 80
name: httpd-listener
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httpd-route
annotations:
konghq.com/strip-path: 'true'
spec:
parentRefs:
- name: httpd-gateway
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: httpd-service
port: 8000
ここまで行った後で、kubectl get pods -A
を実行したときにすべてのpodのREADYの項目が1/1になったことを確認してから最後にminikube service -n kong kong-gateway-proxy
を実行します。すると、次のような結果が表示されます。
|-----------|--------------------|--------------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|--------------------|--------------------|---------------------------|
| kong | kong-gateway-proxy | kong-proxy/80 | http://192.168.49.2:31834 |
| | | kong-proxy-tls/443 | http://192.168.49.2:30392 |
|-----------|--------------------|--------------------|---------------------------|
🏃 Starting tunnel for service kong-gateway-proxy.
|-----------|--------------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|--------------------|-------------|------------------------|
| kong | kong-gateway-proxy | | http://127.0.0.1:60649 |
| | | | http://127.0.0.1:60650 |
|-----------|--------------------|-------------|------------------------|
[kong kong-gateway-proxy http://127.0.0.1:60649
http://127.0.0.1:60650]
ブラウザのURLバーに127.0.0.1:<ポート番号(この場合は60649)>
と入力すればapache2のウェルカムページが表示されます。