Cloud/Cloud Native

How to use Hashicorp Waypoint

Jacob_baek 2020. 10. 27. 21:00

Introduce Waypoint

아래 링크를 참고

How to use Waypoint on Minikube

여기서는 배포환경을 minikube(kubernetes)로 지정하고 진행한다.

여기서 minikube라고 별다른 것이 있지는 않다고 판단되고 vanilla version의 kubernetes에 waypoint를 통해 배포가 이루어진다라고 이해해도 무방할거라 보인다.

환경

동작될 환경에 대한 정보이다.

jacob@jacob-laptop:~$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
jacob@jacob-laptop:~/workspace$ kubectl config get-contexts
CURRENT   NAME                                      CLUSTER                AUTHINFO          NAMESPACE
          default                                   default                default           
          default/api-crc-testing:6443/kube:admin   api-crc-testing:6443   kube:admin        default
          jacob-context                             jacob-cluster          jacob-user        
          jacobbaek-context                         infra-kubernetes       jacobbaek         jacobbaek
*         minikube                                  minikube               minikube          

waypoint installation

waypoint binary를 다운로드 혹은 설치하고 해당 waypoint 명령을 통해 build/deploy/release를 수행해보자.

jacob@jacob-laptop:~$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
jacob@jacob-laptop:~$ sudo apt install waypoint
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  waypoint
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 69.5 MB of archives.
After this operation, 138 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com focal/main amd64 waypoint amd64 0.1.4 [69.5 MB]
Fetched 69.5 MB in 8s (8,434 kB/s)                                                              
Selecting previously unselected package waypoint.
(Reading database ... 377269 files and directories currently installed.)
Preparing to unpack .../waypoint_0.1.4_amd64.deb ...
Unpacking waypoint (0.1.4) ...
Setting up waypoint (0.1.4) ...

waypoint server install

waypoint install 명령을 통해 server 설치가 가능하다.
여기서 platform은 kubernetes,docker를 선택하여 설치할수 있다.
kubernetes의 경우 현재 kubectl config get-contexts에 지정되어 있는 context를 기반으로 진행된다.

jacob@jacob-laptop:~/workspace$ waypoint install --platform=kubernetes -accept-tos
✓ Creating Kubernetes resources...
 │ service/waypoint created
 │ statefulset.apps/waypoint-server created
✓ Kubernetes StatefulSet reporting ready
✓ Waiting for Kubernetes service to become ready..
✓ Configuring server...
Waypoint server successfully installed and configured!

The CLI has been configured to connect to the server automatically. This
connection information is saved in the CLI context named "install-1603776039".
Use the "waypoint context" CLI to manage CLI contexts.

The server has been configured to advertise the following address for
entrypoint communications. This must be a reachable address for all your
deployments. If this is incorrect, manually set it using the CLI command
"waypoint server config-set".

Advertise Address: 192.168.49.200:9701
Web UI Address: https://192.168.49.200:9702

여기서 한가지 알아두어야 하는것은 -accept-tos 옵션을 붙여야 한다는것이다.
Terms of Service 관련 내용으로 다음과 같다.

-accept-tos
    Pass to accept the Terms of Service and Privacy Policy to use the
    Waypoint URL Service. This is required if the URL service is enabled
    and you're using the HashiCorp-provided URL service rather than
    self-hosting. See the privacy policy at https://hashicorp.com/privacy
    and the ToS at https://waypointproject.io/terms The default is false.

또한 platform은 kubernetes, docker 등을 사용할수 있다. 아래 링크가 platform에 대한 설명이 나와 있는 링크이며 별다른 내용이 없는것으로 보아 아직은 container 기반의 환경만을 지원하는것이 아닌가 생각된다. (waypoint keynote상에서는 ec2 이야기도 있었던거 같아서.... 좀더 survey 해야할것 같다..)

주의
waypoint는 loadbalancer로 service의 type을 생성하고 이에 대한 verification과정이 수행되기에
metallb와 같은 loadbalancer type을 사용할수 있는 환경이 필요하다.
하여 minikube addons enable metallb을 수행하고 진행하는것을 권장한다.

install이 완료되면 해당 context 정보를 아래와 같이 확인할 수 있다.

jacob@jacob-laptop:~/workspace$ waypoint context list
    |        NAME        |   SERVER ADDRESS     
----+--------------------+----------------------
  * | install-1603776039 | 192.168.49.200:9701  

만약 아래와 같이 docker에 대해서도 설치를 한 경우라면

jacob@jacob-laptop:~/workspace$ waypoint install --platform=docker -accept-tos
✓ Installing Waypoint server to docker
✓ Server container started!
✓ Configuring server...
Waypoint server successfully installed and configured!

The CLI has been configured to connect to the server automatically. This
connection information is saved in the CLI context named "install-1603778170".
Use the "waypoint context" CLI to manage CLI contexts.

The server has been configured to advertise the following address for
entrypoint communications. This must be a reachable address for all your
deployments. If this is incorrect, manually set it using the CLI command
"waypoint server config-set".

Advertise Address: waypoint-server:9701
Web UI Address: https://localhost:9702

다음과 같이 context 가 두개 출력되고 실제 배포할 platform에 매칭되는 context를 사전에 선택해 놓아야 한다.

jacob@jacob-laptop:~/workspace$ waypoint context list
    |        NAME        |   SERVER ADDRESS     
----+--------------------+----------------------
  * | install-1603778170 | localhost:9701       
    | install-1603776039 | 192.168.49.200:9701  

jacob@jacob-laptop:~/workspace$ waypoint context use install-1603776039
Context "install-1603776039" is now the default.
jacob@jacob-laptop:~/workspace$ waypoint context list
    |        NAME        |   SERVER ADDRESS     
----+--------------------+----------------------
    | install-1603778170 | localhost:9701       
  * | install-1603776039 | 192.168.49.200:9701  

waypoint init and up

이제부터 실제 clone 받은 소스를 기반으로 build, deploy, release를 수행하는 과정을 알아보도록 하겠다.

여기서는 제공되는 waypoint example이 아닌 개인적으로 sample로 만들어 놓은 project로 진행을 해보겠다.

jacob@jacob-laptop:~/workspace$ git clone https://github.com/jacobbaek/python-code-runner-on-k8s.git
jacob@jacob-laptop:~/workspace$ cd python-code-runner-on-k8s
jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ 

먼저 아래 waypoint.hcl이 생성되어 있어야 한다.
(Jenkins를 사용해보았다면 Jenkinsfile 과 같은 개념이라 보면 된다.)

project = "jacobbaek-py"

app "k8spysample" {
  labels = {
    "service" = "k8spysample",
    "env" = "dev"
  }

  build {
    use "docker" {}
    registry {
      use "docker" {
        image = "dubaek/k8spysample"
        tag = "0.2"
      }
    }
  }
  deploy {
    use "kubernetes" {
      probe_path = "/"
      service_port = 5000
    }
  }

  release {
    use "kubernetes" {
    }
  }
}

다음 3가지 과정을 수행하도록 하였다.

  • build : docker plugin을 사용하여 image를 build 하고 push까지 수행한다. 참고로 docker build 가 수행되기 위해서는 Dockerfile이 사전에 생성되어야 한다.
    (만약 local에서만 사용된다면 local = true로 registry 상에 추가하면 된다.)
  • deploy : kubernetes plugin을 사용하여 kubernetes 환경에 배포를 진행한다.
    여기서 사용되는 option들은 아래 링크에 존재한다. (service_port, probe_path 등..)
  • release : 여기서는 kubernetes 상에 별도의 설정할 resource가 필요하지는 않기에 별도의 내용을 추가하지는 않았다.

여기서 알아두어야 할 것은
별도의 manifest yaml 파일을 생성하지 않아도 된다는것이다.
또한 docker build에 대한 선언이 있는 경우(위의 경우)는 Dockerfile이 있어야 한다.
결론적으로는 application을 구동할 source code와 waypoint.hcl, Dockerfile(이또한 cloud native buildpack 사용시 불필요) 만 존재하면 된다.

먼저 hashicorp 제품들이 그렇듯이 init(초기화) 과정을 수행한다.

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ waypoint init
✓ Configuration file appears valid
✓ Connection to Waypoint server was successful
✓ Project "example-nodejs" and all apps are registered with the server.
✓ Plugins loaded and configured successfully
✓ Authentication requirements appear satisfied.

Project initialized!

You may now call 'waypoint up' to deploy your project or
commands such as 'waypoint build' to perform steps individually.

초기화 과정을 통해 .waypoint 디렉토리와 함께 아래의 디렉토리와 파일이 생성된다.

├── .waypoint
│   ├── cache
│   │   └── app
│   │       └── example-nodejs
│   │           └── component
│   │               ├── builder
│   │               │   └── pack
│   │               ├── platform
│   │               │   └── kubernetes
│   │               ├── registry
│   │               │   └── docker
│   │               └── releasemanager
│   │                   └── kubernetes
│   └── data
│       └── app
│           └── example-nodejs
│               └── component
│                   ├── builder
│                   │   └── pack
│                   ├── platform
│                   │   └── kubernetes
│                   ├── registry
│                   │   └── docker
│                   └── releasemanager
│                       └── kubernetes

이제 앞서 정의된 waypoint.hcl 파일을 기반으로 build, deploy, release를 수행해보자.

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ waypoint up
✓ Initializing Docker client...
✓ Building image...
 │ Step 9/10 : EXPOSE 5000
 │  ---> Running in 50ca096274fa
 │ Removing intermediate container 50ca096274fa
 │  ---> f9dce49a9b0a
 │ Step 10/10 : CMD ["python", "app.py"]
 │  ---> Running in 723456cad914
 │ Removing intermediate container 723456cad914
 │  ---> 5455e1bff988
 │ Successfully built 5455e1bff988
 │ Successfully tagged waypoint.local/k8spysample:latest
✓ Injecting Waypoint Entrypoint...
✓ Tagging Docker image: waypoint.local/k8spysample:latest => dubaek/k8spysample:0.2
✓ Pushing Docker image...
 │ 48fd0662e424: Layer already exists
 │ 817923b47cba: Layer already exists
 │ 0fb2e27dc3b8: Layer already exists
 │ a995c5106335: Layer already exists
 │ 17bdf5e22660: Layer already exists
 │ d37096232ed8: Layer already exists
 │ 6add0d2b5482: Layer already exists
 │ 4ef54afed780: Layer already exists
 │ 0.2: digest: sha256:e3c1dbcb3bc7b9a70b4da12d845484338182ebf1d5106c3c5ad026008897
 │ 2f2e size: 3264
⠋ Docker image pushed: dubaek/k8spysample:0.2

» Deploying...
✓ Kubernetes client connected to https://192.168.49.2:8443 with namespace default
✓ Creating deployment...
✓ Deployment successfully rolled out!

» Releasing...
✓ Kubernetes client connected to https://192.168.49.2:8443 with namespace default
✓ Creating service...
✓ Service is ready!

The deploy was successful! A Waypoint deployment URL is shown below. This
can be used internally to check your deployment and is not meant for external
traffic. You can manage this hostname using "waypoint hostname."

   Release URL: http://10.100.58.106:80
Deployment URL: https://usually-sincere-kangaroo--v2.waypoint.run

먼저 빌드과정중 아래와 같이 example-nodejs 파일이 생성되었음을 확인할수 있다.

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ docker images | grep dubaek
dubaek/k8spysample            0.2                 e3c1f3a59277        3 minutes ago       958MB
dubaek/k8spysample            <none>              920126c78548        19 minutes ago      958MB

그 다음 deploy가 진행된 이후 kubernetes에 배포된 resource를 확인해보면 다음과 같다.

jacob@jacob-laptop:~/workspace/waypoint-examples$ kubectl get all -l name=k8spysample-01enmfkrm2weycys23yd1148ya
NAME                                                          READY   STATUS    RESTARTS   AGE
pod/k8spysample-01enmfkrm2weycys23yd1148ya-7c89848596-q28c8   1/1     Running   0          5m25s

NAME                                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/k8spysample-01enmfkrm2weycys23yd1148ya-7c89848596   1         1         1       5m25s

실제 지정한 service_port로 livenessProbe 및 readinessProbe까지 생성이 되었다.

마지막으로 public url 을 하나 제공해주는데 이는 개발과정중 손쉽게 확인할수 있도록 public url을 무료로 서비스해주고 있다.
허나 이는 개발용으로 빠르게 확인하는 목적외에는 사용하지 말라는 경고도 있으니 이를 참고하여 좀더 쉬운 개발/배포를 하면 좋을것이라 판단된다.

주의
docker image로 진행되기 때문에 kubernetes 환경이라면 kubernetes가 현재 빌드한 image를 pull할 수 있는 환경이어야 한다. 하여 push 과정을 추가가 필요하다. 즉, build 과정중 registry 항목에 대한 명확한 정의가 필요하다.

추가로 배포가 완료된 이후 waypoint 명령을 통해 실제 동작되는 app에 대한 log 확인 / bash 연결도 가능하다.

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ waypoint logs
2020-10-27T08:28:01.568Z PEYPMA:  * Serving Flask app "app" (lazy loading)
2020-10-27T08:28:01.568Z PEYPMA:  * Environment: production
2020-10-27T08:28:01.568Z PEYPMA:    WARNING: This is a development server. Do not use it
in a production deployment.
2020-10-27T08:28:01.568Z PEYPMA:    Use a production WSGI server instead.
2020-10-27T08:28:01.568Z PEYPMA:  * Debug mode: off
2020-10-27T08:28:01.575Z PEYPMA:  * Running on http://0.0.0.0:5000/ (Press CTRL+C to
quit)
2020-10-27T08:28:08.567Z PEYPMA: 172.18.0.1 - - [27/Oct/2020 08:28:08] "GET /
HTTP/1.1" 200 -
2020-10-27T08:28:09.034Z PEYPMA: 172.18.0.1 - - [27/Oct/2020 08:28:09] "GET /
HTTP/1.1" 200 -

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ waypoint exec bash
Connected to deployment v3
root@k8spysample-01enmjqkdn1a6khkktqgyrm6qc-b5f66b5b8-xm6cp:/app# pwd
/app

위에서 사용한 waypoint.hcl과 관련 sample app의 경우 아래 github link를 참고하면 된다.

UI 접근

아래와 같이 UI로 접근도 가능하며 waypoint.hcl에 지정했던 project에 app 이름으로 생성된것을 확인할수 있으며 log와 build,deploy,release에 대한 이력도 확인해볼수 있다.

접근을 위해 token 발급을 아래와 같이할수 있으며 이에 대해서는 관리를 철저히 해주기를 권장하고 있다.

jacob@jacob-laptop:~/workspace/python-code-runner-on-k8s$ waypoint token new
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

간단한 개인총평

manifest를 별도로 생성하지 않고 자동으로 생성해주는것은 생각보다 매력이 있다. 다만 어디까지 자동으로 만들어질수 있느냐에 대해서는 좀더 확인이 필요할것같다. 자동화에는 필히 제한적인 부분이 존재하기 때문에...

또한 local 혹은 private kubernetes 외에 AWS를 시작으로 GKE, AKS 까지 배포를 하나의 Application을 별도의 코드 변경없이 해준다고 하고 있어서 해당 부분은 매력이 충분이 있다고 판단되고 아직 초기 단계라 보이기에 지켜볼 필요가 있다고 생각된다.

참고사이트