Tutorial

官方在线实验平台https://kubemetes.io/docs/tutorials/

基础

https://kubernetes.io/docs/tutorials/kubernetes-basics/
image.png


点击左侧菜单的“Learn Kubernetes Basics” -> “Create a Cluster” -> “Interactive Tutorial - Creating a Cluster
在终端中执行如下命令即可开始初始化集群:

  1. $ minikube start
  2. * minikube v1.3.0 on Ubuntu 18.04
  3. * Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
  4. * Using the running none "minikube" VM ...
  5. * Waiting for the host to be provisioned ...
  6. * Preparing Kubernetes v1.15.0 on Docker 18.09.5 ...
  7. - kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
  8. * Relaunching Kubernetes using kubeadm ...
  9. * Waiting for: apiserver proxy etcd scheduler controller dns
  10. * Done! kubectl is now configured to use "minikube"

image.png

查看版本

  1. $ kubectl version
  2. Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:23:26Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
  3. Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.0", GitCommit:"e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529", GitTreeState:"clean", BuildDate:"2019-06-19T16:32:14Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

查看集群信息

  1. $ kubectl cluster-info
  2. Kubernetes master is running at https://172.17.0.29:8443
  3. KubeDNS is running at https://172.17.0.29:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

查看节点信息

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. minikube Ready master 8m28s v1.15.0

创建一个deployment,需要指定名称是kubernetes-bootcamp 镜像是gcr.io/google-samples/kubernetes-bootcamp:v1 端口是8080

  1. $ kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
  2. kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
  3. deployment.apps/kubernetes-bootcamp created

image.png查看当前创建的deployments

  1. $ kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. kubernetes-bootcamp 1/1 1 1 2m52s

开启服务代理

  1. $ kubectl proxy
  2. Starting to serve on 127.0.0.1:800

重新开启一个终端窗口 测试curl命令

  1. $ curl http://localhost:8001/version
  2. {
  3. "major": "1",
  4. "minor": "15",
  5. "gitVersion": "v1.15.0",
  6. "gitCommit": "e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529",
  7. "gitTreeState": "clean",
  8. "buildDate": "2019-06-19T16:32:14Z",
  9. "goVersion": "go1.12.5",
  10. "compiler": "gc",
  11. "platform": "linux/amd64"
  12. }

获取pod名称

  1. $ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
  2. $ echo Name of the Pod:$POD_NAME
  3. Name of the Pod:kubernetes-bootcamp-5b48cfdcbd-ngsqd

查看日志

  1. $ kubectl logs kubernetes-bootcamp-5b48cfdcbd-wfll7
  2. Kubernetes Bootcamp App Started At: 2019-10-06T06:14:28.743Z | Running On: kubernetes-bootcamp-5b48cfdcbd-wfll7

查看环境变量

  1. $ kubectl exec kubernetes-bootcamp-5b48cfdcbd-wfll7 env
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. HOSTNAME=kubernetes-bootcamp-5b48cfdcbd-wfll7
  4. KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
  5. KUBERNETES_PORT_443_TCP_PROTO=tcp
  6. KUBERNETES_PORT_443_TCP_PORT=443
  7. KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
  8. KUBERNETES_SERVICE_HOST=10.96.0.1
  9. KUBERNETES_SERVICE_PORT=443
  10. KUBERNETES_SERVICE_PORT_HTTPS=443
  11. KUBERNETES_PORT=tcp://10.96.0.1:443
  12. NPM_CONFIG_LOGLEVEL=info
  13. NODE_VERSION=6.3.1
  14. HOME=/root

查看服务

  1. $ kubectl get services
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 106

查看deployment

  1. $ kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. kubernetes-bootcamp 1/1 1 1 7m10s

将deployment转换成service

  1. $ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080service/kubernetes-bootcamp exposed

查看service

  1. $ kubectl get services
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15m
  4. kubernetes-bootcamp NodePort 10.100.62.163 <none> 8080:30195/TCP 11m

Play with Kubernetes

https://labs.play-with-k8s.com

K8S训练营

地址:https://k8s.bingohuang.com/index.html
图片.png