第一章:Kubectl命令使用
- 1.1、 kubectl命令行工具
- 1.2、 apply和create的区别
- 1.3、 启动单实例nginx&&dry run输出yaml文件
- 1.4、 get、delete、set、edit命令使用
- 1.5、 日志查看命令
-
- 1.6、 容器内的执行命令
第一章:Kubectl命令使用
1.1、Kubectl命令行自动补全
https://kubernetes.io/zh-cn/docs/reference/kubectl/cheatsheet/
source <(kubectl completion bash) # 在 bash 中设置当前 shell 的自动补全,要先安装 bash-completion 包。
echo "source <(kubectl completion bash)" >> ~/.bashrc # 在你的 bash shell 中永久地添加自动补全
验证自动补全
kubectl get后tab键按两下
k8s集群会读取如下的文件配置
[root@k8s-master01 ~]# ll /etc/kubernetes/admin.kubeconfig
-rw------- 1 root root 6451 Nov 24 13:00 /etc/kubernetes/admin.kubeconfig
这个文件和cat ~/.kube/config文件信息是一致的
记录了链接apiserver的信息
- 在cka考试过程中,每次都会使用kubectl config use-context hk8s
[root@k8s-master01 ~]# kubectl config use-context hk8s
error: no context exists with the name: "hk8s"
1.2、apply -f和create -f的区别
1、在二进制安装k8s的1.24集群的时候,我们已经部署了dashboard,继续使用create -f的时候,会提示已经创建过
[root@k8s-master01 dashboard]# kubectl create -f dashboard.yaml
Error from server (AlreadyExists): error when creating "dashboard.yaml": namespaces "kubernetes-dashboard" already exists
2、然而使用kubectl apply -f dashboard.yaml的时候他并不会报错,而是会重新去加载更新配置(就算配置没有做任何变更)
- 给多个yaml文件进行创建
kubectl create -f dashboard.yaml -f nginx.yaml
或者
kubectl create -f dashboard.yaml,nginx.yaml
1.3、 启动单实例nginx&&dry run输出yaml文件
1、 启动单实例nginx
[root@k8s-master01 dashboard]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
You have new mail in /var/spool/mail/root
2、查看nginx的状态
[root@k8s-master01 dashboard]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 0/1 1 0 14s
3、直接输出nginx的yaml文件并没有去创建资源
[root@k8s-master01 dashboard]# kubectl create deployment nginx --image=nginx --dry-run -oyaml
W1125 23:07:18.231667 48012 helpers.go:636] --dry-run is deprecated and can be replaced with --dry-run=client.
看到提示:--dry-run已经过时了这个参数,需要替换为--dry-run=client
4、此时去查看发现它并没有实际运行,还是之前的那个nginx
[root@k8s-master01 dashboard]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 9m42s
5、也可以把这个输出为yaml文件,输出在当前目录下
[root@k8s-master01 practise]# kubectl create deployment nginx --image=nginx --dry-run -oyaml > pod.yaml
W1125 23:11:07.853077 50092 helpers.go:636] --dry-run is deprecated and can be replaced with --dry-run=client.
[root@k8s-master01 practise]# ll
total 4
-rw-r--r-- 1 root root 384 Nov 25 23:11 pod.yaml
1.3、 启动单实例nginx&&dry run输出yaml文件
- kubectl 的delete
1、删除这个deployment
[root@k8s-master01 practise]# kubectl delete deploy nginx
deployment.apps "nginx" deleted
You have new mail in /var/spool/mail/root
[root@k8s-master01 practise]# kubectl get deploy
No resources found in default namespace.
2、删除的时候需要注意命名空间,如果说你的文件没有指定namespace的时候,需要加上它的命名空间;如果文件中已经指定namespace的时候,删除的时候就不需要加上命名空间
1.4、 get、delete、set、edit命令使用
- kubectl的get命令文章来源:https://www.toymoban.com/news/detail-404641.html
1、当前命名空间下的所有services
[root@k8s-master01 practise]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 30d
You have new mail in /var/spool/mail/root
2、查看指定命名空间下的services
[root@k8s-master01 practise]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
calico-typha ClusterIP 192.168.59.207 <none> 5473/TCP 33h
kube-dns ClusterIP 192.168.0.10 <none> 53/UDP,53/TCP,9153/TCP 33h
metrics-server ClusterIP 192.168.153.84 <none> 443/TCP 33h
3、查看所有命名空间下的services
[root@k8s-master01 practise]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 30d
kube-system calico-typha ClusterIP 192.168.59.207 <none> 5473/TCP 33h
kube-system kube-dns ClusterIP 192.168.0.10 <none> 53/UDP,53/TCP,9153/TCP 33h
kube-system metrics-server ClusterIP 192.168.153.84 <none> 443/TCP 33h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 192.168.116.130 <none> 8000/TCP 33h
kubernetes-dashboard kubernetes-dashboard NodePort 192.168.141.173 <none> 443:31790/TCP 33h
4、输出所有命名空间下pod的ip地址:
[root@k8s-master01 practise]# kubectl get pod -A -owide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default nginx 1/1 Running 0 101m 172.25.92.65 k8s-master02 <none> <none>
kube-system calico-kube-controllers-7949fc8985-ftnqd 1/1 Running 0 33h 172.17.125.5 k8s-node01 <none> <none>
- 没有命名空间隔离性
[root@k8s-master01 practise]# kubectl get node
[root@k8s-master01 practise]# kubectl get clusterrole
- 怎么知道大部分资源都是有命名空间隔离性的呢?
1、具有命名空间隔离性的资源
[root@k8s-master01 practise]# kubectl api-resources --namespaced=true
2、不具有命名空间隔离性的资源,把true改成false
[root@k8s-master01 practise]# kubectl api-resources --namespaced=true
- 根据名称进行排序
kubectl get po -n kube-system --sort-by=.metadata.name
- 在创建pod的时候都是有一个标签的,使用–show-labels,可以看到pod的这个标签
[root@k8s-master01 practise]# kubectl get po -n kube-system --show-labels
NAME READY STATUS RESTARTS AGE LABELS
calico-kube-controllers-7949fc8985-ftnqd 1/1 Running 0 33h k8s-app=calico-kube-controllers,pod-template-hash=7949fc8985
calico-node-2rm9m 1/1 Running 0 30h controller-revision-hash=f5f6f79bf,k8s-app=calico-node,pod-template-generation=1
- 过滤出标签为controller-revision-hash的pod
[root@k8s-master01 practise]# kubectl get po -n kube-system -l controller-revision-hash
NAME READY STATUS RESTARTS AGE
calico-node-2rm9m 1/1 Running 0 30h
calico-node-5ht5h 1/1 Running 2 30h
calico-node-ctxmw 1/1 Running 2 30h
calico-node-mw5xk 1/1 Running 1 30h
calico-node-rdpgn 1/1 Running 0 30h
- kubectl的set、replace、edit命令
1、使用edit编辑
[root@k8s-master01 practise]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
2、修改后会有提示
[root@k8s-master01 practise]# kubectl edit deployment nginx
deployment.apps/nginx edited
You have new mail in /var/spool/mail/root
3、kubectl set -h 查看命令帮助
4、对yaml文件进行某些更改后,使用replace一下,使用apply也是可以的
[root@k8s-master01 practise]# kubectl replace -f pod.yaml
deployment.apps/nginx replaced
1.5、容器日志命令查看
- logs只对pod有用,describe不仅仅对pod有用,对deployment也是有用的
1、注意命名空间:
[root@k8s-master01 practise]# kubectl logs -f calico-node-ctxmw
Error from server (NotFound): pods "calico-node-ctxmw" not found
2、要加上-n kube-system,-f表示实时打印
[root@k8s-master01 practise]# kubectl logs -f calico-node-ctxmw -n kube-system
3、把-f去掉之后,再加上--tail 10,只打印最近的10行日志不持续打印
[root@k8s-master01 practise]# kubectl logs calico-node-5ht5h -n kube-system --tail 10
4、如果里面有多个容器的时候使用-c
kubectl logs my-pod -c my-container # 获取 Pod 容器的日志(标准输出, 多容器场景)
5、不同状态的pod使用不同的命令去查看:
Containerreating的时候使用describe
- 把pod的日志文件导入到某个文件中:
1、logs查看信息:
[root@k8s-master01 practise]# kubectl logs nginx-8f458dc5b-ltfpl
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/11/25 15:49:03 [notice] 1#1: using the "epoll" event method
2022/11/25 15:49:03 [notice] 1#1: nginx/1.23.2
2022/11/25 15:49:03 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/11/25 15:49:03 [notice] 1#1: OS: Linux 4.19.12-1.el7.elrepo.x86_64
2022/11/25 15:49:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/11/25 15:49:03 [notice] 1#1: start worker processes
2022/11/25 15:49:03 [notice] 1#1: start worker process 29
2022/11/25 15:49:03 [notice] 1#1: start worker process 30
2、把这个pod的日志重定向到某个文件中
[root@k8s-master01 practise]# kubectl logs nginx-8f458dc5b-ltfpl > /root/practise/1.log
1.6、容器内执行命令
1、进入到容器内执行命令:
[root@k8s-master01 practise]# kubectl exec -ti calico-node-rdpgn -n kube-system -- sh
Defaulted container "calico-node" out of: calico-node, upgrade-ipam (init), install-cni (init)
sh-4.4# ls
bin boot dev etc home host included-source lib lib64 licenses lost+found media mnt opt proc root run srv sys tmp usr var
sh-4.4#
2、kubectl的top命令:
[root@k8s-master01 practise]# kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master01 222m 11% 1193Mi 63%
k8s-master02 225m 11% 1138Mi 60%
k8s-master03 225m 11% 1208Mi 64%
k8s-node01 115m 5% 886Mi 47%
k8s-node02 96m 4% 505Mi 27%
3、查看容器的使用量:
[root@k8s-master01 practise]# kubectl top pod
NAME CPU(cores) MEMORY(bytes)
nginx 0m 3Mi
nginx-8f458dc5b-ltfpl 0m 3Mi
查看指定命名空间下的pod,找出内存最高的:文章来源地址https://www.toymoban.com/news/detail-404641.html
[root@k8s-master01 practise]# kubectl top po -n kube-system
NAME CPU(cores) MEMORY(bytes)
calico-kube-controllers-7949fc8985-ftnqd 11m 23Mi
calico-node-2rm9m 45m 90Mi
calico-node-5ht5h 34m 176Mi
calico-node-ctxmw 39m 84Mi
calico-node-mw5xk 50m 101Mi
calico-node-rdpgn 44m 86Mi
calico-typha-5fd94485df-9j2zg 6m 28Mi
coredns-74469d9cc9-mtcqv 2m 18Mi
metrics-server-79dfb4cc89-lqtjs 6m 26Mi
[root@k8s-master01 practise]# echo "calico-node-5ht5h" > /root/practise/xxx
到了这里,关于kubectl的基础命令使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!