Docker for Mac with Kubernetes初次尝试

首先,2018-01-09日Docker公司宣布了Docker for Mac支持Kubernetes

后来陆续尝试了几次,今天终于成功了,所以记录下。

安装Docker for Mac with Kubernetes

首先,安装Docker for Mac Edge版本:

brew cask install docker-edge

设置代理(我用的是https://github.com/netheril96/MEOW):

开启Kubernetes:

 

等待安装:

OK了:

kubectl version也能看到客户端版本和服务端版本了:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T09:42:01Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}

创建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 pods --namespace kube-system
NAME READY STATUS RESTARTS AGE
etcd-docker-for-desktop 1/1 Running 0 6m
kube-apiserver-docker-for-desktop 1/1 Running 2 6m
kube-controller-manager-docker-for-desktop 1/1 Running 0 6m
kube-dns-6f4fd4bdf-zl9dh 3/3 Running 0 7m
kube-proxy-xsx8n 1/1 Running 0 7m
kube-scheduler-docker-for-desktop 1/1 Running 0 5m
kubernetes-dashboard-5bd6f767c7-6szsl 1/1 Running 0 1m
$ kubectl get deployments --namespace kube-system
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kube-dns 1 1 1 1 7m
kubernetes-dashboard 1 1 1 1 1m

先运行kubectl proxy:

$ kubectl proxy
Starting to serve on 127.0.0.1:8001

然后访问:

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

即可看到dashboard:

来一个Wordpress的例子吧

clone仓库:

git clone https://github.com/kubernetes/examples.git
cd examples/mysql-wordpress-pd/

然后基本上按照官方教程来就好了

创建MySQL密码

创建密码

$ kubectl create secret generic mysql-pass --from-literal=password=YOUR_PASSWORD
secret "mysql-pass" created

使用kubectl get secrets 可以看到有一个名字为mysql-pass 的secret

$ kubectl get secrets
NAME TYPE DATA AGE
default-token-z7lmq kubernetes.io/service-account-token 3 15m
mysql-pass Opaque 1 3s

部署MySQL

开始部署MySQL服务:

$ kubectl create -f mysql-deployment.yaml
service "wordpress-mysql" created
persistentvolumeclaim "mysql-pv-claim" created
deployment "wordpress-mysql" created

看下pvc创建好了没有:

$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound pvc-32bcbb1a-1143-11e8-a423-025000000001 20Gi RWO hostpath 1m

看下MySQL的pod创建好了没有:

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
wordpress-mysql-58cf8dc9f9-5grjs 1/1 Running 0 1m

部署Wordpress

$ kubectl create -f wordpress-deployment.yaml
service "wordpress" created
persistentvolumeclaim "wp-pv-claim" created
deployment "wordpress" created

检查下pvc和service的状态:

$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound pvc-32bcbb1a-1143-11e8-a423-025000000001 20Gi RWO hostpath 3m
wp-pv-claim Bound pvc-a5f97603-1143-11e8-a423-025000000001 20Gi RWO hostpath 11s
$ kubectl get services wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.96.2.58 <pending> 80:30734/TCP 32s

此处EXTERNAL-IP为<pending>,这个我也不清楚为何。

等一会之后,访问http://127.0.0.1/ ,就可以看到熟悉的WordPress安装界面了:

清理Wordpress

kubectl delete secret mysql-pass
kubectl delete deployment -l app=wordpress
kubectl delete service -l app=wordpress
kubectl delete pvc -l app=wordpress

 

Leave a Reply

Your email address will not be published. Required fields are marked *