OpenShift v3 beta1 を試す

tamesu OpenShift v3 Beta1 が公開されましたので、環境構築とサンプルのデプロイ方法をご紹介します。

ホストOSのブラウザからアクセスだと、サイトのリダイレクトが発生した場合にちゃんと動かなかったりと苦労するので、今回はクライアントも一緒に使える All In One 環境としてセットアップします。

■仮想環境の準備

VirtualBox, Vagrantのインストールは、OpenShift v3 beta1rc1 を試す - akubicharm’s blogを見てください。

■■OpenShift v3をクローン

git clone git://github.com/openshift/origin
以降の説明では、cloneしたディレクトリをとします。

■■仮想OSの準備

cd <ORIGIN_HOME>
vagrant up
環境が作成されたら、ログインして設定を確認します。
vagrant ssh
■■■GUIパッケージのインストールと有効化
ここで作成される環境はサーバモードなので、All in Oneで試せるようにGUIを有効にします。
設定方法はfedora 21 server をgraphical modeに変更する - akubicharm’s blogを参考にしてください。
■■■Vagrantの設定をUIモードに変更
    # ################################
    # Set VirtualBox provider settings
    config.vm.provider "virtualbox" do |v, override|
      override.vm.box     = vagrant_openshift_config['virtualbox']['box_name'] unless dev_cluster
      override.vm.box_url = vagrant_openshift_config['virtualbox']['box_url'] unless dev_cluster
      override.ssh.insert_key = vagrant_openshift_config['insert_key']

      v.memory            = vagrant_openshift_config['memory']
      v.cpus              = vagrant_openshift_config['cpus']
      v.customize ["modifyvm", :id, "--cpus", "2"]
      # to make the ha-proxy reachable from the host, you need to add a port forwarding rule from 1080 to 80, wh
ich
      # requires root privilege. Use iptables on linux based or ipfw on BSD based OS:
      # sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 1080
      # sudo ipfw add 100 fwd 127.0.0.1,1080 tcp from any to any 80 in
        # enable GUI mode
        v.gui = true
    end if vagrant_openshift_config['virtualbox']    
ここまでできたら、仮想OSを再起動します。
vagrant halt
vagrant up
f:id:akubicharm:20150212020343p:plainユーザ=vagrant、パスワード=vagrant でログインできます。 ホストOSからは
vagrant ssh
でログイン可能です。
仮想OSではホストOSでcloneしたOpenShiftのリポジトリが/data/src/github.com/openshift/originにマウントされています。。

■OpenShiftの実行

■■systemd でOpenShiftの起動ゲスト
OSでOpenShiftを起動します。
$ cd /data/src/github.com/openshift/origin
$ sudo su # ./hack/build-go.sh ++ Building go targets for linux/amd64: cmd/openshift ++ Placing binaries $ sudo systemctl start openshift
openshiftのステータスを確認します。
sudo systemctl status openshift
結果がこんな風に出力されれば、OpenShiftは正常に動作しています。
    systemctl status openshift
● openshift.service - OpenShift
   Loaded: loaded (/usr/lib/systemd/system/openshift.service; disabled)
   Active: active (running) since 水 2015-02-11 15:28:18 UTC; 1h 48min ago
     Docs: https://github.com/openshift/origin
 Main PID: 1323 (openshift)
   CGroup: /system.slice/openshift.service
           └─1323 /data/src/github.com/openshift/origin/_output/local/go/bin/openshift start --public-master=https://10.0.2.15:8443
(2015/2/11時点のソースの場合)
ここで注目するのは、
--public-master=https://10.0.2.15:8443
10.0.2.15 は VirtualBoxのNATのIPアドレスで、ここではOpenShiftが動作しているホストのIPアドレスになります。All In One環境では https://localhost:8443 にアクセスすると管理機能が利用できるようになります。
 
(2015/2/12時点のソースの場合)
--listen=http://0.0.0.0:8080というオプションになっていたので、
KUBERNETES_MASTER=http://localhost:8080
のままで良さそうです。
 

■■ prebuildバイナリでの実行

https://github.com/openshift/origin/releases/ から取得したプリビルドのバイナリで実行する場合は、以下の様にします。

   openshift start &> openshift.log & 


■■環境変数の設定

export KUBECONFIG=/openshift.local.certificates/admin/.kubeconfig
export KUBERNETES_MASTER=https://localhost:8443
export CURL_CA_BUNDLE=/openshift.local.certificates/admin/root.crt
export CERT_DIR=/openshift.local.certificates/master
  • KUBERNETES_MASTER
    管理ノードのアドレス。デフォルトでは http://localhost:8080 に設定されていますが、OpenShiftの起動オプションで --public-master=https://10.0.2.15:8443 にあわせる必要があります。
  • KUBECONFIG
    kubernetesの設定ファイル。systemdでopenshiftを実行した場合は /openshift.local.certificates/admin/.kubeconfig を設定します。それ以外の場合は、openshift コマンドを実行したディレクトリに openshift.local.certifications ディレクトリが作成されています。
  • CURL_CA_BUNDLE
  • CERT_DIR
    管理コンソールに https でアクセスする場合の認証情報が保存されているディレクトリを指定します。
    ※CERT_DIRを指定しない場合、osc コマンドの実行時には『--insecure-skip-tls-verify』オプションを指定します。
    2015/2/12時点のソースでは listen-portがhttp://localhost:8080になっているのでこの環境変数は不要。その代わりに install-registry.sh を実行する際にポートとプロトコルを指定します。
    export API_PORT=8080
    export API_SCHEME=http

Dockerリポジトリの作成

リポジトリ作成のスクリプト環境変数CERT_DIRを参照しています。
cd /data/src/github.com/openshift/origin
hack/install-registry.sh
PodとServiceの確認をします。
# osc get pods
POD                       IP                  CONTAINER(S)         IMAGE(S)                    HOST                           LABELS                                                                                                             STATUS
docker-registry-1-d73jb   172.17.0.3          registry-container   openshift/docker-registry   openshiftdev.local/127.0.0.1   deployment=docker-registry-1,deploymentconfig=docker-registry,name=registrypod,template=docker-registry-template   Running
# osc get services
NAME                LABELS                                    SELECTOR            IP                  PORT
docker-registry     template=docker-registry-template         name=registrypod    172.30.17.249       5001
kubernetes          component=apiserver,provider=kubernetes                 172.30.17.2         443
kubernetes-ro       component=apiserver,provider=kubernetes                 172.30.17.1         80    
Dockerレジストリにアクセスできることを確認します。
# curl `osc get services docker-registry -o template --template="{{ .portalIP}}:{{ .port }}"`
"docker-registry server (dev) (v0.9.0)"

ここまでできたら、OpenShifの管理コンソールにアクセできるようになります。管理コンソールには admin/admin でログイン可能です。

f:id:akubicharm:20150212193042p:plain

f:id:akubicharm:20150212025527p:plain

プロジェクトの作成

cd /data/src/github.com/openshift/origin/examples/sample-app
osc create -f project.json

サンプルアプリケーションをデプロイします

githubで公開されているサンプルアプリケーションruby-hello-worldをデプロイします。ここでデプロイするアプリケーションは Source To Image 方式のビルドなので、webhookに指定したリポジトリソースコードの変更をコミットするか、明示的にビルドコマンドが発行された場合に、ビルド&デプロイが実行されます。
今回は、明示的にビルドコマンドを発行します。

cd /data/src/github.com/openshift/origin/examples/sample-app
osc process -n test -f application-template-stibuild.json | osc create -n test -f - 
osc start-build -n test ruby-sample-build
osc get -n test builds --watch

-n test でネームスペースを指定しています。

f:id:akubicharm:20150212031124p:plain

frontend の IPアドレスとポートにアクセスすると Key/Valueを登録するRubyのアプリケーションが見えます。

f:id:akubicharm:20150212182528p:plain