kubectl命令使用

kubectl命令使用

create

kubectl create deployment NAME --image=image -- [COMMAND] [args...] //格式
[root@master ~]# kubectl create deployment test1 --image busybox
deployment.apps/test1 created  //使用busybox镜像创建一个test1的pod

[root@master ~]# kubectl get pod
NAME                     READY   STATUS             RESTARTS   AGE
test1-78d64fd9b9-4ihbm   0/1     CrashLoopBackOff   3          114s  //可以看到处于退出状态,因为busybox使用的是sh,没有任务就会退出

[root@master ~]# kubectl create deployment test2 --image busybox -- sleep 60
deployment.apps/test2 created
[root@master ~]# kubectl get pod
NAME                     READY   STATUS             RESTARTS   AGE
test2-7c95bf5bcb-wdf2   1/1     Running            0          17s  //正在运行

[root@master ~]# kubectl create deployment web --image nginx --replicas 3  // 创建使用nginx镜像创建三个pod,名字为web
deployment.apps/web created  

[root@master ~]# kubectl get pod
web-96d5df5c8-2kqfx      1/1     Running            0          57s
web-96d5df5c8-ld842      1/1     Running            0          57s
web-96d5df5c8-vtwks      1/1     Running            0          57s

[root@master ~]# kubectl get pods -o wide  //查看pod运行的节点位置
web-96d5df5c8-2kqfx      1/1     Running     0          2m2s    10.244.2.3   node2.example.com   <none>           <none>
web-96d5df5c8-ld842      1/1     Running     0          2m2s    10.244.1.6   node1.example.com   <none>           <none>
web-96d5df5c8-vtwks      1/1     Running     0          2m2s    10.244.2.4   node2.example.com   <none>           <none>

[root@master ~]# kubectl create deployment web01 --image nginx --port=80  //暴露80端口号

run

// 启动一个 nginx pod
[root@master ~]# kubectl run nginx --image nginx
pod/nginx created
[root@master ~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE                NOMINATED NODE   READINESS GATES
nginx                    1/1     Running   0          36s     10.244.1.7   node1.example.com   <none>           <none>

[root@master ~]# kubectl delete pods nginx  //删除nginx的pod
pod "nginx" deleted

[root@master ~]# kubectl run nginx --images nginx --port 80  // 暴露容器的80端口号

// 在容器中设置标签“app=nginx”和“env=prod”
[root@master ~]# kubectl run nginx --image nginx --labels "app=nginx,env=prod"
pod/nginx created


[root@master ~]# kubectl describe pod nginx  //描述nginx信息

// 测试运行
[root@master ~]#  kubectl run nginx --image nginx --dry-run server  //不会真正运行
W1219 01:41:21.786495  157488 helpers.go:553] --dry-run is deprecated and can be replaced with --dry-run=client.
pod/nginx created (dry run)

delete

[root@master ~]# kubectl delete deployment test1 // 删除test1,使用deployment类型,因为我们当时创建的时候使用的是deployment类型
deployment.apps "test1" deleted

[root@master ~]# kubectl get pod
NAME                     READY   STATUS        RESTARTS   AGE
nginx-6799fc88d8-thr6q   1/1     Running       0          4h2m
test2-7c95bf5bcb-tqgn5   0/1     Terminating   8          32m
web-96d5df5c8-2kqfx      1/1     Running       0          30m
web-96d5df5c8-ld842      1/1     Running       0          30m
web-96d5df5c8-vtwks      1/1     Running       0          30m

[root@master ~]# kubectl delete deployment test2
deployment.apps "test2" deleted

[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-thr6q   1/1     Running   0          4h2m
web-96d5df5c8-2kqfx      1/1     Running   0          31m
web-96d5df5c8-ld842      1/1     Running   0          31m
web-96d5df5c8-vtwks      1/1     Running   0          31m

[root@master ~]# kubectl get svc  
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        29h
nginx        NodePort    10.99.74.11     <none>        80:31173/TCP   28h
web          ClusterIP   10.109.101.12   <none>        8080/TCP       12m

// 删除service类型的pod
[root@master ~]# kubectl delete svc nginx
'service "nginx" deleted
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP    29h
web          ClusterIP   10.109.101.12   <none>        8080/TCP   15m

[root@master ~]# kubectl delete pods --all  
[root@master ~]# kubectl delete pod foo --force  

expose

[root@master ~]# kubectl expose deployment web --port 8080 --target-port 80
service/web exposed  //将pod中的80暴露到宿主机上的8080
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        29h
nginx        NodePort    10.99.74.11     <none>        80:31173/TCP   27h
web          ClusterIP   10.109.101.12   <none>        8080/TCP       22s


[root@master ~]# curl 10.109.101.12:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

get

[root@master ~]# kubectl get pods  //列出所有的pod
NAME                     READY   STATUS             RESTARTS   AGE
nginx-6799fc88d8-thr6q   1/1     Running            0          3h41m
test1-78d64fd9b9-8gmbc   0/1     CrashLoopBackOff   7          15m
test2-7c95bf5bcb-tqgn5   0/1     CrashLoopBackOff   5          11m
web-96d5df5c8-2kqfx      1/1     Running            0          9m48s
web-96d5df5c8-ld842      1/1     Running            0          9m48s
web-96d5df5c8-vtwks      1/1     Running            0          9m48s

[root@master ~]# kubectl get pods -o wide  //显示所有pod的详细信息
NAME                     READY   STATUS             RESTARTS   AGE     IP           NODE                NOMINATED NODE   READINESS GATES
nginx-6799fc88d8-thr6q   1/1     Running            0          3h41m   10.244.2.2   node2.example.com   <none>           <none>
test1-78d64fd9b9-8gmbc   0/1     CrashLoopBackOff   7          15m     10.244.1.4   node1.example.com   <none>           <none>
test2-7c95bf5bcb-tqgn5   0/1     CrashLoopBackOff   5          12m     10.244.1.5   node1.example.com   <none>           <none>
web-96d5df5c8-2kqfx      1/1     Running            0          10m     10.244.2.3   node2.example.com   <none>           <none>
web-96d5df5c8-ld842      1/1     Running            0          10m     10.244.1.6   node1.example.com   <none>           <none>
web-96d5df5c8-vtwks      1/1     Running            0          10m     10.244.2.4   node2.example.com   <none>           <none>

[root@master ~]# kubectl get deployment web  //查看你指定类型的pod,类型加pod名
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    3/3     3            3           13m

[root@master ~]# kubectl get svc  //列出所有服务
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        29h
nginx        NodePort    10.99.74.11   <none>        80:31173/TCP   27h

edit
编辑一个资源

# 运行一个pod类型的nginx,名字叫web1,定义的标签是app=nginx
[root@master ~]# kubectl run web1 --image=nginx --labels="app=nginx"
pod/web1 created
# 查看
[root@master ~]# kubectl get pods
NAME   READY   STATUS    RESTARTS   AGE
web1   1/1     Running   0          19s
# 使用edit命令编辑
[root@master ~]# kubectl edit pods/web1
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-12-20T06:43:02Z"
  labels:
    app: test   # 修改为test
  name: web1
........ 
# 查看是否更改成功
[root@master ~]# kubectl describe pods/web1
Name:         web1
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.220.20
Start Time:   Mon, 20 Dec 2021 01:43:02 -0500
Labels:       app=test    # 编辑成功

scale
动态扩展

#创建一个deployment类型的nginx的pod 
[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
# 查看
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-gqq2s   1/1     Running   0          19s
web1                     1/1     Running   0          17m
# 使用scale扩展
[root@master ~]# kubectl scale --replicas 4 deployment/nginx
deployment.apps/nginx scaled
# 扩展后查看多了几个相同类型的pod
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-b5r9p   1/1     Running   0          33s
nginx-85b98978db-gqq2s   1/1     Running   0          2m16s
nginx-85b98978db-t8bcq   1/1     Running   0          33s
nginx-85b98978db-vxkmt   1/1     Running   0          33s
web1                     1/1     Running   0          19m

# 如果只需要2个deployment类型的nginx的pod
[root@master ~]# kubectl scale --replicas 2 deployment/nginx
deployment.apps/nginx scaled
# 查看
[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-gqq2s   1/1     Running   0          4m20s
nginx-85b98978db-t8bcq   1/1     Running   0          2m37s
web1                     1/1     Running   0          21m

autoscale
自动扩展,给定一个范围,自动根据业务的访问量增加或减少

[root@master ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx
horizontalpodautoscaler.autoscaling/nginx autoscaled

[root@master ~]# kubectl get hpa
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   <unknown>/60%   2         7         0          9s

cluster-info
显示集群信息

[root@master ~]# kubectl cluster-info
Kubernetes control plane(kubernetes 控制面板) is running at https://192.168.220.17:6443
CoreDNS is running at https://192.168.220.17:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
(为了进一步调试和诊断集群问题,使用'kubectl cluster-info dump')

drain
驱逐节点上的应用,准备下线维护

describe
显示指定pod的详细信息

[root@master ~]# kubectl describe pod web1
Name:         web1
Namespace:    default
Priority:     0
Node:         node1.example.com/192.168.220.20
Start Time:   Mon, 20 Dec 2021 01:43:02 -0500
Labels:       app=test
Annotations:  <none>
Status:       Running
IP:           10.244.1.8
IPs:
  IP:  10.244.1.8

logs
查看日志

[root@master ~]# kubectl logs deployment/nginx
Found 4 pods, using pod/nginx-85b98978db-gqq2s
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/12/20 07:00:17 [notice] 1#1: using the "epoll" event method
2021/12/20 07:00:17 [notice] 1#1: nginx/1.21.4
2021/12/20 07:00:17 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/20 07:00:17 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 07:00:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 07:00:17 [notice] 1#1: start worker processes
2021/12/20 07:00:17 [notice] 1#1: start worker process 31
2021/12/20 07:00:17 [notice] 1#1: start worker process 32
2021/12/20 07:00:17 [notice] 1#1: start worker process 33
2021/12/20 07:00:17 [notice] 1#1: start worker process 34

attach
附加在一个容器里

[root@master ~]# kubectl attach web1
If you don't see a command prompt, try pressing enter.

exec
进到容器内执行一个命令

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-4h4hl   1/1     Running   0          45m
nginx-85b98978db-gqq2s   1/1     Running   0          58m
nginx-85b98978db-t8bcq   1/1     Running   0          56m
nginx-85b98978db-w4r4v   1/1     Running   0          45m
web1                     1/1     Running   0          75m

[root@master ~]# kubectl exec web1 -- date
Mon Dec 20 07:58:28 UTC 2021

[root@master ~]# kubectl exec web1 -- ls -l /
total 12
drwxr-xr-x   2 root root 4096 Dec  1 00:00 bin
drwxr-xr-x   2 root root    6 Oct  3 09:15 boot
drwxr-xr-x   5 root root  360 Dec 20 06:43 dev
drwxr-xr-x   1 root root   41 Dec  2 10:59 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Dec  2 10:58 docker-entrypoint.sh
drwxr-xr-x   1 root root   19 Dec 20 06:43 etc
drwxr-xr-x   2 root root    6 Oct  3 09:15 home
drwxr-xr-x   1 root root   45 Dec  1 00:00 lib
drwxr-xr-x   2 root root   34 Dec  1 00:00 lib64
drwxr-xr-x   2 root root    6 Dec  1 00:00 media
drwxr-xr-x   2 root root    6 Dec  1 00:00 mnt
drwxr-xr-x   2 root root    6 Dec  1 00:00 opt
dr-xr-xr-x 199 root root    0 Dec 20 06:43 proc
drwx------   2 root root   37 Dec  1 00:00 root
drwxr-xr-x   1 root root   38 Dec 20 06:43 run
drwxr-xr-x   2 root root 4096 Dec  1 00:00 sbin
drwxr-xr-x   2 root root    6 Dec  1 00:00 srv
dr-xr-xr-x  13 root root    0 Dec 20 06:43 sys
drwxrwxrwt   1 root root    6 Dec  2 10:59 tmp
drwxr-xr-x   1 root root   66 Dec  1 00:00 usr
drwxr-xr-x   1 root root   19 Dec  1 00:00 var

[root@master ~]# kubectl exec -it web1 -- /bin/bash
root@web1:/# ls
bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
root@web1:/# exit
exit

port-forward
转发端口到pod里面

[root@master ~]# kubectl create deployment nginx --image=nginx
deployment.apps/nginx created

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          18s
web1                     1/1     Running   0          3h4m

[root@master ~]# kubectl port-forward deployment/nginx 80
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80
Handling connection for 80

[root@master ~]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...............

root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/nginx 80
Forwarding from 0.0.0.0:80 -> 80

#允许所有IP访问80端口
[root@master ~]# curl http://192.168.220.17
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

cp
拷贝文件或目录到容器中,或者从容器内向外拷贝

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          13m
web1                     1/1     Running   0          3h17m

[root@master ~]# kubectl cp /root/init nginx-85b98978db-9vbc7:/

[root@master ~]# kubectl exec nginx-85b98978db-9vbc7 -- ls -l /
total 16
drwxr-xr-x   2 root root 4096 Dec  1 00:00 bin
drwxr-xr-x   2 root root    6 Oct  3 09:15 boot
drwxr-xr-x   5 root root  360 Dec 20 09:47 dev
drwxr-xr-x   1 root root   41 Dec  2 10:59 docker-entrypoint.d
-rwxrwxr-x   1 root root 1202 Dec  2 10:58 docker-entrypoint.sh
drwxr-xr-x   1 root root   19 Dec 20 09:47 etc
drwxr-xr-x   2 root root    6 Oct  3 09:15 home
-rw-r--r--   1 root root  178 Dec 20 10:00 init

label
给资源设置、更新标签

[root@master ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7   1/1     Running   0          19m
web1                     1/1     Running   0          3h23m

# 添加标签
[root@master ~]# kubectl label --overwrite=true deployment nginx app=test
deployment.apps/nginx unlabeled

#查看
[root@master ~]# kubectl describe deployment/nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 20 Dec 2021 04:46:45 -0500
Labels:                 app=test
...........

# 追加标签
[root@master ~]# kubectl label pods nginx-85b98978db-9vbc7 xyz=123
pod/nginx-85b98978db-9vbc7 labeled

# 查看
[root@master ~]# kubectl describe pod nginx-85b98978db-9vbc7
Name:         nginx-85b98978db-9vbc7
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.220.21
Start Time:   Mon, 20 Dec 2021 04:46:45 -0500
Labels:       app=nginx
              pod-template-hash=85b98978db
              xyz=123
 ..................             
              
# 更改标签
[root@master ~]# kubectl label pod nginx-85b98978db-9vbc7 --overwrite xyz=111
pod/nginx-85b98978db-9vbc7 labeled
# 查看
[root@master ~]# kubectl describe pod nginx-85b98978db-9vbc7
Name:         nginx-85b98978db-9vbc7
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.220.21
Start Time:   Mon, 20 Dec 2021 04:46:45 -0500
Labels:       app=nginx
              pod-template-hash=85b98978db
              xyz=111

api-resources
查看所有资源

[root@master ~]# kubectl api-resources
NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
bindings                                       v1                                     true         Binding
componentstatuses                 cs           v1                                     false        ComponentStatus
configmaps                        cm           v1                                     true         ConfigMap
endpoints                         ep           v1                                     true         Endpoints
events                            ev           v1                                     true         Event

apr-versions
显示受支持的API版本

[root@master ~]# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2

deploument
控制器部署镜像

[root@master ~]# kubectl create deployment nginx111 --image lizhenliang/java-demo

root@master ~]# kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
nginx-85b98978db-9vbc7     1/1     Running   0          28m
nginx111-f44dcbcb5-mzmn8   1/1     Running   0          34s
web1                       1/1     Running   0          3h32m

# 暴露端口使用service
[root@master ~]# kubectl expose deployment nginx111 --port=80 --target-port=8080 --type=NodePort
service/nginx111 exposed

[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        2d3h
nginx111     NodePort    10.104.225.197   <none>        80:30081/TCP   2m22s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值