LoginSignup
8
13

More than 5 years have passed since last update.

CentOS 7にMinikube環境を構築する

Last updated at Posted at 2018-10-19

CentOS上でMinikubeを動かしたメモ。Linuxで動かすなら以下記事のように普通にkubeadmで動かす方がよいように思うが、addonが使えたり便利な面もあるかもしれない。

AWSでCentOS 7にKubernetes 1.11をkubeadmでインストール

環境はAWSを使用。VPCやインスタンスの準備については記載を省略するが、上の記事には書いているのでそちらを参照のこと。

OSの基本的な準備

OSを更新。

sudo yum update

ホスト名変更。

sudo hostnamectl set-hostname --static minikube

再起動後も変わらないようにする。

sudo vi /etc/cloud/cloud.cfg

以下を追加。

preserve_hostname: true

ホスト名をhostsに追加。

10.0.0.59   minikube

一度再起動して、ログインし直す。

sudo reboot

Dockerのインストール

前提パッケージのインストール。

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

Docker CEのリポジトリをセットアップする。

sudo yum-config-manager \
  --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

インストール可能なバージョンを確認する。

[centos@minikube ~]$ sudo yum list docker-ce --showduplicates | sort -r
 * updates: ftp.iij.ad.jp
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: ftp.iij.ad.jp
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: ftp.iij.ad.jp
Available Packages
[centos@minikube ~]$

推奨となっている17.03を指定してインストールする。バージョンを指定する場合は、同時にdocker-ce-selinuxも指定して--setopt=obsoletes=0オプションを指定してあげる必要がある。

sudo yum install -y --setopt=obsoletes=0 docker-ce-17.03.2.ce docker-ce-selinux-17.03.2.ce

Dockerレポジトリは無効にしておく。今後使いたい場合はyumコマンドに--enablerepo="docker-ce-stable"オプションをつけて使う。

sudo yum-config-manager --disable docker-ce-stable

DockerデーモンをOS起動時に自動起動するよう設定し、起動する。

sudo systemctl enable docker && sudo systemctl start docker

centosユーザーでdockerコマンドを実行できるようにしておく。

[centos@minikube ~]$ sudo gpasswd -a centos docker
ユーザ centos をグループ docker に追加
[centos@minikube ~]$ id centos
uid=1000(centos) gid=1000(centos) groups=1000(centos),4(adm),10(wheel),190(systemd-journal),994(docker)
[centos@minikube ~]$

kubectl CLIのインストール

yumでインストールもできるがここでは、単にバイナリをダウンロードして/usr/local/binに置く。

curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && \
  chmod +x kubectl && \
  sudo cp kubectl /usr/local/bin/ && \
  rm kubectl

バージョンを確認しておく。

[centos@minikube ~]$ kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.1", GitCommit:"4ed3216f3ec431b140b1d899130a69fc671678f4", GitTreeState:"clean", BuildDate:"2018-10-05T16:46:06Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
[centos@minikube ~]$

Minikubeのインストール

Minikubeもダウンロードしてバイナリを配置。

VERSION="v0.30.0"
curl -Lo minikube https://storage.googleapis.com/minikube/releases/$VERSION/minikube-linux-amd64 && \
  chmod +x minikube && \
  sudo cp minikube /usr/local/bin/ && \
  rm minikube

バージョンを確認。

[centos@minikube ~]$ minikube version
minikube version: v0.30.0
[centos@minikube ~]$

socatとebtablesのインストール

[WARNING FileExisting-ebtables]: ebtables not found in system path
[WARNING FileExisting-socat]: socat not found in system path

Minikubeの起動時に上記の警告が出たので入れておく。警告であるがsocatはHelmも使用するので入れておく。

sudo yum -y install socat ebtables

cri-toolsのインストール

[ERROR FileExisting-crictl]: crictl not found in system path

Minikubeが上記エラーで起動しなかったので入れておく。こちらは必須。

VERSION="v1.12.0"
curl -LO https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$VERSION-linux-amd64.tar.gz

/usr/local/binにパスを通す

CentOSのrootユーザーはデフォルトで/usr/local/binにパスが通っていない。export PATH=/usr/local/bin:PATHを実行してからminikube startを実行しても、Minikubeが内部的にsudoしてコマンドを実行しているときにパスが通らず、cri-toolsのチェックでエラーになってしまうため、以下の設定を変えておく。

sudo visudo
# Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/local/bin:/usr/bin

その他

やった方がいいかもしれない手順。

  • スワップを無効にする。今回はもともとswap領域がなかった。
  • SELinuxを無効にする。今回は有効のままだが大丈夫そうだった。
  • Firewalldを停止する。今回はもともと停止していた。

Minikubeの起動

Minikubeを起動する。--vm-driver=none--kubernetes-version=v1.11.1を指定する。

以下公式GitHubに仮想マシンドライバなしで起動するスクリプトがあるので参考にする。

rootユーザーで起動

rootユーザーで起動する場合は以下。この場合はkubectlも基本root実行。

sudo -i
minikube start --vm-driver=none --kubernetes-version=v1.11.1

(実行例)

[root@minikube ~]# minikube start --vm-driver=none --kubernetes-version=v1.11.1
Starting local Kubernetes v1.11.1 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading kubeadm v1.11.1
Downloading kubelet v1.11.1
Finished Downloading kubeadm v1.11.1
Finished Downloading kubelet v1.11.1
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
===================
WARNING: IT IS RECOMMENDED NOT TO RUN THE NONE DRIVER ON PERSONAL WORKSTATIONS
    The 'none' driver will run an insecure kubernetes apiserver as root that may leave the host vulnerable to CSRF attacks

When using the none driver, the kubectl config and credentials generated will be root owned and will appear in the root home directory.
You will need to move the files to the appropriate location and then set the correct permissions.  An example of this is below:

    sudo mv /root/.kube $HOME/.kube # this will write over any previous configuration
    sudo chown -R $USER $HOME/.kube
    sudo chgrp -R $USER $HOME/.kube

    sudo mv /root/.minikube $HOME/.minikube # this will write over any previous configuration
    sudo chown -R $USER $HOME/.minikube
    sudo chgrp -R $USER $HOME/.minikube

This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
Loading cached images from config file.
[root@minikube ~]#

centosユーザーでkubecltが実行できるようにしたい場合は設定ファイルをコピーしておく。

sudo cp -r /root/.kube $HOME/.kube
sudo chown -R $USER $HOME/.kube
sudo chgrp -R $USER $HOME/.kube
sudo cp -r /root/.minikube $HOME/.minikube
sudo chown -R $USER $HOME/.minikube
sudo chgrp -R $USER $HOME/.minikube

コピーした.kube/configの中身も修正が必要。/root/.minikube/となっている部分を/home/centos/に書き換えること。

non-rootユーザーで起動

non-rootユーザーで起動する場合は以下。

export MINIKUBE_WANTUPDATENOTIFICATION=false # アップデートの情報を表示しない
export MINIKUBE_WANTREPORTERRORPROMPT=false  # エラーレポートのプロンプトを表示しない
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true        # configファイルを自動的に適切な場所に移動して適切なパーミッションを設定する
mkdir -p $HOME/.kube
mkdir -p $HOME/.minikube
touch $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
sudo -E minikube start --vm-driver=none --kubernetes-version=v1.11.1

(実行例)

[centos@minikube ~]$ sudo -E minikube start --vm-driver=none --kubernetes-version=v1.11.1
Starting local Kubernetes v1.11.1 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading kubeadm v1.11.1
Downloading kubelet v1.11.1
Finished Downloading kubeadm v1.11.1
Finished Downloading kubelet v1.11.1
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
===================
WARNING: IT IS RECOMMENDED NOT TO RUN THE NONE DRIVER ON PERSONAL WORKSTATIONS
    The 'none' driver will run an insecure kubernetes apiserver as root that may leave the host vulnerable to CSRF attacks

Loading cached images from config file.
[centos@minikube ~]$

Minikube環境の削除

Minikubeの起動に失敗した場合など、Minikube環境を削除してやり直す場合は以下でよさそう。

minikube stop
minikube delete
rm -rf $HOME/.minikube/ $HOME/.kube/
sudo rm -rf /etc/kubernetes
sudo rm -rf /data/minikube
sudo rm -rf /var/lib/kubelet
sudo rm -rf /var/lib/kubeadm.yaml
sudo rm -rf /etc/systemd/system/kubelet.service.d

Helmのインストール

Minikubeとは直接関係ないがインストールしておく。

VERSION="v2.11.0"
curl -LO https://storage.googleapis.com/kubernetes-helm/helm-$VERSION-linux-amd64.tar.gz
tar zxvf helm-$VERSION-linux-amd64.tar.gz
sudo cp linux-amd64/helm /usr/local/bin/
rm -rf linux-amd64
rm -f helm-$VERSION-linux-amd64.tar.gz
[centos@minikube ~]$ sudo -i
[root@minikube ~]# helm init
Creating /root/.helm
Creating /root/.helm/repository
Creating /root/.helm/repository/cache
Creating /root/.helm/repository/local
Creating /root/.helm/plugins
Creating /root/.helm/starters
Creating /root/.helm/cache/archive
Creating /root/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /root/.helm.

Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.

Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
Happy Helming!
[root@minikube ~]#
[root@minikube ~]# helm version
Client: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.11.0", GitCommit:"2e55dbe1fdb5fdb96b75ff144a339489417b146b", GitTreeState:"clean"}
[root@minikube ~]#

centosユーザーでもhelmを使えるようにする場合は、centosユーザーでもhelm initを実行する。

[centos@minikube ~]$ helm init
Creating /home/centos/.helm
Creating /home/centos/.helm/repository
Creating /home/centos/.helm/repository/cache
Creating /home/centos/.helm/repository/local
Creating /home/centos/.helm/plugins
Creating /home/centos/.helm/starters
Creating /home/centos/.helm/cache/archive
Creating /home/centos/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /home/centos/.helm.
Warning: Tiller is already installed in the cluster.
(Use --client-only to suppress this message, or --upgrade to upgrade Tiller to the current version.)
Happy Helming!
[centos@minikube ~]$

参考リンク

More documentation around vm-driver=none for local use

8
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
13