一.kubectl 安装
kubectl是用于针对Kubernetes集群运行命令的命令行接口。你可以通过kubectl部署集群应用,检查和管理集群资源,查看日志等。
1.安装
brew install kubernetes-cli
2.增加可执行权限
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
3.验证
kubectl version
二.kubectl配置
1.配置文件
2.配置生效
- 将复制的配置复制到$HOME/.kube/config文件
-
export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config
3.配置验证
-
kubectl config view
-
kubectl cluster-info
-
kubectl get node
三.kubectl使用
1. 配置
$ kubectl config view
# 显示合并后的 kubeconfig 配置
# 同时使用多个 kubeconfig 文件并查看合并后的配置
$ KUBECONFIG=~/.kube/config:~/.kube/kubconfig2 kubectl config view
# 获取 e2e 用户的密码
$ kubectl config view -o jsonpath='{.users[?(@.name == “e2e”)].user.password}’
$ kubectl config current-context
# 显示当前的上下文
$ kubectl config use-context my-cluster-name
# 设置默认上下文为 my-cluster-name
# 向 kubeconf 中增加支持基本认证的新集群
$ kubectl config set-credentials kubeuser/foo.kubernetes.com –username=kubeuser –password=kubepassword
# 使用指定的用户名和 namespace 设置上下文
$ kubectl config set-context gce –user=cluster-admin –namespace=foo \ && kubectl config use-context gce
2.创建对象
$ kubectl create -f ./my-manifest.yaml
# 创建资源
$ kubectl create -f ./my1.yaml -f ./my2.yaml
# 使用多个文件创建资源
$ kubectl create -f ./dir
# 使用目录下的所有清单文件来创建资源
$ kubectl create -f https://git.io/vPieo
# 使用 url 来创建资源
$ kubectl run nginx –image=nginx
# 启动一个 nginx 实例
$ kubectl explain pods,svc
# 获取 pod 和 svc 的文档
3.查看类命令
1 # 获取节点和服务版本信息
2 kubectl get nodes
3 # 获取节点和服务版本信息,并查看附加信息
4 kubectl get nodes -o wide
5
6 # 获取pod信息,默认是default名称空间
7 kubectl get pod
8 # 获取pod信息,默认是default名称空间,并查看附加信息【如:pod的IP及在哪个节点运行】
9 kubectl get pod -o wide
10 # 获取指定名称空间的pod
11 kubectl get pod -n kube-system
12 # 获取指定名称空间中的指定pod
13 kubectl get pod -n kube-system podName
14 # 获取所有名称空间的pod
15 kubectl get pod -A
16 # 查看pod的详细信息,以yaml格式或json格式显示
17 kubectl get pods -o yaml
18 kubectl get pods -o json
19
20 # 查看pod的标签信息
21 kubectl get pod -A –show-labels
22 # 根据Selector(label query)来查询pod
23 kubectl get pod -A –selector=”k8s-app=kube-dns”
24
25 # 查看运行pod的环境变量
26 kubectl exec podName env
27 # 查看指定pod的日志
28 kubectl logs -f –tail 500 -n kube-system kube-apiserver-k8s-master
29
30 # 查看所有名称空间的service信息
31 kubectl get svc -A
32 # 查看指定名称空间的service信息
33 kubectl get svc -n kube-system
34
35 # 查看componentstatuses信息
36 kubectl get cs
37 # 查看所有configmaps信息
38 kubectl get cm -A
39 # 查看所有serviceaccounts信息
40 kubectl get sa -A
41 # 查看所有daemonsets信息
42 kubectl get ds -A
43 # 查看所有deployments信息
44 kubectl get deploy -A
45 # 查看所有replicasets信息
46 kubectl get rs -A
47 # 查看所有statefulsets信息
48 kubectl get sts -A
49 # 查看所有jobs信息
50 kubectl get jobs -A
51 # 查看所有ingresses信息
52 kubectl get ing -A
53 # 查看有哪些名称空间
54 kubectl get ns
55
56 # 查看pod的描述信息
57 kubectl describe pod podName
58 kubectl describe pod -n kube-system kube-apiserver-k8s-master
59 # 查看指定名称空间中指定deploy的描述信息
60 kubectl describe deploy -n kube-system coredns
61
62 # 查看node或pod的资源使用情况
63 # 需要heapster 或metrics-server支持
64 kubectl top node
65 kubectl top pod
66
67 # 查看集群信息
68 kubectl cluster-info 或 kubectl cluster-info dump
69 # 查看各组件信息【172.16.1.110为master机器】
70 kubectl -s https://172.16.1.110:6443 get componentstatuses
4.操作类命令
1 # 创建资源
2 kubectl create -f xxx.yaml
3 # 应用资源
4 kubectl apply -f xxx.yaml
5 # 应用资源,该目录下的所有 .yaml, .yml, 或 .json 文件都会被使用
6 kubectl apply -f <directory>
7 # 创建test名称空间
8 kubectl create namespace test
9
10 # 删除资源
11 kubectl delete -f xxx.yaml
12 kubectl delete -f <directory>
13 # 删除指定的pod
14 kubectl delete pod podName
15 # 删除指定名称空间的指定pod
16 kubectl delete pod -n test podName
17 # 删除其他资源
18 kubectl delete svc svcName
19 kubectl delete deploy deployName
20 kubectl delete ns nsName
21 # 强制删除
22 kubectl delete pod podName -n nsName –grace-period=0 –force
23 kubectl delete pod podName -n nsName –grace-period=1
24 kubectl delete pod podName -n nsName –now
25
26 # 编辑资源
27 kubectl edit pod podName