Think Twice

Memorandum

CentOS7 minimal で Kubernetes ~part 3~

今回はマニュアルに従ってLabel, ReplicationController, Service を試してみる。github.com

Label

マニュアルに従うだけ。

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: mynginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
podをlabelで確認
$ kubectl get pods -l app=mynginx
NAME       READY     STATUS    RESTARTS   AGE
nginx           1/1       Running   0          2m
podを削除
$ kubectl delete pod nginx

Replication Controller

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  # selector identifies the set of Pods that this
  # replication controller is responsible for managing
  selector:
    app: mynginx
  # podTemplate defines the 'cookie cutter' used for creating
  # new pods when necessary
  template:
    metadata:
      labels:
        # Important: these labels need to match the selector above
        # The api server enforces this constraint.
        app: mynginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
Replication Controller の作成
$ kubectl create -f replication-controller.yaml
replicationcontrollers/nginx-controller
Replication Controller の確認
$ kubectl get rc
CONTROLLER         CONTAINER(S)   IMAGE(S)   SELECTOR      REPLICAS
nginx-controller   nginx          nginx      app=mynginx   2
pod の確認
$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-controller-0a2ye   1/1       Running   0          4m
nginx-controller-sonfl   1/1       Running   0          4m
pod をscaleさせる
$ kubectl scale --replicas=4 rc nginx-controller
scaled
$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-controller-02oqh   0/1       Pending   0          12s
nginx-controller-0a2ye   1/1       Running   0          4m
nginx-controller-jnqk8   0/1       Pending   0          12s
nginx-controller-sonfl   1/1       Running   0          4m
[kube@master ~]$

Service

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 8000 # the port that this service should serve on
    # the container on each pod to connect to, can be a name
    # (e.g. 'www') or a number (e.g. 80)
    targetPort: 80
    protocol: TCP
  # just like the selector in the replication controller,
  # but this time it identifies the set of pods to load balance
  # traffic to.
  selector:
    app: mynginx

service 作成

$ kubectl create -f service.yaml
services/nginx-service
$ kubectl get service
NAME            LABELS                                    SELECTOR      IP(S)            PORT(S)
kubernetes      component=apiserver,provider=kubernetes   <none>        10.254.0.1       443/TCP
nginx-service   <none>                                    app=mynginx   10.254.207.252   8000/TCP