標籤:

基於 Docker for MAC 的 Kubernetes 本地環境搭建與應用部署

基於 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}}t{{.Image }}t{{.Command}}"nNAMES IMAGE COMMANDnk8s_compose_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0 docker/kube-compose-controller "/compose-controller…"nk8s_POD_compose-75f8bb4779-stxv9_docker_3c963862-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_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…"nk8s_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…"nk8s_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=…"nk8s_kube-proxy_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/kube-proxy-amd64 "/usr/local/bin/kube…"nk8s_POD_kube-dns-545bc4bfd4-799pr_kube-system_139bf000-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_POD_kube-proxy-rrd8t_kube-system_139b00df-f9f4-11e7-93cc-025000000001_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_kube-scheduler_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0 gcr.io/google_containers/kube-scheduler-amd64 "kube-scheduler --ad…"nk8s_kube-apiserver_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0 gcr.io/google_containers/kube-apiserver-amd64 "kube-apiserver --ad…"nk8s_etcd_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0 gcr.io/google_containers/etcd-amd64 "etcd --advertise-cl…"nk8s_kube-controller-manager_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0 gcr.io/google_containers/kube-controller-manager-amd64 "kube-controller-man…"nk8s_POD_kube-apiserver-docker-for-desktop_kube-system_f7a81e8fe624bd46059fc6084e86bb81_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_POD_kube-controller-manager-docker-for-desktop_kube-system_8d1848c1e562e35a225e402988eadcd1_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_POD_kube-scheduler-docker-for-desktop_kube-system_972d74c9fc2f4ebd8ab673058e386a65_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"nk8s_POD_etcd-docker-for-desktop_kube-system_56a21c0a5f545c0cca5388c457bb1b3b_0 gcr.io/google_containers/pause-amd64:3.0 "/pause"n

關於各個容器的作用,可以參閱 這裡。在安裝過程中,Docker 也為我們安裝了 kubectl 控制命令:

$ kubectl get namespacesn$ kubectl get posts --namespace kube-systemn

接下來我們可以使用 kubectl 命令來創建簡單的 kubernetes-dashboard 服務:

? ~ kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yamlnsecret "kubernetes-dashboard-certs" creatednserviceaccount "kubernetes-dashboard" creatednrole "kubernetes-dashboard-minimal" creatednrolebinding "kubernetes-dashboard-minimal" createdndeployment "kubernetes-dashboard" creatednservice "kubernetes-dashboard" createdn

服務安裝完畢後可以查看部署的容器與服務:

? ~ kubectl get deployments --namespace kube-systemnNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEnkube-dns 1 1 1 1 22mnkubernetes-dashboard 1 1 1 0 26sn? ~ kubectl get services --namespace kube-systemnNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnkube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 22mnkubernetes-dashboard ClusterIP 10.111.242.95 <none> 443/TCP 30sn

在 Dashboard 啟動完畢後,可以使用 kubectl 提供的 Proxy 服務來訪問該面板:

$ kubectl proxynn# 打開如下地址:n# http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/n

如果訪問報錯,可以嘗試編輯 kubernetes-dashboard 服務,或者參閱這裡:

$ kubectl -n kube-system edit service kubernetes-dashboardnn# Please edit the object below. Lines beginning with a # will be ignored,n# and an empty file will abort the edit. If an error occurs while saving this file will ben# reopened with the relevant failures.n#napiVersion: v1n...n name: kubernetes-dashboardn namespace: kube-systemn resourceVersion: "343478"n selfLink: /api/v1/namespaces/kube-system/services/kubernetes-dashboard-headn uid: 8e48f478-993d-11e7-87e0-901b0e532516nspec:n clusterIP: 10.100.124.90n externalTrafficPolicy: Clustern ports:n - port: 443n protocol: TCPn targetPort: 8443n selector:n k8s-app: kubernetes-dashboardn sessionAffinity: Nonen type: ClusterIP ->> NodePortnstatus:n loadBalancer: {}n

訪問上述地址,我們可以看到登錄界面:

此時可暫時直接跳過,進入到控制面板中:

Docker 同樣為我們提供了簡單的應用示範,可以直接使用如下的 Docker Compose 配置文件:

version: 3.3nnservices:n web:n build: webn image: dockerdemos/lab-webn volumes:n - "./web/static:/static"n ports:n - "80:80"nn words:n build: wordsn image: dockerdemos/lab-wordsn deploy:n replicas: 5n endpoint_mode: dnsrrn resources:n limits:n memory: 16Mn reservations:n memory: 16Mnn db:n build: dbn image: dockerdemos/lab-dbn

然後使用 stack 命令創建應用棧:

$ docker stack deploy --compose-file stack.yml demonnStack demo was creatednWaiting for the stack to be stable and running...n - Service web has one container runningn

? 應用棧創建完畢後,可以使用 kubectl 查看創建的 Pods:

$ kubectl get podsnnNAME READY STATUS RESTARTS AGEndb-7f99cc64b7-cbd9t 1/1 Running 0 2mnweb-758c6998f8-tmxfm 1/1 Running 0 2mnwords-54bf6c5d57-8bxc8 1/1 Running 0 2mnwords-54bf6c5d57-dzxm8 1/1 Running 0 2mnwords-54bf6c5d57-k2448 1/1 Running 0 2mnwords-54bf6c5d57-mhh4p 1/1 Running 0 2mnwords-54bf6c5d57-w2q82 1/1 Running 0 2mn

也可以來查看部署的集群與服務:

$ kubectl get deploymentsnNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEndb 1 1 1 1 3mnweb 1 1 1 1 3mnwords 5 5 5 5 3mnn$ kubectl get servicesnNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEndb ClusterIP None <none> 55555/TCP 3mnkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 52mnweb LoadBalancer 10.97.154.28 <pending> 80:30577/TCP 3mnwords ClusterIP None <none> 55555/TCP 3mn

可以看到這裡的 web 有所謂的 LoadBalancer 類型,即可以對外提供服務。最後我們還可以用 stack 與 kubectl 命令來刪除應用:

$ docker stack remove demon$ kubectl delete deployment kubernetes-dashboard --namespace kube-systemn

推薦閱讀:

如何編寫最佳的Dockerfile
docker的幾點疑問?
在雲環境中的應用部署方式上,Docker會不會取代KVM、Xen之類的虛擬機技術?

TAG:Docker | Kubernetes |