目前使用k8s 要么用的物理機搭建的環境,要么就是使用docker for mac 中kubernetes 的特性,為了本地調試方便,使用下minikube
minukube 包含的特性
- 負載均衡器
- 多集群
- nodeports 支持
- 持久化數據卷
- ingress
- dashboard
- 執行容器運行時
- 配置apiserver 以及kubelet選項
- addons 支持
- nvidia GPU 支持
- 文件系統掛載
下載安裝包
直接可以在github 下載
配置環境變量
配置到path 路徑即可
啟動
minukube 實際上是kubeadm 安裝的,同時minikube 在啟動的時候我們可以配置比較多的參數,比如執行鏡像源,配置docker 加速
配置主機內存的大小
- 啟動命令
minikube start --help
幫助內容
Starts a local kubernetes cluster
Options:
--apiserver-ips=[]: A set of apiserver IP Addresses which are used in the generated
certificate for kubernetes. This can be used if you want to make the apiserver available from
outside the machine
--apiserver-name='minikubeCA': The apiserver name which is used in the generated certificate
for kubernetes. This can be used if you want to make the apiserver available from outside the
machine
--apiserver-names=[]: A set of apiserver names which are used in the generated certificate for
kubernetes. This can be used if you want to make the apiserver available from outside the machine
--apiserver-port=8443: The apiserver listening port
--cache-images=true: If true, cache docker images for the current bootstrapper and load them
into the machine. Always false with --vm-driver=none.
--container-runtime='docker': The container runtime to be used (docker, crio, containerd)
--cpus=2: Number of CPUs allocated to the minikube VM
--cri-socket='': The cri socket path to be used
--disable-driver-mounts=false: Disables the filesystem mounts provided by the hypervisors
--disk-size='20000mb': Disk size allocated to the minikube VM (format: <number>[<unit>], where
unit = b, k, m or g)
--dns-domain='cluster.local': The cluster dns domain name used in the kubernetes cluster
--dns-proxy=false: Enable proxy for NAT DNS requests (virtualbox)
--docker-env=[]: Environment variables to pass to the Docker daemon. (format: key=value)
--docker-opt=[]: Specify arbitrary flags to pass to the Docker daemon. (format: key=value)
--download-only=false: If true, only download and cache files for later use - don't install or
start anything.
--enable-default-cni=false: Enable the default CNI plugin (/etc/cni/net.d/k8s.conf). Used in
conjunction with "--network-plugin=cni"
--extra-config=: A set of key=value pairs that describe configuration that may be passed to
different components.
The key should be '.' separated, and the first part before the dot is the component to apply the
configuration to.
Valid components are: kubelet, kubeadm, apiserver, controller-manager, etcd, proxy, scheduler
Valid kubeadm parameters: ignore-preflight-errors, dry-run, kubeconfig, kubeconfig-dir, node-name,
cri-socket, experimental-upload-certs, certificate-key, rootfs, pod-network-cidr
--feature-gates='': A set of key=value pairs that describe feature gates for
alpha/experimental features.
--host-dns-resolver=true: Enable host resolver for NAT DNS requests (virtualbox)
--host-only-cidr='192.168.99.1/24': The CIDR to be used for the minikube VM (only supported
with Virtualbox driver)
--hyperkit-vpnkit-sock='': Location of the VPNKit socket used for networking. If empty,
disables Hyperkit VPNKitSock, if 'auto' uses Docker for Mac VPNKit connection, otherwise uses the
specified VSock.
--hyperkit-vsock-ports=[]: List of guest VSock ports that should be exposed as sockets on the
host (Only supported on with hyperkit now).
--hyperv-virtual-switch='': The hyperv virtual switch name. Defaults to first found. (only
supported with HyperV driver)
--image-mirror-country='': Country code of the image mirror to be used. Leave empty to use the
global one. For Chinese mainland users, set it to cn
--image-repository='': Alternative image repository to pull docker images from. This can be
used when you have limited access to gcr.io. Set it to "auto" to let minikube decide one for you.
For Chinese mainland users, you may use local gcr.io mirrors such as
registry.cn-hangzhou.aliyuncs.com/google_containers
--insecure-registry=[]: Insecure Docker registries to pass to the Docker daemon. The default
service CIDR range will automatically be added.
--iso-url='https://storage.googleapis.com/minikube/iso/minikube-v1.3.0.iso': Location of the
minikube iso
--keep-context=false: This will keep the existing kubectl context and will create a minikube
context.
--kubernetes-version='v1.15.2': The kubernetes version that the minikube VM will use (ex:
v1.2.3)
--kvm-gpu=false: Enable experimental NVIDIA GPU support in minikube
--kvm-hidden=false: Hide the hypervisor signature from the guest in minikube
--kvm-network='default': The KVM network name. (only supported with KVM driver)
--kvm-qemu-uri='qemu:///system': The KVM QEMU connection URI. (works only with kvm2 driver on
linux)
--memory='2000mb': Amount of RAM allocated to the minikube VM (format: <number>[<unit>], where
unit = b, k, m or g)
--mount=false: This will start the mount daemon and automatically mount files into minikube
--mount-string='/Users:/minikube-host': The argument to pass the minikube mount command on
start
--network-plugin='': The name of the network plugin
--nfs-share=[]: Local folders to share with Guest via NFS mounts (Only supported on with
hyperkit now)
--nfs-shares-root='/nfsshares': Where to root the NFS Shares (defaults to /nfsshares, only
supported with hyperkit now)
--no-vtx-check=false: Disable checking for the availability of hardware virtualization before
the vm is started (virtualbox)
--registry-mirror=[]: Registry mirrors to pass to the Docker daemon
--service-cluster-ip-range='10.96.0.0/12': The CIDR to be used for service cluster IPs.
--uuid='': Provide VM UUID to restore MAC address (only supported with Hyperkit driver).
--vm-driver='virtualbox': VM driver is one of: [virtualbox parallels vmwarefusion hyperkit
vmware]
--wait=true: Wait until Kubernetes core services are healthy before exiting
Usage:
minikube start [flags] [options]
- 啟動
從上邊的幫助上我們可以看出還是比較全的參數控制,啟動也比較簡單
minikube start --image-mirror-country=cn --registry-mirror=https://b3uey254.mirror.aliyuncs.com
效果
😄 minikube v1.3.1 on Darwin 10.13.6
✅ Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
💡 Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
🏃 Using the running virtualbox "minikube" VM
⌛ Waiting for the host to be provisioned
🐳 Preparing Kubernetes v1.15.2 on Docker 18.09.8
啟動ingress && 運行一個簡單的demo
- 通過addons 的方式啟動ingress
minikube addons enable ingress
- 運行簡單deploy(nginx服務)
kubectl run nginx --image=nginx --port=80
- 暴露服務
kubectl expose deployment nginx --port=80 --target-port=80
- 創建ingress
yaml 定義 ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx
spec:
rules:
- host: mynginx.dalong.com
http:
paths:
- path: /
backend:
serviceName: nginx
servicePort: 80
運行
kubectl apply -f ingress.yaml
- 配置本機host
獲取minikube ip
minikube ip
寫入host 文件
比如我的
192.168.99.102 mynginx.dalong.com
- 訪問效果
minukube 幾個方便的 命令
- dashboard
minikube dashboard

效果
- ssh
登陸進kuberenete 的環境
minukue ssh
效果
- 獲取minikube ip
ip 可以用來快速訪問環境
minikube ip
- 其他命令
比如mount 進行文件掛載logs 查看日志
說明
使用mnikube 對於開發者是比較方便的,本地可以隨意部署kuberntes 環境並銷毀,同時官方也比較人性,考慮了中國的實際情況,同時對於依賴的鏡像
minikube 會cache,位置在~/.minikube/cache/images/
對於我們來說可以快速的啟動kubernetes 環境
參考資料
https://github.com/kubernetes/minikube
https://minikube.sigs.k8s.io/docs/start/