ASH84

Software Engineer/Developer, co-founder of Payhere. Ex-Banksalad. Intereseted in iteroperability, bootstrap company, writting.

Hello Node Kubernetes

created:2019-01-23
updated:2019-01-23
edit

본 포스팅은 Qwiklabs 의 Kubernetes in the Google Cloud 를 기반으로 하고 있습니다.

이번 시간에 실습해 볼 것들:

위의 그림에서 보면 GKE 와 GCR 그리고 k8s 의 관계를 설명해주고 있다. GCP 안에서의 관계는 저런식이지만 GCR 이 DockerHub 로 대체될 수 있고, GKE 가 AWS 로 대체 될 수 있을것 같다.

Node.js app

    var http = require('http');
    var handleRequest = function(request, response) {
      response.writeHead(200);
      response.end("Hello World!");
    }
    var www = http.createServer(handleRequest);
    www.listen(8080);

Docker container image 만들기

    FROM node:6.9.2
    EXPOSE 8080
    COPY server.js .
    CMD node server.js
    docker build -t gcr.io/PROJECT_ID/hello-node:v1 .
    docker run -d -p 8080:8080 gcr.io/PROJECT_ID/hello-node:v1

실행하면, Cloud shell 에서 preview on port 8080 버튼을 통해서 8080 포트를 웹 브라우저에서 볼 수 가 있다.

    gcloud docker -- push gcr.io/PROJECT_ID/hello-node:v1

클러스터 만들기

    gcloud config set project PROJECT_ID
    gcloud container clusters create hello-world \
                    --num-nodes 2 \
                    --machine-type n1-standard-1 \
                    --zone us-central1-a

Pod 생성하기

    kubectl run hello-node \
        --image=gcr.io/PROJECT_ID/hello-node:v1 \
        --port=8080
    kubectl get deployments
    kubectl get pods

외부 트래픽 연결하기

    kubectl expose deployment hello-node --type="LoadBalancer"
    kubectl get services

    NAME         CLUSTER-IP     EXTERNAL-IP      PORT(S)    AGE
    hello-node   10.3.250.149   104.154.90.147   8080/TCP   1m
    kubernetes   10.3.240.1     <none>           443/TCP    5m

서비스 스케일업 하기

    kubectl scale deployment hello-node --replicas=4
    kubectl get deployment

    NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    hello-node   4         4         4            4           16m

서비스의 롤링업그레이드

    kubectl edit deployment hello-node

#k8s  #node  #gcp  #gcr