基於 Docker for MAC 的 Kubernetes 本地環境搭建與應用部署歸納於分散式容器與編排,參考了 Docker + Kubernetes一文,更多 Docker 或者 Kubernetes 相關資料參閱 Awesome Reference。
下載最新的 Docker for Mac 或者 Edge 版本,即可以看到內建的 Kubernetes 叢集,直接點選安裝即可在本地搭建好單節點的 Kubernetes 環境:
安裝完畢後,如果我們也勾選了 Show system containers 選項,那麼使用如下的 Docker 命令,能看到自動安裝的 Kubernetes 相關容器:
➜ ~ docker container ls --format "table{{.Names}} {{.Image }} {{.Command}}"
NAMES IMAGE COMMAND
k8s_compose_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0 docker/kube-compose-controller "/compose-controller…"
k8s_POD_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_sidecar_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/k8s-dns-sidecar-amd64 "/sidecar --v=2 --lo…"
k8s_dnsmasq_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 "/dnsmasq-nanny -v=2…"
k8s_kubedns_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/k8s-dns-kube-dns-amd64 "/kube-dns --domain=…"
k8s_kube-proxy_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/kube-proxy-amd64 "/usr/local/bin/kube…"
k8s_POD_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_POD_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_kube-scheduler_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0 gcr.io/google_containers/kube-scheduler-amd64 "kube-scheduler --ad…"
k8s_kube-apiserver_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0 gcr.io/google_containers/kube-apiserver-amd64 "kube-apiserver --ad…"
k8s_etcd_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0 gcr.io/google_containers/etcd-amd64 "etcd --advertise-cl…"
k8s_kube-controller-manager_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0 gcr.io/google_containers/kube-controller-manager-amd64 "kube-controller-man…"
k8s_POD_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_POD_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_POD_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
k8s_POD_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"
複製程式碼
關於各個容器的作用,可以參閱 這裡。在安裝過程中,Docker 也為我們安裝了 kubectl 控制命令:
$ kubectl get namespaces
$ kubectl get posts --namespace kube-system
複製程式碼
接下來我們可以使用 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 <none> 53/UDP,53/TCP 22m
kubernetes-dashboard ClusterIP 10.111.242.95 <none> 443/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
# Please edit the object below. Lines beginning with a `#` will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
...
name: kubernetes-dashboard
namespace: kube-system
resourceVersion: "343478"
selfLink: /api/v1/namespaces/kube-system/services/kubernetes-dashboard-head
uid: 8e48f478-993d-11e7-87e0-901b0e532516
spec:
clusterIP: 10.100.124.90
externalTrafficPolicy: Cluster
ports:
- port: 443
protocol: TCP
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
sessionAffinity: None
type: ClusterIP ->> NodePort
status:
loadBalancer: {}
複製程式碼
訪問上述地址,我們可以看到登入介面:
此時可暫時直接跳過,進入到控制皮膚中:
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.yml 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-7f99cc64b7-cbd9t 1/1 Running 0 2m
web-758c6998f8-tmxfm 1/1 Running 0 2m
words-54bf6c5d57-8bxc8 1/1 Running 0 2m
words-54bf6c5d57-dzxm8 1/1 Running 0 2m
words-54bf6c5d57-k2448 1/1 Running 0 2m
words-54bf6c5d57-mhh4p 1/1 Running 0 2m
words-54bf6c5d57-w2q82 1/1 Running 0 2m
複製程式碼
也可以來檢視部署的叢集與服務:
$ 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 <none> 55555/TCP 3m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 52m
web LoadBalancer 10.97.154.28 <pending> 80:30577/TCP 3m
words ClusterIP None <none> 55555/TCP 3m
複製程式碼
可以看到這裡的 web 有所謂的 LoadBalancer 型別,即可以對外提供服務。最後我們還可以用 stack 與 kubectl 命令來刪除應用:
$ docker stack remove demo
$ kubectl delete deployment kubernetes-dashboard --namespace kube-system
複製程式碼