株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

Kubernetese勉強中です。
Serviceのおべんきょうをしました
「Kubernetses完全ガイド」をそのままやりました

ClusterIP

  • 内部トラフィックのみ
  • ホストオンリーアダプタ的な感じ

ファイル

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sample-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: sample-app
  template:
    metadata:
      labels:
        app: sample-app
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.12
          ports:
            - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: sample-clusterip
spec:
  type: ClusterIP
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
  selector:
    app: sample-app

動作確認

# 各PodのIPアドレス
~/Desktop ❯❯❯ kubectl get pods -l app=sample-app -o custom-columns="Name:{metadata.name},IP:{status.podIP}"                    ✘ 130
Name                                 IP
sample-deployment-86b68b4c5b-c8sbx   172.17.0.4
sample-deployment-86b68b4c5b-p4vqn   172.17.0.5
sample-deployment-86b68b4c5b-rp5qk   172.17.0.6

# ClusterIPサービス
~/Desktop ❯❯❯ kubectl describe svc sample-clusterip
Name:              sample-clusterip
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"sample-clusterip","namespace":"default"},"spec":{"ports":[{"name":"http-port",...
Selector:          app=sample-app
Type:              ClusterIP
IP:                10.108.164.168
Port:              http-port  8080/TCP
TargetPort:        80/TCP
Endpoints:         172.17.0.4:80,172.17.0.5:80,172.17.0.6:80
Session Affinity:  None
Events:            <none>

# 各Podのトップページをホスト名にする
~/Desktop ❯❯❯ for PODNAME in `kubectl get pods -l app=sample-app -o jsonpath='{.items[*].metadata.name}'`;do kubectl exec -it ${PODNAME} -- cp /etc/hostname /usr/share/nginx/html/index.html;done

# Podを一時的に作ってアクセス アクセスするごとにばらける
~/Desktop ❯❯❯ kubectl run --image=centos:6 --restart=Never --rm -i testpod -- curl -s http://10.108.164.168:8080
sample-deployment-86b68b4c5b-c8sbx

ExternaleIP

  • 外部へサービスを公開するとき
  • Type自体はClusterIP
  • externalIPsに指定したIPアドレスはNodeが実際に持っているIPアドレスを指定

Nodeが動いているIPアドレスを確認

~/Desktop ❯❯❯ kubectl get nodes -o custom-columns="NAME:{metadata.name},IP:{status.addresses[].address}"
NAME       IP
minikube   192.168.99.101

ファイル

apiVersion: v1
kind: Service
metadata:
  name: sample-externalip
spec:
  type: ClusterIP
  externalIPs:
    - 192.168.99.101
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
  selector:
    app: sample-app

ローカルから動作確認

~/Desktop ❯❯❯ curl -s http://192.168.99.101:8080
sample-deployment-86b68b4c5b-rp5qk
~/Desktop ❯❯❯

NodePort

  • 外部へサービスを公開するときに
  • ExternalIPと違う点は、全Nodeで待ち受ける

ファイル

apiVersion: v1
kind: Service
metadata:
  name: sample-nodeport
spec:
  type: NodePort
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8080
      targetPort: 80
      nodePort: 30080
  selector:
    app: sample-app

動作確認

~/Desktop ❯❯❯ curl -s http://192.168.99.101:30080
sample-deployment-86b68b4c5b-rp5qk

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)