下载最新的 Docker for Mac/Windows Edge 版本,即可以看到内置的 Kubernetes 集群,直接点击Apply即可在本地搭建好单节点的Kubernetes 环境;前提是有VPN,不然谷歌的gcr.io/google_containers一系列的镜像下载不了,Mac推荐使用Flow VPN和Brook,Windows推荐使用Brook:
安装完毕后,如果我们也勾选了 Show system containers 选项,那么使用如下的 Docker 命令,能看到自动安装的 Kubernetes 相关容器,总共有16个容器,和10个镜像(包括k8s的Controller,Schedule,ETCD,ApiServer,Proxy,Pause,DNS,DashBoard等):
镜像列表如下:
➜ ~ docker images
REPOSITORY TAG
k8s.gcr.io/kubernetes-dashboard-amd64 v1.8.3
gcr.io/google_containers/kube-controller-manager-amd64 v1.9.2
gcr.io/google_containers/kube-proxy-amd64 v1.9.2
gcr.io/google_containers/kube-apiserver-amd64 v1.9.2
gcr.io/google_containers/kube-scheduler-amd64 v1.9.2
gcr.io/google_containers/etcd-amd64 3.1.11
gcr.io/google_containers/k8s-dns-sidecar-amd64 1.14.7
gcr.io/google_containers/k8s-dns-kube-dns-amd64 1.14.7
gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 1.14.7
gcr.io/google_containers/pause-amd64 3.0
容器列表如下:
$ docker container ls --format "table{{.Names}}"
NAMES
k8s_compose_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0
k8s_POD_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0
k8s_sidecar_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0
k8s_dnsmasq_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0
k8s_kubedns_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0
k8s_kube-proxy_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0
k8s_POD_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0
k8s_POD_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0
k8s_kube-scheduler_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0
k8s_kube-apiserver_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0
k8s_etcd_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0
k8s_kube-controller-manager_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0
k8s_POD_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0
k8s_POD_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0
k8s_POD_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0
k8s_POD_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0
接下来我们可以使用 kubectl 命令来创建简单的 kubernetes-dashboard 服务:
$ kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/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
服务安装完毕后可以查看部署的容器与服务:
➜ ~ kubectl get deployments --namespace kube-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kube-dns 1 1 1 1 22m
kubernetes-dashboard 1 1 1 0 26s
➜ ~ kubectl get services --namespace kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP 22m
kubernetes-dashboard ClusterIP 10.111.242.9 5443/TCP 30s
在 Dashboard 启动完毕后,可以使用 kubectl 提供的 Proxy 服务来访问该面板:
$ kubectl proxy
# 打开如下地址:
# http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
如果访问报错,可以尝试编辑 kubernetes-dashboard 服务:
$ kubectl -n kube-system edit service kubernetes-dashboard
将倒数第3行的“type:ClusterIP”,改成“type:NodePort”。
访问上述地址,我们可以看到登录界面:
此时可暂时直接跳过,进入到控制面板中,即完成单节点k8s的搭建:
Docker 同样为我们提供了简单的应用示范,可以直接使用如下的 Docker Compose 配置文件:
version: '3.3'
services:
web:
build: web
image: dockerdemos/lab-web
volumes:
- "./web/static:/static"
ports:
- "80:80"
words:
build: words
image: dockerdemos/lab-words
deploy:
replicas: 5
endpoint_mode: dnsrr
resources:
limits:
memory: 16M
reservations:
memory: 16M
db:
build: db
image: dockerdemos/lab-db
然后使用 stack 命令创建应用栈:
$ docker stack deploy --compose-file stack-demo.yaml demo
Stack demo was created
Waiting for the stack to be stable and running...
- Service web has one container running
应用栈创建完毕后,可以使用 kubectl 查看创建的 Pods:
➜ ~ kubectl get pods
NAME READY STATUS RESTARTS AGE
db-6745d5bdb-njdbw 1/1 Running 0 20h
web-78846f5bbf-bjf8n 1/1 Running 0 20h
words-85c5b7685b-42qmm 1/1 Running 0 20h
words-85c5b7685b-56rwg 1/1 Running 0 20h
words-85c5b7685b-9lzl9 1/1 Running 0 20h
words-85c5b7685b-dnngv 1/1 Running 0 20h
words-85c5b7685b-stxh5 1/1 Running 0 20h
也可以来查看部署的集群与服务:
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
db 1 1 1 1 3m
web 1 1 1 1 3m
words 5 5 5 5 3m
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP None 55555/TCP 3m
kubernetes ClusterIP 10.96.0.1 443/TCP 52m
web LoadBalancer 10.97.154.28 80:30577/TCP 3m
words ClusterIP None 55555/TCP 3m
可以看到这里的 web 有所谓的 LoadBalancer 类型,即可以对外提供服务。最后我们还可以用 stack 与 kubectl 命令来删除应用:
$ docker stack remove demo
$ kubectl delete deployment kubernetes-dashboard --namespace kube-system