![見出し画像](https://assets.st-note.com/production/uploads/images/56200825/rectangle_large_type_2_eae933e84137a3aa52d9bc0df27e16cc.png?width=1200)
k8sのservice ClusterIPの実装
kuberneetesのserviceの種類
・ClusterIP:クラスター内でPodが利用出来るIPアドレス
・NodePort:外部サーバと通信出来るClusterIP
ClusterIPの機能も内包している
・LoadBalancer:NodePortをさらに拡張させたもの
・ExternalName:外部サービスに公開する名前
k8sのPodはサービスを実際に提供するコンテナ(≒サーバ)
ですがserviceはそのコンテナまでのLBやRT的な役割を担っている
というのがざっくりしたイメージだと思います。
この記事を書くに参考にしているサイト
Udemyの講義動画になります。
講義では環境にminikubeを使っていますが、私はmicrok8sを
使っています。minikubeでうまくいかなかった方は
microk8sを使ってみると良いかもしれません。
非常にわかりやすお勧めです。
ClusterIPの設定方法
まずClusterIPを設定する対象となるPodを作成します。
今回は講義動画に習って同じhelloworld Podを生成しました。
rot@master:~# microk8s kubectl run --image gcr.io/google-samples/hello-app:1.0 --restart Never helloworld
pod/helloworld created
root@master:~# microk8s kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld 1/1 Running 0 44s
このPodに対してClusterIPを生成していきます。
生成する方法は想像よりかなり簡単でした。
expose(≒晒す)、pod(Podを)だけです。
exposeしたらget serviceで精製されたかを確認します。
※Podのポートと、serviceのポートは合わせるのが一般的なようです。
root@master:~# microk8skubectl expose pod helloworld --type ClusterIP --port 8080 --name helloworld-clusterip
service/helloworld-clusterip exposed
root@master:~# microk8s kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 7m10s
helloworld-clusterip ClusterIP 10.152.183.28 <none> 8080/TCP 23s
試験用のcurl Podを生成し、helloworld PodのIPアドレスと
helloworld Podをexposeして生成されたClusterIPのIPアドレスで
helloworld Podに接続出来るか確認してみます。
出力の結果、接続出来ていることが確認出来ます!
root@master:~# microk8s kubectl run --restart Never --image=curlimages/curl:7.68.0 -it --rm curl sh
If you don't see a command prompt, try pressing enter.
/ $
/ $ curl 10.1.219.67:8080
Hello, world!
Version: 1.0.0
Hostname: helloworld
/ $
/ $ curl 10.152.183.28:8080
Hello, world!
Version: 1.0.0
Hostname: helloworld
【別のCLI上で】
root@master:~# microk8s kubectl get pod
NAME READY STATUS RESTARTS AGE
helloworld 1/1 Running 0 6m59s
curl 1/1 Running 0 26s
root@master:~# microk8s kubectl get pod --output wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
helloworld 1/1 Running 0 8m7s 10.1.219.67 master <none> <none>
curl 1/1 Running 0 94s 10.1.219.68 master <none> <none>
この時の環境は以下のようなイメージになります。
Udemyの講師はこの時
curl <Pod name>
でもcurl出来ていたので自分の環境でも出来ないかを
確かめてみたところ、以下の様にアクセスが出来ないことが
確認出来ました。
/ $ curl helloworld-clusterip:8080
curl: (6) Could not resolve host: helloworld-clusterip
これはmicrok8sのdnsというadd-onが稼働していないことが原因です。
microk8s enable dnsコマンドでdns add-onを有効化します。
root@master:~# microk8s status
microk8s is running
high-availability: no
datastore master nodes: 127.0.0.1:19001
datastore standby nodes: none
addons:
enabled:
ha-cluster # Configure high availability on the current node
disabled:
ambassador # Ambassador API Gateway and Ingress
cilium # SDN, fast with full network policy
dashboard # The Kubernetes dashboard
dns # CoreDNS
fluentd # Elasticsearch-Fluentd-Kibana logging and monitoring
gpu # Automatic enablement of Nvidia CUDA
helm # Helm 2 - the package manager for Kubernetes
helm3 # Helm 3 - Kubernetes package manager
host-access # Allow Pods connecting to Host services smoothly
ingress # Ingress controller for external access
istio # Core Istio service mesh services
jaeger # Kubernetes Jaeger operator with its simple config
keda # Kubernetes-based Event Driven Autoscaling
knative # The Knative framework on Kubernetes.
kubeflow # Kubeflow for easy ML deployments
linkerd # Linkerd is a service mesh for Kubernetes and other frameworks
metallb # Loadbalancer for your Kubernetes cluster
metrics-server # K8s Metrics Server for API access to service metrics
multus # Multus CNI enables attaching multiple network interfaces to pods
openebs # OpenEBS is the open-source storage solution for Kubernetes
openfaas # openfaas serverless framework
portainer # Portainer UI for your Kubernetes cluster
prometheus # Prometheus operator for monitoring and logging
rbac # Role-Based Access Control for authorisation
registry # Private image registry exposed on localhost:32000
storage # Storage class; allocates storage from host directory
traefik # traefik Ingress controller for external access
root@master:~# microk8s enable dns
Enabling DNS
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
clusterrole.rbac.authorization.k8s.io/coredns created
clusterrolebinding.rbac.authorization.k8s.io/coredns created
Restarting kubelet
DNS is enabled
get serviceでk8sのserviceを確認してみます。
dnsというserviceが増えたのが確認出来ますね。
root@master:~# microk8s kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 33m
default helloworld-clusterip ClusterIP 10.152.183.28 <none> 8080/TCP 26m
kube-system kube-dns ClusterIP 10.152.183.10 <none> 53/UDP,53/TCP,9153/TCP 2m10s
curl Podを生成しなおしたのち、Pod内の/etc/resolv.confを
確認してみます。dns add-onの情報が記載されていることが分かります。
root@master:~# microk8s kubectl get service -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 33m
default helloworld-clusterip ClusterIP 10.152.183.28 <none> 8080/TCP 26m
kube-system kube-dns ClusterIP 10.152.183.10 <none> 53/UDP,53/TCP,9153/TCP 2m10s
Pod名でcurl出来るか確認してみます。
出力の結果からアクセス出来ていることがわかります。
/ $ curl helloworld-clusterip:8080
Hello, world!
Version: 1.0.0
Hostname: helloworld
この時の環境は以下のようになります。
この記事が気に入ったらサポートをしてみませんか?