kubernetes中kubectl的管理方法有哪些
发表于:2025-01-24 作者:千家信息网编辑
千家信息网最后更新 2025年01月24日,这篇文章为大家分享kubernetes中kubectl的管理方法。文章涵盖kubectl 陈述式管理方法、kubectl 声明式管理方法以及kubectl的核心资源介绍,希望大家通过这篇文章能有所收获
千家信息网最后更新 2025年01月24日kubernetes中kubectl的管理方法有哪些
这篇文章为大家分享kubernetes中kubectl的管理方法。文章涵盖kubectl 陈述式管理方法、kubectl 声明式管理方法以及kubectl的核心资源介绍,希望大家通过这篇文章能有所收获。
一、kubectl 陈述式管理方法
kubectl小洁:kubectl是官方的CLI命令行工具,用于apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理k8s各种资源的一种有效途径。1、查看当前集群所有命名空间[root@test-nodes1 ~]# kubectl get namespaceNAME STATUS AGEdefault Active 42hkube-node-lease Active 42hkube-public Active 42hkube-system Active 42h------------------------------------------------------------------------------------------2、查看default命名空间下的所有资源[root@test-nodes1 ~]# kubectl get all -n defaultNAME READY STATUS RESTARTS AGEpod/nginx-ds-76fr8 0/1 ImagePullBackOff 0 39hpod/nginx-ds-zz7jn 0/1 ErrImagePull 0 39hpod/nginx-ds1-qg45q 1/1 Running 0 39hpod/nginx-ds1-whnmv 1/1 Running 0 39h#pod资源NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1 443/TCP 42h#service资源NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGEdaemonset.apps/nginx-ds 2 2 0 2 0 39hdaemonset.apps/nginx-ds1 2 2 2 2 2 39h#pod控制器------------------------------------------------------------------------------------------3、创建与删除命名空间[root@test-nodes1 ~]# kubectl create namespace testnamespace/test created[root@test-nodes1 ~]# kubectl get namespaceNAME STATUS AGEdefault Active 42hkube-node-lease Active 42hkube-public Active 42hkube-system Active 42htest Active 6s[root@test-nodes1 ~]# kubectl delete namespace testnamespace "test" deleted------------------------------------------------------------------------------------------4、创建deployment(pods控制器)资源[root@test-nodes1 ~]# kubectl create deployment nignx-dp --image=test-harbor.cedarhd.com/public/nginx:v1.7.9 -n kube-publicdeployment.apps/nignx-dp created------------------------------------------------------------------------------------------5、查看kube-public命名空间下的deployment资源控制器[root@test-nodes1 ~]# kubectl get deployment -n kube-publicNAME READY UP-TO-DATE AVAILABLE AGEnignx-test 0/1 1 0 70s------------------------------------------------------------------------------------------6、查看kube-public命名空间下pods的运行情况[root@test-nodes1 ~]# kubectl get deployment -n kube-publicNAME READY UP-TO-DATE AVAILABLE AGEnignx-test 1/1 1 1 21s------------------------------------------------------------------------------------------7、查看kube-public空间下的pods资源[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnignx-dp-7f6d4979bd-nnc57 0/1 ImagePullBackOff 0 13m 172.7.21.4 test-nodes1.cedarhd.com nignx-test-655d6fbcb5-r9t57 1/1 Running 0 4m1s 172.7.22.4 test-nodes2.cedarhd.com ------------------------------------------------------------------------------------------8、在test-nodes2.cedarhd.com节点上curl 172.7.22.4 nginx[root@test-nodes2 ~]# curl 172.7.22.4Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.
------------------------------------------------------------------------------------------9、查看deployment下具体pods(nginx-test)详细信息[root@test-nodes1 ~]# kubectl describe deployment nignx-test -n kube-publicName: nignx-testNamespace: kube-publicCreationTimestamp: Mon, 03 Feb 2020 02:02:59 -0500Labels: app=nignx-testAnnotations: deployment.kubernetes.io/revision: 1Selector: app=nignx-testReplicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailableStrategyType: RollingUpdateMinReadySeconds: 0RollingUpdateStrategy: 25% max unavailable, 25% max surgePod Template: Labels: app=nignx-test Containers: nginx: Image: test-harbor.cedarhd.com/public/nginx:v1.7.9 Port: Host Port: Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailableOldReplicaSets: NewReplicaSet: nignx-test-655d6fbcb5 (1/1 replicas created)Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 22m deployment-controller Scaled up replica set nignx-test-655d6fbcb5 to 1 ------------------------------------------------------------------------------------------10、进入nginx-test pod资源[root@test-nodes1 ~]# kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGEnignx-test-655d6fbcb5-r9t57 1/1 Running 0 24m[root@test-nodes1 ~]# kubectl exec -ti nignx-test-655d6fbcb5-r9t57 /bin/bash -n kube-public#与docker 操作方法一致root@nignx-test-655d6fbcb5-r9t57:/# ip addr1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever12: eth0@if13: mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:07:16:04 brd ff:ff:ff:ff:ff:ff inet 172.7.22.4/24 brd 172.7.22.255 scope global eth0 valid_lft forever preferred_lft forever------------------------------------------------------------------------------------------11、删除pod资源(即重启)[root@test-nodes1 ~]# kubectl delete pod nignx-test-655d6fbcb5-r9t57 -n kube-publicpod "nignx-test-655d6fbcb5-r9t57" deleted[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnignx-dp-7f6d4979bd-nnc57 0/1 ImagePullBackOff 0 40m 172.7.21.4 test-nodes1.cedarhd.com nignx-test-655d6fbcb5-c42z8 1/1 Running 0 18s 172.7.21.5 test-nodes1.cedarhd.com ------------------------------------------------------------------------------------------12、删除deployment[root@test-nodes1 ~]# kubectl get deploy -n kube-publicNAME READY UP-TO-DATE AVAILABLE AGEnignx-dp 0/1 1 0 53mnignx-test 1/1 1 1 36m[root@test-nodes1 ~]# kubectl delete deploy nignx-dp -n kube-publicdeployment.extensions "nignx-dp" deleted------------------------------------------------------------------------------------------13、为nginx-dp pod资源创建service资源,保障pod的高可用,通过集群IP访问[root@test-nodes1 ~]# kubectl get all -n kube-publicNAME READY STATUS RESTARTS AGEpod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 17sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-dp 1/1 1 1 17sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-dp-5b9b697bcc 1 1 1 17s[root@test-nodes1 ~]# kubectl get pods -n kube-public -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 3m19s 172.7.21.4 test-nodes1.cedarhd.com [root@test-nodes1 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-publicservice/nginx-dp exposed[root@test-nodes1 ~]# kubectl get svc -n kube-public #查看service资源NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx-dp ClusterIP 192.168.224.56 80/TCP 12m[root@test-nodes1 ~]# kubectl get all -n kube-publicNAME READY STATUS RESTARTS AGEpod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 5m21sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/nginx-dp ClusterIP 192.168.224.56 80/TCP 79sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-dp 1/1 1 1 5m21sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-dp-5b9b697bcc 1 1 1 5m21s[root@test-nodes1 ~]# curl 192.168.224.56Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.
Thank you for using nginx.
[root@test-nodes1 ~]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConnTCP 192.168.0.1:443 nq -> 10.3.153.221:6443 Masq 1 0 0 -> 10.3.153.222:6443 Masq 1 0 0 TCP 192.168.224.56:80 nq -> 172.7.21.4:80 Masq 1 0 0
二、kubectl 声明式管理方法
小结:声明式资源管理方法依赖于-资源配置清单(yaml\json),偏于修改POD配置。
1、查看当前pod的资源配置清单[root@test-nodes1 ~]# kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGEnginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 5h31m-nodes1.cedarhd.com [root@test-nodes1 ~]# kubectl get pods nginx-dp-5b9b697bcc-jtrlp -n kube-public -o yamlapiVersion: v1kind: Pod #类型为podmetadata: creationTimestamp: "2020-02-03T07:50:11Z" generateName: nginx-dp-5b9b697bcc- labels: app: nginx-dp pod-template-hash: 5b9b697bcc name: nginx-dp-5b9b697bcc-jtrlp namespace: kube-public ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: nginx-dp-5b9b697bcc uid: 30bbaf90-c97e-4167-9419-45a632e9b3ce resourceVersion: "222257" selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-5b9b697bcc-jtrlp uid: 59d63844-8ee7-4d7e-8536-d4e5de1ba903spec: containers: - image: test-harbor.cedarhd.com/public/nginx:v1.7.9 imagePullPolicy: IfNotPresent name: nginx resources: {} terminationMessagePath: /dev/termination-log terminationMessagePolicy: File volumeMounts: - mountPath: /var/run/secrets/kubernetes.io/serviceaccount name: default-token-nt4w9 readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: test-nodes1.cedarhd.com priority: 0 restartPolicy: Always schedulerName: default-scheduler securityContext: {} serviceAccount: default serviceAccountName: default terminationGracePeriodSeconds: 30 tolerations: - effect: NoExecute key: node.kubernetes.io/not-ready operator: Exists tolerationSeconds: 300 - effect: NoExecute key: node.kubernetes.io/unreachable operator: Exists tolerationSeconds: 300 volumes: - name: default-token-nt4w9 secret: defaultMode: 420 secretName: default-token-nt4w9status: conditions: - lastProbeTime: null lastTransitionTime: "2020-02-03T07:50:11Z" status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: "2020-02-03T07:50:14Z" status: "True" type: Ready - lastProbeTime: null lastTransitionTime: "2020-02-03T07:50:14Z" status: "True" type: ContainersReady - lastProbeTime: null lastTransitionTime: "2020-02-03T07:50:11Z" status: "True" type: PodScheduled containerStatuses: - containerID: docker://745d5ad3412e5bccf2fb27dacce57e76987e8f6881cdb3aec79912888ba37ad6 image: test-harbor.cedarhd.com/public/nginx:v1.7.9 imageID: docker-pullable://test-harbor.cedarhd.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2 lastState: {} name: nginx ready: true restartCount: 0 state: running: startedAt: "2020-02-03T07:50:13Z" hostIP: 10.3.153.221 phase: Running podIP: 172.7.21.4 qosClass: BestEffort startTime: "2020-02-03T07:50:11Z"------------------------------------------------------------------------------------------2、获取service资源的配置清单[root@test-nodes1 ~]# kubectl get service -n kube-publicNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx-dp ClusterIP 192.168.224.56 80/TCP 5h31m[root@test-nodes1 ~]# kubectl get service nginx-dp -o yaml -n kube-publicapiVersion: v1kind: Service #类型为servicemetadata: creationTimestamp: "2020-02-03T07:54:13Z" labels: app: nginx-dp name: nginx-dp namespace: kube-public resourceVersion: "222606" selfLink: /api/v1/namespaces/kube-public/services/nginx-dp uid: 1b2310b5-6016-4692-b632-5c43d6dc4de5spec: clusterIP: 192.168.224.56 ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx-dp sessionAffinity: None type: ClusterIPstatus: loadBalancer: {} ------------------------------------------------------------------------------------------3、explain查看字段帮助说明kubectl explain service.metadata ------------------------------------------------------------------------------------------4、新建一个service的资源配置清单[root@test-nodes1 ~]# vi nginx-ds-svc.yamlapiVersion: v1kind: Servicemetadata: labels: app: nginx-ds name: nginx-ds namespace: kube-publicspec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx-ds sessionAffinity: None type: ClusterIP------------------------------------------------------------------------------------------5、通过声明式资源配置清单创建一个service资源[root@test-nodes1 ~]# kubectl create -f nginx-ds-svc.yaml service/nginx-ds created[root@test-nodes1 ~]# kubectl get service -n kube-publicNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx-dp ClusterIP 192.168.224.56 80/TCP 5h39mnginx-ds ClusterIP 192.168.66.3 80/TCP 16s------------------------------------------------------------------------------------------6、在线修改nginx-ds的 service 资源端口为81[root@test-nodes1 ~]# kubectl get svc -n kube-publicNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx-dp ClusterIP 192.168.224.56 80/TCP 5h49mnginx-ds ClusterIP 192.168.66.3 80/TCP 10m[root@test-nodes1 ~]# kubectl edit svc nginx-ds -n kube-public# 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: v1kind: Servicemetadata: creationTimestamp: "2020-02-03T13:23:41Z" labels: app: nginx-ds name: nginx-ds namespace: kube-public resourceVersion: "250724" selfLink: /api/v1/namespaces/kube-public/services/nginx-ds uid: 5840630d-e00d-4e98-91a1-2b65a1eb22f4spec: clusterIP: 192.168.66.3 ports: - port: 81 #修改对外端口81 protocol: TCP targetPort: 80 selector: app: nginx-ds sessionAffinity: None type: ClusterIPstatus: loadBalancer: {}"/tmp/kubectl-edit-wp634.yaml" 27L, 684C writtenservice/nginx-ds edited[root@test-nodes1 ~]# kubectl get svc -n kube-publicNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx-dp ClusterIP 192.168.224.56 80/TCP 5h50mnginx-ds ClusterIP 192.168.66.3 81/TCP 10m------------------------------------------------------------------------------------------7、删除一个service资源[root@test-nodes1 ~]# kubectl delete svc nginx-ds -n kube-publicservice "nginx-ds" deleted
三、关于kubectl的核心资源理解
[root@test-nodes1 ~]# kubectl get all -n kube-publicNAME READY STATUS RESTARTS AGEpod/nginx-dp-5b9b697bcc-jtrlp 1/1 Running 0 6h2m#pod资源,承载容器应用所在NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/nginx-dp ClusterIP 192.168.224.56 80/TCP 5h67m#service资源,Service是Kubernetes里最核心的资源对象之一,Service定义了一个服务的访问入口地址,前端的应用(Pod)通过这个入口地址访问其背后的一组由Pod副本组成的集群实力NAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-dp 1/1 1 1 6h2m#deployment资源,Deployment同样也是Kubernetes系统的一个核心概念,主要职责和RC一样的都是保证Pod的数量和健康,可理解为pod控制器,当我们删除pod时,会再启动,不是真正删除,如需删除该pod,必须删除该控制器NAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-dp-5b9b697bcc 1 1 1 6h2m
四、通过kubectl单独创建一个pod\deployment\service
1、创建一个nginx pod
[root@test-nodes1 ~]# vi nginx-pod.yaml
apiVersion: v1kind: Podmetadata: name: nginx labels: app: webspec: containers: - name: nginx image: test-harbor.cedarhd.com/public/nginx:v1.7.9 ports: - containerPort: 80[root@test-nodes1 ~]# kubectl create -f nginx-pod.yaml #创建一个PODpod/nginx created[root@test-nodes1 ~]# kubectl get allNAME READY STATUS RESTARTS AGEpod/nginx 1/1 Running 0 10m #刚刚创建的PODpod/nginx-ds1-qg45q 1/1 Running 0 47hpod/nginx-ds1-whnmv 1/1 Running 0 47hNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1
443/TCP 2d2hNAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGEdaemonset.apps/nginx-ds1 2 2 2 2 2 47h------------------------------------------------------------------------------------------2、为该pod创建一个service[root@test-nodes1 ~]# kubectl expose pod nginx --port=80 -n defaultservice/nginx exposed[root@test-nodes2 ~]# kubectl get all NAME READY STATUS RESTARTS AGEpod/nginx 1/1 Running 0 14mpod/nginx-ds1-qg45q 1/1 Running 0 47hpod/nginx-ds1-whnmv 1/1 Running 0 47hNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1 443/TCP 2d2hservice/nginx ClusterIP 192.168.123.163 80/TCP 107sNAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGEdaemonset.apps/nginx-ds1 2 2 2 2 2 47h[root@test-nodes2 ~]# curl 192.168.123.163 获取nginx页面------------------------------------------------------------------------------------------3、创建一个deployment[root@test-nodes2 ~]# kubectl create deployment nginx-test --image=test-harbor.cedarhd.com/public/nginx:v1.7.9deployment.apps/nginx-test created[root@test-nodes2 ~]# kubectl get allNAME READY STATUS RESTARTS AGEpod/nginx 1/1 Running 0 27mpod/nginx-ds1-qg45q 1/1 Running 0 47hpod/nginx-ds1-whnmv 1/1 Running 0 47hpod/nginx-test-5674474869-5nr7j 1/1 Running 0 4sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1 443/TCP 2d2hservice/nginx ClusterIP 192.168.123.163 80/TCP 15mNAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGEdaemonset.apps/nginx-ds1 2 2 2 2 2 47hNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-test 1/1 1 1 4sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-test-5674474869 1 1 1 4s
以上就是kubernetes中kubectl管理方法的介绍,内容较为全面,小编相信有部分知识点可能是我们日常工作可能会见到或用到的。希望你能通过这篇文章学到更多知识。
资源
方法
管理
空间
配置
控制器
清单
控制
核心
资源配置
命令
篇文章
集群
信息
入口
地址
知识
端口
类型
应用
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发用户故事的案例
淮安技术管理软件开发平台
ssm软件开发环境
配置型管理软件开发平台
免费无代码软件开发
兴通科技互联网公司
微服务架构 数据库
阿里服务器托管
金融的网络安全产品厂家
荒野行动最新服务器
数据库和java结合实验
教务网络安全的制度与要求任务点
江苏通用软件开发销售价格
永济软件开发系统
联想sr258服务器调试教程
星球软件开发商
特斯拉服务器调试
网络安全专家谭晓生视频
网络安全整治清查总结
境外网络服务器
淮北求职招聘软件开发公司
网络安全公司从业人员人数
服务器 几个 哈利波特
益阳帮助软件开发服务
软件开发成本 差异
太原理工大数据库原理及应用试卷
ddns如何设置服务器
小哲网络安全那些事
查询数据库判断是否有数据
教务网络安全的制度与要求任务点