基於 Docker for MAC 的 Kubernetes 本地環境搭建與應用部署概括於 分佈式容器與編排,參考了 Docker + Kubernetes一文,更多 Docker 或者 Kubernetes 相關資料參閱 Awesome Reference。
下載最新的 Docker for Mac 或者 Edge 版本,便可以看到內置的 Kubernetes 集羣,直接點擊安裝便可在本地搭建好單節點的 Kubernetes 環境:git
安裝完畢後,若是咱們也勾選了 Show system containers 選項,那麼使用以下的 Docker 命令,能看到自動安裝的 Kubernetes 相關容器:github
➜ ~ docker container ls --format "table{{.Names}}\t{{.Image }}\t{{.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 控制命令:web
$ kubectl get namespaces $ kubectl get posts --namespace kube-system
接下來咱們能夠使用 kubectl 命令來建立簡單的 kubernetes-dashboard 服務:docker
➜ ~ 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
服務安裝完畢後能夠查看部署的容器與服務:api
➜ ~ 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 服務來訪問該面板:session
$ kubectl proxy # 打開以下地址: # http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
若是訪問報錯,能夠嘗試編輯 kubernetes-dashboard 服務,或者參閱這裏:app
$ 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: {}
訪問上述地址,咱們能夠看到登陸界面:dom
此時可暫時直接跳過,進入到控制面板中:分佈式
Docker 一樣爲咱們提供了簡單的應用示範,能夠直接使用以下的 Docker Compose 配置文件:ide
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