Kubernetes--kubectl
一、Kubectl命令行说明
类型 | 命令 | 描述 |
基础命令 | create | 通过文件名或标准输入创建资源 |
expose | 将一个资源公开为一个新的kubernetes服务 | |
run |
创建并运行一个特定的镜像,可能是副本 创建一个deployment或job管理创建的容器 |
|
set |
配置应用资源 修改现有应用程序资源 |
|
get | 显示一个或多个资源 | |
explain | 文档参考资料 | |
edit | 使用默认的编辑器编辑一个资源 | |
delete | 通过文件名、标准输入、资源名称或标签选择器来删除资源 | |
部署命令 | rollout | 管理资源的发布 |
rolling-update | 执行指定复制控制器的滚动更新 | |
scale | 扩容或缩容Pod数量,Deployment、ReplicasSet、RC或Job | |
autoscale | 创建一个自动选择扩容或者缩容并设置Pod数量 | |
集群管理命令 | certificate | 修改证书资源 |
cluster-info | 显示集群信息 | |
top | 显示资源(CPUMemory/Storage) | |
cordon | 标记节点为不可调度 | |
uncordon | 标记节点可调度 | |
drain | 维护期间排除节点 | |
taint | 更新一个或多个节点上的污点 | |
排错/调试 | describe | 显示特定资源或资源组的详细信息 |
logs | 打印pod中容器的日志 | |
attach | 连接到一个运行的容器,既可以查看output stream,也可以与容器(stdin)进行交互 | |
exec | 在容器中执行命令 | |
port-forward | 将一个或多个本地端口转发到pod中 | |
proxy | 运行Kubernetes API服务器的代理 | |
cp | 从容器中复制文件或目录 | |
auth | 检查授权 | |
高级命令 | apply | 通过文件名或标准输入将配置应用于资源 |
patch | 使用(patch)补丁修改、更新资源的字段 | |
replace | 用文件名或标准输入替换资源 | |
convert | 在不同的API版本之间转换配置文件 | |
设置命令 | label | 更新资源的标签 |
annotate | 更新资源上的注释 | |
completion | 输出指定shell的代码(bash or zsh) | |
其他命令 | api-versions | 在服务器上打印支持的API版本,格式为“group / version” |
config | 修改Kubernetes的文件 | |
help | help命令 | |
plugin | 显示安装的插件 | |
version | 显示版本信息 |
具体可以参考:https://kubernetes.io/docs/reference/kubectl/kubectl/
二、kubectl事例
(1)创建一个deployment(deployment用来管理Pod和RS)
# kubectl run hello-world --replicas=3 --labels="app=example_nginx" --image=nginx:1.10 --port=80
deployment.apps "hello-world" created # 备注
hello-world : deployment的名称 --replicas:副本数 --labels:标签(非唯一,用于识别用途等特点) --image:使用的镜像 --port:暴露的端口
(2)查看pod
[root@master-01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-world-76c54b84b-b5bcz 1/1 Running 0 11m
hello-world-76c54b84b-bmhsd 1/1 Running 0 11m
hello-world-76c54b84b-rgn2q 1/1 Running 0 11m
[root@master-01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-world-76c54b84b-b5bcz 1/1 Running 0 11m 10.20.184.81 master-01
hello-world-76c54b84b-bmhsd 1/1 Running 0 11m 10.20.254.104 node-03
hello-world-76c54b84b-rgn2q 1/1 Running 0 11m 10.20.190.57 node-01
(3)查看deployment
[root@master-01 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-world 3 3 3 3 12
(4)查看pod的描述信息
[root@master-01 ~]# kubectl describe pod hello-world-76c54b84b-b5bcz
Name: hello-world-76c54b84b-b5bcz
Namespace: default
Node: master-01/172.16.60.95
Start Time: Wed, 20 Jun 2018 09:53:07 +0800
Labels: app=example_nginx
pod-template-hash=327106406
Annotations: <none>
Status: Running
IP: 10.20.184.81
Controlled By: ReplicaSet/hello-world-76c54b84
.
.
.
(5)描述deployment信息
[root@master- ~]# kubectl describe deploy/hello-world
Name: hello-world
Namespace: default
CreationTimestamp: Wed, Jun :: +
Labels: app=example_nginx
Annotations: deployment.kubernetes.io/revision=
Selector: app=example_nginx
Replicas: desired | updated | total | available | unavailable
StrategyType: RollingUpdate
MinReadySeconds:
RollingUpdateStrategy: max unavailable, max surge
Pod Template:
Labels: app=example_nginx
Containers:
hello-world:
Image: nginx:1.10
Port: /TCP
Host Port: /TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: hello-world-76c54b84b (/ replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 18m deployment-controller Scaled up replica set hello-world-76c54b84b to
(6)查看其它命名空间的资源(--namespace xxx / -n xxx)
[root@master-01 ~]# kubectl get pod -o wide -n kube-system
NAME READY STATUS RESTARTS AGE IP NODE
calico-kube-controllers-98989846-gjk55 1/1 Running 1 8d 172.16.60.98 node-02
calico-node-4mxvv 1/1 Running 1 8d 172.16.60.98 node-02
calico-node-ftg9v 1/1 Running 1 8d 172.16.60.96 master-02
calico-node-hctvm 1/1 Running 1 8d 172.16.60.97 node-01
calico-node-rbv5b 1/1 Running 1 8d 172.16.60.99 node-0
(7)edit修改配置
# 将刚才port的80 改为8088 [root@master-01 ~]# kubectl edit deploy hello-world ...
...
...
ports:
- containerPort: 8088
...
...
describe检查
[root@master-01 ~]# kubectl describe deploy/hello-world
...
...
...
Pod Template:
Labels: app=example_nginx
Containers:
hello-world:
Image: nginx:1.10
Port: 8088/TCP
Host Port: 0/TCP
(8)创建一个Service对象,暴露Deployment端口
[root@master-01 ~]# kubectl expose deploy/hello-world --port=88 --type=NodePort --target-port=80 --name=example-nginx-service
service "example-nginx-service" exposed # 备注
--port=88:Service服务的端口 --target-port=80: 容器暴露的端口 --type=NodePort:会随机开放一个宿主机端口(端口范围在apiserver中定义)
查看/描述 服务
[root@master-01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-nginx-service NodePort 10.254.43.65 <none> 88:40591/TCP 5m [root@master-01 ~]# kubectl describe svc example-nginx-service
Name: example-nginx-service
Namespace: default
Labels: app=example_nginx
Annotations: <none>
Selector: app=example_nginx
Type: NodePort
IP: 10.254.43.65
Port: <unset> 88/TCP
TargetPort: 80/TCP
NodePort: <unset> 40591/TCP
Endpoints: 10.20.184.82:80,10.20.190.59:80,10.20.254.106:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
访问nginx
# 可以通过Service 的cluster-ip:88 / ndoe-ip:40591 [root@master-01 ~]# curl 10.254.43.65:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 任意node-ip
[root@master-01 ~]# curl 172.16.60.95:40591
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
(9)创建一个Service对象,暴露UDP端口
# kubectl expose deploy/hello-world --port=4123 --type=NodePort --protocol=udp --target-port=80 --name=example-upd-service
(10)查看Pod日志
[root@master-01 ~]# kubectl logs pods/hello-world-76c54b84b-rx4vz 172.16.60.95 - - [20/Jun/2018:02:35:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.16.60.95 - - [20/Jun/2018:02:36:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
172.16.60.95 - - [20/Jun/2018:02:36:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36" "-"
(11)使用标签查询Pod
[root@master-01 ~]# kubectl get pod --selector="app=example_nginx" -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-world-76c54b84b-8zn5j 1/1 Running 0 18m 10.20.190.59 node-01
hello-world-76c54b84b-pbp9h 1/1 Running 0 18m 10.20.254.106 node-03
hello-world-76c54b84b-rx4vz 1/1 Running 0 18m 10.20.184.82 master-01
Kubernetes--kubectl的更多相关文章
- Kubernetes kubectl 命令
kubectl 命令用来操作 Kubernetes 集群中的资源对象,包括对资源的创建.删除.查看.修改.配置.运行等 命令语法:kubectl [command] [TYPE] [NAME] [fl ...
- [Kubernetes]kubectl命令补全出错
在kubernetes集群中,命令补全能够省很多事,但是这两天就很奇怪 kubectl get pod -n kube+tab键自动补全Namespace的时候出现错误 kubectl get pod ...
- Kubernetes kubectl 命令概述
kubectl用于运行Kubernetes集群命令的管理工具. 语法 kubectl [command] [TYPE] [NAME] [flags] command:指定要在一个或多个资源执行的操作 ...
- Kubernetes,kubectl常用命令详解
kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...
- Kubernetes - kubectl proxy
最近在玩flink部署在k8s上,但是k8s以前没玩过,参照前几天写的文章可部署一个简单的k8shttps://www.cnblogs.com/felixzh/p/9726244.html 在参照fl ...
- [Kubernetes] Kubectl and Pod
1. Create and run a Pod kubectl run my-nginx --image=nginx:alpine We can run kubectl get all to see ...
- kubernetes kubectl 命令自动补全
yum install -y bash-completion source /usr/share/bash-completion/bash_completion source <(kubectl ...
- Kubernetes源码之旅:从kubectl到API Server
概述: Kubernetes项目目前依然延续着之前爆炸式的扩张.急需能够理解Kubernetes原理并且贡献代码的软件开发者.学习Kubernetes源码并不容易.Kubernetes是使用相对年轻的 ...
- kubernetes入门(08)kubernetes单机版的安装和使用
kubectl get - 类似于 docker ps ,查询资源列表 kubectl describe - 类似于 docker inspect ,获取资源的详细信息 kubectl logs - ...
- yum方式安装kubernetes
环境准备 master01 node01 node02,连通网络,修改hosts文件,确认3台主机相互解析 vim /etc/hosts 127.0.0.1 localhost localhost.l ...
随机推荐
- 20172308 实验二《Java面向对象程序设计 》实验报告
20172308 2017-2018-2 <程序设计与数据结构>实验2报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 周亚杰 学号:20172308 实验教师:王 ...
- 求int型数组和最大子数组 续
之前的博文里已经实现过该程序的构思.编译.运行,本次就不再重复与之相雷同的内容. 题目:与别人借组,借助求int型数组最大和子数组的问题,考虑大数溢出和int取值范围的问题 要求: 调试程序 当子数 ...
- 图论 Kruskal算法 并查集
#include<iostream> #include<cstring> #include<string> #include<cstdio> #incl ...
- 【贪心算法】POJ-1017
一.题目 Description A factory produces products packed in square packets of the same height h and of th ...
- profibus总线和profibus dp的区别
profibus总线和profibus dp的区别:PROFBUS是一种国际性的开放式的现场总线标准,它既可以用于高速并且对于时间苛求的数据传输,也可以用于大范围的复杂通讯场合.PROFBUS-DP是 ...
- vue 选项卡(转载)
!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-e ...
- ubuntu下安装lamp环境
使用普通用户来安装lamp环境: 1.安装apache: sudo apt-get install apache2
- http和https的优缺点,区别与工作原理
文章内容 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可 ...
- 深入理解JAVA虚拟机阅读笔记6——线程安全与锁优化
线程安全:如果一个对象可以安全的被多个线程同时使用,那它就是线程安全的. 一.Java中的线程安全 1.不可变 不可变的对象一定是线程安全的.String.枚举类型.java.lang.Number的 ...
- BZOJ3252 攻略(贪心+dfs序+线段树)
考虑贪心,每次选价值最大的链.选完之后对于链上点dfs序暴力修改子树.因为每个点最多被选一次,复杂度非常正确. #include<iostream> #include<cstdio& ...