检查当前kubectl的上下文

  1. $ kubectl config view

创建上下文

# 查看当前上下文
$ kubectl config current-context

# 创建一个开发环境上下文件
$ kubectl config set-context dev --namespace=development --cluster=xxxx --user=xxxx

# 创建一个生产环境上下文件
$ kubectl config set-context prod --namespace=production --cluster=xxxx --user=xxxx

切换上下文

# 切换到开发环境上下文
$ kubectl config use-context dev

# 切换到生产环境上下文
$ kubectl config use-context prod

查看k8s版本

$ 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"}

查看k8s集群信息

$ kubectl cluster-info
  Kubernetes master is running at https://172.17.0.51:8443
  KubeDNS is running at https://172.17.0.51:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

查看所有节点列表

$ kubectl get nodes

创建一个deployment

$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

$ kubectl get deployments

$ echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; 
kubectl proxy

$ 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"
  }

$ 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-75bccb7d87-894qf

kubectl常用命令:

  • kubectl get - list resources
  • kubectl describe - show detailed information about a resource
  • kubectl logs - print the logs from a container in a pod
  • kubectl exec - execute a command on a container in a pod

操作POD和NODE

$ kubectl get pods

$ kubectl describe pods

$ echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; kubectl proxy

$ curl http://localhost:8001/version

$ 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-kwnl8

$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/
    Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-kwnl8 | v=1

$ kubectl logs $POD_NAME
    Kubernetes Bootcamp App Started At: 2019-12-16T09:45:19.433Z | Running On:  kubernetes-bootcamp-5b48cfdcbd-kwnl8

    Running On: kubernetes-bootcamp-5b48cfdcbd-kwnl8 | Total Requests: 1 | App Uptime: 600.048 seconds | Log Time: 2019-12-16T09:55:19.481Z

$ kubectl exec $POD_NAME env
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  HOSTNAME=kubernetes-bootcamp-5b48cfdcbd-kwnl8
  KUBERNETES_SERVICE_PORT_HTTPS=443
  KUBERNETES_PORT=tcp://10.96.0.1:443
  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
  NPM_CONFIG_LOGLEVEL=info
  NODE_VERSION=6.3.1
  HOME=/root

# 进入容器
$ kubectl exec -ti $POD_NAME bash
    root@kubernetes-bootcamp-5b48cfdcbd-kwnl8:/#

# 在容器中查看指定文件
$ cat server.js
# 在容器中请求容器内的应用api
$ curl localhost:8080
# 退出容器
$ exit

操作Service

$ kubectl get services

# 在节点中暴露一个节点端口
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

$ kubectl describe services/kubernetes-bootcamp
    Name:                     kubernetes-bootcamp
  Namespace:                default
  Labels:                   run=kubernetes-bootcamp
  Annotations:              <none>
  Selector:                 run=kubernetes-bootcamp
  Type:                     NodePort
  IP:                       10.104.135.86
  Port:                     <unset>  8080/TCP
  TargetPort:               8080/TCP
  NodePort:                 <unset>  30145/TCP
  Endpoints:                172.18.0.2:8080
  Session Affinity:         None
  External Traffic Policy:  Cluster
  Events:                   <none>

$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT

$ curl $(minikube ip):$NODE_PORT
        Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-ww9hf | v=1

$ kubectl describe deployment

# 使用标签查询pod列表
$ kubectl get pods -l run=kubernetes-bootcamp

$ kubectl get services -l run=kubernetes-bootcamp

$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

# 给Pod添加新标签 要应用新标签,我们使用label命令,后面跟着对象类型、对象名称和新标签
$ kubectl label pod $POD_NAME app=v1

$ kubectl describe pods $POD_NAME

$ kubectl get pods -l app=v1

# 删除一个Service 在这里可以用标签过滤
$ kubectl delete service -l run=kubernetes-bootcamp

$ kubectl get services

# 删除服务后,在集群中访问失败
$ curl $(minikube ip):$NODE_PORT
# 但可以在pod内访问成功
$ kubectl exec -ti $POD_NAME curl localhost:8080

Scaling

$ kubectl get deployments

# 扩容部署为4个 后面跟 部署类型/名称 所需实例数
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4

$ kubectl get deployments

# 查看Pod已经增加到了4个
$ kubectl get pods -o wide

$ kubectl describe deployments/kubernetes-bootcamp

# 查看服务
$ kubectl describe services/kubernetes-bootcamp

$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT

# 测试负载均衡
$ curl $(minikube ip):$NODE_PORT

# 缩减应用为2个
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2

$ kubectl get deployments

$ kubectl get pods -o wide

执行滚动更新应用

$ kubectl get deployments

$ kubectl get pods

$ kubectl describe pods

# 更新应用 要将应用程序的映像更新到版本2,请使用set image命令,然后是部署名称和新的映像版本
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

$ kubectl get pods

$ kubectl describe services/kubernetes-bootcamp

$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT

$ curl $(minikube ip):$NODE_PORT

# 查看更新是否成功
$ kubectl rollout status deployments/kubernetes-bootcamp

$ kubectl describe pods

# 再次更新为另一个V10版本
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10

$ kubectl get deployments

$ kubectl get pods

$ kubectl describe pods

# 回滚到前一个版本
$ kubectl rollout undo deployments/kubernetes-bootcamp

$ kubectl get pods

$ kubectl describe pods

删除指定的节点

$ kubectl delete node 节点名称

将指定的节点设置为不可调度

$ kubectl cordon $节点名称