| 资源类型 | 简称 |
|---|---|
| certificatesigningrequests | csr |
| componentstatuses | cs |
| configmaps | cm |
| customresourcedefinition | crd |
| daemonsets | ds |
| deployments | deploy |
| endpoints | ep |
| events | ev |
| horizontalpodautoscalers | hpa |
| ingresses | ing |
| limitranges | limits |
| namespaces | ns |
| networkpolicies | netpol |
| nodes | no |
| persistentvolumeclaims | pvc |
| persistentvolumes | pv |
| poddisruptionbudgets | pdb |
| pods | po |
| podsecuritypolicies | psp |
| replicasets | rs |
| replicationcontrollers | rc |
| resourcequotas | quota |
| serviceaccounts | sa |
| services | svc |
| statefulsets | sts |
| storageclasses | sc |
# 使用yaml文件 导入k8s中的资源 语法 kubectl apply -f (文件名) # 使用yaml文件 导入k8s中的资源 示例 kubectl apply -f test.yaml # 使用yaml文件 删除k8s中的资源 语法 # kubectl delete -f 文件名 # 使用yaml文件 删除k8s中的资源 示例 kubectl delete -f test.yaml获取信息
# 获取某namespace下的资源列表 语法 kubectl -n (namespace) get (type) -A [-o wide] # 获取某namespace下的资源列表 示例 kubectl -n kube-system get service -A kubectl -n kube-system get service -A -o wide # 获取某个资源信息 语法 kubectl -n (namespace) get (type) (name) [-o wide] # 获取某个资源信息 示例 kubectl -n kubesphere-system get service ks-controller-manager kubectl -n kubesphere-system get service ks-controller-manager -o wide # 获取某个资源 yaml 语法 kubectl -n (namespace) get (type) (name) -o yaml # 获取某个资源 yaml 示例 kubectl -n kubesphere-system get service ks-controller-manager -o yaml # 获取某个资源 yaml文件 语法 kubectl -n (namespace) get (type) (name) -o yaml > (filename) # 获取某个资源 yaml文件 示例 kubectl -n kubesphere-system get service ks-controller-manager -o yaml > ks-controller-manager.yaml # 获取某个资源 描述信息 语法 kubectl -n (namespace) describe (type) (name) # 获取某个资源 描述信息 示例 kubectl -n kubesphere-system describe service ks-controller-manager # 删除某个资源 语法 kubectl -n (namespace) delete (type) (name) # 删除某个资源 示例 kubectl -n kubesphere-system delete service ks-controller-manager查看pod日志
# 查看pod日志 语法 kubectl -n (namespace) logs -f (podName) # 查看pod日志 示例 kubectl -n kubesphere-system logs -f ks-controller-manager-5648cdcf54-4wbvw # 查看pod日志输出到文件 语法 kubectl -n (namespace) logs -f (podName) > (logFileName) # 查看pod日志输出到文件 示例 kubectl -n kubesphere-system logs -f ks-controller-manager-5597bc4988-6t2hl > 5597bc4988-6t2hl.logpod中执行命令
# 进入pod 语法 kubectl -n (namespace) exec -it (podname) /bin/sh kubectl -n (namespace) exec -it (podname) /bin/bash # 进入pod 示例 kubectl -n hrx exec -it recruit-talent-web-649858fd7-wwtmz /bin/sh kubectl -n hrx exec -it recruit-talent-web-649858fd7-wwtmz /bin/bash删除Pod(更新资源时可使用删除pod方式让k8s自动重建pod)
删除名称包含’hbp-'的pod
kubectl -n hrx get pods | grep hbp- | awk ‘{print $1}’ | xargs kubectl -n hrx delete pod
# 策略合并,入遇到数组会使用新的替换旧的数组
kubectl patch deployment patch-demo --type merge --patch "$(cat test_patch.yaml)"
# 策略合并,数组会生成并集
kubectl patch deployment patch-demo --type strategic --patch "$(cat test_patch.yaml)"
# type (默认 strategic) 同 策略合并
kubectl patch deployment patch-demo --patch "$(cat test_patch.yaml)"
# 策略合并,入遇到数组会使用新的替换旧的数组
kubectl patch deployment patch-demo --type merge --patch-file test_patch1.yaml
# 策略合并,数组会生成并集
kubectl patch deployment patch-demo --type strategic --patch-file test_patch1.yaml
# type (默认 strategic) 同 策略合并
kubectl patch deployment patch-demo --patch-file test_patch1.yaml
# json 示例
# 设置testdep副本数为4
kubectl patch -n test deployment testdep --patch '{"spec":{"replicas":4}}'
# yaml 示例(有数组时推荐)
fileName=hbp-s3adapter-s3volumes.yaml
echo 'spec:' > $fileName
echo ' template:' >> $fileName
echo ' spec:' >> $fileName
echo ' volumes:' >> $fileName
echo ' - name: s3-volume' >> $fileName
echo ' persistentVolumeClaim:' >> $fileName
echo ' claimName: s3' >> $fileName
echo ' containers:' >> $fileName
echo ' - name: hbp-s3adapter' >> $fileName
echo ' volumeMounts:' >> $fileName
echo ' - name: s3-volume' >> $fileName
echo ' mountPath: /wls/wls81/s3' >> $fileName
kubectl -n hrx patch deployment hbp-s3adapter --patch-file $fileName
rm -f $fileName



