Tutorial
官方在线实验平台https://kubemetes.io/docs/tutorials/
基础
https://kubernetes.io/docs/tutorials/kubernetes-basics/
点击左侧菜单的“Learn Kubernetes Basics” -> “Create a Cluster” -> “Interactive Tutorial - Creating a Cluster”
在终端中执行如下命令即可开始初始化集群:
$ minikube start
* minikube v1.3.0 on Ubuntu 18.04
* Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
* Using the running none "minikube" VM ...
* Waiting for the host to be provisioned ...
* Preparing Kubernetes v1.15.0 on Docker 18.09.5 ...
- kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
* Relaunching Kubernetes using kubeadm ...
* Waiting for: apiserver proxy etcd scheduler controller dns
* Done! kubectl is now configured to use "minikube"
查看版本
$ kubectl version
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"}
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"}
查看集群信息
$ kubectl cluster-info
Kubernetes master is running at https://172.17.0.29:8443
KubeDNS is running at https://172.17.0.29:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
查看节点信息
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 8m28s v1.15.0
创建一个deployment,需要指定名称是kubernetes-bootcamp 镜像是gcr.io/google-samples/kubernetes-bootcamp:v1 端口是8080
$ kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
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.
deployment.apps/kubernetes-bootcamp created
查看当前创建的deployments
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 2m52s
开启服务代理
$ kubectl proxy
Starting to serve on 127.0.0.1:800
重新开启一个终端窗口 测试curl命令
$ curl http://localhost:8001/version
{
"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"
}
获取pod名称
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod:$POD_NAME
Name of the Pod:kubernetes-bootcamp-5b48cfdcbd-ngsqd
查看日志
$ kubectl logs kubernetes-bootcamp-5b48cfdcbd-wfll7
Kubernetes Bootcamp App Started At: 2019-10-06T06:14:28.743Z | Running On: kubernetes-bootcamp-5b48cfdcbd-wfll7
查看环境变量
$ kubectl exec kubernetes-bootcamp-5b48cfdcbd-wfll7 env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-5b48cfdcbd-wfll7
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
查看服务
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 106
查看deployment
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 7m10s
将deployment转换成service
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080service/kubernetes-bootcamp exposed
查看service
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15m
kubernetes-bootcamp NodePort 10.100.62.163 <none> 8080:30195/TCP 11m
Play with Kubernetes
https://labs.play-with-k8s.com