0%

Kubernetes-kubectl

管理Kubernetes集群的工具


阿里云源安装
1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1
yum install -y kubectl

安装
  1. 下载最新版的kubectl客户端

    https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md>

  2. 安装和配置

    https://kubernetes.io/docs/tasks/tools/install-kubectl/>

  3. 验证安装

    1
    kuberctl version

    kuberctl-version.jpg

  4. 配置集群凭证

    您可以使用scp命令安全地将主节点的配置从 Kubernetes 集群主 VM 中的 /etc/kubernetes/kube.conf 复制到本地计算机的 $HOME/.kube/configkubectl 预期凭据所在的位置)。

    1
    2
    mkdir $HOME/.kube
    scp root@<master-public-ip>:/etc/kubernetes/kube.conf $HOME/.kube/config

    公有云环境也可以在集群配置页面获取到config配置

  5. 验证连接

    1
    kubect get all
命令参数
help-获取帮助信息
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
zhaohongye ~ ^-^ #kubectl help
kubectl controls the Kubernetes cluster manager.

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose 使用 replication controller, service, deployment 或者 pod
并暴露它作为一个 新的 Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
run-container 在集群中运行一个指定的镜像. This command is
deprecated, use "run" instead

Basic Commands (Intermediate):
get 显示一个或更多 resources
explain 查看资源的文档
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or
by resources and label selector

Deploy Commands:
rollout Manage the rollout of a resource
rolling-update 完成指定的 ReplicationController 的滚动升级
scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job
设置一个新的副本数量
autoscale 自动调整一个 Deployment, ReplicaSet, 或者
ReplicationController 的副本数量

Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info 显示集群信息
top Display Resource (CPU/Memory/Storage) usage.
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints

Troubleshooting and Debugging Commands:
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp 复制 files 和 directories 到 containers
和从容器中复制 files 和 directories.
auth Inspect authorization

Advanced Commands:
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
convert 在不同的 API versions 转换配置文件

Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or
zsh)

Other Commands:
api-versions Print the supported API versions on the server, in the form of
"group/version"
config 修改 kubeconfig 文件
help Help about any command
plugin Runs a command-line plugin
version 输出 client 和 server 的版本信息

Usage:
kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).
get-获取信息

Display one or many resources

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
Examples:
# List all pods in ps output format.
kubectl get pods

# List all pods in ps output format with more information (such as node name).
kubectl get pods -o wide

# List a single replication controller with specified NAME in ps output format.
kubectl get replicationcontroller web

# List a single pod in JSON output format.
kubectl get -o json pod web-pod-13je7

# List a pod identified by type and name specified in "pod.yaml" in JSON output format.
kubectl get -f pod.yaml -o json

# Return only the phase value of the specified pod.
kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}

# List all replication controllers and services together in ps output format.
kubectl get rc,services

# List one or more resources by their type and names.
kubectl get rc/web service/frontend pods/web-pod-13je7

# List all resources with different types.
kubectl get all
scale-扩缩容

Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Examples:
# Scale a replicaset named 'foo' to 3.
kubectl scale --replicas=3 rs/foo

# Scale a resource identified by type and name specified in "foo.yaml" to 3.
kubectl scale --replicas=3 -f foo.yaml

# If the deployment named mysql's current size is 2, scale mysql to 3.
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

# Scale multiple replication controllers.
kubectl scale --replicas=5 rc/foo rc/bar rc/baz

# Scale statefulset named 'web' to 3.
kubectl scale --replicas=3 statefulset/web

kubectl scale Deployment node-vcg-web --replicas=1
kubectl scale Deployment node-vcg-web --replicas=10

批量扩缩容

1
2
3
for i in `kubectl get deployment | awk '{print $1}' |grep -v NAME`; do
kubectl scale Deployment $i --replicas=1
done
exec-在container中执行命令

Execute a command in a container.

1
2
3
4
5
6
7
8
9
Options:
-c, --container='': Container name. If omitted, the first container in the pod will be chosen
-p, --pod='': Pod name
-i, --stdin=false: Pass stdin to the container
-t, --tty=false: Stdin is a TTY
Examples:
kubectl exec 123456-7890 -c ruby-container -it -- bash -il
Usage:
kubectl exec POD [-c CONTAINER] -- COMMAND [args...] [options]