常用K8S命令
Pod
kubectl --kubeconfig=kubeconfig.yaml get pods
kubectl --kubeconfig=kubeconfig.yaml describe pod hello-api
kubectl --kubeconfig=kubeconfig.yaml edit pod hello-api
Ingress
kubectl --kubeconfig=kubeconfig.yaml create -f hello-api-ingress.yaml
kubectl --kubeconfig=kubeconfig.yaml get ingresses
kubectl --kubeconfig=kubeconfig.yaml describe ingress hello-api
kubectl --kubeconfig=kubeconfig.yaml edit ingresses hello-api
SVC
kubectl --kubeconfig=kubeconfig.yaml create -f hello-api-svc.yaml
kubectl --kubeconfig=kubeconfig.yaml get svc
kubectl --kubeconfig=kubeconfig.yaml describe svc hello-api
kubectl --kubeconfig=kubeconfig.yaml edit hello-api
Stop pods
kubectl scale deployment <deployment name> -n <namespace> --replicas=0
Start pods
kubectl scale deployment <deployment name> -n <namespace> --replicas=2
Restart pods
kubectl rollout restart deployment -n <namespace>
更新任务状态
kubectl patch cronjobs vaqa-confluence-job -p '{"spec" : {"suspend" : true }}'
滚动升级
kubectl set image deployment <deployment name> <container name>=<image full name>:<new tag> kubectl patch deployment <deployment name> -p '{"spec": {"template": {"spec": {"containers": [{"name":"containername","image": "yourimagename:tag"}]}}}}'
查看滚动升级过程
kubectl rollout status deployment <deployment name>
查看历史升级版本
kubectl rollout history deployment <deployment name>
回滚到deployment指定的版本
kubectl rollout undo deployment <deployment name> --to-revision=<revision value>
CronJob
kubectl --kubeconfig=kubeconfig.yaml apply -f cron-job.yaml
kubectl --kubeconfig=kubeconfig.yaml get cronjob hello-job
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello-job * * * * * False 0 <none> 14s
kubectl --kubeconfig=kubeconfig.yaml get jobs --watch
NAME COMPLETIONS DURATION AGE
hello-job-28160783 0/1 0s
hello-job-28160783 0/1 3s 3s
hello-job-28160783 0/1 0s 3s
hello-job-28160783 1/1 5s 8s
kubectl --kubeconfig=kubeconfig.yaml get pods
NAME READY STATUS RESTARTS AGE
hello-64cdb46cc5-cjjf7 1/1 Running 0 80d
hello-job-28160783-lbwwn 0/1 Completed 0 41s
kubectl --kubeconfig=kubeconfig.yaml logs pod/hello-job-28160783-lbwwn
Tue Jul 18 02:23:05 UTC 2023
hello cron job
kubectl --kubeconfig=kubeconfig.yaml edit cronjob/hello-job
cronjob.batch/hello-job edited
kubectl --kubeconfig=kubeconfig.yaml get pods
NAME READY STATUS RESTARTS AGE
hello-64cdb46cc5-cjjf7 1/1 Running 0 80d
hello-job-28160783-lbwwn 0/1 Completed 0 2m
hello-job-28160784-55zqr 0/1 Completed 0 57s
-- 可通过以下命令查看job创建不成功的原因,查看输出中有个events
kubectl --kubeconfig=kubeconfig.yaml describe job hello-job-28160762
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello-job
spec:
schedule: "* * * * *"
jobTemplate:
spec:
backoffLimit: 2
template:
spec:
containers:
- command:
- /bin/sh
- -c
- date; resp=$(curl -X GET $URL); echo $resp;
env:
- name: URL
value: http://icodesoft.com/testcronjob
image: byrnedo/alpine-curl
imagePullPolicy: IfNotPresent
name: curl
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 100m
memory: 256Mi
restartPolicy: Never
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
startingDeadlineSeconds: 100
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 1
suspend: false