查看版本

kubectl version

查看节点

kubectl get nodes

部署app

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

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

再如:

run test --image=preparedman/mytomcat:tagname --port=

查看app

kubectl proxy

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

{
"major": "",
"minor": "",
"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}}{{"\n"}}{{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:
PriorityClassName: <none>
Node: minikube/172.17.0.72
Start Time: Wed, Apr :: +
Labels: pod-template-hash=6bf84cb898
run=kubernetes-bootcamp
Annotations: <none>
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: /TCP
Host Port: /TCP
State: Running
Started: Wed, Apr :: +
Ready: True
Restart Count:
Environment: <none>
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: <none>
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 

查看service详情

kubectl describe services/kubernetes-bootcamp

结果:

Name:                     kubernetes-bootcamp
Namespace: default
Labels: run=kubernetes-bootcamp
Annotations: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.105.231.53
Port: <unset> /TCP
TargetPort: /TCP
NodePort: <unset> /TCP
Endpoints: 172.18.0.4:
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

创建一个环境变量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}}{{"\n"}}{{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:

扩容

设置deploymentsreplica数量为4

kubectl scale deployments/kubernetes-bootcamp --replicas=

查看结果:

可以看到修改replica设置生效

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

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

kubectl get pods -o wide

结果:

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

查看4个pod

kubectl describe deployments/kubernetes-bootcamp

结果:

Replicas:                desired |  updated |  total |  available |  unavailable

查看service是否是负载均衡的

查看具体IP

kubectl describe services/kubernetes-bootcamp

结果:

Endpoints:                172.18.0.2:,172.18.0.4:,172.18.0.6: +  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=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-qg5xh | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-bn98t | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=

缩容

kubectl scale deployments/kubernetes-bootcamp --replicas=

## 更新到版本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: <none>
Selector: run=kubernetes-bootcamp
Type: NodePort
IP: 10.98.28.235
Port: <unset> /TCP
TargetPort: /TCP
NodePort: <unset> /TCP
Endpoints: 172.18.0.10:,172.18.0.11:,172.18.0.8: + more...
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

结果:

访问版本2,且每次访问不同的地址

Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-86c8g | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-fx9tf | v=
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=

确认更新

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

kubernetes常用命令:缩容扩容回滚的更多相关文章

  1. Docker Kubernetes 常用命令

    Docker Kubernetes 常用命令 增 # 通过文件名或标准输入创建资源. kubectl create # 读取指定文件内容,进行创建.(配置文件可指定json,yaml文件). kube ...

  2. kubernetes常用命令

    #.查询信息 kubectl get [需要查询的服务]   node 节点componentstatuses 简写 cs 组件状态namespaces 简写 ns 名命空间pod pod信息 添加  ...

  3. Kubernetes 常用命令

    文章摘自:https://blog.csdn.net/felix_yujing/article/details/51622132 1 查看类命令--- # 查看集群信息 kubectl cluster ...

  4. 最新版Kubernetes常用命令大全

    #查看所有namespace的pods运行情况 kubectl get pods --all-namespaces #查看具体pods,记得后边跟namespace名字哦 kubectl get po ...

  5. 【kubevirt】VirtualMachineInstanceReplicaSet(vmis)-扩缩容-弹性伸缩

    @ 目录 概述/理解 使用场景 创建vmis 扩缩容 弹性伸缩 方法1 方法2 概述/理解 VirtualMachineInstanceReplicaSet(vmis)确保指定数量的 VirtualM ...

  6. kubernetes 知识点及常用命令

    一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...

  7. Docker Swarm 常用命令

    # 管理配置文件 docker config     # 查看已创建配置文件     - docker config ls     # 将已有配置文件添加到docker配置文件中     - dock ...

  8. Docker Swarm(二)常用命令

    # 管理配置文件 docker config     # 查看已创建配置文件     - docker config ls     # 将已有配置文件添加到docker配置文件中     - dock ...

  9. 不能回滚的Redis事务还能用吗

    前言 事务是关系型数据库的特征之一,那么作为 Nosql 的代表 Redis 中有事务吗?如果有,那么 Redis 当中的事务又是否具备关系型数据库的 ACID 四大特性呢? Redis 有事务吗 这 ...

随机推荐

  1. Python下编写Windows自动化测试软件

    https://www.jianshu.com/p/be3c46c7a905 uiautomation模块学习笔记 前段时间,由于个人需要,在网上查找了一些关于Windows平台下自动化测试的资料,最 ...

  2. 创建Maven之后,Java包下无法直接创建servlet的原因:

    ---恢复内容开始--- 没有导入servlet-api的依赖,导入完依赖就可以直接创建了   依赖如下:   <dependency> <groupId>javax.serv ...

  3. Reactive Extensions (Rx) 入门(4) —— Rx的事件编程

    译文:https://blog.csdn.net/fangxing80/article/details/7685393 原文:http://www.atmarkit.co.jp/fdotnet/int ...

  4. Linux常用命令简述--dirname与basename

    dirname 获取父目录 basename 显示最后的目录名或文件名 .dirname [root@liang ~]# dirname /etc/httpd/ /etc [root@liang ~] ...

  5. 前端微服务初试(singleSpa)

    1.基本概念 实现一套微前端架构,可以把其分成四部分(参考:https://alili.tech/archive/11052bf4/) 加载器:也就是微前端架构的核心,主要用来调度子应用,决定何时展示 ...

  6. IIS基本设置、回收机制、性能、并发、安全性

    通常把站点发布到IIS上运行正常后,很少会去考虑IIS提供的各种参数,如何配置才是最适合当前站点运行需要的?这篇文章,从基本设置.回收机制.性能.并发.安全性等IIS设置讲解应当如何优化. 先来“II ...

  7. 第08组 Beta冲刺(2/4)

    队名 八组评分了吗 组长博客链接(2分) 组员1李昕晖(组长) 过去两天完成了哪些任务 文字/口头描述 12月9号了解各个小组的进度与难以攻破的地方,晚上安排开会,安排新的冲刺任务. 重新分配小组及个 ...

  8. Spring注解驱动第三讲--@Filter介绍

    上一讲主要针对@ComponentScan注解做了一些说明,本文主要对@Filter的扫描条件,再做一些详细的介绍 1,FilterType.ANNOTATION 按照注解的方式进行扫描.后面clas ...

  9. intellij idea远程调试

    有时候发布后的包不得不进行debug,但是又不方便本地开发环境直接debug模拟,所以不得不需要远程debug. 启动参数 首先在服务端使用JVM的-Xdebug参数启动Jar包. java -Xde ...

  10. 【C++】C++中的lambda表达式和函数对象

    目录结构: contents structure [-] lambda表达式 lambda c++14新特性 lambda捕捉表达式 泛型lambda表达式 函数对象 函数适配器 绑定器(binder ...