GKEを使う - LoadBalancerを使わずに外部にサービスを公開する(2)

勉強用としてはLoadBalancerの費用は高いため、LoadBalancerを使わずに外部にサービスを公開することを考えます。
前回の続きです。

Kubernetes クラスタを作る

1
2
3
4
5
6
7
8
9
10
11
$ gcloud container clusters create test-cluster \
--preemptible \
--machine-type=e2-micro \
--disk-size=10 \
--enable-autoscaling --num-nodes=2 --max-nodes=3 \
--no-enable-master-authorized-networks \
--enable-ip-alias \
--enable-private-nodes \
--network my-net \
--subnetwork my-subnet \
--master-ipv4-cidr 172.16.0.32/28
1
2
3
CLUSTER_NAME=`kubectl config current-context`
CONTEXT_NAME=gke-test-001
kubectl config rename-context $CLUSTER_NAME $CONTEXT_NAME

ノードを確認します。

1
2
3
4
5
$ kubectl get node
NAME STATUS ROLES AGE VERSION
gke-test-cluster-default-pool-32841587-04g2 Ready <none> 3m v1.20.10-gke.1600
gke-test-cluster-default-pool-32841587-wt8v Ready <none> 3m1s v1.20.10-gke.1600

Ingress Controllerをインストール

1
2
3
mkdir gke-test
cd gke-test
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.4/deploy/static/provider/cloud/deploy.yaml

LoadBalancerをNodePortに変更します。
type: LoadBalancer -> type: NodePort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Source: ingress-nginx/templates/controller-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
labels:
helm.sh/chart: ingress-nginx-4.0.6
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/version: 1.0.4
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: ingress-nginx
spec:
type: NodePort # <- LoadBalancer
externalTrafficPolicy: Local
ipFamilyPolicy: SingleStack
ipFamilies:
- IPv4

DeploymentをDaemonSetに変更します。

kind: Deployment -> kind: DaemonSet

1
2
3
4
5
6
7
8
9
10
11
12
13
# Source: ingress-nginx/templates/controller-deployment.yaml
apiVersion: apps/v1
kind: DaemonSet # <- Deployment
metadata:
labels:
helm.sh/chart: ingress-nginx-4.0.6
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/version: 1.0.4
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/component: controller
name: ingress-nginx-controller
namespace: ingress-nginx

hostNetworkを有効にします。

1
2
3
4
5
6
7
spec:
dnsPolicy: ClusterFirst
hostNetwork: true # Add
containers:
- name: controller
image: k8s.gcr.io/ingress-nginx/controller:v1.0.4@sha256:545cff00370f28363dad31e3b59a94ba377854d3a11f18988f5f9e56841ef9ef
imagePullPolicy: IfNotPresent

ホストの80ポートと443ポートを使用します。

1
2
3
4
5
6
7
8
9
10
11
12
ports:
- name: http
containerPort: 80
protocol: TCP
hostPort: 80 # Add
- name: https
containerPort: 443
protocol: TCP
hostPort: 443 # Add
- name: webhook
containerPort: 8443
protocol: TCP

インストールします。

1
kubectl apply -f  deploy.yaml

実行されていることを確認します。

1
2
3
4
5
$ kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.88.11.88 <none> 80:32373/TCP,443:30989/TCP 89s
ingress-nginx-controller-admission ClusterIP 10.88.14.12 <none> 443/TCP 89s

この状態でhttps://foo.oct-26-1985.comにアクセスするとnginxの404エラーに変わります。
これでクラスタ内のnginxにアクセスできていることがわかります。