【k8s】k8s命令

K8S命令


详细帮助: kubectl --help


1. 解释说明

kubectl explain: 最有用的命令,查询所有配置的说明文档

kubectl explain node
kubectl explain deploy
kubectl explain deploy.spec
kubectl explain deploy.spec.template.spec.containers.image

2. 节点

kubectl get nodes -o wide

STATUS: 节点状态
AGE: 节点创建时长

2.1 查询集群节点信息

3. 类型

kubectl api-resources   # 查询所有资源类型

列出所有Kubernetes类型以及简写

4. 查询

kubectl get all -A

all: 所有类型,可用po、deploy、svc等具体类型替换
-A, --all-namespaces 范围是所有namespace,可用-n xxx 具体查询
-o wide:详细信息 -o yaml: 输出yaml格式

# 查询label为app=myapp的所有组件信息
kubectl get xxx -A -l app=myapp

# 显示所有lable
kubectl get --show-lables xxx -A

4.1 查询deploy

# 列出xxx命名空间下所有po, 并列出详细信息,包括所处实例服务器ip
kubectl get deploy -o wide -n xxx  
# 列出xxx命名空间下名为deployNameA的deployment
kubectl get deploy deployNameA -n xxx   
# 输出xxx命名空间下名为poNameA的deployment的yaml文件
kubectl get deploy deployNameA -o yaml -n xxx 

4.2 其他查询

kubectl get svc -n xxx  | grep serviceNameA # 列出service
kubectl get configMap -n xxx
kubectl get secret -n xxx
kubectl get statefulsets -n xxx     # kubectl get sts -n xxx 

5. 更新、部署

5.1 新建

# 新建资源
kubectl create -f xxx.yaml 

5.2 更新

# 更新资源
kubectl apply -f xxx.yaml 
# 更新deployNameA(弹出编辑器,保存即更新)
kubectl edit deploy deployNameA -n xxx

5.3 Patch更新

需求:

  1. 更新image
  2. 更新最低资源需求
  3. 添加最新pullSecret

update.yaml

spec:
  template:
    spec:
      containers:
        - name: 'containerName'     # 为了定位container,更新image必须填写name
          image: 'xxx.com/new/image:2.1.20240716.1'
          resources:
            requests:
              cpu: "250m" # 250m = 0.25cpu
              memory: "512Mi"
      imagePullSecrets:
        - name: secretName

更新脚本如下

kubectl patch deploy deployNameA -n xxx --patch "$(cat update.yaml)"

6. 节点调度

6.1 节点调度开关

# 关闭节点调度, pod无法调度到node1
kubectl cordon node1
# 开启节点调度
kubectl uncordon node1

6.2 调度节点配置

按节点名称配置

kind: Deployment
metadata:
spec:
  replicas: 3
  template:
    metadata:
    spec:
      nodeName: node1
      containers:

按节点标签调度

kind: Deployment
metadata:
spec:
  replicas: 3
  template:
    metadata:
    spec:
      nodeSelector: 
        env:prod
      containers:

6.3 节点亲和性配置

requiredDuringSchedulingIgnoredDuringExecution: 硬亲和性,必须满足才能调度,否则Pending
preferredDuringSchedulingIgnoredDuringExecution:软亲和性,倾向性调度,满足大部分情况
可以混用,在不满足requiredDuringSchedulingIgnoredDuringExecution时,启用软亲和性

kind: Pod
spec:
  containers:
  - image: xxx:user-1.0.1
    name: user-system
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: app
            operator: In  # In, NotIn, Exists, DoesNotExist. Gt, and Lt.
            values:
            - user
            - order
kind: Pod
spec:
  containers:
  - image: xxx:user-1.0.1
    name: user-system
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  affinity:
    nodeAffinity:
		preferredDuringSchedulingIgnoredDuringExecution:
	      - weight: 100 # 1 - 100越高越优先
	        preference:
	          matchExpressions:
	          - key: env
	            operator: In   # In, NotIn, Exists, DoesNotExist. Gt, and Lt.
	            values:
	            - prod
	     - weight: 50
	        preference:
	          matchExpressions:
	          - key: app
	            operator: In   # In, NotIn, Exists, DoesNotExist. Gt, and Lt.
	            values:
	            - user
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值