1台でkubernetesを試す(CentOS 7)

ほぼgithubのマニュアルそのままですが、まずは1台でmasterとminionを兼ねて作ってみます
https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/getting-started-guides/centos/centos_manual_config.md

バージョン

* CentOS-7.0-x86_64-Minimal
* docker 1.3.2
* kubernetes 0.9.0
* etcd 0.4.6

## インストール
CentOS7-minimalをインストールしたVMを用意、ホスト名はcentos-masterにしています

Dockerをインストール

```
# yum update -y
# yum install docker
```

リポジトリを追加

```
# vi /etc/yum.repos.d/\_virt7-testing.repo

[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-testing/x86_64/os/
gpgcheck=0
```

kubernetesをインストール

```
# yum -y install --enablerepo=virt7-testing kubernetes
```

依存関係でetcdもインストールされる。etcdの0.4.6がインストールされればOK
2.0.1がインストールされた場合は、0.4.6を指定して再インストールする

```
# yum erase etcd
# yum install http://cbs.centos.org/kojifiles/packages/etcd/0.4.6/7.el7.centos/x86_64/etcd-0.4.6-7.el7.centos.x86_64.rpm
# yum -y install --enablerepo=virt7-testing kubernetes
```

ホスト名を登録

```
# echo "10.255.199.171 centos-master" >> /etc/hosts
```

## kubernetesの設定

### 共通設定
masterにもminionにも共通の設定（今回は区別しませんが）

etcdのエンドポイント設定

```
# vi /etc/kubernetes/config

  # Comma seperated list of nodes in the etcd cluster
- #KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:4001"
+ KUBE_ETCD_SERVERS="--etcd_servers=http://centos-master:4001"
```

ファイアウォール停止

```
# systemctl disable firewalld
# systemctl stop firewalld
```

### masterの設定

APIserverの設定

```
# vi /etc/kubernetes/apiserver

  # The address on the local server to listen to.
- #KUBE_API_ADDRESS="--address=127.0.0.1"
+ KUBE_API_ADDRESS="--address=0.0.0.0"

...

  # How the replication controller and scheduler find the kube-apiserver
- #KUBE_MASTER="--master=127.0.0.1:8080"
+ KUBE_MASTER="--master=http://centos-master:8080"
```

※↑マニュアル通りKUBE_MASTER="--master=centos-master:8080"を指定すると
Failed to list *api.Service: Get centos-master:8080: unsupported protocol scheme "centos-master"
というエラーが出たのでこうしてます。

Minionのアドレスを指定。今回はmasterがminionを兼ねる

```
# vi /etc/kubernetes/controller-manager
  # Comma seperated list of minions
- #KUBELET_ADDRESSES="--machines=127.0.0.1"
+ KUBELET_ADDRESSES="--machines=centos-master"
```

masterのサービス起動

```
# for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do 
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
done
```

下記エラーが発生
Unable to listen for secure (open /var/run/kubernetes/apiserver.crt: no such file or directory); will try again.
下記を参考に対応
http://ask.projectatomic.io/en/question/199/missing-apiservercrt-unable-to-listen-for-secure/

```
# vi /etc/systemd/system/kube-apiserver.service.d/10-varrun-build.conf

[Service]
# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=-/usr/bin/mkdir /var/run/kubernetes
ExecStartPre=/usr/bin/chown -R kube:kube /var/run/kubernetes/
```


###　minionの設定

kubeletの設定

```
# vi /etc/kubernetes/kubelet

  # The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
- #KUBELET_ADDRESS="--address=127.0.0.1"
+ KUBELET_ADDRESS="--address=0.0.0.0"

...

  # You may leave this blank to use the actual hostname
- #KUBELET_HOSTNAME="--hostname_override=127.0.0.1"
+ KUBELET_HOSTNAME="--hostname_override=centos-master"
```

minionのサービス起動

```
# for SERVICES in kube-proxy kubelet docker; do 
    systemctl restart $SERVICES
    systemctl enable $SERVICES
    systemctl status $SERVICES 
done
```

## 動作確認

```lang:
# kubectl get minions
NAME                LABELS              STATUS
centos-master       <none>              Ready

# kubectl get pods
POD                 IP                  CONTAINER(S)        IMAGE(S)            HOST                LABELS              STATUS

# kubectl get services
NAME                LABELS                                    SELECTOR            IP                  PORT
kubernetes          component=apiserver,provider=kubernetes   <none>              10.254.0.2          443
kubernetes-ro       component=apiserver,provider=kubernetes   <none>              10.254.0.1          80

# kubectl get replicationControllers
CONTROLLER          CONTAINER(S)        IMAGE(S)            SELECTOR            REPLICAS
```

## サンプルのguestbookを試してみる

githubのexamples/guestbook-goを試してみます
https://github.com/GoogleCloudPlatform/kubernetes/blob/master/examples/guestbook-go/README.md

```
# git clone kubernetes
# cd kubernetes/examples/guestbook-go
```

※examples/guestbookは以下の問題で上手く行かなかった
https://github.com/GoogleCloudPlatform/kubernetes/issues/4414


Redis MasterのReplicationControllerを作成。自動的にPodが作られる

```
# kubectl create -f redis-master-controller.json
redis-master-controller

# kubectl get rc
CONTROLLER                CONTAINER(S)        IMAGE(S)            SELECTOR                 REPLICAS
redis-master-controller   redis-master        gurpartap/redis     name=redis,role=master   1

# kubectl get pods
POD                             IP                  CONTAINER(S)        IMAGE(S)            HOST                           LABELS                   STATUS
redis-master-controller-fplln   172.17.0.2          redis-master        gurpartap/redis     centos-master/10.255.199.171   name=redis,role=master   Running
```

Redis MasterのServiceを作成

```
# kubectl create -f redis-master-service.json
redis-master

# kubectl get services
NAME                LABELS                                    SELECTOR                 IP                  PORT
kubernetes          component=apiserver,provider=kubernetes   <none>                   10.254.0.2          443
kubernetes-ro       component=apiserver,provider=kubernetes   <none>                   10.254.0.1          80
redis-master        <none>                                    name=redis,role=master   10.254.138.228      6379
```

Redis SlaveのReplicationControllerを作成

```
# kubectl create -f redis-slave-controller.json
redis-slave-controller

# kubectl get rc
CONTROLLER                CONTAINER(S)        IMAGE(S)            SELECTOR                 REPLICAS
redis-master-controller   redis-master        gurpartap/redis     name=redis,role=master   1
redis-slave-controller    redis-slave         gurpartap/redis     name=redis,role=slave    2

# kubectl get pods
POD                             IP                  CONTAINER(S)        IMAGE(S)            HOST                           LABELS                   STATUS
redis-master-controller-fplln   172.17.0.2          redis-master        gurpartap/redis     centos-master/10.255.199.171   name=redis,role=master   Running
redis-slave-controller-gziey    172.17.0.3          redis-slave         gurpartap/redis     centos-master/10.255.199.171   name=redis,role=slave    Running
redis-slave-controller-oh43e    172.17.0.4          redis-slave         gurpartap/redis     centos-master/10.255.199.171   name=redis,role=slave    Running
```

Redis SlaveのServiceを作成

```
# kubectl create -f redis-slave-service.json
redis-slave

# kubectl get services
NAME                LABELS                                    SELECTOR                 IP                  PORT
kubernetes          component=apiserver,provider=kubernetes   <none>                   10.254.0.2          443
kubernetes-ro       component=apiserver,provider=kubernetes   <none>                   10.254.0.1          80
redis-master        <none>                                    name=redis,role=master   10.254.138.228      6379
redis-slave         name=redis,role=slave                     name=redis,role=slave    10.254.49.145       6379
```

Frontendの設定ファイルを編集。今回はローカルに作っているのでExternalLoadBalancerの代わりにpublicIPを指定する

```
# vi guestbook-service.json
   "port": 3000,
   "containerPort": "http-server",
   "selector": { "name": "guestbook" },
-  "createExternalLoadBalancer": true
+  "publicIPs":["your-public-ip"]
``` 

FrontendのReplicationControllerを作成

```
# kubectl create -f guestbook-controller.json
guestbook-controller

# kubectl get rc
CONTROLLER                CONTAINER(S)        IMAGE(S)               SELECTOR                 REPLICAS
guestbook-controller      guestbook           kubernetes/guestbook   name=guestbook           3
redis-master-controller   redis-master        gurpartap/redis        name=redis,role=master   1
redis-slave-controller    redis-slave         gurpartap/redis        name=redis,role=slave    2

# kubectl get pods
POD                             IP                  CONTAINER(S)        IMAGE(S)               HOST                           LABELS                   STATUS
guestbook-controller-0133o      172.17.0.5          guestbook           kubernetes/guestbook   centos-master/10.255.199.171   name=guestbook           Running
guestbook-controller-hh2gd      172.17.0.7          guestbook           kubernetes/guestbook   centos-master/10.255.199.171   name=guestbook           Running
guestbook-controller-ls6k1      172.17.0.6          guestbook           kubernetes/guestbook   centos-master/10.255.199.171   name=guestbook           Running
redis-master-controller-fplln   172.17.0.2          redis-master        gurpartap/redis        centos-master/10.255.199.171   name=redis,role=master   Running
redis-slave-controller-gziey    172.17.0.3          redis-slave         gurpartap/redis        centos-master/10.255.199.171   name=redis,role=slave    Running
redis-slave-controller-oh43e    172.17.0.4          redis-slave         gurpartap/redis        centos-master/10.255.199.171   name=redis,role=slave    Running
```

FrontendのServiceを作成

```
# kubectl create -f guestbook-service.json
guestbook

# kubectl get services
NAME                LABELS                                    SELECTOR                 IP                  PORT
guestbook           <none>                                    name=guestbook           10.254.166.244      3000
kubernetes          component=apiserver,provider=kubernetes   <none>                   10.254.0.2          443
kubernetes-ro       component=apiserver,provider=kubernetes   <none>                   10.254.0.1          80
redis-master        <none>                                    name=redis,role=master   10.254.138.228      6379
redis-slave         name=redis,role=slave                     name=redis,role=slave    10.254.49.145       6379
```

ブラウザからFrontendの設定ファイルに指定した<your-public-ip:3000>にアクセス
問題が無ければ、guestbookの画面が表示され、利用することができる