Play with Kubernetes

Play with Kubernetes提供了一个免费的Kubernets体验环境,每次创建的集群最长可以使用4小时。

集群初始化

打开play-with-k8s.com,点击”ADD NEW INSTANCE”,并在新INSTANCE(名字为node1)的TERMINAL中初始化kubernetes master:

  1. $ kubeadm init --apiserver-advertise-address $(hostname -i)
  2. Initializing machine ID from random generator.
  3. [kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
  4. [init] Using Kubernetes version: v1.7.0
  5. [init] Using Authorization modes: [Node RBAC]
  6. [preflight] Skipping pre-flight checks
  7. [certificates] Generated CA certificate and key.
  8. [certificates] Generated API server certificate and key.
  9. [certificates] API Server serving cert is signed for DNS names [node1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.1.3]
  10. [certificates] Generated API server kubelet client certificate and key.
  11. [certificates] Generated service account token signing key and public key.
  12. [certificates] Generated front-proxy CA certificate and key.
  13. [certificates] Generated front-proxy client certificate and key.
  14. [certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
  15. [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
  16. [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
  17. [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
  18. [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
  19. [apiclient] Created API client, waiting for the control plane to become ready
  20. [apiclient] All control plane components are healthy after 25.001152 seconds
  21. [token] Using token: 35e301.77277e7cafee013c
  22. [apiconfig] Created RBAC rules
  23. [addons] Applied essential addon: kube-proxy
  24. [addons] Applied essential addon: kube-dns
  25. Your Kubernetes master has initialized successfully!
  26. To start using your cluster, you need to run (as a regular user):
  27. mkdir -p $HOME/.kube
  28. sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  29. sudo chown $(id -u):$(id -g) $HOME/.kube/config
  30. You should now deploy a pod network to the cluster.
  31. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  32. http://kubernetes.io/docs/admin/addons/
  33. You can now join any number of machines by running the following on each node
  34. as root:
  35. kubeadm join --token 35e301.77277e7cafee013c 10.0.1.3:6443
  36. Waiting for api server to startup...........
  37. Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
  38. daemonset "kube-proxy" configured

注意:记住输出中的kubeadm join --token 35e301.77277e7cafee013c 10.0.1.3:6443命令,后面会用来添加新的节点。

配置kubectl

  1. mkdir -p $HOME/.kube
  2. cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  3. chown $(id -u):$(id -g) $HOME/.kube/config

初始化网络

  1. kubectl apply -n kube-system -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

创建Dashboard

  1. curl -L -s https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml | sed 's/targetPort: 9090/targetPort: 9090\n type: LoadBalancer/' | kubectl apply -f -

稍等一会,在页面上方会显示Dashborad服务的端口号,点击端口号就可以访问Dashboard页面。

添加新的节点

点击”ADD NEW INSTANCE”,并在新INSTANCE的TERMINAL中输入前面第一步记住的kubeadm join命令,如

  1. kubeadm join --token 35e301.77277e7cafee013c 10.0.1.3:6443

回到node1的TERMINAL,输入kubectl get node即可查看所有Node的状态。等所有Node状态都变成Ready后,整个Kubernetes集群都搭建好了。