minikube部署k8s

Posted czl389

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了minikube部署k8s相关的知识,希望对你有一定的参考价值。

1. 下载安装minikube

最新版(目前是v1.20.0)

https://storage.googleapis.com/minikube/releases/latest/minikube-installer.exe

2. minikube start 命令

  • windows 环境下,minikube需要启动虚拟机,这里使用virtualbox。
minikube start --driver=virtualbox --registry-mirror=https://registry.docker-cn.com,https://shraym0v.mirror.aliyuncs.com --embed-certs=true --image-mirror-country=cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

说明:通过上面的命令启动minikube,会出现有的镜像无法拉取的问题,也就是说registry.cn-hangzhou.aliyuncs.com/google_containers镜像仓库很多镜像不存在,经过不断的测试,使用如下命令就可以正常启动并拉取镜像,所以我们不需要设置这两个参数:–image-mirror-country=cn和 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

  • 成功的命令如下
minikube start --driver=virtualbox --registry-mirror=https://registry.docker-cn.com,https://dmkkj9xm.mirror.aliyuncs.com --embed-certs=true

3. 验证minikube和检查k8s状态

C:\\Windows\\system32>minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
C:\\Windows\\system32>minikube kubectl -- get nodes
NAME       STATUS   ROLES                  AGE   VERSION
minikube   Ready    control-plane,master   28m   v1.20.2
C:\\Windows\\system32>minikube kubectl -- get pods -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-74ff55c5b-mrhr2            1/1     Running   0          28m
kube-system   etcd-minikube                      1/1     Running   0          28m
kube-system   kube-apiserver-minikube            1/1     Running   0          28m
kube-system   kube-controller-manager-minikube   1/1     Running   0          28m
kube-system   kube-proxy-z87px                   1/1     Running   0          28m
kube-system   kube-scheduler-minikube            1/1     Running   0          28m
kube-system   storage-provisioner                1/1     Running   1          28m
C:\\Windows\\system32>minikube kubectl -- get svc -A
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP                  29m
kube-system   kube-dns     ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   29m
C:\\Windows\\system32>minikube kubectl -- get cs -A
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME                 STATUS      MESSAGE                                                                                       ERROR
controller-manager   Unhealthy   Get "http://127.0.0.1:10252/healthz": dial tcp 127.0.0.1:10252: connect: connection refused
scheduler            Unhealthy   Get "http://127.0.0.1:10251/healthz": dial tcp 127.0.0.1:10251: connect: connection refused
etcd-0               Healthy     "health":"true"                       

4. windows 使用kubectl 命令

C:\\Users\\xxx\\.minikube\\cache\\windows\\v1.20.2\\kubectl.exe 拷贝到C:\\Program Files\\Kubernetes\\Minikube目录下即可。 因为后者已经加入环境变量Path, 所以可以直接运行kubectl命令。

C:\\Windows\\system32>kubectl cluster-info
Kubernetes control plane is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

5. 在minikube 虚拟机中使用kubectl 命令

  • CMD运行minikube ssh 即可ssh进入虚拟机。
C:\\Windows\\system32>minikube ssh
                         _             _
            _         _ ( )           ( )
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
/' _ ` _ `\\| |/' _ `\\| || , <  ( ) ( )| '_`\\  /'__`\\
| ( ) ( ) || || ( ) || || |\\`\\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\\___/'(_,__/'`\\____)

$ whoami
docker
$

但是这样有点不方便,可以先获取虚拟机IP和登录方式,然后使用xshell登录。

  • 查询虚拟机ip
C:\\Windows\\system32>minikube ip
192.168.99.100
  • 登录用户为docker(通过whoami 查询得知)

  • ssh登录,需要配置ssh秘钥,秘钥地址在C:\\Users\\xxx\\.minikube\\machines\\minikube\\id_rsa

  • 将kubectl 文件拷贝进虚拟机内

minikube cp C:\\Users\\xxx\\.minikube\\cache\\linux\\v1.20.2\\kubectl /usr/bin/kubectl
  • 将k8s config 拷贝进虚拟机内
minikube cp C:\\Users\\xxx\\.kube\\config /root/.kube/config 

在虚拟内切换root后, 即可执行kubectl

6. 开启dashboard 服务

C:\\Windows\\system32>minikube dashboard
* 正在开启 dashboard ...
  - Using image kubernetesui/dashboard:v2.1.0
  - Using image kubernetesui/metrics-scraper:v1.0.4
* 正在验证 dashboard 运行情况 ...
* Launching proxy ...
* 正在验证 proxy 运行状况 ...
* Opening http://127.0.0.1:54567/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
C:\\Windows\\system32>kubectl get svc -A
NAMESPACE              NAME                        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP   10.96.0.1        <none>        443/TCP                  4h40m
kube-system            kube-dns                    ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   4h40m
kubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   10.106.205.128   <none>        8000/TCP                 2m57s
kubernetes-dashboard   kubernetes-dashboard        ClusterIP   10.108.222.19    <none>        80/TCP                   2m57s

7. 部署一个nginx服务

# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-rmnjp   1/1     Running   0          91s
# kubectl expose deployment nginx --type=NodePort --port=80
service/nginx exposed
# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE     SELECTOR
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        4h45m   <none>
nginx        NodePort    10.99.80.130   <none>        80:30421/TCP   70s     app=nginx

使用虚拟机ip+端口, 访问nginx

# curl -I http://10.0.2.15:30421
HTTP/1.1 200 OK
Server: nginx/1.21.0
Date: Wed, 09 Jun 2021 06:22:01 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 25 May 2021 12:28:56 GMT
Connection: keep-alive
ETag: "60aced88-264"
Accept-Ranges: bytes

8. 删除刚刚部署的nginx服务

# kubectl delete deployment,svc nginx
service "nginx" deleted
deployment.apps "nginx" deleted

以上是关于minikube部署k8s的主要内容,如果未能解决你的问题,请参考以下文章

K8S二进制部署---单节点master

minikube部署k8s

minikube部署k8s

minikube部署k8s

minikube部署k8s

JenkinsJenkins在CentOS和k8s(minikube)上的部署记录