日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kubernetes的Kubectl命令的使用

發布時間:2024/1/1 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kubernetes的Kubectl命令的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Kubectl基礎命令的使用

文章目錄

  • Kubectl基礎命令的使用
      • kubernetes基本概念
      • edit(編輯資源)
      • scale(動態擴展)
      • autoscale(自動擴展)
      • cluster-info(顯示集群信息)
      • describe(顯示指定pod的詳細信息)
      • logs(查看日志)
      • attach(附加在一個容器里)
      • exec(執行容器命令)
      • prot-forward(轉發一個或多個端口到pod里面去)
      • cp(復制)
      • label(標簽)
      • api-resources(查看所有資源)
      • api-versions(API版本)
      • 部署deployment類型控制器pod

kubernetes基本概念

Pod:k8s最小部署單元,一組容器的集合

Deployment:最常見的控制器,用于更高級別部署和管理Pod

Service:為一組Pod提供負載均衡,對外提供統一訪問入口

Label:標簽,附加到某個資源上,用于關聯對象、查詢和篩選

Namespaces∶命名空間。將對象邏輯上隔離,也利于權限控制

edit(編輯資源)

#編輯一個資源 # 運行一個pod類型的nginx,名字叫nginx,定義的標簽是app=nginx [root@master ~]# kubectl run nginx --image=nginx --labels="app=nginx" pod/nginx created [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 12s# 使用edit命令編輯 [root@master ~]# kubectl edit pods/nginx # 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: v1 kind: Pod metadata:creationTimestamp: "2021-12-20T09:26:53Z"labels:app: test # 把原本app:nginx改為app:testname: nginxnamespace: default# 查看是否更改成功 [root@master ~]# kubectl describe pods/nginx Name: nginx Namespace: default Priority: 0 Node: node2.example.com/192.168.240.60 Start Time: Mon, 20 Dec 2021 04:26:53 -0500 Labels: app=test Annotations: <none> Status: Running IP: 10.244.1.25

scale(動態擴展)

#創建一個deployment類型的nginx2的pod [root@master ~]# kubectl create deployment nginx2 --image=nginx deployment.apps/nginx2 created [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 7m38s nginx2-85bf7b8976-n9vcl 1/1 Running 0 13s# 使用scale擴展 [root@master ~]# kubectl scale --replicas 4 deployment/nginx2 deployment.apps/nginx2 scaled # 擴展后查看多了幾個相同類型的pod [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 8m55s nginx2-85bf7b8976-bmcq4 0/1 ContainerCreating 0 11s nginx2-85bf7b8976-n9vcl 1/1 Running 0 90s nginx2-85bf7b8976-qzfhz 0/1 ContainerCreating 0 11s nginx2-85bf7b8976-xk594 0/1 ContainerCreating 0 11s# 如果只需要2個deployment類型的nginx的pod [root@master ~]# kubectl scale --replicas 2 deployment/nginx2 deployment.apps/nginx2 scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx2-85bf7b8976-4dm2h 1/1 Running 0 39s nginx2-85bf7b8976-fwj55 1/1 Running 0 49s

autoscale(自動擴展)

自動擴展,給定一個范圍,自動根據業務的訪問量增加或減少

[root@master ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx2 horizontalpodautoscaler.autoscaling/nginx2 autoscaled [root@master ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx Deployment/nginx <unknown>/80% 1 3 3 29m nginx2 Deployment/nginx2 <unknown>/60% 2 7 0 10s

cluster-info(顯示集群信息)

[root@master ~]# kubectl cluster-info Kubernetes control plane is running at https://192.168.240.30:6443 CoreDNS is running at https://192.168.240.30:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

describe(顯示指定pod的詳細信息)

[root@master ~]# kubectl describe pod nginx Name: nginx2-85bf7b8976-4dm2h Namespace: default Priority: 0 Node: node2.example.com/192.168.240.60 Start Time: Mon, 20 Dec 2021 04:38:20 -0500 Labels: app=nginx2pod-template-hash=85bf7b8976 Annotations: <none> Status: Running IP: 10.244.1.31 ····省略部分·····

logs(查看日志)

[root@master ~]# kubectl logs deployment/nginx2 Found 2 pods, using pod/nginx2-85bf7b8976-fwj55 /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 2021/12/20 09:38:13 [notice] 1#1: using the "epoll" event method 2021/12/20 09:38:13 [notice] 1#1: nginx/1.21.4 2021/12/20 09:38:13 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 2021/12/20 09:38:13 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64 2021/12/20 09:38:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576 2021/12/20 09:38:13 [notice] 1#1: start worker processes 2021/12/20 09:38:13 [notice] 1#1: start worker process 31

attach(附加在一個容器里)

[root@master ~]# kubectl attach nginx-85b98978db-cpcjn If you don't see a command prompt, try pressing enter.(相當于是在后臺運行)

exec(執行容器命令)

進到容器內執行一個命令

[root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx2-85bf7b8976-4dm2h 1/1 Running 0 7m1s nginx2-85bf7b8976-fwj55 1/1 Running 0 7m11s nginx3-6f97cd4f65-lhbld 1/1 Running 0 23s [root@master ~]# kubectl exec nginx3-6f97cd4f65-lhbld -- date Mon Dec 20 09:45:55 UTC 2021 [root@master ~]# kubectl exec -it nginx3-6f97cd4f65-lhbld -- /bin/bash root@nginx3-6f97cd4f65-lhbld:/# ls bin docker-entrypoint.d home media proc sbin tmp boot docker-entrypoint.sh lib mnt root srv usr dev etc lib64 opt run sys var

prot-forward(轉發一個或多個端口到pod里面去)

[root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-85b98978db-kdrj6 0/1 ContainerCreating 0 13s wb1-5dbfb96758-d2b4j 1/1 Running 0 4h6m [root@master ~]# kubectl port-forward deployment/nginx 80 #可以變成':80'(隨機一個端口號,只能本機訪問)或者變成指定的端口號'8080:80'(本機訪問用80,其它主機訪問就可以用8080) Forwarding from 127.0.0.1:80 -> 80 Forwarding from [::1]:80 -> 80[root@master ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ······· LISTEN 0 128 127.0.0.1:80 0.0.0.0:* LISTEN 0 128 127.0.0.1:10257 0.0.0.0:* ······[root@master ~]# curl http://127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx 80 Forwarding from 0.0.0.0:80 -> 80#允許所有IP訪問80端口 [root@master ~]# curl http://192.168.240.30 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>

cp(復制)

拷貝文件或目錄到容器中,或者從容器內向外拷貝

#將init文件拷貝到nginx的/usr目錄下 [root@master ~]# kubectl cp /root/init nginx-85b98978db-kdrj6:/usr[root@master ~]# kubectl exec nginx-85b98978db-kdrj6 -- ls -l /usr total 4 drwxr-xr-x 1 root root 261 Dec 2 10:59 bin drwxr-xr-x 2 root root 6 Oct 3 09:15 games drwxr-xr-x 2 root root 6 Oct 3 09:15 include -rw-r--r-- 1 root root 178 Dec 20 08:03 init

label(標簽)

給資源設置、更新標簽

#創建標簽 [root@master ~]# kubectl label --overwrite=true deployment nginx app=test deployment.apps/nginx unlabeled#查看標簽 [root@master ~]# kubectl describe pod nginx Name: nginx Namespace: default Priority: 0 Node: node2.example.com/192.168.240.60 Start Time: Mon, 20 Dec 2021 03:20:34 -0500 Labels: run=nginx Annotations: <none> Status: Running IP: 10.244.1.14#追加標簽 [root@master ~]# kubectl label pods nginx abc=ABC pod/nginx labeled##查看已經最加成功 [root@master ~]# kubectl describe pod nginx Name: nginx Namespace: default Priority: 0 Node: node2.example.com/192.168.240.60 Start Time: Mon, 20 Dec 2021 03:20:34 -0500 Labels: abc=ABCrun=nginx Annotations: <none> Status: Running IP: 10.244.1.14#更改標簽 [root@master ~]# kubectl label pod nginx --overwrite abc=BCD pod/nginx labeled#查看是否更改成功 [root@master ~]# kubectl describe pod nginx Name: nginx Namespace: default Priority: 0 Node: node2.example.com/192.168.240.60 Start Time: Mon, 20 Dec 2021 03:20:34 -0500 Labels: abc=BCDrun=nginx Annotations: <none> Status: Running IP: 10.244.1.14

api-resources(查看所有資源)

查看所有資源

[root@master ~]# kubectl api-resources NAME SHORTNAMES APIVERSION NAMESPACED KIND bindings v1 true Binding componentstatuses cs v1 false ComponentStatus configmaps cm v1 true ConfigMap endpoints ep v1 true Endpoints events ev v1 true Event limitranges limits v1 true LimitRange namespaces ns v1 false Namespace nodes no v1 false Node persistentvolumeclaims pvc v1 true PersistentVolumeClaim persistentvolumes pv v1 false PersistentVolume pods po v1 true Pod podtemplates v1 true PodTemplate replicationcontrollers rc v1 true ReplicationController resourcequotas quota v1 true ResourceQuota secrets v1 true Secret ·····省略部分·····

api-versions(API版本)

打印受支持的API版本

[root@master ~]# kubectl api-versions admissionregistration.k8s.io/v1 apiextensions.k8s.io/v1 apiregistration.k8s.io/v1 apps/v1 authentication.k8s.io/v1 authorization.k8s.io/v1 autoscaling/v1 autoscaling/v2 autoscaling/v2beta1 autoscaling/v2beta2 batch/v1 batch/v1beta1 certificates.k8s.io/v1 coordination.k8s.io/v1 discovery.k8s.io/v1 discovery.k8s.io/v1beta1 events.k8s.io/v1 events.k8s.io/v1beta1 flowcontrol.apiserver.k8s.io/v1beta1 flowcontrol.apiserver.k8s.io/v1beta2 networking.k8s.io/v1 node.k8s.io/v1 node.k8s.io/v1beta1 policy/v1 policy/v1beta1 rbac.authorization.k8s.io/v1 scheduling.k8s.io/v1 storage.k8s.io/v1 storage.k8s.io/v1beta1 v1

部署deployment類型控制器pod

[root@master ~]# kubectl create deployment nginx --image lizhenliang/java-demo deployment.apps/nginx created [root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-74458cff54-svm4r 1/1 Running 0 101s[root@master ~]# kubectl get deploy,pods NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/nginx 1/1 1 1 3m17sNAME READY STATUS RESTARTS AGE pod/nginx-74458cff54-svm4r 1/1 Running 0 3m17s

使用Service將pod暴露出去

[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=8080 --type=NodePort service/nginx exposed [root@master ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25h nginx NodePort 10.111.4.125 <none> 80:30407/TCP 13s

訪問應用

[root@master ~]# curl http://192.168.240.30:30407 #端口號隨機生成,通過getsvc獲取 <!DOCTYPE html> <html> <head lang="en"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>把美女帶回家應用案例</title><meta name="description" content="把美女帶回家應用案例"><meta name="keywords" content="index"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> ·····省略部分······ [root@master ~]# kubectl create deploy nginx --replicas 3 --image nginx:1.20 deployment.apps/nginx created[root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-6d777db949-h4zr7 1/1 Running 0 75s nginx-6d777db949-nxv7s 1/1 Running 0 75s nginx-6d777db949-xdkb6 1/1 Running 0 75s[root@master ~]# kubectl autoscale deploy nginx --min 1 --max 3 horizontalpodautoscaler.autoscaling/nginx autoscaled[root@master ~]# kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE nginx Deployment/nginx <unknown>/80% 1 3 3 25s

0 75s
nginx-6d777db949-nxv7s 1/1 Running 0 75s
nginx-6d777db949-xdkb6 1/1 Running 0 75s

[root@master ~]# kubectl autoscale deploy nginx --min 1 --max 3
horizontalpodautoscaler.autoscaling/nginx autoscaled

[root@master ~]# kubectl get hpa
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
nginx Deployment/nginx /80% 1 3 3 25s

總結

以上是生活随笔為你收集整理的kubernetes的Kubectl命令的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。