Mac上Kubernetes快速上手

安装 minikube 和 kubernetes-cli:

$ brew cask install minikube
$ brew install kubernetes-cli

没有 brew ? 没有 brew cask ?
那你需要 https://brew.sh/brew tap caskroom/cask

vm driver默认是virtualbox,如果需要xhyve(推荐),还需安装:

$ brew install docker-machine-driver-xhyve 

安装完,按提示要求,执行:

$ sudo chown root:wheel /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
$ sudo chmod u+s /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

验证是否安装成功

$ minikube help
$ kubectl help

创建 local k8s cluster

$ minikube start --vm-driver=xhyve

如果不用xhyve或未安装docker-machine-driver-xhyve, 省略--vm-driver=xhyve

GFW内的同学,还需要指定代理(HTTP代理)和minikube-v*.*.*.iso的镜像地址:

$ minikube start --docker-env HTTP_PROXY=http://myproxy:thePort --docker-env HTTPS_PROXY=http://myproxy:thePort --iso-url=http://mirror/of/minikube-v0.10.4.iso --vm-driver=xhyve

自建http代理,参考 Privoxy HTTP 代理配置

先小玩一下:

$ kubectl get namespaces
$ kubectl get pods
$ kubectl get svc
$ kubectl get deployments
$ kubectl get ingresses

部署 ghost blog:

新建一个ghost.yaml, 内容如下:

kind: Service
apiVersion: v1
metadata:
  name: ghost
spec:
  selector:
    app: ghost
  ports:
    - port: 2368
      targetPort: 2368
      nodePort: 31000
  type: NodePort

---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: ghost
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: ghost
    spec:
      containers:
        - name: ghost
          image: ghost:alpine
          imagePullPolicy: Always
          livenessProbe:
            httpGet:
              path: /
              port: 2368
          readinessProbe:
            httpGet:
              path: /
              port: 2368
      restartPolicy: Always

然后:

$ kubectl apply -f /path/to/ghost.yml

再次玩一下

会看到,ghost在pods、svc、deployments的列表中都有。

浏览器访问

先获取 minikube的ip

$ minikube ip
192.168.64.18

浏览器访问 http://192.168.64.18:31000

$ curl http://$(minikube ip):31000

后记

minikube中清理掉ghost?

$ kubectl delete service,deployment ghost

Back to top

comments powered by Disqus