课程分享:
安装kubectl
Kubectl 是 Kubernetes 自带的命令行工具,可以用它直接操作 Kubernetes。
在线方式
macOS,执行:
# using brew https://brew.sh/ brew install kubernetes-cli
Linux,执行:
curl -LO 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 mv kubectl /usr/local/bin/
Windows,执行:
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/windows/amd64/kubectl.exe
离线方式
可以从kubernetes库上直接下载,方法如下:
- step 1: 访问官方github网址:https://github.com/kubernetes/kubernetes/releases
- step 2: 找到想使用的发布版本,在每个发布版本的最后一行有类似“CHANGELOG-1.10.md”这样的内容,点击超链进入;
- step 3: 然后进入“Client Binaries”区域;
- step 4: 选择和目标机器系统匹配的二进制包下载;
- step 5: 解压缩,放入/usr/local/bin目录;
安装minikube
阿里云(推荐)
首先记住阿里云发布的minikube地址:https://github.com/AliyunContainerService/minikube
阿里云提供了最新的Minikube修改版的文件,可以直接下载使用
Mac OSX
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.3.1/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Linux
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.3.1/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
Windows
下载 minikube-windows-amd64.exe 文件,并重命名为 minikube.exe
官网:
- macOS 10.12 (Sierra)
- 要求安装 hypervisor,比如 hyperkit (推荐)或 VirtualBox
- 使用 brew :
brew cask install minikube
- 或者使用 curl:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && sudo install minikube-darwin-amd64 /usr/local/bin/minikube
- Windows 10
- 要求安装 hypervisor,比如 VirtualBox (推荐)或 HyperV
- BIOS 中必须开启 VT-x/AMD-v virtualization
- 使用 chocolatey
choco install minikube
- 或者通过链接下载: Download and run the installer
- Linux
- 要求安装 kvm2 driver (推荐)或 VirtualBox
- BIOS 中必须开启 VT-x/AMD-v virtualization
- 使用 curl:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && sudo install minikube-linux-amd64 /usr/local/bin/minikube
问题1: VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path
CentOS7中安装 VirtualBox
Minikube使用了VirtualBox来启动kubenetes集群;在未安装VirtualBox minikube start时会报如下错误:
Unable to start VM: create: precreate: VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path
建议直接使用yum源方式安装VirtualBox ,配置正确的repo文件即可:
step 1: vim /etc/yum.repos.d/virtualbox.repo
step 2: # 将以下内容复制到文件内保存,之后使用yum命令即可安装VirtualBox的安装。
#具体内容如下:
[virtualbox]
name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox
baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
step 3: #执行以下命令,完成VirtualBox 的安装
yum -y install VirtualBox-5.2
启动minikube
确认你的 minikube 至少是 v1.2.0:
$ minikube version minikube version: v1.2.0
缺省Minikube使用VirtualBox驱动来创建Kubernetes本地环境
minikube start
问题2:Error: [VBOX_VTX_DISABLED] create: precreate: This computer doesn't have VT-X/AMD-v enabled
解决方式:
step 1:关闭虚拟机(注意:是关闭,不是挂起;否则为关机状态下,下图不能勾选!)
step 2 :设置虚拟化Intel VT-x/EPT
注意: 这里我们使用的是 VirtualBox,如果你用的其它,可能会需要另外的配置,请按照上面的链接查找。
[root@localhost ~]# minikube start
* minikube v1.3.1 on Centos 7.6.1810
! Please don't run minikube as root or with 'sudo' privileges. It isn't necessary with virtualbox driver.
* 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.
* Starting existing virtualbox VM for "minikube" ...
* Waiting for the host to be provisioned ...
* Preparing Kubernetes v1.15.2 on Docker 18.09.8 ...
* Downloading kubeadm v1.15.2
* Downloading kubelet v1.15.2
* Relaunching Kubernetes using kubeadm ...
* Waiting for: apiserver proxy etcd scheduler controller dns
* Done! kubectl is now configured to use "minikube"
minikube start -p <集群名字>
默认集群名字是minikube
[root@localhost ~]# minikube start -p K8sTest * [K8sTest] minikube v1.3.1 on Centos 7.2.1511 ! Please don't run minikube as root or with 'sudo' privileges. It isn't necessary with virtualbox driver. * Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers * Creating virtualbox VM (CPUs=2, Memory=2000MB, Disk=20000MB) ... * Preparing Kubernetes v1.15.2 on Docker 18.09.8 ... * Pulling images ... * Launching Kubernetes ... * Waiting for: apiserver proxy etcd scheduler controller dns * Done! kubectl is now configured to use "K8sTest"
通过cat ~/.kube/config 验证多了一个cluster集群
[root@localhost ~]# cat ~/.kube/config apiVersion: v1 clusters: - cluster: certificate-authority: /root/.minikube/ca.crt server: https://192.168.99.101:8443 name: K8sTest - cluster: certificate-authority: /root/.minikube/ca.crt server: https://192.168.99.100:8443 name: minikube contexts: - context: cluster: K8sTest user: K8sTest name: K8sTest - context: cluster: minikube user: minikube name: minikube current-context: K8sTest kind: Config preferences: {} users: - name: K8sTest user: client-certificate: /root/.minikube/client.crt client-key: /root/.minikube/client.key - name: minikube user: client-certificate: /root/.minikube/client.crt client-key: /root/.minikube/client.key
验证
执行下面的命令:
$ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 4m5s v1.15.0
若输出正常,则表示创建成功。
minikube ssh 验证k8s里的服务
但是,但是,minikube start并不代表,整个k8s集群搭建好了,因为k8s里的服务还需要起,比如API server,scheduler,kubelet等等,他们都是以容器的方式在后台启动。
那怎么判断成功了呢?
我们可以通过minikube ssh进到虚机里,然后看看是否有一些container运行起来了
minikube dashboard
在本地运行
minikube dashboard
会在本地弹出浏览器,就是Kubernetes的dashboard,那基本上恭喜您,安装成功了
问题3:error: failed to discover supported resources: Get https://192.168.99.100:8443/api: Service Unavailable
$ minikube start Starting local Kubernetes v1.6.4 cluster... ... $ cat ~/.kube/config apiVersion: v1 clusters: - cluster: certificate-authority: /root/.minikube/ca.crt server: https://192.168.99.100:8443 name: minikube ... $ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8000 error: failed to discover supported resources: Get https://192.168.99.100:8443/api: Service Unavailable
可以调试使用以下步骤:在端口8443
kubectl
会谈kube-apiserver
做它的事。试试curl -k https://192.168.99.100:8443
,看看是否有积极的回应。如果失败,则意味着kube-apiserver
根本没有运行。您可以尝试重新启动虚拟机或重建minikube,以查看它是否第二次正常启动。- 如果你觉得很勇敢,也可以直接调试虚拟机。在这种情况下,获取由minikube启动的虚拟机上的shell。运行
docker ps | grep apiserver
以检查kube-apiserver
吊舱是否正在运行。也请尝试ps aux | grep apiserver
以检查它是否本机运行。如果两者都没有显示结果,请使用journalctl -xef
检查日志。
笔者:遇到这个问题,是重启vmware解决了,如果你有更好的解决方式,也说下,我补充修改
配置镜像加速器
为了拉取Docker Hub镜像,需要为 Docker daemon 配置镜像加速,参考阿里云镜像服务 或中科大镜像加速地址https://docker.mirrors.ustc.edu.cn
,比如
minikube start --registry-mirror=https://xxxxxx.mirror.aliyuncs.com
针对Docker客户端版本大于 1.10.0 的用户
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://uy35zvn6.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
Minikube 命令
[root@localhost ~]# minikube help Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows. Basic Commands: start Starts a local kubernetes cluster status Gets the status of a local kubernetes cluster stop Stops a running local kubernetes cluster delete Deletes a local kubernetes cluster dashboard Access the kubernetes dashboard running within the minikube cluster Images Commands: docker-env Sets up docker env variables; similar to '$(docker-machine env)' cache Add or delete an image from the local cache. Configuration and Management Commands: addons Modify minikube's kubernetes addons config Modify minikube config profile Profile gets or sets the current minikube profile update-context Verify the IP address of the running cluster in kubeconfig. Networking and Connectivity Commands: service Gets the kubernetes URL(s) for the specified service in your local cluster tunnel tunnel makes services of type LoadBalancer accessible on localhost Advanced Commands: mount Mounts the specified directory into minikube ssh Log into or run a command on a machine with SSH; similar to 'docker-machine ssh' kubectl Run kubectl Troubleshooting Commands: ssh-key Retrieve the ssh identity key path of the specified cluster ip Retrieves the IP address of the running cluster logs Gets the logs of the running instance, used for debugging minikube, not user code. update-check Print current and latest version number version Print the version of minikube Other Commands: completion Outputs minikube shell completion for the given shell (bash or zsh) Use "minikube <command> --help" for more information about a given command.
minikube在线使用体验
https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/
参考链接:
https://yq.aliyun.com/articles/221687
https://github.com/caicloud/kube-ladder/edit/master/tutorials/lab1-installation.md
https://blog.csdn.net/java_zyq/article/details/88971407
https://stackoverrun.com/cn/q/12359425