Mac系统上搭建Kubernetes环境

准备

大家在安装Kubernetes的时候,大多数人都遇到了一直处理starting的状态。其实都是因为依赖的docker images不存在。而且由于墙等问题的存在,大家可以安装之前提前把镜像下载好。

我这里找到了很多的文档,找齐了我这边能支持跑起来的image

➜  kubernetes  docker images
REPOSITORY                                                                TAG  
k8s.gcr.io/kube-apiserver                                                 v1.16.0  
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver        v1.16.0  
k8s.gcr.io/kube-proxy                                                     v1.16.0  
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy            v1.16.0  
k8s.gcr.io/kube-proxy                                                     v1.14.6  
k8s.gcr.io/kube-apiserver                                                 v1.14.6  
k8s.gcr.io/kube-scheduler                                                 v1.14.6  
k8s.gcr.io/kube-controller-manager                                        v1.14.6  
docker/kube-compose-controller                                            v0.4.23  
docker/kube-compose-api-server                                            v0.4.23  
k8s.gcr.io/coredns                                                        1.3.1  
k8s.gcr.io/kubernetes-dashboard-amd64                                     v1.10.1  
k8s.gcr.io/etcd                                                           3.3.10  
k8s.gcr.io/kube-proxy-amd64                                               v1.10.11  
k8s.gcr.io/kube-apiserver-amd64                                           v1.10.11  
k8s.gcr.io/kube-controller-manager-amd64                                  v1.10.11  
k8s.gcr.io/kube-scheduler-amd64                                           v1.10.11  
docker/kube-compose-controller                                            v0.4.12  
docker/kube-compose-api-server                                            v0.4.12  
k8s.gcr.io/etcd-amd64                                                     3.1.12  
k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64                                    1.14.8  
k8s.gcr.io/k8s-dns-sidecar-amd64                                          1.14.8  
k8s.gcr.io/k8s-dns-kube-dns-amd64                                         1.14.8  
k8s.gcr.io/pause-amd64                                                    3.1  
k8s.gcr.io/pause                                                          3.1  
k8s.gcr.io/storage-provisioner                                            v1.8.1  
registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner   v1.8.1  

再启动,等一会儿就应该能看到Docker与Kubernetes都起来了。

http://static.cyblogs.com/WX20191015-184532@2x.png

创建kubernetes-dashboard

接下来我们可以使用 kubectl 命令来创建简单的 kubernetes-dashboard 服务:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml  
# 这个可以把文件下载下来,后面就可以本地了。

kubectl apply -f /Users/chenyuan/Tools/Docker/kubernetes/kubernetes-dashboard.yaml

secret "kubernetes-dashboard-certs" created  
serviceaccount "kubernetes-dashboard" created  
role "kubernetes-dashboard-minimal" created  
rolebinding "kubernetes-dashboard-minimal" created  
deployment "kubernetes-dashboard" created  
service "kubernetes-dashboard" created  

服务安装完毕后可以查看部署的容器与服务:

➜  kubernetes  kubectl get deployments --namespace kube-system
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE  
coredns                2/2     2            2           4d3h  
kubernetes-dashboard   1/1     1            1           3d8h  
➜  kubernetes  kubectl get services --namespace kube-system
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE  
kube-dns               ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP   4d3h  
kubernetes-dashboard   ClusterIP   10.96.229.197   <none>        443/TCP                  4d2h  

启动dashboard

在 Dashboard 启动完毕后,可以使用 kubectl 提供的 Proxy 服务来访问该面板

kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'  

启动服务后,不要切断控制台,不然服务就中断了。

浏览器输入:

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/  

http://static.cyblogs.com/WX20191015-182857@2x.png

获取Token

然后并没有跳过的按钮,所以必须通过Kubeconfig或者Token的方式。

我这里是通过Token,那我们怎么知道Token的值是多少呢?

➜  kubernetes  kubectl get secret -n=kube-system
NAME                                             TYPE                                  DATA   AGE  
...
default-token-sznp4                              kubernetes.io/service-account-token   3      4d3h  
...
# 这里只列出default-token-sznp4

获取Token值,然后把得到的值输入进去就可以了。

➜  kubernetes  kubectl describe secret -n=kube-system default-token-sznp4
Name:         default-token-sznp4  
Namespace:    kube-system  
Labels:       <none>  
Annotations:  kubernetes.io/service-account.name: default  
              kubernetes.io/service-account.uid: 064afefb-ebf6-11e9-ac8c-025000000001

Type:  kubernetes.io/service-account-token

Data  
====
ca.crt:     1025 bytes  
namespace:  11 bytes  
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkZWZhdWx0LXRva2VuLXN6bnA0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImRlZmF1bHQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIwNjRhZmVmYi1lYmY2LTExZTktYWM4Yy0wMjUwMDAwMDAwMDEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06ZGVmYXVsdCJ9.u5HTqt7A_4H_0f9ny-AgfmWNo7TSWZsRpjXot1iN8G6oOnt4uDQiS_kiUduwtqqeYC2hjZ2yKPt0NNML9Op1RSAEuTkXiRvJxnCX8GjQeqCD4lzXeqqQ9mTxCVlGijJLaP5VJ2qQtLM0Gwt9eJCYxugGqqHqys7QXdPzcH3WESno0tXNt25klC5ZXNFSeyE-AqLpP3SjmW7W6IBHx89uY28SXmdvTjnCuZyaBlpkgOensdMS7-BpycTzq63NIcp5TR7tM3AdHjsUlSJ2D9YqW_xzMcEDncmjKpbVJ6W9w494L-Z0dOjHkI7gaQSE2Bwi6AqCaGEWKTgMCSWmIBfkrg  

通过compose的case启动服务

去Github找了一个Demo,跑几个服务起来。案例地址:git@github.com:docker/compose-on-kubernetes.git

我把其中的案例copy到了我自己的目录,大概是这样子。

➜  kubernetes  tree -L 2
.
├── config-exercise
│   └── config-demo
├── db
│   ├── Dockerfile
│   └── words.sql
├── docker-compose.yml
├── kubernetes-dashboard.yaml
├── web
│   ├── Dockerfile
│   ├── dispatcher.go
│   └── static
└── words
    ├── Dockerfile
    ├── pom.xml
    └── src

着重看一下docker-compose内容。

➜  kubernetes  cat docker-compose.yml
version: '3.3'

services:  
  web:
    build: web
    image: dockerdemos/lab-web
    ports:
     - "80:80"

  words:
    build: words
    image: dockerdemos/lab-words
    deploy:
      replicas: 5

  db:
    build: db
    image: dockerdemos/lab-db
    volumes:
    - test-volume:/test-volume

验证

然后刷新页面,就可以看到搭建的节点都在Kubernetes的控制台上面可以发现了。

http://static.cyblogs.com/WX20191015-174028@2x.png

用Docker命令查看本地的服务

➜  Desktop  docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES  
02d0691dee78        19b138d3318a        "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes                           k8s_db_db-0_default_d8be11b3-ef2e-11e9-ac8c-025000000001_0  
f719b60a99c8        b1e9c4adf655        "java -Xmx8m -Xms8m …"   11 minutes ago      Up 11 minutes                           k8s_words_words-745db75bdf-4slj7_default_d8734615-ef2e-11e9-ac8c-025000000001_0  
4a2ce12bd5e8        b1e9c4adf655        "java -Xmx8m -Xms8m …"   11 minutes ago      Up 11 minutes                           k8s_words_words-745db75bdf-bfz5c_default_d8799138-ef2e-11e9-ac8c-025000000001_0  
755679d0813b        a7ba5776710d        "/dispatcher"            11 minutes ago      Up 11 minutes                           k8s_web_web-8ffd8b7f4-scdmz_default_d86e9b2e-ef2e-11e9-ac8c-025000000001_0  
ab8bbda27700        b1e9c4adf655        "java -Xmx8m -Xms8m …"   11 minutes ago      Up 11 minutes                           k8s_words_words-745db75bdf-w2dxd_default_d878bdf2-ef2e-11e9-ac8c-025000000001_0  
5c5943bd4f34        b1e9c4adf655        "java -Xmx8m -Xms8m …"   11 minutes ago      Up 11 minutes                           k8s_words_words-745db75bdf-bzdbg_default_d86ebe31-ef2e-11e9-ac8c-025000000001_0  
6e1b7bbffaa9        b1e9c4adf655        "java -Xmx8m -Xms8m …"   11 minutes ago      Up 11 minutes                           k8s_words_words-745db75bdf-2xwgr_default_d87525d6-ef2e-11e9-ac8c-025000000001_0  

其他命令

应用栈创建完毕后,可以使用 kubectl 查看创建的 Pods:

kubectl get pods  

也可以来查看部署的集群与服务:

kubectl get deployments  

可以看到这里的 web 有所谓的 LoadBalancer 类型,即可以对外提供服务。最后我们还可以用 stack 与 kubectl 命令来删除应用:

docker stack remove demo 

kubectl delete deployment kubernetes-dashboard --namespace kube-system