Kubernetes kubectl 命令详解

邬宏扬
2023-12-01
1. 首先get svc

kubectl get svc --all-namespaces
准备修改这个服务
kube-ops  jenkins             NodePort       10.105.29.106   
<none>    8080:30003/TCP,50000:50000/TCP

kubectl edit svc/jenkins -n kube-ops

编辑pod
 kubectl edit deployment podName -n 命名空间


#进入容器环境
[root@master ~]# kubectl exec -it mysql-3013141301-2000x /bin/bash


常用命令
kubectl cluster-info   # 查看集群信息
kubectl get all --all-namespaces
kubectl get componentstatuses (kubectl get cs)  # 查看集群组件状态

kubectl get pods -A         # 获取所有命名空间下的pod
kubectl get pods -o wide    # 获取pod的详细信息
kubectl get pods -n kube-system  # 查看命名空间下的pod

kubectl label nodes node01 zone=foo  给node01节点打一个标签zone=foo

kubectl get nodes node01 -o yaml | grep taints
-o yaml 获取结果转换成 yaml 格式输出
grep taints 满足的条件

kubectl get namespace          # 查看命名空间
kubectl create namespace dev   # 创建命名空间

kubectl logs pod coredns-79677db9bd-qcc4n --namespace kube-system  # 查看pod日志
kubectl describe pod coredns-79677db9bd-qcc4n --namespace kube-system  # 查看pod详细日志

kubectl exec -it podName<指定pod名> -c containerName<指定容器名> -- /bin/bash # 进入Pod里的容器

缺少stream.io模块
[root@master01 ~]# nginx
nginx: [emerg] unknown directive "stream" in /etc/nginx/nginx.conf:13

$ yum -y install nginx-mod-stream


1、使用 kubectl apply 来创建指定目录中配置文件所定义的所有对象,除非对应对象已经存在:
	kubectl apply -f tomcat.yaml
    修改tomcat.yaml文件后,更新资源也是这条命令 kubectl apply -f tomcat.yaml


2、kubectl delete
(1)删除名为 "tomcat" 的Pod
	kubectl delete pod tomcat
     删除名为 "foo" 的service
	kubectl delete service foo
	
	 删除 Label name = myLabel的pod
	kubectl delete pods -l name=myLabel

	 删除 Label name = myLabel的Service
	kubectl delete services -l name=myLabel


(2)使用 tomcat.yaml中指定的资源类型和名称删除pod
	kubectl delete -f tomcat.yaml
	
(3)删除所有pod
    kubectl delete pods --all

3、查看帮助文档
[root@master01 ~]# kubectl explain service.spec.type
KIND:     Service
VERSION:  v1

FIELD:    type <string>

DESCRIPTION:
     type determines how the Service is exposed. Defaults to ClusterIP. Valid
     options are ExternalName, ClusterIP, NodePort, and LoadBalancer.
     "ClusterIP" allocates a cluster-internal IP address for load-balancing to
     endpoints. Endpoints are determined by the selector or if that is not
     specified, by manual construction of an Endpoints object or EndpointSlice
     objects. If clusterIP is "None", no virtual IP is allocated and the
     endpoints are published as a set of endpoints rather than a virtual IP.
     "NodePort" builds on ClusterIP and allocates a port on every node which
     routes to the same endpoints as the clusterIP. "LoadBalancer" builds on
     NodePort and creates an external load-balancer (if supported in the current
     cloud) which routes to the same endpoints as the clusterIP. "ExternalName"
     aliases this service to the specified externalName. Several other fields do
     not apply to ExternalName services. More info:
     https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types

 类似资料: