Contents
概要
KubernetesのNginxコンテナに対してServiceオブジェクトのClusterIPを介して接続します。
前回はPod-IPに対して接続確認を行いましたが、Pod-IPはPod単位である為、Deploymentで複数の同じ役割のPodを作成する場合は一般的にClusterIPを使用して接続します。ClusterIPは複数のPod-IPをひとつにまとめたものと考えると分かりやすく、要はLoadbalancerの仮想IPの考え方と同じです。一般的なLoadbalancerと異なるのは、IPではなく、selectorのlabel(名前)を使用して転送先のPodを特定しています。
また、ClusterIPもPodIPと同じく、Cluster外から接続できません。Cluster内のPod間連携が主な用途になると思います。
手順
Namespace
複数のオブジェクトが作成される為、専用のNamespaceを作成します。
Namespaceの作成
root@k8s-01:~# kubectl create namespace nginx-clusterip-ns
namespace/nginx-clusterip-ns created
Namespaceの確認
root@k8s-01:~# kubectl get namespace
NAME STATUS AGE
nginx-clusterip-ns Active 63s
Deployment
Deployment用Manifestの作成
Pod用Manifestを作成します。Serviceを使用する場合は複数のPodが想定される為、Deploymentで作成します。
root@k8s-01:~# vim /root/k8s/yaml/nginx-clusterip-dep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-clusterip-dep
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
Deployment用Manifestの実行
Deployment用ManifestからDeploymentを作成します。
root@k8s-01:~# kubectl create -n nginx-clusterip-ns -f /root/k8s/yaml/nginx-clusterip-dep.yaml
deployment.apps/nginx-clusterip-dep created
Deploymentのステータス確認
Deploymentのステータスを確認します。
root@k8s-01:~# kubectl get deployment -n nginx-clusterip-ns
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-clusterip-dep 2/2 2 2 23s
Service
Service用Manifestの作成
Service用Manifestを作成します。Serviceでtypeを指定しない場合はClusterIPになります。また、selectorをDeploymentと合わせるのがポイントですね。
root@k8s-01:~# vim /root/k8s/yaml/nginx-clusterip-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip-svc
spec:
selector:
app: nginx
ports:
- port: 80
Service用Manifestの実行
Service用ManifestからServiceを作成します。
root@k8s-01:~# kubectl create -n nginx-clusterip-ns -f /root/k8s/yaml/nginx-clusterip-svc.yaml
service/nginx-clusterip-svc created
Serviceのステータス確認
Serviceのステータスを確認します。ClusterIPに10.102.189.225がアサインされています。
root@k8s-01:~# kubectl get service -n nginx-clusterip-ns NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-clusterip-svc ClusterIP 10.102.189.22580/TCP 56s
オブジェクト全体の確認
nginx-clusterip-nsの全オブジェクトを確認します。
root@k8s-01:~# kubectl get all -n nginx-clusterip-ns -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-clusterip-svc ClusterIP 10.102.189.22580/TCP 56s root@k8s-01:~# kubectl get all -n nginx-clusterip-ns -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/nginx-clusterip-dep-6799fc88d8-6kt9x 1/1 Running 0 11m 10.255.179.58 k8s-02 pod/nginx-clusterip-dep-6799fc88d8-v2phk 1/1 Running 0 11m 10.255.179.57 k8s-02 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR service/nginx-clusterip-svc ClusterIP 10.102.189.225 80/TCP 5m4s app=nginx NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR deployment.apps/nginx-clusterip-dep 2/2 2 2 11m nginx nginx app=nginx NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR replicaset.apps/nginx-clusterip-dep-6799fc88d8 2 2 2 11m nginx nginx app=nginx,pod-template-hash=6799fc88d8
ClusterIPを介したNginxコンテナの動作確認
ClusterIPを介してNginxコンテナにHTTPで接続
Cluster内のNodeからClusterIPにcurlで接続すると応答があります。尚、本稿の内容ではCluster外からNginxコンテナに接続することはできません。
root@k8s-01:~# curl 10.102.189.225
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
※各Pod-IPに対しても同様に応答があります。
環境の削除
本稿で作成した環境を削除します。
root@k8s-01:~# kubectl delete namespace nginx-clusterip-ns
namespace "nginx-clusterip-ns" deleted