LoginSignup
2
0

More than 5 years have passed since last update.

Minikube on Mac(Pod編)

Last updated at Posted at 2019-04-14

Minikube on Mac(Pod編)

Mac に Minikube を導入して色々と試してみたメモです。とりあえず MySQL Pod を動かすところまで。

環境

  • macOS Mojave 10.14.4
  • Docker Desktop 2.0.0.3
  • kubernetes-cli 1.14.0
    • brew install kubernetes-cli
  • minikube 1.0.0
    • brew cask install minikube

Minikube とは

Minikube とは 1 Node(Master Node と Worker Node を兼務)構成のシンプルな k8s Cluster をローカルで動作させるツールです。macOS だと Node を VirtualBox で動かすか HyperKit で動かすかの二択になると思いますが、今回は HyperKit で動作させます。

HyperKit

HyperKit は xhyve/bhyve というハイパーバイザを内包する仮想化ツールで、xhyve は OS X 10.10 Yosemite 以降に搭載された Hypervisor.framework 上で動く hypervisor らしい。

さっそく実践:Pod を立てる

早速 Minikube Cluster を作成します。--vm-driver で HyperKit を指定。

NOTE:~ ohno$ sudo minikube start --vm-driver=hyperkit
😄  minikube v1.0.0 on darwin (amd64)
🤹  Downloading Kubernetes v1.14.0 images in the background ...
🔥  Creating hyperkit VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
💿  Downloading Minikube ISO ...
 142.88 MB / 142.88 MB [============================================] 100.00% 0s
📶  "minikube" IP address is 192.168.65.9
🐳  Configuring Docker as the container runtime ...
🐳  Version of container runtime is 18.06.2-ce
⌛  Waiting for image downloads to complete ...
✨  Preparing Kubernetes environment ...
💾  Downloading kubeadm v1.14.0
💾  Downloading kubelet v1.14.0
🚜  Pulling images required by Kubernetes v1.14.0 ...
🚀  Launching Kubernetes v1.14.0 using kubeadm ...
⌛  Waiting for pods: apiserver proxy etcd scheduler controller dns
🔑  Configuring cluster permissions ...
🤔  Verifying component health .....
💗  kubectl is now configured to use "minikube"
🏄  Done! Thank you for using minikube!

起動後の状態。

NOTE:~ ohno$ sudo minikube status
host: Running
kubelet: Running
apiserver: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.65.9
NOTE:~ ohno$ sudo kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   12m   v1.14.0

Minikube Node の実体は HyperKit で動作している仮想マシンで boot2docker という軽量 Linux ディストリビューションが利用されています。minikube ssh で仮想マシンにログインできます。Minikube の hostPath volume はこの VM 上のファイル・ディレクトリになります。(Mac のローカルでは無い)

NOTE:~ ohno$ sudo minikube ssh
                         _             _
            _         _ ( )           ( )
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)

$ uname -a
Linux minikube 4.15.0 #1 SMP Tue Mar 26 02:53:14 UTC 2019 x86_64 GNU/Linux

早速 Pod を作っていきます。今回は MySQL コンテナに PersistentVolume をマウントする感じで Pod を立てます。マニフェストはこんな感じです。

mysql-pod-hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  volumes:
  - name: mysql-data
    persistentVolumeClaim:
      claimName: mysql-pvc
  containers:
  - image: mysql:latest
    name: mysql
    volumeMounts:
    - name: mysql-data
      mountPath: /var/lib/mysql
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: password
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  accessModes:
    - ReadWriteOnce
    - ReadOnlyMany
  storageClassName: standard
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: /data/mysql-data/
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  resources:
    requests:
      storage: 1Gi
  accessModes:
  - ReadWriteOnce
  storageClassName: standard

マニフェストを apply してリソースを作成。

NOTE:~ ohno$ sudo kubectl apply -f mysql-pod-hostpath.yaml
pod/mysql created
persistentvolume/mysql-pv created
persistentvolumeclaim/mysql-pvc created
NOTE:~ ohno$ sudo kubectl get pods
NAME    READY   STATUS              RESTARTS   AGE
mysql   0/1     ContainerCreating   0          10s
NOTE:~ ohno$ sudo kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
mysql   1/1     Running   0          18s

接続確認

立てた Pod に接続してみます。kubectl port-forward でローカルポートを Pod に転送するようにします。

NOTE:~ ohno$ sudo kubectl port-forward pods/mysql 3306:3306
Password:
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306

ここではクライアントに MySQL Workbench をインストールして CLI 用に PATH を通しています。

NOTE:~ ohno$ export PATH=$PATH:/Applications/MySQLWorkbench.app/Contents/MacOS

mysql で接続。

NOTE:~ ohno$ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

無事つながりました。

2
0
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
2
0