如果您想快速搭建k8s环境进行学习和开发,可以通过Docker快速完成Minikube(单节点的k8s)的部署,通过Minikube体验各类K8S的基础服务;
以下是本次实战的环境信息,Windows环境下的docker也可以按照以下步骤进行:
至此,Minikube安装和启动成功,可以用kubectl来操作了,接下来我们把图形化页面也装好;
kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
命令执行成功后,控制台输出如下:
secret "kubernetes-dashboard-certs" created
secret "kubernetes-dashboard-csrf" created
serviceaccount "kubernetes-dashboard" created
role.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
rolebinding.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created
deployment.apps "kubernetes-dashboard" created
service "kubernetes-dashboard" created
NAME READY STATUS RESTARTS AGE
etcd-docker-for-desktop 1/1 Running 0 3h
kube-apiserver-docker-for-desktop 1/1 Running 0 3h
kube-controller-manager-docker-for-desktop 1/1 Running 0 3h
kube-dns-86f4d74b45-ms6dr 3/3 Running 0 3h
kube-proxy-fflkn 1/1 Running 0 3h
kube-scheduler-docker-for-desktop 1/1 Running 1 3h
kubernetes-dashboard-669f9bbd46-m9qkw 1/1 Running 0 2m
如果您看到的Pod状态不全是Running,请稍作等待,直到状态全部变成Running; 3. 上面的命令显示有个名为kubernetes-dashboard-669f9bbd46-m9qkw的Pod,这就是图形化页面应用的Pod名,执行以下命令,将本机8443端口的请求都转发到该Pod的8443端口,这样我们就能访问到此Pod提供的图形化页面的web服务了:
nohup kubectl port-forward kubernetes-dashboard-669f9bbd46-m9qkw 8443:8443 --namespace=kube-system &
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | awk '/^deployment-controller-token-/{print $1}') | awk '$1=="token:"{print $2}'
执行上述命令后,在控制台返回的内容就是登录dashboard用的Token,我这里得到的是eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkZXBsb3ltZW50LWNvbnRyb2xsZXItdG9rZW4tZnJrNnoiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGVwbG95bWVudC1jb250cm9sbGVyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNjdhNGJhN2MtN2VhZC0xMWU5LWJiOWUtMDI1MDAwMDAwMDAxIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRlcGxveW1lbnQtY29udHJvbGxlciJ9.CVjhOGJyuSSRDd1zfLd-ZvRMPcPXmSSIUDPdomZYcR8-kYZBks4BBMMm-V8HExtoyVpqc4_lhjE04AmzvH951pd81O6IgV5UAIS89XwFBrrpo1RqbTUOcQqKBxhldfmerU4Ggtiv3y6C3mZgIop1sY0-3C6wsh9pNLaFAULSf2iYDtWIpNIEbdBseSW3ARBjEm_Yed1XXzGwC1D5Yy1G-BlzjwuujxgbvcqyvsgC3A8eKRU1wQjA1LvbtNE2LcZvlefGbV7WT7HDTDroxcMcEEI7PeHwEuzs8DL_Dhi-GZZ3z7PnXowjkwx41xNEzKXffAWI8CPs2f80ntfB2W5Drg
记下这一长串内容,稍后会用到;
接下来试着创建Pod和Service,验证这个Minikube环境是否能用来作为学习和开发环境:
kubectl run my-tomcat --image=tomcat:7.0.94-jre7-alpine --replicas=2 --port=8080
kubectl expose deployment my-tomcat --port=8080 --target-port=8080 --external-ip=192.168.50.7 --type=LoadBalancer
至此,Minikube及其dashboard的安装和体验都完成了,如果您想体验k8s的基本功能,希望本文能给你一些参考。