k8s命令操作等记录
Kubernetes 命令一 kuberctl1 查看命名空间中的资源kubectl get pods --namespace kube-system2 查看命名空间的描述kubectl describe ns kube-systemName:kube-systemLabels:kubernetes.io/metadata.name=kube-systemAnnotations:<none&
选择合适的Kubernetes (K8S) 版本时,通常会选择那些标记为“LTS”(Long Term Support)的版本,因为这些版本通常更稳定并提供更长时间的支持。Kubernetes 1.21 LTS版本被认为是最稳定的版本之一
Kubernetes 命令
k8s的更新日志
https://github.com/kubernetes/kubernetes/tree/master/CHANGELOG
kubectl命令自动补全
安装bash-completion工具
yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
以上只是临时起作用,每次登录终端都得起效需要加入到/root/.bashrc文件中
echo "source /usr/share/bash-completion/bash_completion " >> /.bashrc
echo "source <(kubectl completion bash)" >> /.bashrc
echo "source <(helm completion bash)" >> ~/.bashrc
一 kuberctl
1 查看命名空间中的资源
kubectl get pods --namespace kube-system
2 查看命名空间的描述
kubectl describe ns kube-system
Name: kube-system
Labels: kubernetes.io/metadata.name=kube-system
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
3 describe pod 查看pod的相关信息
kubectl describe pods kube-flannel-ds-amd64-zqs6k --namespace kube-system
4 describe node
kubectl describe node k8s-master1
5 获取dashboard的相关信息
kubectl get pods,svc -n kubernetes-dashboard
##获取网络映射
kubectl get svc -n kubernetes-dashboard
6 获取所有的命名空间
kubectl get pods --all-namespaces
7 直接删除命名空间
kubectl delete namespaces kubernetes-dashboard
8 删除命名空间中的节点
--force 强制
kubectl delete pods kube-flannel-ds-amd64-x9hg9 -n kube-system --force
9 访问命名空间
curl --insecure https://192.168.56.200:6443/api/v1/kube-system
10.1 查看需要授权证书的节点
[root@k8s-master logs]# kubectl get csr
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
node-csr-tsic3mMf63p3vsp_7fZDJ_OrdLPNu0I1B8Z82kGzblQ 17h kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap <none> Approved
10.2 已经授权的证书无法在deny,但可以通过delete来进行删除操作
[root@k8s-master logs]# kubectl get csr
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
node-csr-tsic3mMf63p3vsp_7fZDJ_OrdLPNu0I1B8Z82kGzblQ 17h kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap <none> Approved
#已经授权的证书无法在deny,但可以通过delete来进行删除操作
[root@k8s-master logs]# kubectl delete csr node-csr-tsic3mMf63p3vsp_7fZDJ_OrdLPNu0I1B8Z82kGzblQ
certificatesigningrequest.certificates.k8s.io "node-csr-tsic3mMf63p3vsp_7fZDJ_OrdLPNu0I1B8Z82kGzblQ" deleted
[root@k8s-master logs]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
etcd-0 Healthy {"health":"true","reason":""}
controller-manager Healthy ok
[root@k8s-master logs]# kubectl get csr
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
node-csr-tsic3mMf63p3vsp_7fZDJ_OrdLPNu0I1B8Z82kGzblQ 4s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap <none> Pending
二 Deployment 应用
1 Pod 容器的集合,通常将紧密相关的一组容器放到一个pod中,同一个pod中的所有容器共享IP地址和
Port空间,它们在一个network namespace中
2 k8s部署一个应用
kubectl run 命名 --image=docker.io/kubernetes-bootcamp:v1 --port=8080
默认情况下,所有pod只能在集群内部访问,为了能够从外部访问,需要将容器的8080端口映射到节点的端口
kubectl expose deployment/kubernetes-bootcamp --type=“NodePort” --port 8080
3 k8s获取端口映射
kubectl get services
curl 主机名:映射的端口
4 k8s获取副本数
kubectl get deployments
5 scale
k8s将副本数增加到3个
kubectl scale deployments/kubernetes-bootcamp --replicas=3
[root@k8s-master1 package]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/3 3 1 2d4h
[root@k8s-master1 package]# kubectl delete deployments/nginx-deployment
deployment.apps "nginx-deployment" deleted
6 k8s删除一个副本
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get deployments
kubectl get pods
7 滚动更新
kubectl set image deployments/kubernetes-bootcamp \
kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
8 回退到v1版本
kubectl rellout undo deployments/kubernetes-bootcamp
验证 curl 主机名:映射的端口
9 获取某一概念的帮助explain
kubectl explain deloyment
kubectl explain 命令用于查看某个 Kubernetes 对象的详细信息,例如对象的定义、字段、类型和其他相关信息。这个命令可以用来了解和掌握 Kubernetes 对象的结构和行为。
1. 如果你想查看某个 Kubernetes 命令的用法,可以使用以下步骤:
使用 kubectl help 命令来查看所有可用命令的列表,找到你想要查看用法的命令。例如,如果你想查看 kubectl expose 命令的用法,可以输入以下命令:
kubectl help expose
2. 如果你想查看命令的详细用法信息,可以使用 kubectl help <command> --detail 命令。例如,如果你想查看 kubectl expose 命令的详细用法信息,可以输入以下命令:
kubectl help expose --detail
这将显示一个非常详细的使用说明,包括命令的各种选项和参数。
3. 如果你想查看某个特定参数的用法,可以使用 kubectl help <command> <parameter> 命令。例如,如果你想查看 kubectl expose 命令中的 --type 参数的用法,可以输入以下命令:
kubectl help expose --type
这将显示该参数的用法信息。
10 kubectl edit查看 集群中当前运行的资源
[root@k8s-master1 cfg]# kubectl edit deployment nginx-deployment
#Please edit the object below. Lines beginning with a '#' will be ignored,
#and an empty file will abort the edit. If an error occurs while saving this file will be
#reopened with the relevant failures.
#
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:stable","name":"nginx","ports":[{"containerPort":80}]}]}}}}
creationTimestamp: "2021-09-06T07:07:49Z"
generation: 1
labels:
app: nginx
name: nginx-deployment
namespace: default
resourceVersion: "117504"
uid: 7c4a2831-edcb-4db9-85f8-b557ee2debd7
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:stable
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
resources: {}
11 销毁
如果只是短时间内不用可以只删除service,如果长时间不用删除deployment和service
[root@kube-master ~]# kubectl delete -f dashboard-service.yaml
service "kubernetes-dashboard" deleted
[root@kube-master ~]# kubectl delete -f dashboard-deployment.yaml
deployment "kubernetes-dashboard-latest" deleted
12 授权
kubectl get clusterrolebinding #查看集群中的clusterrolebingding
kubectl get clusterrolebinding cluster-admin -o yaml #获取名称为cluster-admin的clusterrolebingding配置信息
13 显示完整信息
-o wide 查看详细信息
[root@k8s-master1 ~]# kubectl get pod,svc --all-namespaces -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
default pod/busybox 1/1 Running 54 169d 10.244.1.172 k8s-node1 <none> <none>
default pod/nginx-deployment-5ff58d798d-lxvbn 1/1 Running 17 159d 10.244.0.48 k8s-master1 <none> <none>
default pod/nginx-deployment-5ff58d798d-r2k8b 1/1 Running 22 162d 10.244.0.35 k8s-master1 <none> <none>
default pod/producer-consumer 2/2 Running 16 153d 10.244.0.43 k8s-master1 <none> <none>
kube-system pod/blackbox-exporter-799f9c6db6-46ckf 1/1 Running 1 70d 10.244.1.173 k8s-node1 <none> <none>
kube-system pod/cadvisor-62qdw 1/1 Running 1 70d 192.168.56.201 k8s-node1 <none> <none>
kube-system pod/cadvisor-6b8mp 1/1 Running 1 70d 192.168.56.200 k8s-master1 <none> <none>
kube-system pod/coredns-7fddc7545-4l6pm 1/1 Running 37 169d 10.244.1.193 k8s-node1 <none> <none>
kube-system pod/kube-flannel-ds-98mlk 1/1 Running 20 168d 192.168.56.200 k8s-master1 <none> <none>
kube-system pod/kube-flannel-ds-cdjgn 1/1 Running 10 166d 192.168.56.201 k8s-node1 <none> <none>
kube-system pod/kube-state-metrics-5c896ff656-mtqxl 1/1 Running 4 70d 10.244.0.52 k8s-master1 <none> <none>
kube-system pod/node-exporter-2gwkv 1/1 Running 1 70d 192.168.56.200 k8s-master1 <none> <none>
kube-system pod/node-exporter-zf8dq 1/1 Running 1 70d 192.168.56.201 k8s-node1 <none> <none>
kubernetes-dashboard pod/dashboard-metrics-scraper-79c5968bdc-fq4s9 1/1 Running 34 166d 10.244.0.49 k8s-master1 <none> <none>
kubernetes-dashboard pod/kubernetes-dashboard-66cfbc8556-s45rx 1/1 Running 18 162d 10.244.1.195 k8s-node1 <none> <none>
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 166d <none>
default service/nginx-service NodePort 10.0.0.126 <none> 80:32153/TCP 162d app=nginx
default service/web-svc NodePort 10.0.0.204 <none> 8080:30392/TCP 154d run=web
kube-system service/blackbox-exporter ClusterIP 10.0.0.151 <none> 9115/TCP 70d app=blackbox-exporter
kube-system service/kube-dns ClusterIP 10.0.0.2 <none> 53/UDP,53/TCP,9153/TCP 169d k8s-app=kube-dns
kubernetes-dashboard service/dashboard-metrics-scraper ClusterIP 10.0.0.177 <none> 8000/TCP 172d k8s-app=dashboard-metrics-scraper
kubernetes-dashboard service/kubernetes-dashboard NodePort 10.0.0.138 <none> 443:30001/TCP 172d k8s-app=kubernetes-dashboard
14 创建命名空间
kubectl create namespace test-env
kubectl run nginx --image=nginx --namespace=test-env
15 如何在节点上使用kubectl命令
[root@k8s-master1 ~]# scp -r ~/.kube 192.168.56.201:~/
[root@k8s-master1 ~]# scp /usr/bin/kubectl 192.168.56.201:/usr/bin
16 查看日志
kubectl logs container_name -n namespace
17 导出yaml
导出Pod的配置信息到文件:
kubectl get pod my-pod -n my-namespace -o yaml > pod.yaml
导出Deployment的配置信息到文件:
kubectl get deployment my-deployment -n my-namespace -o yaml > deployment.yaml
导出Service的配置信息到文件:
kubectl get service my-service -n my-namespace -o yaml > service.yaml
18 复制本地文件到pod
kubectl cp <本地路径> <目标Pod名称>:<容器内的目标路径>
19 查看日志
kubectl logs -f calico-node-2w9vh -n kube-system --tail I0
-f 追踪日志
–tail 从后到前,查看日志多少行
更多推荐
所有评论(0)