kubectl 创建对象
- 创建资源 $ kubectl create -f ./my-manifest.yaml
- 使用多个文件创建资源 $ kubectl create -f ./my1.yaml -f ./my2.yaml
- 使用目录下的所有清单文件(yaml)来创建资源 $ kubectl create -f ./dir
- 使用url创建资源 $ kubectl create -f https://git.io/vPieo
- 启动一个nginx实例 $ kubectl run nginx --image=nginx
- 获取pod和svc的文档 $ kubectl explain pods
- kubectl Scale 资源
- 扩展pod下容器数量
root@bx-florencetest-k8smaster1[ K8S-ADMIN ]:~ kubectl scale --replicas=0 -n florence-4qjxvbk2 deployment moddpap
deployment.extensions/moddpap scaled
root@bx-florencetest-k8smaster1[ K8S-ADMIN ]:~ kubectl scale --replicas=1 -n florence-4qjxvbk2 deployment moddpap
deployment.extensions/moddpap scaled
- 扩展名称为foo的资源到3个,是否使用rs取决于yaml中的编写
$ kubectl scale --replicas=3 rs/foo
例如yaml中kind: Deployment ,则应通过下面方法扩展
$ kubectl scale --replicas=3 deployment/foo
或者直接通过创建资源的yaml文件扩展
$ kubectl scale --replicas=3 -f foo.yaml
kubectl 与运行中的pod交互
- 查看pod日志, -f 持续查看
kubectl logs -n florence-4qjxvbk2 moddpap-844d4fd9d5-9n2wk
- 转发pod中的80端口到本地的5000端口
root@bx-florencetest-k8smaster1[ K8S-ADMIN ]:~ kubectl port-forward -n florence-4qjxvbk2 moddpap-844d4fd9d5-9n2wk 5000:80
Forwarding from 127.0.0.1:5000 -> 80
Forwarding from [::1]:5000 -> 80
- 在已存在的容器中执行命令
kubectl exec -n florence-4qjxvbk2 moddpap-844d4fd9d5-9n2wk -- ls /home/
- 获取namespace
[root@bx-florence-k8smaster1 ~]* kubectl get ns
- 生成ingress配置
[root@bx-florence-k8smaster1 ~]* kubectl get ingresses -n prod -oyaml > all-prod-ingress.yaml
[root@bx-florence-k8smaster1 ~]* ll all-prod-ingress.yaml
-rw-r--r-- 1 root root 83 May 23 01:19 all-prod-ingress.yaml
- 获取线上pod list
[root@bx-florence-k8smaster1 ~]* kubectl get pods -n florence-8mu2rlvw -o wide
- 进入指定pod的容器内
[root@bx-florence-k8smaster1 ~]* kubectl exec -it florence-5cb7ffdf68-77gfb -c florence -n florence-8mu2rlvw bash
- 获取pod的分配的内部IP
[root@bx-florence-k8smaster1 ~]* kubectl describe pod florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw|grep IP
IP: 10.254.58.48
- 查看k8S某个pod的信息
[root@bx-florence-k8smaster1 ~]* kubectl get pods florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
florence-5cb7ffdf68-77gfb 1/1 Running 0 6h36m 10.254.58.48 10.11.22.52 <none> <none>
[root@bx-florence-k8smaster1 ~]* kubectl get pods florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw
NAME READY STATUS RESTARTS AGE
florence-5cb7ffdf68-77gfb 1/1 Running 0 6h58m
- 详细信息查询
- JSON格式显示Pod的完整信息
[root@bx-florence-k8smaster1 ~]* kubectl get pod florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw --output yaml
- YAML方式显示Pod的完整信息
[root@bx-florence-k8smaster1 ~]* kubectl get pod florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw --output json
- 状态和生命周期查询
[root@bx-florence-k8smaster1 ~]* kubectl describe pods florence-5cb7ffdf68-77gfb -n florence-8mu2rlvw
Name: florence-5cb7ffdf68-77gfb
Namespace: florence-8mu2rlvw
Priority: 0
PriorityClassName: <none>
Node: 10.11.22.52/10.11.22.52
Start Time: Fri, 22 May 2020 18:31:52 +0800
Labels: app=florence
department=ihospital
group=ihospital
pod-template-hash=5cb7ffdf68
scene=main
Annotations: podpreset.admission.kubernetes.io/podpreset-paas-env: 11479649
Status: Running
IP: 10.254.58.48
Controlled By: ReplicaSet/florence-5cb7ffdf68
Containers:
florence:
Container ID: docker://688475bdad4cb9fc293c71e1b0366843521d6986c38f6eb1cf6fadb9b69d52e2
Image: insurance-medicine-florence-backend/florence:feature_522-232652
Image ID: docker-pullable://insurance-medicine-florence-backend/florence@sha256:e78c88b9ae532b642ad6bc2226643c99bfddda7c96a6560e33956c2092e5ee69
Port: <none>
Host Port: <none>
State: Running
Started: Fri, 22 May 2020 18:32:05 +0800
Ready: True
Restart Count: 0
Limits:
cpu: 3
memory: 3200Mi
Requests:
cpu: 3
memory: 3200Mi
Liveness: tcp-socket :80 delay=120s timeout=10s period=10s * success=1 * failure=1
Readiness: tcp-socket :80 delay=50s timeout=1s period=10s * success=1 * failure=1
Environment:
PAAS_CLUSTER: florence
PAAS_TARGET: prod
PAAS_CLUSTER_KUBERNETES: 1
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gpbhp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-gpbhp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gpbhp
Optional: false
QoS Class: Guaranteed
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
* * * * * * * * * *
[root@bx-florence-k8smaster1 ~]* kubectl get pods -n kafka log-kafka-0
NAME READY STATUS RESTARTS AGE
log-kafka-0 2/2 Running 3 79m
[root@bx-florence-k8smaster1 ~]* kubectl get pods -n kafka log-kafka-0 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
log-kafka-0 2/2 Running 3 79m 10.254.5.59 10.11.22.41 <none> <none>
- 查看POD的log
[root@bx-florence-k8smaster1 ~]* kubectl logs -f -n kafka log-archiver-79bb645dbb-9pvpr
- 获取florence Deployment部署的服务
[root@bx-florence-k8smaster1 ~]* kubectl get deployment -n florence-8mu2rlvw | grep florence
florence 5/5 5 5 102d
florence-agent 1/1 1 1 104d
florence-cms 1/1 1 1 104d
florence-consult-service 2/2 2 2 74d
florence-doctor 1/1 1 1 103d
florence-messager 3/3 3 3 102d
florence-patient 3/3 3 3 98d
florence-pm-master 3/3 3 3 74d
florence-pm-schedule-master 1/1 1 1 74d
florence-search 2/2 2 2 103d
florence-update 1/1 1 1 102d
florence-wx-accesstoken 3/3 3 3 105d
- 更改deployment配置
[root@bx-florence-k8smaster1 ~]* kubectl edit deployments -n kafka log-archiver
- 更新POD
kubectl replace -f hello-world-pod.yaml
- 删除POD
kubectl delete pod hello-world
- 重建POd
kubectl replace --force -f hello-world-pod.yaml
标签:kubectl,bx,操作手册,root,日常,pod,K8S,florence,k8smaster1
From: https://www.cnblogs.com/tengfei520/p/16749857.html