Skip to content
k8s study
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.docker
.helm/stable/hello-rails-on-k8s
app
bin
config
db
lib
log
public
storage
test
tmp
vendor
.babelrc
.editorconfig
.gitignore
.postcssrc.yml
.ruby-version
Gemfile
Gemfile.lock
README.md
Rakefile
config.ru
package.json
yarn.lock

README.md

hello-rails-on-k8s

k8s の勉強

Rails を k8s 環境で動かす

helm

https://github.com/helm/helm

Hands on

0. Preparation

ローカルだと Ingress で Service を公開することはできない
クラスタ外からの HTTP リクエストを Service にルーティングするために nginx_ingress_controller をデプロイしておく

$ kubectl -f apply https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.16.2/deploy/mandatory.yaml
$ kubectl -f apply https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.16.2/deploy/provider/cloud-generic.yaml

Check deployed services and pods

$ kubectl get svc,pod --namespace ingress-nginx
NAME                           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
service/default-http-backend   ClusterIP      10.110.86.111    <none>        80/TCP                       1d
service/ingress-nginx          LoadBalancer   10.105.208.212   localhost     80:32414/TCP,443:31696/TCP   1d

NAME                                            READY   STATUS    RESTARTS   AGE
pod/default-http-backend-7fb86fb47d-6th9z       1/1     Running   1          1d
pod/nginx-ingress-controller-78bd49949c-5vh5r   1/1     Running   0          1d

1. Build containers

$ docker build -f .docker/containers/app/Dockerfile   -t hello-rails-on-k8s-app:latest .
$ docker build -f .docker/containers/nginx/Dockerfile -t hello-rails-on-k8s-nginx:latest .
$ docker build -f .docker/containers/db/Dockerfile    -t hello-rails-on-k8s-db:latest .

2. Deploy by kubectl

Deploy

For application

$ kubectl apply -f .docker/kubernetes/webserver.yml
$ kubectl apply -f .docker/kubernetes/dbserver.yml
$ kubectl apply -f .docker/kubernetes/ingress.yml

For logging

$ kubectl apply -f .docker/kubernetes/fluentd.yml
$ kubectl apply -f .docker/kubernetes/elasticsearch.yml
$ kubectl apply -f .docker/kubernetes/kibana.yml

Check

Request to rails

curl http://localhost -H "Host: hello-rails-on-k8s.local"

Access to Kibana
http://localhost:30050

Note:
curl http://localhost -H "Host: hello-rails-on-k8s.local" は、
http://localhost に対してリクエストするんだけど、あたかも http://hello-rails-on-k8s.local としてリクエストしたように見せかけられる
ref: curlをhost指定して実行する - するめとめがね

Delete objects

$ kubectl delete -f .docker/kubernetes/webserver.yml
$ kubectl delete -f .docker/kubernetes/dbserver.yml
$ kubectl delete -f .docker/kubernetes/fluentd.yml
$ kubectl delete -f .docker/kubernetes/elasticsearch.yml
$ kubectl delete -f .docker/kubernetes/kibana.yml
$ kubectl delete -f .docker/kubernetes/ingress.yml

3. Deploy by helm

Deploy

$ helm install --name hello-rails-on-k8s .helm/stable/hello-rails-on-k8s/

Check

Request to rails

$ curl http://localhost -H "Host: hello-rails-on-k8s.local"

Delete objects

$ helm delete hello-rails-on-k8s
# and purge
$ helm del --purge hello-rails-on-k8s
You can’t perform that action at this time.