N^2周回遅れでKubernetesに入門してみた(ネットワーク編)

N^2周回遅れでKubernetesに入門してみました。

# 環境
Kubernetesの環境として今回はEKS(AWS)を利用しました。

# 確認すること

### 1.serviceのtypeとネットワークアクセス
- コンテナのnginxのポート: 80
- serviceのポート: 8080
- NortPort: 32269

とすると

|serviceのtype|from Container|from Node|from Internet|
|---|---|---|---|
|ClusterIP|ClusterIP:8080でアクセス可能|ClusterIP:8080でアクセス可能|N/A|
|NodePort|ClusterIP:8080でアクセス可能|ClusterIP:8080 / NodeのIP:32269でアクセス可能|N/A|
|LoadBalancer|ClusterIP:8080でアクセス可能|ClusterIP:8080 / NodeのIP:32269でアクセス可能|LBのホスト名:8080でアクセス可能|

### 2. kubectl run / expose / apply -fと作成されるリソース

|コマンド|pod|replicaset|deployment|service|
|---|---|---|---|---|
|kubectl run|◯|◯|◯| |
|kubectl expose| | | |◯|
|kubectl apply -f [kind: Podのファイル]|◯| | | |
|kubectl apply -f [kind: ReplicaSetのファイル]|◯|◯| | |
|kubectl apply -f [kind: ServiceとReplicaSetのファイル]|◯|◯| |◯|
|kubectl apply -f [kind: ServiceとDeploymentのファイル]|◯|◯|◯|◯|
◯ 作成される

# 作業概要
1. kubectl runでpodとreplicasetとdeploymentを作成する
2. kubectl runで--replicas 3を指定する
3. kubectl exposeでserviceを作成する
4. kubectl exposeで--type NodePortを指定してserviceを作成する
5. kubectl exposeで--type LoadBalancerを指定してserviceを作成する
6. kubectl apply -fでpodを作成する
7. kubectl apply -fでreplicasetを作成する
8. kubectl apply -fでserviceとreplicasetを作成する
9. kubectl apply -fでserviceとdeploymentを作成する

# 1. kubectl runでpodとreplicasetとdeploymentを作成する

```
$ kubectl run nginx --image=nginx
```

podが1個作成されます。

```
$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-65899c769f-wzmcs   1/1       Running   0          2m
$ kubectl get replicasets
NAME               DESIRED   CURRENT   READY     AGE
nginx-65899c769f   1         1         1         2m
$ kubectl get deployments
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx     1         1         1            1           2m
```

# 2. kubectl runで--replicas 3を指定する

```
$ kubectl delete deployments
$ kubectl run nginx --image nginx --replicas 3
```

今度はpodが3個作成されます。

```
$ kubectl get pods -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP                NODE
nginx-65899c769f-68qhh   1/1       Running   0          33s       192.168.230.217   ip-192-168-203-105.ec2.internal
nginx-65899c769f-p6scl   1/1       Running   0          33s       192.168.166.148   ip-192-168-130-138.ec2.internal
nginx-65899c769f-wltgm   1/1       Running   0          33s       192.168.119.72    ip-192-168-114-90.ec2.internal
$ kubectl get replicasets -o wide
NAME               DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES    SELECTOR
nginx-65899c769f   3         3         3         1m        nginx        nginx     pod-template-hash=2145573259,run=nginx
$ kubectl get deployments -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES    SELECTOR
nginx     3         3         3            3           1m        nginx        nginx     run=nginx
```

# 3. kubectl exposeでserviceを作成する

port 8080、target-port(nginxのport) 80を指定して、serviceを作成します。
デフォルトでtype=ClusterIPになります。

```
$ kubectl expose deployments nginx --port 8080 --target-port 80
```

```
$ kubectl get services -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1      <none>        443/TCP    2h        <none>
nginx        ClusterIP   10.100.71.177   <none>        8080/TCP   5m        run=nginx
$ kubectl get services -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-23T13:21:55Z
    labels:
      component: apiserver
      provider: kubernetes
    name: kubernetes
    namespace: default
    resourceVersion: "807105"
    selfLink: /api/v1/namespaces/default/services/kubernetes
    uid: a44578cf-bf33-11e8-bc6f-12564c8934b4
  spec:
    clusterIP: 10.100.0.1
    ports:
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
    sessionAffinity: ClientIP
    sessionAffinityConfig:
      clientIP:
        timeoutSeconds: 10800
    type: ClusterIP
  status:
    loadBalancer: {}
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-23T15:23:27Z
    labels:
      run: nginx
    name: nginx
    namespace: default
    resourceVersion: "818169"
    selfLink: /api/v1/namespaces/default/services/nginx
    uid: 9ede44a8-bf44-11e8-bc6f-12564c8934b4
  spec:
    clusterIP: 10.100.71.177
    ports:
    - port: 8080
      protocol: TCP
      targetPort: 80
    selector:
      run: nginx
    sessionAffinity: None
    type: ClusterIP
  status:
    loadBalancer: {}
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
```

### コンテナからアクセスする

ClusterIPを確認して、

```
$ kubectl get services -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1      <none>        443/TCP    2h        <none>
nginx        ClusterIP   10.100.71.177   <none>        8080/TCP   9m        run=nginx
```

コンテナにログインしてcurlをインストール後、ClusterIP:8080にアクセスします。

```
$ kubectl exec -it nginx-65899c769f-68qhh bash
root@nginx-65899c769f-68qhh:/# apt-get update
root@nginx-65899c769f-68qhh:/# apt-get install curl
root@nginx-65899c769f-68qhh:/# curl http://10.100.71.177:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
root@nginx-65899c769f-68qhh:/#
```

### ノードからアクセスする

EC2にログイン後、ClusterIP:8080にアクセスします。

```
$ ssh -i ***** ec2-user@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
$ curl http://10.100.71.177:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
```

# 4. kubectl exposeで--type NodePortを指定してserviceを作成する

type=NodePortを指定してserviceを作成します。

```
$ kubectl delete service nginx
$ kubectl expose deployments nginx --port 8080 --target-port 80 --type NodePort
```

PORT(S)にnodeportの32269が表示されます。

```
$ kubectl get services -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1       <none>        443/TCP          2h        <none>
nginx        NodePort    10.100.231.225   <none>        8080:32269/TCP   13s       run=nginx
$ kubectl get services -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-23T13:21:55Z
    labels:
      component: apiserver
      provider: kubernetes
    name: kubernetes
    namespace: default
    resourceVersion: "807105"
    selfLink: /api/v1/namespaces/default/services/kubernetes
    uid: a44578cf-bf33-11e8-bc6f-12564c8934b4
  spec:
    clusterIP: 10.100.0.1
    ports:
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
    sessionAffinity: ClientIP
    sessionAffinityConfig:
      clientIP:
        timeoutSeconds: 10800
    type: ClusterIP
  status:
    loadBalancer: {}
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-23T15:48:53Z
    labels:
      run: nginx
    name: nginx
    namespace: default
    resourceVersion: "820463"
    selfLink: /api/v1/namespaces/default/services/nginx
    uid: 2c7d6d45-bf48-11e8-b870-0e2e4b1da204
  spec:
    clusterIP: 10.100.231.225
    externalTrafficPolicy: Cluster
    ports:
    - nodePort: 32269
      port: 8080
      protocol: TCP
      targetPort: 80
    selector:
      run: nginx
    sessionAffinity: None
    type: NodePort
  status:
    loadBalancer: {}
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
```

### ノードからアクセスする

ログインしたノードから[localhost:nodeport]でアクセスできます。

```
$ ssh -i ***** ec2-user@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
$ curl http://localhost:32269
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
```

上記のログインしたノードから、[別のノード(EC2)のローカルIPアドレス:nodeport]を指定してもアクセスできます。

```
$ curl http://192.168.114.90:32269
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
```

# 5. kubectl exposeで--type LoadBalancerを指定してserviceを作成する

```
$ kubectl delete service nginx
$ kubectl expose deployments nginx --port 8080 --target-port 80 --type LoadBalance
```

EXTERNAL-IPに、AWSのCLBのホスト名が表示されます。

```
$ kubectl get services -o wide
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP                                                              PORT(S)          AGE       SELECTOR
kubernetes   ClusterIP      10.100.0.1     <none>                                                                   443/TCP          11h       <none>
nginx        LoadBalancer   10.100.17.22   a18a9a65fbf9011e8bc6f12564c8934b-XXXXXXXXX.us-east-1.elb.amazonaws.com   8080:30279/TCP   27s       run=nginx
$ kubectl get services -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-23T13:21:55Z
    labels:
      component: apiserver
      provider: kubernetes
    name: kubernetes
    namespace: default
    resourceVersion: "807105"
    selfLink: /api/v1/namespaces/default/services/kubernetes
    uid: a44578cf-bf33-11e8-bc6f-12564c8934b4
  spec:
    clusterIP: 10.100.0.1
    ports:
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
    sessionAffinity: ClientIP
    sessionAffinityConfig:
      clientIP:
        timeoutSeconds: 10800
    type: ClusterIP
  status:
    loadBalancer: {}
- apiVersion: v1
  kind: Service
  metadata:
    creationTimestamp: 2018-09-24T00:23:44Z
    labels:
      run: nginx
    name: nginx
    namespace: default
    resourceVersion: "866768"
    selfLink: /api/v1/namespaces/default/services/nginx
    uid: 18a9a65f-bf90-11e8-bc6f-12564c8934b4
  spec:
    clusterIP: 10.100.17.22
    externalTrafficPolicy: Cluster
    ports:
    - nodePort: 30279
      port: 8080
      protocol: TCP
      targetPort: 80
    selector:
      run: nginx
    sessionAffinity: None
    type: LoadBalancer
  status:
    loadBalancer:
      ingress:
      - hostname: a18a9a65fbf9011e8bc6f12564c8934b-XXXXXXXXX.us-east-1.elb.amazonaws.com
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
```

### ローカルPCからアクセスする

```
$ curl http://a18a9a65fbf9011e8bc6f12564c8934b-XXXXXXXXX.us-east-1.elb.amazonaws.com:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
```

# 6. kubectl apply -fでpodを作成する

```nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    ports:
    - containerPort: 80
```

```
$ kubectl apply -f nginx-pod.yaml
pod/nginx created
$ kubectl get pods -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP                NODE
nginx     1/1       Running   0          15s       192.168.202.207   ip-192-168-203-105.ec2.internal
$ kubectl get pods -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"nginx","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"nginx","ports":[{"containerPort":80}]}]}}
    creationTimestamp: 2018-09-24T02:03:19Z
    name: nginx
    namespace: default
    resourceVersion: "875778"
    selfLink: /api/v1/namespaces/default/pods/nginx
    uid: 0255c063-bf9e-11e8-bc6f-12564c8934b4
  spec:
    containers:
    - image: nginx
      imagePullPolicy: Always
      name: nginx
      ports:
      - containerPort: 80
        protocol: TCP
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: default-token-pqfrk
        readOnly: true
    dnsPolicy: ClusterFirst
    nodeName: ip-192-168-203-105.ec2.internal
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - name: default-token-pqfrk
      secret:
        defaultMode: 420
        secretName: default-token-pqfrk
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: 2018-09-24T02:03:19Z
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: 2018-09-24T02:03:20Z
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: 2018-09-24T02:03:19Z
      status: "True"
      type: PodScheduled
    containerStatuses:
    - containerID: docker://32b5974532c2dd690370582796193962f991fcab27da3a7dd2bd818ac2a80215
      image: nginx:latest
      imageID: docker-pullable://nginx@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
      lastState: {}
      name: nginx
      ready: true
      restartCount: 0
      state:
        running:
          startedAt: 2018-09-24T02:03:20Z
    hostIP: 192.168.203.105
    phase: Running
    podIP: 192.168.202.207
    qosClass: BestEffort
    startTime: 2018-09-24T02:03:19Z
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
```

```
$ kubectl delete pods nginx
```

# 7. kubectl apply -fでreplicasetを作成する

```nginx-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
          - containerPort: 80
```

```
$ kubectl apply -f nginx-replicaset.yaml
replicaset.apps/nginx created
$ kubectl get pods -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP                NODE
nginx-87xpz   1/1       Running   0          19s       192.168.153.61    ip-192-168-130-138.ec2.internal
nginx-qbrn2   1/1       Running   0          19s       192.168.119.72    ip-192-168-114-90.ec2.internal
nginx-slkw8   1/1       Running   0          19s       192.168.230.217   ip-192-168-203-105.ec2.internal
$ kubectl get replicasets -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES    SELECTOR
nginx     3         3         3         32s       nginx        nginx     app=nginx
$ kubectl get services -o wide
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1   <none>        443/TCP   1h        <none>
$ kubectl get replicasets -o yaml
apiVersion: v1
items:
- apiVersion: extensions/v1beta1
  kind: ReplicaSet
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"nginx","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx","name":"nginx","ports":[{"containerPort":80}]}]}}}}
    creationTimestamp: 2018-09-24T02:31:29Z
    generation: 1
    labels:
      app: nginx
    name: nginx
    namespace: default
    resourceVersion: "878383"
    selfLink: /apis/extensions/v1beta1/namespaces/default/replicasets/nginx
    uid: f19b05ed-bfa1-11e8-bc6f-12564c8934b4
  spec:
    replicas: 3
    selector:
      matchLabels:
        app: nginx
    template:
      metadata:
        creationTimestamp: null
        labels:
          app: nginx
      spec:
        containers:
        - image: nginx
          imagePullPolicy: Always
          name: nginx
          ports:
          - containerPort: 80
            protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        dnsPolicy: ClusterFirst
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        terminationGracePeriodSeconds: 30
  status:
    availableReplicas: 3
    fullyLabeledReplicas: 3
    observedGeneration: 1
    readyReplicas: 3
    replicas: 3
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
```

```
$ kubectl delete replicasets nginx
```

# 8. kubectl apply -fでserviceとreplicasetを作成する

```nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
          - containerPort: 80
```

```
$ kubectl apply -f nginx-service.yaml
service/nginx created
replicaset.apps/nginx created
$ kubectl get pods -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP                NODE
nginx-2hrmq   1/1       Running   0          20s       192.168.119.72    ip-192-168-114-90.ec2.internal
nginx-rbhqq   1/1       Running   0          20s       192.168.230.217   ip-192-168-203-105.ec2.internal
nginx-srdbh   1/1       Running   0          20s       192.168.153.61    ip-192-168-130-138.ec2.internal
$ kubectl get replicasets -o wide
NAME      DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES    SELECTOR
nginx     3         3         3         32s       nginx        nginx     app=nginx
$ kubectl get services -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1      <none>        443/TCP    2h        <none>
nginx        ClusterIP   10.100.63.238   <none>        8080/TCP   44s       app=nginx
$ ssh -i ***** ec2-user@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com
$ curl http://10.100.63.238:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
```

```
$ kubectl delete replicasets,services nginx
```

# 9. kubectl apply -fでserviceとdeploymentを作成する

```nginx-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  sessionAffinity: None
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
          - containerPort: 80
```

```
$ kubectl apply -f nginx-deployment.yaml
service/nginx created
deployment.apps/nginx created
$ kubectl get pods -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP                NODE
nginx-666865b5dd-4bzpz   1/1       Running   0          29s       192.168.119.72    ip-192-168-114-90.ec2.internal
nginx-666865b5dd-bc72w   1/1       Running   0          29s       192.168.153.61    ip-192-168-130-138.ec2.internal
nginx-666865b5dd-zmq2c   1/1       Running   0          29s       192.168.230.217   ip-192-168-203-105.ec2.internal
$ kubectl get replicasets -o wide
NAME               DESIRED   CURRENT   READY     AGE       CONTAINERS   IMAGES    SELECTOR
nginx-666865b5dd   3         3         3         48s       nginx        nginx     app=nginx,pod-template-hash=2224216188
$ kubectl get service -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE       SELECTOR
kubernetes   ClusterIP   10.100.0.1      <none>        443/TCP    2h        <none>
nginx        ClusterIP   10.100.99.241   <none>        8080/TCP   58s       app=nginx
$ kubectl get deployments -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES    SELECTOR
nginx     3         3         3            3           1m        nginx        nginx     app=nginx
```

# 雑感

奥が深い...ような気がする