kubectl是什么
kubernetes API是管理其各种资源对象的唯一入口,它提供了一个RESTful风格的CRUD(create,read,Update,delete)接口用于查询和修改集群状态,并且将结果存储于etcd中,API Server也是用于更新ETCD中资源对象的唯一途径,整个集群的其他所有组件都需要通过它来完成查询和修改操作,kubectl的核心功能在于通过API Server操作k8s的各种资源对象,它支持三种操作方式,其中命令是的使用最为简单,是了解K8S集群管理的一种有效途径
kubectl常用的命令
命令语法格式
- kubectl [command] [TYPE] [NAME] [flags]
- 上面的命令是: kubectl命令行中,指定执行什么操作(command),指定什么类型资源对象(type),指定此类型的资源对象名称(name),指定可选参数(flags),后面的参数就是为了修饰那个唯一的对象
- 属于典型的英文语法,比如你是老师,你说,小明(kubectl)买(command)方便面(type)老坛酸菜方便面(name)桶装的(flag)。命令的根本原则是准确性,不能有异议!
查看集群信息
- kubectl cluster-info
[root@k8s-master01 nginx]# kubectl cluster-info
Kubernetes master is running at https://172.18.15.116:6443
CoreDNS is running at https://172.18.15.116:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@k8s-master01 nginx]#
查看集群版本信息
- kubectl version
- kubectl version —short=tre
其中第二条命令能更精简的显示集群版本信息
[root@k8s-master01 nginx]# kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:44:51Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:36:15Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
[root@k8s-master01 nginx]# kubectl version --short=true
Client Version: v1.16.9
Server Version: v1.16.9
[root@k8s-master01 nginx]#
查看集群上运行了哪些资源
- 语法格式 kubectl get - “-“表示代表资源名称,其中资源名称后面可用跟上选项”-o wide”表示可用查看一些具体的信息
- 可以加上-n选项跟上pod所在的名称空间,如果不加则默认只会打印出default名称空间里面的Pod
- 也可以加上—all-namespaces选项打印出所有名称空间的pod资源信息
列出有哪些Pod资源
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get pods
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 31m
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d
[root@k8s-master01 nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
busybox 1/1 Running 0 31m 10.244.59.2 172.18.15.113 <none> <none>
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d 10.244.38.3 172.18.15.114 <none> <none>
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d 10.244.38.7 172.18.15.114 <none> <none>
[root@k8s-master01 nginx]#
#打印出所有名称空间的Pod资源信息
[root@k8s-master01 nginx]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
default my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
kube-system coredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
#只打印出kube-system名称空间的Pod
[root@k8s-master01 nginx]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-59c6ddbf5d-bdr92 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
列出有哪些deployment资源,默认也是打印出default名称空间的deployment资源
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get deployment
[root@k8s-master01 nginx]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 2/2 2 2 2d2h
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
my-nginx 2/2 2 2 2d2h my-nginx daocloud.io/library/nginx:1.13.0-alpine app=my-nginx
[root@k8s-master01 nginx]#
#打印出所有的名称空间的deployment
[root@k8s-master01 nginx]# kubectl get deployment --all-namespaces
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
default my-nginx 2/2 2 2 2d3h
kube-system coredns 1/1 1 1 2d3h
[root@k8s-master01 nginx]#
列出有哪些Service资源,默认也是打印出default名称空间的
- -n选项可以打印出你指定的名称空间的资源
- —all-namespaces可以打印出所有的名称的资源
kubectl get svc
[root@k8s-master01 nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d2h app=my-nginx
[root@k8s-master01 nginx]#
#只打印出kube-system名称空间的资源
[root@k8s-master01 nginx]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h
[root@k8s-master01 nginx]#
#打印出所有名称空间的资源
[root@k8s-master01 nginx]# kubectl get svc --all-namespaces
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
default my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
kube-system kube-dns ClusterIP 10.99.110.110 <none> 53/UDP,53/TCP,9153/TCP 2d3h
[root@k8s-master01 nginx]#
列出集群上面有哪些名称空间 kubectl get ns
[root@k8s-master01 nginx]# kubectl get ns
NAME STATUS AGE
default Active 8d
kube-node-lease Active 8d
kube-public Active 8d
kube-system Active 8d
[root@k8s-master01 nginx]#
查看某一个资源更详细的信息
- 语法格式:kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME) [options]
查看某一个Pod资源的详细信息 kubectl describe pod podname
[root@k8s-master01 nginx]# kubectl describe pod my-nginx-854bbd7557-phvsl
Name: my-nginx-854bbd7557-phvsl
Namespace: default
Priority: 0
Node: 172.18.15.114/172.18.15.114
Start Time: Mon, 01 Jun 2020 10:58:41 +0800
Labels: app=my-nginx
pod-template-hash=854bbd7557
Annotations: <none>
Status: Running
IP: 10.244.38.3
IPs:
IP: 10.244.38.3
Controlled By: ReplicaSet/my-nginx-854bbd7557
Containers:
my-nginx:
Container ID: docker://56d6ff87faf07a332dcf625b86301946c6f7ef956634ea1f0bf147767af6df7b
Image: daocloud.io/library/nginx:1.13.0-alpine
Image ID: docker-pullable://daocloud.io/library/nginx@sha256:5c36f962c506c379bd63884976489c9c5e700c1496a6e8ea13dc404b1d258f76
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 01 Jun 2020 10:58:44 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gvn55 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-gvn55:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gvn55
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
[root@k8s-master01 nginx]#
查看某一个service资源的详细信息,如果不加Service资源明,则默认会把所有的service资源都显示出来 kubectl describe svc servicename
[root@k8s-master01 nginx]# kubectl describe svc
Name: kubernetes
Namespace: default
Labels: component=apiserver
provider=kubernetes
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.99.0.1
Port: https 443/TCP
TargetPort: 6443/TCP
Endpoints: 172.18.15.116:6443
Session Affinity: None
Events: <none>
Name: my-nginx
Namespace: default
Labels: app=my-nginx
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
Selector: app=my-nginx
Type: NodePort
IP: 10.99.44.234
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 13359/TCP
Endpoints: 10.244.38.3:80,10.244.38.7:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master01 nginx]#
# 加资源命访问
[root@k8s-master01 nginx]# kubectl describe svc my-nginx
Name: my-nginx
Namespace: default
Labels: app=my-nginx
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
Selector: app=my-nginx
Type: NodePort
IP: 10.99.44.234
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 13359/TCP
Endpoints: 10.244.38.3:80,10.244.38.7:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master01 nginx]#
打印出Pod容器的日志,默认就是Pod类型
- 语法格式:kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 164m
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]# kubectl logs my-nginx-854bbd7557-phvsl
10.244.38.1 - - [02/Jun/2020:03:32:22 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@k8s-master01 nginx]#
如何进入到Pod的容器
- 语法格式:kubectl exec (POD | TYPE/NAME) [-c CONTAINER] [flags] — COMMAND [args…] [options]
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 167m
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]# kubectl exec -ti my-nginx-854bbd7557-phvsl /bin/sh
/ #
/ #
/ #
如何删除一个Pod,删除的方法有多种,通常我们应该是怎么创建的Pod就怎么删除,因为Pod有两种运行方式,一种是基于命令创建的,一种是基于配置清单创建的,但是结果都是需要使用kubectl命令来删除,只是使用的选项不同而已,
- 语法格式:kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | —all)]) [options]
命令式创建的Pod删除方法,因为命令创建的Pod通常是没有配置文件清单的,所以需要先get出来需要删除的Pod
- kubectl delete Podname
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 171m
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl delete pod busybox
pod "busybox" deleted
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
配置清单式的Pod删除方法,这种方法删除Pod非常简单,怎么创建的就怎么删除,只需要把apply或者create换成delete
#我们先使用配置清单创建出一个基于配置清单的Pod
[root@k8s-master01 nginx]# kubectl apply -f busybox.yml
pod/busybox created
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 ContainerCreating 0 5s
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 0 10s
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
#删除基于配置清单的Pod
[root@k8s-master01 nginx]# kubectl delete -f busybox.yml
pod "busybox" deleted
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
[root@k8s-master01 nginx]#
创建资源对象
- 语法格式:kubectl run NAME —image=image [—env=”key=value”] [—port=port] [—replicas=replicas] [—dry-run=bool] [—overrides=inline-json] [—command] — [COMMAND] [args…] [options]
- 创建资源对象有run,create,apply等命令
- 直接通过kubectl run命令及相关选项创建的资源对象被称为直接命令式操作
- 用户也可以直接根据配置清单来创建资源对象
下面是基于命令式创建一个名称为web的deployment和一个名称为web-svc的Service,镜像使用的是Nginx1.13的alpine版本,并且创建了5个副本的示例
[root@k8s-master01 nginx]# kubectl run web --image=nginx:1.13.0-alpine --replicas=5
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/web created
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h 10.244.38.3 172.18.15.114 <none> <none>
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h 10.244.38.7 172.18.15.114 <none> <none>
web-6ff4758d4d-5zkpb 1/1 Running 0 47s 10.244.38.4 172.18.15.114 <none> <none>
web-6ff4758d4d-666wt 1/1 Running 0 47s 10.244.59.4 172.18.15.113 <none> <none>
web-6ff4758d4d-n8plp 1/1 Running 0 47s 10.244.59.2 172.18.15.113 <none> <none>
web-6ff4758d4d-qk4mv 1/1 Running 0 47s 10.244.59.3 172.18.15.113 <none> <none>
web-6ff4758d4d-vmmbc 1/1 Running 0 47s 10.244.38.5 172.18.15.114 <none> <none>
[root@k8s-master01 nginx]#
#创建一个名称为web-svc的Service资源,并且将web这个deployment里面的Pod资源包含进去
[root@k8s-master01 nginx]# kubectl expose deployment/web --name=web-svc --port=80
service/web-svc exposed
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
web-svc ClusterIP 10.99.221.103 <none> 80/TCP 72s
[root@k8s-master01 nginx]#
#查看一下之前创建的5个Pod是否被包含到了这个Service资源里面
[root@k8s-master01 nginx]# kubectl describe svc web-svc
Name: web-svc
Namespace: default
Labels: run=web
Annotations: <none>
Selector: run=web
Type: ClusterIP
IP: 10.99.221.103
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.38.4:80,10.244.38.5:80,10.244.59.2:80 + 2 more...
Session Affinity: None
Events: <none>
[root@k8s-master01 nginx]#
#删除,因为我们没有配置清单,所以这里删除就比较烦,如果你的操作步骤多了,很有可能自己也会忘记自己创建的对象名称叫什么
#先删除Pod相关资源步骤:
#1.找出刚刚创建的deployment
[root@k8s-master01 nginx]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 2/2 2 2 2d3h
web 5/5 5 5 15m
[root@k8s-master01 nginx]#
#2.删除这个deployment
[root@k8s-master01 nginx]# kubectl delete deployment web
deployment.apps "web" deleted
[root@k8s-master01 nginx]#
#查看Pod,可以看到Pod资源已经是Terminating状态,虽然我这里比较方便,但是生产环境可能会有很多类似的资源,查找起来就没有这么方便了,所以推荐使用配置清单的方式去创建相关资源
[root@k8s-master01 nginx]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-854bbd7557-phvsl 1/1 Running 0 2d3h
my-nginx-854bbd7557-t7f7z 1/1 Running 0 2d3h
web-6ff4758d4d-5zkpb 0/1 Terminating 0 16m
[root@k8s-master01 nginx]#
#删除service
[root@k8s-master01 nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
web-svc ClusterIP 10.99.221.103 <none> 80/TCP 15m
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl delete svc web-svc
service "web-svc" deleted
[root@k8s-master01 nginx]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d
my-nginx NodePort 10.99.44.234 <none> 80:13359/TCP 2d3h
[root@k8s-master01 nginx]#
基于配置清单创建出一个名称为my-nginx的deployment,里面运行了5个副本,并且再创建出一个名称为my-nginx的Service资源
[root@k8s-master01 nginx]# cat my-nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 5
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: daocloud.io/library/nginx:1.13.0-alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
app: my-nginx
spec:
type: NodePort
selector:
app: my-nginx
ports:
- name: http
port: 80
targetPort: 80
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl apply -f my-nginx.yml
deployment.apps/my-nginx created
service/my-nginx created
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-854bbd7557-9c9tq 1/1 Running 0 23s 10.244.59.2 172.18.15.113 <none> <none>
my-nginx-854bbd7557-rvtkb 1/1 Running 0 23s 10.244.38.5 172.18.15.114 <none> <none>
my-nginx-854bbd7557-w676k 1/1 Running 0 23s 10.244.38.3 172.18.15.114 <none> <none>
my-nginx-854bbd7557-wbvjf 1/1 Running 0 23s 10.244.38.4 172.18.15.114 <none> <none>
my-nginx-854bbd7557-wfx8k 1/1 Running 0 23s 10.244.59.3 172.18.15.113 <none> <none>
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx
[root@k8s-master01 nginx]#
[root@k8s-master01 nginx]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.99.0.1 <none> 443/TCP 8d <none>
my-nginx NodePort 10.99.238.73 <none> 80:21954/TCP 43s app=my-nginx
[root@k8s-master01 nginx]# kubectl describe svc my-nginx
Name: my-nginx
Namespace: default
Labels: app=my-nginx
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"my-nginx"},"name":"my-nginx","namespace":"default"},"spe...
Selector: app=my-nginx
Type: NodePort
IP: 10.99.238.73
Port: http 80/TCP
TargetPort: 80/TCP
NodePort: http 21954/TCP
Endpoints: 10.244.38.3:80,10.244.38.4:80,10.244.38.5:80 + 2 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
[root@k8s-master01 nginx]#
关于如何删除前面有些,可以参考
小结
- 每个子命令下面还有众多的子命令,大家可以去网上找相关的资料查看,这里写的常用的也并不全,这里写的只是告诉大家一个基本的用法,使用的是实话可以使用kubectl Type —help来获取相关的帮助信息,一定要多动手去敲一敲才能深刻的理解