kubectl命令管理工具

网友投稿 425 2022-11-09

kubectl命令管理工具

kubectl命令管理工具

Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。

//帮助信息 [root@localhost bin]# kubectl --help kubectl controls the Kubernetes cluster manager. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/ Basic Commands (Beginner): create Create a resource from a file or from stdin. expose 使用 replication controller, service, deployment 或者 pod 并暴露它作为一个 新的 Kubernetes Service run 在集群中运行一个指定的镜像 set 为 objects 设置一个指定的特征 Basic Commands (Intermediate): explain 查看资源的文档 get 显示一个或更多 resources edit 在服务器上编辑一个资源 delete Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: rollout Manage the rollout of a resource scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job 设置一个新的副本数量 autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量 Cluster Management Commands: certificate 修改 certificate 资源. cluster-info 显示集群信息 top Display Resource (CPU/Memory/Storage) usage. cordon 标记 node 为 unschedulable uncordon 标记 node 为 schedulable drain Drain node in preparation for maintenance taint 更新一个或者多个 node 上的 taints Troubleshooting and Debugging Commands: describe 显示一个指定 resource 或者 group 的 resources 详情 logs 输出容器在 pod 中的日志 attach Attach 到一个运行中的 container exec 在一个 container 中执行一个命令 port-forward Forward one or more local ports to a pod proxy 运行一个 proxy 到 Kubernetes API server cp 复制 files 和 directories 到 containers 和从容器中复制 files 和 directories. auth Inspect authorization

项目的生命周期,创建--发布--更新--回滚--删除

//创建 kubectl run命令 kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options] 示例: [root@localhost bin]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. deployment.apps/nginx-deployment created [root@localhost bin]# kubectl get pods ]NAME READY STATUS RESTARTS AGE nginx-dbddb74b8-whwhl 1/1 Running 0 2d17h nginx-deployment-5477945587-b8r6m 1/1 Running 0 70s nginx-deployment-5477945587-dz8hb 1/1 Running 0 70s nginx-deployment-5477945587-wd82l 1/1 Running 0 70s [root@localhost bin]# kubectl get all NAME READY STATUS RESTARTS AGE pod/nginx-dbddb74b8-whwhl 1/1 Running 0 2d17h pod/nginx-deployment-5477945587-b8r6m 1/1 Running 0 3m9s pod/nginx-deployment-5477945587-dz8hb 1/1 Running 0 3m9s pod/nginx-deployment-5477945587-wd82l 1/1 Running 0 3m9s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.0.0.1 443/TCP 12d NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.apps/nginx 1 1 1 1 4d23h deployment.apps/nginx-deployment 3 3 3 3 3m9s NAME DESIRED CURRENT READY AGE replicaset.apps/nginx-dbddb74b8 1 1 1 4d23h replicaset.apps/nginx-deployment-5477945587 3 3 3 3m9s //删除 [root@localhost bin]# kubectl delete deploy/nginx deployment.extensions "nginx" deleted [root@localhost bin]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-5477945587-b8r6m 1/1 Running 0 12m nginx-deployment-5477945587-dz8hb 1/1 Running 0 12m nginx-deployment-5477945587-wd82l 1/1 Running 0 12m [root@localhost bin]# kubectl delete deploy/nginx-deployment [root@localhost bin]# kubectl get pods No resources found. //项目周期 1:创建nginx [root@localhost bin]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3 kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. deployment.apps/nginx created [root@localhost bin]# kubectl get pods,deployment NAME READY STATUS RESTARTS AGE pod/nginx-7697996758-jbln5 1/1 Running 0 47s pod/nginx-7697996758-xgxzd 1/1 Running 0 47s pod/nginx-7697996758-xjdlz 1/1 Running 0 47s NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.extensions/nginx 3 3 3 3 47s [root@localhost bin]# kubectl get pods,deployment,replicaset NAME READY STATUS RESTARTS AGE pod/nginx-7697996758-jbln5 1/1 Running 0 87s pod/nginx-7697996758-xgxzd 1/1 Running 0 87s pod/nginx-7697996758-xjdlz 1/1 Running 0 87s NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.extensions/nginx 3 3 3 3 87s NAME DESIRED CURRENT READY AGE replicaset.extensions/nginx-7697996758 3 3 3 87s

发布nginx service提供负载均衡的功能

kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type] [options] [root@localhost bin]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort service/nginx-service exposed [root@localhost bin]# kubectl get pods,svc NAME READY STATUS RESTARTS AGE pod/nginx-7697996758-jbln5 1/1 Running 0 10m pod/nginx-7697996758-xgxzd 1/1 Running 0 10m pod/nginx-7697996758-xjdlz 1/1 Running 0 10m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.0.0.1 443/TCP 12d service/nginx-service NodePort 10.0.0.247 80:38804/TCP 2m35s //查看资源对象简写 [root@localhost bin]# kubectl api-resources //查看关联后端的节点 [root@localhost bin]# kubectl get endpoints NAME ENDPOINTS AGE kubernetes 192.168.195.131:6443,192.168.195.149:6443 12d nginx-service 172.17.27.3:80,172.17.31.3:80,172.17.31.4:80 5m40s //网络状态详细信息 [root@localhost bin]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE nginx-7697996758-jbln5 1/1 Running 0 14m 172.17.27.3 192.168.195.151 nginx-7697996758-xgxzd 1/1 Running 0 14m 172.17.31.3 192.168.195.150 nginx-7697996758-xjdlz 1/1 Running 0 14m 172.17.31.4 192.168.195.150 //服务暴露的端口 [root@localhost bin]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 443/TCP 12d nginx-service NodePort 10.0.0.247 80:38804/TCP 7m55s

在node01操作,查看负载均衡端口38804kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式

[root@localhost ~]# yum install ipvsadm -y [root@localhost ~]# ipvsadm -L -n TCP 192.168.195.150:38804 rr -> 172.17.27.3:80 Masq 1 0 0 -> 172.17.31.3:80 Masq 1 0 0 -> 172.17.31.4:80 Masq 1 0 0 //在node02操作 同样安装ipvsadmin工具查看 [root@localhost ~]# ipvsadm -L -n TCP 192.168.195.151:38804 rr -> 172.17.27.3:80 Masq 1 0 0 -> 172.17.31.3:80 Masq 1 0 0 -> 172.17.31.4:80 Masq 1 0 0 //在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件) [root@localhost bin]# kubectl get pods ]NAME READY STATUS RESTARTS AGE nginx-7697996758-jbln5 1/1 Running 0 30m nginx-7697996758-xgxzd 1/1 Running 0 30m nginx-7697996758-xjdlz 1/1 Running 0 30m [root@localhost bin]# kubectl logs nginx-7697996758-jbln5 172.17.27.1 - - [10/Feb/2020:04:51:56 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-" 2020/02/10 04:51:56 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.27.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.195.151:38804", referrer: "http://192.168.195.151:38804/" 172.17.27.1 - - [10/Feb/2020:04:51:56 +0000] "GET /favicon.ico HTTP/1.1" 404 555 ""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-" [root@localhost bin]# kubectl logs nginx-7697996758-xgxzd [root@localhost bin]# kubectl logs nginx-7697996758-xjdlz 172.17.31.1 - - [10/Feb/2020:04:51:43 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-" 2020/02/10 04:51:44 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.31.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.195.150:38804", referrer: "http://192.168.195.150:38804/" 172.17.31.1 - - [10/Feb/2020:04:51:44 +0000] "GET /favicon.ico HTTP/1.1" 404 555 ""Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" "-"

[root@localhost bin]# kubectl set --help ]Configure application resources These commands help you make changes to existing application resources. Available Commands: env Update environment variables on a pod template image 更新一个 pod template 的镜像 resources 在对象的 pod templates 上更新资源的 requests/limits selector 设置 resource 的 selector serviceaccount Update ServiceAccount of a resource subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding Usage: kubectl set SUBCOMMAND [options] Use "kubectl --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands). //获取修改模板 [root@localhost bin]# kubectl set image --help Examples: # Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'. kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1 [root@localhost bin]# kubectl set image deployment/nginx nginx=nginx:1.14 deployment.extensions/nginx image updated //处于动态监听状态 [root@localhost bin]# kubectl get pods -w NAME READY STATUS RESTARTS AGE nginx-6ff7c89c7c-7hfsm 0/1 ContainerCreating 0 17s nginx-7697996758-jbln5 1/1 Running 0 70m nginx-7697996758-xgxzd 1/1 Running 0 70m nginx-7697996758-xjdlz 1/1 Running 0 70m nginx-6ff7c89c7c-7hfsm 1/1 Running 0 35s nginx-6ff7c89c7c-v2vww 0/1 Pending 0 0s nginx-7697996758-xgxzd 1/1 Terminating 0 70m nginx-6ff7c89c7c-v2vww 0/1 Pending 0 0s nginx-6ff7c89c7c-v2vww 0/1 ContainerCreating 0 0s nginx-7697996758-xgxzd 0/1 Terminating 0 70m //Ctrl+c中断监听,更新速度快 [root@localhost bin]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-6ff7c89c7c-7hfsm 1/1 Running 0 2m55s nginx-6ff7c89c7c-qj2hd 1/1 Running 0 2m10s nginx-6ff7c89c7c-v2vww 1/1 Running 0 2m20s

[root@localhost bin]# kubectl rollout --help Manage the rollout of a resource. Valid resource types include: * deployments * daemonsets * statefulsets Examples: # Rollback to the previous deployment kubectl rollout undo deployment/abc # Check the rollout status of a daemonset kubectl rollout status daemonset/foo Available Commands: history 显示 rollout 历史 pause 标记提供的 resource 为中止状态 resume 继续一个停止的 resource status 显示 rollout 的状态 undo 撤销上一次的 rollout Usage: kubectl rollout SUBCOMMAND [options] Use "kubectl --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands). //查看历史版本 [root@localhost bin]# kubectl rollout history deployment/nginx deployment.extensions/nginx REVISION CHANGE-CAUSE 1 2 //执行回滚 [root@localhost bin]# kubectl rollout undo deployment/nginx deployment.extensions/nginx //检查回滚状态 [root@localhost bin]# kubectl rollout status deployment/nginx deployment "nginx" successfully rolled out

//删除nginx //查看deployment [root@localhost bin]# kubectl get deploy NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx 3 3 3 3 118m [root@localhost bin]# kubectl delete deployment/nginx deployment.extensions "nginx" deleted [root@localhost bin]# kubectl get deploy No resources found. [root@localhost bin]# kubectl get pods No resources found. //删除服务SVC [root@localhost bin]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 443/TCP 12d nginx-service NodePort 10.0.0.247 80:38804/TCP 114m [root@localhost bin]# kubectl delete svc/nginx-service service "nginx-service" deleted [root@localhost bin]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 443/TCP 12d //查看具体资源的详细信息 [root@localhost bin]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3 kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. deployment.apps/nginx created [root@localhost bin]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-7697996758-75shs 1/1 Running 0 20s nginx-7697996758-b7tjw 1/1 Running 0 20s nginx-7697996758-jddc5 1/1 Running 0 20s [root@localhost bin]# kubectl describe pod nginx-7697996758-75shs Name: nginx-7697996758-75shs Namespace: default Priority: 0 PriorityClassName: Node: 192.168.195.150/192.168.195.150 Start Time: Mon, 10 Feb 2020 14:33:04 +0800 Labels: pod-template-hash=7697996758 run=nginx Annotations: Status: Running IP: 172.17.31.3 Controlled By: ReplicaSet/nginx-7697996758 Containers: nginx: Container ID: docker://e8eafbc943299091943728bf6a378b0752eb6e0320c9bb9fbed933b4af3b1753 Image: nginx:latest Image ID: docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f Port: 80/TCP Host Port: 0/TCP State: Running Started: Mon, 10 Feb 2020 14:33:10 +0800 Ready: True Restart Count: 0 Environment: Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-g4gcx (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-g4gcx: Type: Secret (a volume populated by a Secret) SecretName: default-token-g4gcx Optional: false QoS Class: BestEffort Node-Selectors: Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 2m58s default-scheduler Successfully assigned default/nginx-7697996758-75shs to 192.168.195.150 Normal Pulling 2m56s kubelet, 192.168.195.150 pulling image "nginx:latest" Normal Pulled 2m52s kubelet, 192.168.195.150 Successfully pulled image "nginx:latest" Normal Created 2m52s kubelet, 192.168.195.150 Created container Normal Started 2m52s kubelet, 192.168.195.150 Started container //查看deployment资源 [root@localhost bin]# kubectl describe deployment/nginx Name: nginx Namespace: default CreationTimestamp: Mon, 10 Feb 2020 14:33:04 +0800 Labels: run=nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: run=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: run=nginx Containers: nginx: Image: nginx:latest Port: 80/TCP Host Port: 0/TCP Environment: Mounts: Volumes: Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: NewReplicaSet: nginx-7697996758 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 6m15s deployment-controller Scaled up replica set nginx-7697996758 to 3 //进入pod [root@localhost bin]# kubectl exec -it nginx-7697996758-75shs bash root@nginx-7697996758-75shs:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr root@nginx-7697996758-75shs:/#

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于PICA200内核实现OCP构建模块的应用方案
下一篇:Java字符串常见的操作(比较,查找,替换等)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~