minikube使用笔记
先说环境:
IP | 系统 | 备注 | 其它 | |
Server | 192.168.1.99 | centos 7.6 | 淘汰的破本子 | |
Client | 192.168.1.11 | Win 10 | 当前用的本子 |
启动minikube
$ minikube start * Centos 7.6.1810 上的 minikube v1.13.0 * 根据现有的配置文件使用 docker 驱动程序 * Starting control plane node minikube in cluster minikube * Restarting existing docker container for "minikube" ... * 正在 Docker 19.03.8 中准备 Kubernetes v1.19.0… * Verifying Kubernetes components... * Enabled addons: dashboard, default-storageclass, metrics-server, storage-provisioner * Done! kubectl is now configured to use "minikube" by default
运行dashboard
$ minikube dashboard * 正在验证 dashboard 运行情况 ... * Launching proxy ... * 正在验证 proxy 运行状况 ... * Opening http://127.0.0.1:35967/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser... - http://127.0.0.1:35967/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
由于每于运行dashboard,端口都会变,不方便nginx做转发, 所以我直接指定了一个固定的端口8080
并指定address与accept-hosts,这样的话,在局域网内的其它电脑也可以访问了
$ kubectl proxy --port=8080 --address=0.0.0.0 --accept-hosts='^*$' Starting to serve on [::]:8080
接着在Client电脑上修改hosts文件,添加以下这条
192.168.1.99 k8s.peter.com
在Server中配置好nginx转发
$ cat k8s.conf server { listen 80; server_name k8s.peter.com; index index.html; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/; } }
最后就可以在Client通过浏览器打开以下URL访问Dashboard了
http://k8s.peter.com/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
在minikube集群中运行一个nginx服务
1.创建发布文件nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2.执行发布命令
kubectl apply -f nginx-deploy.yaml
3.查看结果
$ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-585449566-tlznq 1/1 Running 0 8m56s 172.18.0.3 minikube <none> <none>
4.创建service发布文件nginx-svc.yaml
type用的是NodePort,端口是31000
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
type: NodePort
ports:
- port: 8001
targetPort: 80
protocol: TCP
nodePort: 31000
5.创建Service
kubectl apply -f nginx-svc.yaml
6.查看结果
$ kubectl get svc nginx -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR nginx NodePort 10.106.146.47 <none> 8001:31000/TCP 12m app=nginx
7.在Server中通过curl测试nginx是否可以访问,其中的$(minikube service nginx --url)用来获取上面创建的service的访问地址
很明显,我们已经可以在访问nginx服务了
$ curl $(minikube service nginx --url) <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
如果只是在Server机器上访问nginx是不够的,如果希望在Client机器上访问nginx服务那应该怎么办呢.
方法1:我们可以在Server这台机器中通过nginx转发,来访问minikube集群中的nginx服务
nginx配置文件如下
server { listen 80; server_name k8s.peter.com; index index.html; location / { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/; } location /nginx/ { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://172.17.0.2:31000/; } }
然后就可以在浏览器中通过http://k8s.peter.com/nginx来访问了
方法2:在Client这台机器中,通过ssh端口转发,也是可以的,下面我们来试试:
首先,在Client这台机器中执行ssh执行端口转发命令
ssh -N -L 31000:172.17.0.2:31000 root@192.168.1.99
这样我们就可以在Client这台机器上通过http://127.0.0.1:31000来访问minikube集群中的nginx服务了