Linux|容器 & k8s——kubernetes kubectl工具使用

kubectl工具使用 一:kubectl使用 1.pod的查看

[root@master ~]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-f9fd979d6-9t6l5 1/1 Running 0 3h32m coredns-f9fd979d6-tq89x 1/1 Running 0 3h32m etcd-master 1/1 Running 0 3h32m kube-apiserver-master 1/1 Running 0 3h32m kube-controller-manager-master 1/1 Running 1 3h23m kube-flannel-ds-amd64-8r4vk 1/1 Running 0 3h29m kube-flannel-ds-amd64-mj6jf 1/1 Running 0 3h31m kube-flannel-ds-amd64-pwmjd 1/1 Running 0 3h29m kube-proxy-4zgnb 1/1 Running 0 3h21m kube-proxy-8kxfl 1/1 Running 0 3h22m kube-proxy-fxk72 1/1 Running 0 3h21m kube-scheduler-master 1/1 Running 0 3h23m metrics-server-799d467fd5-thq78 1/1 Running 0 3h6m

2.查看异常pod信息:
[root@master ~]# kubectl describe pods kube-flannel-ds-amd64-pwmjd -n kube-system Name: kube-flannel-ds-amd64-pwmjd Namespace: kube-system Priority: 0 Node: node-2/10.0.1.12 Start Time: Tue, 20 Oct 2020 14:32:10 +0800 Labels: app=flannel controller-revision-hash=76ccd4ff4f pod-template-generation=1 tier=node Annotations: Status: Running IP: 10.0.1.12 IPs: IP: 10.0.1.12 Controlled By: DaemonSet/kube-flannel-ds-amd64 ....

3.查看node节点:
[root@master ~]# kubectl get nodes -n kube-system NAME STATUS ROLES AGE VERSION master Ready master 3h38m v1.19.3 node-1 Ready 3h35m v1.19.3 node-2 Ready 3h34m v1.19.3

4.查看k8s集群信息
[root@master ~]# kubectl cluster-info Kubernetes master is running at https://10.0.1.10:6443 KubeDNS is running at https://10.0.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy Metrics-server is running at https://10.0.1.10:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. #加dump显示详细内容

5.查看service信息
[root@master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 25h nginx-deployment LoadBalancer 10.107.237.3380:31266/TCP 4h13m

6.查看所有名称空间内的资源
[root@master ~]# kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default nginx-deployment-585449566-9459t 1/1 Running 0 5h5m default nginx-deployment-585449566-z6qs8 1/1 Running 0 5h default nginx-vcc 1/1 Running 0 4h33m kube-system coredns-f9fd979d6-9t6l5 1/1 Running 0 25h kube-system coredns-f9fd979d6-tq89x 1/1 Running 0 25h kube-system etcd-master 1/1 Running 0 25h kube-system kube-apiserver-master 1/1 Running 0 25h kube-system kube-controller-manager-master 1/1 Running 2 24h kube-system kube-flannel-ds-amd64-8r4vk 1/1 Running 0 24h kube-system kube-flannel-ds-amd64-mj6jf 1/1 Running 0 25h kube-system kube-flannel-ds-amd64-pwmjd 1/1 Running 0 24h kube-system kube-proxy-4zgnb 1/1 Running 0 24h kube-system kube-proxy-8kxfl 1/1 Running 0 24h kube-system kube-proxy-fxk72 1/1 Running 0 24h kube-system kube-scheduler-master 1/1 Running 1 24h kube-system metrics-server-799d467fd5-thq78 1/1 Running 0 24h kubernetes-dashboard kubernetes-dashboard-699759bf-8jkgn 1/1 Running 0 24h kubernetes-dashboard kubernetes-metrics-scraper-864b4f5d68-j825l 1/1 Running 0 24h monitoring prometheus-core-7d5cf5dd-vmrwr 0/1 Pending 0 22h

7.总结
Basic Commands (Beginner): 基本命令(初学者):
create 创建从文件或stdin创建资源
expose 公开获取控制器、服务、部署或pod
【Linux|容器 & k8s——kubernetes kubectl工具使用】Kubernetes Service:Kubernetes服务:
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):基本命令(中级):
explain 解释资源文档
get 获取显示一个或多个资源
edit 编辑编辑服务器上的资源
delete 删除按文件名、stdin、资源和名称,或按资源和标签删除资源
Deploy Commands:部署命令:
rollout 卷展管理资源的卷展
scale 缩放设置部署、复制集或复制控制器的新大小
autoscale 自动缩放自动缩放部署、复制集或复制控制器
Cluster Management Commands:群集管理命令:
certificate 证书修改证书资源
cluster-info 集群信息显示集群信息
top 最高显示资源(CPU/内存/存储)使用率
cordon 警戒线将节点标记为不可调度
uncordon 未关联将节点标记为可调度
taint 污染更新一个或多个节点上的污染
Troubleshooting and Debugging Commands:疑难解答和调试命令:
describe 描述显示特定资源或资源组的详细信息
logs logs打印pod中容器的日志
attach 连接到正在运行的容器上
exec 在容器中执行命令
port-forward 端口转发将一个或多个本地端口转发到机架
proxy 代理运行kubernetesapi服务器的代理
cp cp在容器之间复制文件和目录 auth 授权检查授权
Advanced Commands:高级命令:
diff 实时版本与应用版本的差异
apply 应用按文件名或stdin对资源应用配置
patch 使用策略合并修补程序的资源的修补程序更新字段
replace 替换用文件名或stdin替换资源
wait 等待一个或多个资源上的特定条件
convert 在不同的API版本之间转换转换转换配置文件
kustomize kustomize从目录或远程url构建kustomization目标
Settings Commands:设置命令:
label 标签更新资源上的标签
annotate 批注更新资源上的批注
completion 完成输出指定shell(bash或zsh)的shell完成代码
Linux|容器 & k8s——kubernetes kubectl工具使用
文章图片

二:案例 1.创建包含nginx服务的pod并运行
[root@master ~]# kubectl run nginx-vcc --image=daocloud.io/library/nginx:latest --port=80 --image:指定镜像 --port:指定端口

2.查看创建的pod:
[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-vcc 1/1 Running 0 12m

3.创建完成查看详细信息:
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-vcc 1/1 Running 0 13m 10.244.1.3 node-1

4.在kubernetes集群内任意节点访问该nginx服务:
[root@master ~]# curl 10.244.1.3 Welcome to nginx! - 锐客网 > body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } ....

5.创建与管理service:
将pod创建完成后,访问该pod内的服务只能在集群内部通过pod的的地址去访问该服务;当该pod出现故障后,该pod的控制器会重新创建一个包括该服务的pod,此时访问该服务须要获取该服务所在的新的pod的地址去访问。可以创建一个service,当新的pod的创建完成后,service会通过pod的label连接到该服务,只需通过service即可访问该服务。
创建pod
[root@master nginx]# cat nginx-deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 [root@master nginx]# kubectl create -f nginx-deploy.yaml [root@master nginx]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-585449566-9459t1/1 Running 0 3m49s nginx-deployment-585449566-pn8w5 1/1Running 0 3m49s

删除pod
[root@master ~]# kubectl delete pod nginx-vcc

删除pod后,查看pod会自动创建新的pod
[root@master nginx]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-585449566-9459t 1/1 Running 0 5m39s nginx-deployment-585449566-z6qs8 1/1 Running 0 18s

6.暴露服务
[root@master nginx]# kubectl expose deployment nginx-deployment --port=80 --type=LoadBalancer

查看服务状态(查看对外的端口):
[root@master nginx]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 443/TCP 20h nginx-deployment LoadBalancer 10.107.237.3380:31266/TCP 6m1s

可以看到把端口分配给了31266,找到服务对应运行的节点,使用http://ip:31266进行访问
三:使用扩展 1.pod标签
解决同类型的资源对象越来越多,为了更好的管理,按照标签分组;
常见标签:
release(版本):stable(稳定版)、canary(金丝雀版本、可以理解为测试版)、beta(测试版)
environment(环境变量):dev(开发)、qa(测试)、production(生产)application(应用):ui、as(应用软件)、pc、sctier(架构层级):frontend(前端)、backend(后端)、cache(缓存、隐藏)partition(分区):customerA(客户A)、customerB(客户B)track(品控级别):daily(每天)、weekly(每周)K8s集群中虽然没有对有严格的要求,但是标签还是要做的,方便!

使用:
为现有的pod添加一个标签
[root@master nginx]# kubectl label pod nginx-vcc app=nginx -n default pod/nginx-vcc labeled 注意: -n: 指定namespect名字空间

查看pod标签

[root@master nginx]# kubectl get pods --show-labels -n default NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-585449566-9459t 1/1 Running 0 99m app=nginx,pod-template-hash=585449566 nginx-deployment-585449566-z6qs8 1/1 Running 0 94m app=nginx,pod-template-hash=585449566 nginx-vcc 1/1 Running 0 67m app=nginx,run=nginx-vcc

删除标签
[root@master nginx]# kubectl label pod nginx-vcc app- -n default pod/nginx-vcc labeled

修改标签
[root@master nginx]# kubectl label pod nginx-vcc release=stable -n default pod/nginx-vcc labeled [root@master nginx]# kubectl get pods --show-labels -n default NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-585449566-9459t 1/1 Running 0 112m app=nginx,pod-template-hash=585449566 nginx-deployment-585449566-z6qs8 1/1 Running 0 106m app=nginx,pod-template-hash=585449566 nginx-vcc 1/1 Running 0 80m app=vcc,release=stable,run=nginx-vcc [root@master nginx]# kubectl label pod nginx-vcc release=beta --overwrite -n default pod/nginx-vcc labeled [root@master nginx]# kubectl get pods --show-labels -n default NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-585449566-9459t 1/1 Running 0 112m app=nginx,pod-template-hash=585449566 nginx-deployment-585449566-z6qs8 1/1 Running 0 107m app=nginx,pod-template-hash=585449566 nginx-vcc 1/1 Running 0 81m app=vcc,release=beta,run=nginx-vcc

标签与标签选择器的关系:
如果标签有多个,标签选择器选择其中一个,也可以关联成功!
如果选择器有多个,那么标签必须满足条件,才可关联成功!
标签选择器:标签的查询过滤条件
基于等值关系的(equality-based):”=“、”==“、”!=“前两个等于,最后一个不等于
基于集合关系(set-based):in、notin、exists三种;
使用标签选择器的逻辑:
同时指定的多个选择器之间的逻辑关系为”与“操作;
使用空值的标签选择器意味着每个资源对象都将被选择中;
空的标签选择器无法选中任何资源;
2.node节点标签
查看标签
[root@master nginx]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS master Ready master 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.io/master= node-1 Ready 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-1,kubernetes.io/os=linux node-2 Ready 22h v1.19.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-2,kubernetes.io/os=linux

增加标签
[root@master nginx]# kubectl label nodes node-1 kubernetes.io/username=vcc node/node-1 labeled

减少标签
[root@master nginx]# kubectl label nodes node-1 a- node/node-1 labeled

修改标签
[root@master nginx]# kubectl label nodes node-1 kubernetes.io/username=vic --overwrite node/node-1 labeled

    推荐阅读