一拳搞定 Kubernetes | 安装 Minikube
本文介绍如何在本机运行 Minikube 来安装 kubernetes 集群.
一、前言
本篇是整个 kubernetes 的开篇,只有安装好了 kubernetes 集群,我们才能愉快的在容器编排的海洋畅游。
为何选择 minikube?因为它只需要按照官方文档执行简单的命令就可以运行,上手简单,推荐使用。
二、Minikube 操作
2-1. 暂停 Minikube
1minikube stop
输出:
1* Stopping "minikube" in hyperkit ...
2* "minikube" 已停止
2-2. 启动
1minikube start
输出:
1* Darwin 10.15.6 上的 minikube v1.6.2
2 - KUBECONFIG=/Users/tc/.kube/config.22
3* Selecting 'hyperkit' driver from existing profile (alternates: [])
4* Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
5* Starting existing hyperkit VM for "minikube" ...
6* Waiting for the host to be provisioned ...
7^[* 正在 Docker '19.03.5' 中准备 Kubernetes v1.17.0…
8* 正在启动 Kubernetes ...
9* 完成!kubectl 已经配置至 "minikube"
2-3. 启动 dashboard
1minikube dashboard
输出:
1* Verifying dashboard health ...
2* Launching proxy ...
3* Verifying proxy health ...
4* Opening http://127.0.0.1:58368/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
会自动跳转到浏览器:
2-4. 查看 service 的 URL
1minikube service hello-minikube --url
2-5. 删除 Minikube 集群
1minikube delete
输出:
1* Deleting "minikube" ...
2* The "minikube" cluster has been deleted.
三、kubernetes 操作
创建 deployment
1kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
2kubectl expose deployment hello-minikube --type=NodePort --port=8080
请注意看这个 --type=NodePort
,它表示了服务暴露的模式,也就是你要访问到 k8s 里面一个运行程序的途径。
四、补充说明
3-1. 不能暴露 service
当创建的 service 不加 --type=NodePort
,在 minikube 获取不到 service url。
1minikube service nginx-deployment --namespace=test --url
输出:
1|-----------|------------------|-------------|--------------|
2| NAMESPACE | NAME | TARGET PORT | URL |
3|-----------|------------------|-------------|--------------|
4| test | nginx-deployment | | No node port |
5|-----------|------------------|-------------|--------------|
6* service test/nginx-deployment has no node port
正常情况输出:
1http://192.168.64.3:31022
请求这个地址:
1curl http://192.168.64.3:31022
2
3<!DOCTYPE html>
4<html>
5<head>
6<title>Welcome to nginx!</title>
7<style>
8 body {
9 width: 35em;
10 margin: 0 auto;
11 font-family: Tahoma, Verdana, Arial, sans-serif;
12 }
13</style>
14</head>
15<body>
16<h1>Welcome to nginx!</h1>
17<p>If you see this page, the nginx web server is successfully installed and
18working. Further configuration is required.</p>
19
20<p>For online documentation and support please refer to
21<a href="http://nginx.org/">nginx.org</a>.<br/>
22Commercial support is available at
23<a href="http://nginx.com/">nginx.com</a>.</p>
24
25<p><em>Thank you for using nginx.</em></p>
26</body>
27</html>
有输出,表示请求到了 k8s 集群部署的 nginx。
3-2. dashboard 打不开了
在电脑从公司带回家再次来到公司的时候,出现了一些网络问题,比如 dashboard 无法打开,在浏览器回车 dashboard 地址, 类似:http://127.0.0.1:63999/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/service?namespace=default 后返回:
1{
2 "kind": "Status",
3 "apiVersion": "v1",
4 "metadata": {
5
6 },
7 "status": "Failure",
8 "message": "no endpoints available for service \"http:kubernetes-dashboard:\"",
9 "reason": "ServiceUnavailable",
10 "code": 503
11}
这时候建议 stop minikube 后再重新启动.
3-3. kubeconfig
如上面启动时候的输出一份配置:
- KUBECONFIG=/Users/tc/.kube/config.22
这里 KUBECONFIG 是可以通过环境变量指定的,比如我本地:
export KUBECONFIG=/Users/tc/.kube/config.22
里面的内容如下:
1cat /Users/tc/.kube/config.22
2
3apiVersion: v1
4clusters:
5- cluster:
6 certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURBVENDQWVtZ0F3SUJBZ0lKQU91U0IwYkU3VGIrTUEwR0NTcUdTSWIzRFFFQkN3VUFNQmN4RlRBVEJnTlYKQkFNTURERXdMakUxTWk0eE9ETXVNVEFlRncweE9URXlNekF3TmpV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wxaytoYnB3ZjRreVZrNzNRZWdWTGZaK3Q3ZUZRVUpCMDR2V1RSdmtQcVdoTWhqQXRTR1FObG80TVJ3eklWCit2TDUyVGcxZjNXb09LRldyQkQzM1MzOGhLWlpJbzVKUDlPMmxDUU9VRXhRVzZEcmN0UEp0bmhRbHdEUjQ0a04KL1FxNmFNZz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
7 server: https://30.11.44.31:16443
8 name: microk8s-cluster
9- cluster:
10 certificate-authority: /Users/tc/.minikube/ca.crt
11 server: https://192.168.64.3:8443
12 name: minikube
13contexts:
14- context:
15 cluster: microk8s-cluster
16 user: admin
17 name: microk8s
18- context:
19 cluster: minikube
20 user: minikube
21 name: minikube
22current-context: minikube
23kind: Config
24preferences: {}
25users:
26- name: admin
27 user:
28 password: d0F2R1hDc2RYUEgxWFA4RGtpWVdFQldHM2xrU0Q2M3czZ3ovK3YzNXJxaz0K
29 username: admin
30- name: minikube
31 user:
32 client-certificate: /Users/tc/.minikube/client.crt
33 client-key: /Users/tc/.minikube/client.key
也可以执行命令 kubectl config view
是一样的效果。
如果要访问多个 k8s 集群,可以通过在 kubectl
命令加上 --kubeconfig=[位置]
来实现。
五、参考
https://kubernetes.io/zh/docs/setup/learning-environment/minikube/
https://minikube.sigs.k8s.io/docs/start/
https://kubernetes.io/zh/docs/concepts/configuration/organize-cluster-access-kubeconfig/