kubernetes 常用命令

https://kubernetes.io/zh/docs/tasks/debug-application-cluster/

Kubernetes help manual

1
2
[root@master ~]# kubectl api-resources
[root@master ~]# kubectl api-versions

查看Kubernetes集群版本信息

1
2
3
kuberctl version [-o json|yaml] [--short=true|false]
kuberctl cluster-info [dump]
kuberctl config view

Kubernetes cluster status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 169m v1.18.2
node01 Ready <none> 168m v1.18.2
[root@master ~]# kubectl get pod --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-546565776c-grgtm 1/1 Running 1 168m
kube-system coredns-546565776c-jf54d 1/1 Running 1 168m
kube-system etcd-master 1/1 Running 1 168m
kube-system kube-apiserver-master 1/1 Running 1 168m
kube-system kube-controller-manager-master 1/1 Running 1 168m
kube-system kube-flannel-ds-4jdq9 1/1 Running 1 166m
kube-system kube-flannel-ds-rqnb2 1/1 Running 1 166m
kube-system kube-proxy-kv5bl 1/1 Running 1 168m
kube-system kube-proxy-znnx6 1/1 Running 1 167m
kube-system kube-scheduler-master 1/1 Running 1 168m
[root@master ~]# kubectl get all svc --all-namespaces
error: you must specify only one resource
[root@master ~]# kubectl get svc --all-namespaces
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 168m
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 168m
[root@master ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health":"true"}

kubectl create

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

//创建服务
[root@localhost]# kubectl create -f xx_service.yaml --record
[root@localhost]# kubectl get svc | grep xx_service_name
[root@localhost]# kubectl describe svc/service_name

//查看服务的内部dns地址
[root@localhost]# kubectl run -i --tty busybox --image=busybox --restart=Never
/# nslookup service_name

//通过deployment控制器建立pod
[root@Localhost]# kubectl create -f xx_depolyment.yaml --record=true

//查看已创建的deployment
[root@localhost]# kubectl get deploments

//查看有哪些pod
[root@localhost]# kubectl get pods| grep xx_service

//查看pod日志
[root@localhost]# kubectl logs -f pod_name

//服务伸缩
[root@localhost]# vi xx_deployment.yaml
[root@localhost]# kubectl apply -f xx_deployment.yaml

//服务升级
[root@localhost]# vi xx_deployment.yaml
[root@localhost]# kubectl apply -f xx_deployment.yaml

//监视升级过程
[root@localhost]# kubectl rollout status deployment/deployment_name

//服务回退
[root@localhost]# kubectl rollout undo deployments/deployment_name