课程分享:
- 课程分享:Docker+Kubernetes(k8s)微服务容器化实践
课程分享:Kubernetes(k8s)生产级实践指南 从部署到核心应用
课程分享:(极客时间)深入剖析Kubernetes
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地址: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)
brew cask install minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && sudo install minikube-darwin-amd64 /usr/local/bin/minikube
Windows 10
choco install minikube
Linux
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && sudo install minikube-linux-amd64 /usr/local/bin/minikube
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 至少是 v1.2.0:
$ minikube version
minikube version: v1.2.0
缺省Minikube使用VirtualBox驱动来创建Kubernetes本地环境
minikube start
解决方式:
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' 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&#64;localhost ~]# minikube start -p K8sTest
* [K8sTest] minikube v1.3.1 on Centos 7.2.1511
! Please don&#39;t run minikube as root or with &#39;sudo&#39; privileges. It isn&#39;t necessary with virtualbox driver.
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Creating virtualbox VM (CPUs&#61;2, Memory&#61;2000MB, Disk&#61;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"
[root&#64;localhost ~]# cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:certificate-authority: /root/.minikube/ca.crtserver: https://192.168.99.101:8443name: K8sTest
- cluster:certificate-authority: /root/.minikube/ca.crtserver: https://192.168.99.100:8443name: minikube
contexts:
- context:cluster: K8sTestuser: K8sTestname: K8sTest
- context:cluster: minikubeuser: minikubename: minikube
current-context: K8sTest
kind: Config
preferences: {}
users:
- name: K8sTestuser:client-certificate: /root/.minikube/client.crtclient-key: /root/.minikube/client.key
- name: minikubeuser:client-certificate: /root/.minikube/client.crtclient-key: /root/.minikube/client.key
执行下面的命令&#xff1a;
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 4m5s v1.15.0
若输出正常&#xff0c;则表示创建成功。
但是&#xff0c;但是&#xff0c;minikube start并不代表&#xff0c;整个k8s集群搭建好了&#xff0c;因为k8s里的服务还需要起&#xff0c;比如API server&#xff0c;scheduler&#xff0c;kubelet等等&#xff0c;他们都是以容器的方式在后台启动。
那怎么判断成功了呢&#xff1f;
我们可以通过minikube ssh进到虚机里&#xff0c;然后看看是否有一些container运行起来了
minikube dashboard
在本地运行
minikube dashboard
会在本地弹出浏览器&#xff0c;就是Kubernetes的dashboard&#xff0c;那基本上恭喜您&#xff0c;安装成功了
$ 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&#61;gcr.io/google_containers/echoserver:1.4 --port&#61;8000 error: failed to discover supported resources: Get https://192.168.99.100:8443/api: Service Unavailable
可以调试使用以下步骤&#xff1a;在端口8443
kubectl
会谈kube-apiserver
做它的事。试试curl -k https://192.168.99.100:8443
&#xff0c;看看是否有积极的回应。如果失败&#xff0c;则意味着kube-apiserver
根本没有运行。您可以尝试重新启动虚拟机或重建minikube&#xff0c;以查看它是否第二次正常启动。
如果你觉得很勇敢&#xff0c;也可以直接调试虚拟机。在这种情况下&#xff0c;获取由minikube启动的虚拟机上的shell。运行docker ps | grep apiserver
以检查kube-apiserver
吊舱是否正在运行。也请尝试ps aux | grep apiserver
以检查它是否本机运行。如果两者都没有显示结果&#xff0c;请使用journalctl -xef
检查日志。
笔者&#xff1a;遇到这个问题&#xff0c;是重启vmware解决了&#xff0c;如果你有更好的解决方式&#xff0c;也说下&#xff0c;我补充修改
为了拉取Docker Hub镜像&#xff0c;需要为 Docker daemon 配置镜像加速&#xff0c;参考阿里云镜像服务 或中科大镜像加速地址https://docker.mirrors.ustc.edu.cn
&#xff0c;比如
minikube start --registry-mirror&#61;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 <<-&#39;EOF&#39;
{"registry-mirrors": ["https://uy35zvn6.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Minikube 命令
[root&#64;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 clusterstatus Gets the status of a local kubernetes clusterstop Stops a running local kubernetes clusterdelete Deletes a local kubernetes clusterdashboard Access the kubernetes dashboard running within the minikube clusterImages Commands:docker-env Sets up docker env variables; similar to &#39;$(docker-machine env)&#39;cache Add or delete an image from the local cache.Configuration and Management Commands:addons Modify minikube&#39;s kubernetes addonsconfig Modify minikube configprofile Profile gets or sets the current minikube profileupdate-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 clustertunnel tunnel makes services of type LoadBalancer accessible on localhostAdvanced Commands:mount Mounts the specified directory into minikubessh Log into or run a command on a machine with SSH; similar to &#39;docker-machine ssh&#39;kubectl Run kubectlTroubleshooting Commands:ssh-key Retrieve the ssh identity key path of the specified clusterip Retrieves the IP address of the running clusterlogs Gets the logs of the running instance, used for debugging minikube, not user code.update-check Print current and latest version numberversion Print the version of minikubeOther Commands:completion Outputs minikube shell completion for the given shell (bash or zsh)Use "minikube
minikube在线使用体验
https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/
参考链接&#xff1a;
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