资讯专栏INFORMATION COLUMN

kubernetes | 常用命令-缩容扩容回滚

idealcn / 2948人阅读

摘要:查看版本查看节点部署说明提供名称和镜像地址镜像地址再如查看测试获取名字测试使用进行故障排除主要使用如下命令列出资源显示资源详情打印中的日志在中的上执行命令获取应用配置查看应用是否在运行查看中有哪些结

查看版本
kubectl version
查看节点
kubectl get nodes
部署app

说明: 提供deployment名称和app镜像地址(docker镜像地址)

kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080

再如:

run test --image=preparedman/mytomcat:tagname --port=8088
查看app
kubectl proxy

测试:curl http://localhost:8001/version

{
  "major": "1",
  "minor": "13",
  "gitVersion": "v1.13.3",
  "gitCommit": "721bfa751924da8d1680787490c54b9179b1fed0",
  "gitTreeState": "clean",
  "buildDate": "2019-02-01T20:00:57Z",
  "goVersion": "go1.11.5",
  "compiler": "gc",
  "platform": "linux/amd64"
}

获取pod名字

export POD_NAME=$(kubectl get pods -o go-template --template "{{range .items}}{{.metadata.name}}{{"
"}}{{end}}")

测试:echo Name of the Pod: $POD_NAME

使用kubectl进行故障排除

主要使用如下命令

kubectl get - list resources 列出资源
kubectl describe - show detailed information about a resource 显示资源详情
kubectl logs - print the logs from a container in a pod 打印`pod` 中container的日志
kubectl exec - execute a command on a container in a pod 在`pod`中的container上执行命令

获取应用配置

查看应用是否在运行

kubectl get pods

查看pod 中有哪些container

kubectl describe pods

结果如下:

Name:               kubernetes-bootcamp-6bf84cb898-jk4jc
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               minikube/172.17.0.72
Start Time:         Wed, 24 Apr 2019 13:21:58 +0000
Labels:             pod-template-hash=6bf84cb898
                    run=kubernetes-bootcamp
Annotations:        
Status:             Running
IP:                 172.18.0.4
Controlled By:      ReplicaSet/kubernetes-bootcamp-6bf84cb898
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://55491b363d26b62e432cd4841ed4f65cc5b98e645d172c6ed88feaebcb4ec06c
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 24 Apr 2019 13:22:00 +0000
    Ready:          True
    Restart Count:  0
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-l7v8b (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-l7v8b:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-l7v8b
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  3m6s  default-scheduler  Successfully assigned default/kubernetes-bootcamp-6bf84cb898-jk4jc to minikube
  Normal  Pulled     3m4s  kubelet, minikube  Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
  Normal  Created    3m4s  kubelet, minikube  Created container
  Normal  Started    3m4s  kubelet, minikube  Started container

打印container日志
kubectl logs $POD_NAME
container中直接执行命令

比如:获取pod名字是kubernetes-bootcamp-6bf84cb898-jk4jc的日期,默认使用第一个container container by default

kubectl exec kubernetes-bootcamp-6bf84cb898-jk4jc date  

再入:进入container的命令行环境

kubectl exec kubernetes-bootcamp-6bf84cb898-jk4jc bash

退出使用

exit

暴露你的应用service 列出当前集群中的service
kubectl get services
创建一个新的service并暴露给外部流量
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
查看service详情
$ kubectl describe services/kubernetes-bootcamp

结果:

Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   run=kubernetes-bootcamp
Annotations:              
Selector:                 run=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.105.231.53
Port:                       8080/TCP
TargetPort:               8080/TCP
NodePort:                   32400/TCP
Endpoints:                172.18.0.4:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   
创建一个环境变量NODE_PORT,它的值等于service暴露的端口
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

使用labels 查看label
kubectl describe deployment

你能看到这样一行:

Labels:                 run=kubernetes-bootcamp
通过label查询pod
kubectl get pods -l run=kubernetes-bootcamp
通过label查询service
kubectl get services -l run=kubernetes-bootcamp
获取pod名字,并保存到环境变量POD_NAME
export POD_NAME=$(kubectl get pods -o go-template --template "{{range .items}}{{.metadata.name}}{{"
"}}{{end}}")

echo name of the pod: $POD_NAME
添加新的lebel

使用label命令

kubectl label pod $POD_NAME app=v1

查看:

kubectl describe pods $POD_NAME
删除service 通过label删除service
kubectl delete service -l run=kubernetes-bootcamp

确认删除:

kubectl get services

确认没有暴露给集群外部:

curl $(minikube ip):$NODE_PORT

确认集群内部还可以访问:

kubectl exec -ti $POD_NAME curl localhost:8080

扩容 设置deploymentsreplica数量为4
kubectl scale deployments/kubernetes-bootcamp --replicas=4

查看结果:

可以看到修改replica设置生效

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4/4     4            4           3m17s

pod数量已经改变,查看详情

kubectl get pods -o wide

结果:

NAME                                   READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-6bf84cb898-7tbrg   1/1     Running   0          2m50s   172.18.0.5   minikube              
kubernetes-bootcamp-6bf84cb898-fx68f   1/1     Running   0          3m52s   172.18.0.4   minikube              
kubernetes-bootcamp-6bf84cb898-prgsc   1/1     Running   0          2m50s   172.18.0.6   minikube              
kubernetes-bootcamp-6bf84cb898-qv4gc   1/1     Running   0          2m50s   172.18.0.7   minikube              

查看4个pod

kubectl describe deployments/kubernetes-bootcamp

结果:

Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
查看service是否是负载均衡的 查看具体IP
kubectl describe services/kubernetes-bootcamp

结果:

Endpoints:                172.18.0.2:8080,172.18.0.4:8080,172.18.0.6:8080 + 1 more...
创建环境变量NODE_PORT
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template="{{(index .spec.ports 0).nodePort}}")

echo NODE_PORT=$NODE_PORT
调用请求

可以看到,每次请求的都是不同的pod

curl $(minikube ip):$NODE_PORT

结果:

Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-2l975 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-qg5xh | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-bn98t | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
缩容
kubectl scale deployments/kubernetes-bootcamp --replicas=2

## 更新到版本2

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

验证更新

查看暴露出来的ip和端口

kubectl describe services/kubernetes-bootcamp

结果:

Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   run=kubernetes-bootcamp
Annotations:              
Selector:                 run=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.98.28.235
Port:                       8080/TCP
TargetPort:               8080/TCP
NodePort:                   31419/TCP
Endpoints:                172.18.0.10:8080,172.18.0.11:8080,172.18.0.8:8080 + 1 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   

创建环境变量

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,且每次访问不同的地址

Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-86c8g | v=2
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-fx9tf | v=2
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2
确认更新
kubectl rollout status deployments/kubernetes-bootcamp

回滚

更新到版本10

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10

查看发现报错,因为版本库中没有版本10

kubectl get deployments
kubectl get pods
kubectl describe pods

报错

Warning  Failed     38s (x3 over 77s)  kubelet, minikube  Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found

执行会滚

kubectl rollout undo deployments/kubernetes-bootcamp

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/33180.html

相关文章

  • kubernetes | 常用命令-缩容扩容回滚

    摘要:查看版本查看节点部署说明提供名称和镜像地址镜像地址再如查看测试获取名字测试使用进行故障排除主要使用如下命令列出资源显示资源详情打印中的日志在中的上执行命令获取应用配置查看应用是否在运行查看中有哪些结 查看版本 kubectl version 查看节点 kubectl get nodes 部署app 说明: 提供deployment名称和app镜像地址(docker镜像地址) kub...

    superw 评论0 收藏0
  • 数人云|当K8S遇上微服务-京东金融PaaS平台思考与实践

    摘要:平台上的微服务架构应用再来看一下我眼中的基于当前最流行的微服务架构的设计是什么样的,即我们平台上要运行的典型应用是什么样的。 showImg(https://segmentfault.com/img/remote/1460000010900878); 8月19日的数人云Container Meetup上,张龙老师做了《基于Kubernetes的PaaS平台的设计和思考》的精彩分享,分别...

    Imfan 评论0 收藏0
  • Kubernetes概念与术语

    摘要:标识是与操作对象间的纽带。集群为每个对象维护三类信息对象元数据期望状态与实际状态元数据指对象的基本信息,比如命名标签注释等等,用于识别对象期望状态一般由用户配置来描述的实际状态是由集群各个组件上报的集群实际的运行情况。 综述 学习Kubernetes时,发现它的概念和术语还是比较多的,光靠啃官方文档比较晦涩。所以边学习边整理,对主要的概念和术语做一下分类及简要说明。感觉把重要概念都理解...

    _Suqin 评论0 收藏0
  • Kubernetes 核心概念

    摘要:核心概念是最小的调度单元,可以由一个或者多个容器组成。该模式会跟云服务商有关,比如可以通过等创建一个外部的负载均衡器,将请求转发到对应的服务组。而可以提供外部服务可访问的负载均衡器等。 概述 Kubernetes 有各类资源对象来描述整个集群的运行状态。这些对象都需要通过调用 kubernetes api 来进行创建、修改、删除,可以通过 kubectl 命令工具,也可以直接调用 k8...

    Cobub 评论0 收藏0
  • kubernetes学习

    摘要:知识点核心组件保存整个集群的状态信息,感觉相当于的数据库提供对资源操作的唯一入口,并提供认证授权,访问控制,注册与发现等机制负责维护集群的状态,故障检测,自动扩展,滚动更新等负责对资源的调度,按着预定的调度策略将调度到相应的集群上负责维护容 kubernetes kubernetes 知识点 1. 核心组件 etcd 保存整个集群的状态信息,感觉相当于k8s的数据库 apiserve...

    lncwwn 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<