Kubectl基础命令的使用

Kubectl基础命令的使用

kubernetes基本概念

Pod:k8s最小部署单元,一组容器的集合

Deployment:最常见的控制器,用于更高级别部署和管理Pod

Service:为一组Pod提供负载均衡,对外提供统一访问入口

Label:标签,附加到某个资源上,用于关联对象、查询和筛选

Namespaces∶命名空间。将对象逻辑上隔离,也利于权限控制

edit(编辑资源)

#编辑一个资源
# 运行一个pod类型的nginx,名字叫nginx,定义的标签是app=nginx
[root@master ~]# kubectl run nginx --image=nginx --labels="app=nginx"
pod/nginx created
[root@master ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          12s

# 使用edit命令编辑
[root@master ~]# kubectl edit pods/nginx
# 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-20T09:26:53Z"
  labels:
    app: test  # 把原本app:nginx改为app:test
  name: nginx
  namespace: default

# 查看是否更改成功
[root@master ~]# kubectl describe pods/nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.240.60
Start Time:   Mon, 20 Dec 2021 04:26:53 -0500
Labels:       app=test
Annotations:  <none>
Status:       Running
IP:           10.244.1.25

scale(动态扩展)

#创建一个deployment类型的nginx2的pod
[root@master ~]# kubectl create deployment nginx2 --image=nginx
deployment.apps/nginx2 created
[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx                     1/1     Running   0          7m38s
nginx2-85bf7b8976-n9vcl   1/1     Running   0          13s

# 使用scale扩展
[root@master ~]# kubectl scale --replicas 4 deployment/nginx2
deployment.apps/nginx2 scaled
# 扩展后查看多了几个相同类型的pod
[root@master ~]# kubectl get pods
NAME                      READY   STATUS              RESTARTS   AGE
nginx                     1/1     Running             0          8m55s
nginx2-85bf7b8976-bmcq4   0/1     ContainerCreating   0          11s
nginx2-85bf7b8976-n9vcl   1/1     Running             0          90s
nginx2-85bf7b8976-qzfhz   0/1     ContainerCreating   0          11s
nginx2-85bf7b8976-xk594   0/1     ContainerCreating   0          11s

# 如果只需要2个deployment类型的nginx的pod
[root@master ~]#  kubectl scale --replicas 2 deployment/nginx2
deployment.apps/nginx2 scaled
[root@master ~]#  kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx2-85bf7b8976-4dm2h   1/1     Running   0          39s
nginx2-85bf7b8976-fwj55   1/1     Running   0          49s

autoscale(自动扩展)

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

[root@master ~]# kubectl autoscale --min=2 --max=7 --cpu-percent=60 deployment/nginx2
horizontalpodautoscaler.autoscaling/nginx2 autoscaled
[root@master ~]# kubectl get hpa
NAME     REFERENCE           TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
nginx    Deployment/nginx    <unknown>/80%   1         3         3          29m
nginx2   Deployment/nginx2   <unknown>/60%   2         7         0          10s

cluster-info(显示集群信息)

[root@master ~]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.240.30:6443
CoreDNS is running at https://192.168.240.30:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

describe(显示指定pod的详细信息)

[root@master ~]# kubectl describe pod nginx
Name:         nginx2-85bf7b8976-4dm2h
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.240.60
Start Time:   Mon, 20 Dec 2021 04:38:20 -0500
Labels:       app=nginx2
              pod-template-hash=85bf7b8976
Annotations:  <none>
Status:       Running
IP:           10.244.1.31
····省略部分·····

logs(查看日志)

[root@master ~]# kubectl logs deployment/nginx2
Found 2 pods, using pod/nginx2-85bf7b8976-fwj55
/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 09:38:13 [notice] 1#1: using the "epoll" event method
2021/12/20 09:38:13 [notice] 1#1: nginx/1.21.4
2021/12/20 09:38:13 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2021/12/20 09:38:13 [notice] 1#1: OS: Linux 4.18.0-257.el8.x86_64
2021/12/20 09:38:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/12/20 09:38:13 [notice] 1#1: start worker processes
2021/12/20 09:38:13 [notice] 1#1: start worker process 31

attach(附加在一个容器里)

[root@master ~]# kubectl attach nginx-85b98978db-cpcjn
If you don't see a command prompt, try pressing enter.(相当于是在后台运行)

exec(执行容器命令)

进到容器内执行一个命令

[root@master ~]# kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
nginx2-85bf7b8976-4dm2h   1/1     Running   0          7m1s
nginx2-85bf7b8976-fwj55   1/1     Running   0          7m11s
nginx3-6f97cd4f65-lhbld   1/1     Running   0          23s
[root@master ~]# kubectl exec nginx3-6f97cd4f65-lhbld -- date
Mon Dec 20 09:45:55 UTC 2021
[root@master ~]# kubectl exec -it nginx3-6f97cd4f65-lhbld -- /bin/bash
root@nginx3-6f97cd4f65-lhbld:/# ls
bin   docker-entrypoint.d   home   media  proc  sbin  tmp
boot  docker-entrypoint.sh  lib    mnt    root  srv   usr
dev   etc                   lib64  opt    run   sys   var

prot-forward(转发一个或多个端口到pod里面去)


[root@master ~]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-85b98978db-kdrj6   0/1     ContainerCreating   0          13s
wb1-5dbfb96758-d2b4j     1/1     Running             0          4h6m
[root@master ~]# kubectl port-forward deployment/nginx 80  
#可以变成':80'(随机一个端口号,只能本机访问)或者变成指定的端口号'8080:80'(本机访问用80,其它主机访问就可以用8080)
Forwarding from 127.0.0.1:80 -> 80
Forwarding from [::1]:80 -> 80

[root@master ~]# ss -antl
State     Recv-Q    Send-Q        Local Address:Port        Peer Address:Port    Process ·······                
LISTEN    0         128               127.0.0.1:80               0.0.0.0:*                  
LISTEN    0         128               127.0.0.1:10257            0.0.0.0:*                  
······

[root@master ~]# curl http://127.0.0.1
<!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>

[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.240.30
<!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>

cp(复制 )

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

#将init文件拷贝到nginx的/usr目录下
[root@master ~]# kubectl cp /root/init nginx-85b98978db-kdrj6:/usr

[root@master ~]# kubectl exec nginx-85b98978db-kdrj6 -- ls -l /usr
total 4
drwxr-xr-x 1 root root 261 Dec  2 10:59 bin
drwxr-xr-x 2 root root   6 Oct  3 09:15 games
drwxr-xr-x 2 root root   6 Oct  3 09:15 include
-rw-r--r-- 1 root root 178 Dec 20 08:03 init

label(标签)

给资源设置、更新标签

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

#查看标签
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.240.60
Start Time:   Mon, 20 Dec 2021 03:20:34 -0500
Labels:       run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.1.14

#追加标签
[root@master ~]# kubectl label pods nginx abc=ABC
pod/nginx labeled

##查看已经最加成功
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.240.60
Start Time:   Mon, 20 Dec 2021 03:20:34 -0500
Labels:       abc=ABC
              run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.1.14

#更改标签
[root@master ~]# kubectl label pod nginx --overwrite abc=BCD
pod/nginx labeled

#查看是否更改成功
[root@master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node2.example.com/192.168.240.60
Start Time:   Mon, 20 Dec 2021 03:20:34 -0500
Labels:       abc=BCD
              run=nginx
Annotations:  <none>
Status:       Running
IP:           10.244.1.14

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
limitranges                       limits       v1                                     true         LimitRange
namespaces                        ns           v1                                     false        Namespace
nodes                             no           v1                                     false        Node
persistentvolumeclaims            pvc          v1                                     true         PersistentVolumeClaim
persistentvolumes                 pv           v1                                     false        PersistentVolume
pods                              po           v1                                     true         Pod
podtemplates                                   v1                                     true         PodTemplate
replicationcontrollers            rc           v1                                     true         ReplicationController
resourcequotas                    quota        v1                                     true         ResourceQuota
secrets                                        v1                                     true         Secret
·····省略部分·····

api-versions(API版本)

打印受支持的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
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta2
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

使用deploument控制器部署镜像

[root@master ~]# kubectl create deployment nginx --image lizhenliang/java-demo
deployment.apps/nginx created
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-74458cff54-svm4r   1/1     Running   0          101s

[root@master ~]# kubectl get deploy,pods
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           3m17s

NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-74458cff54-svm4r   1/1     Running   0          3m17s

使用Service将pod暴露出去

[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=8080 --type=NodePort
service/nginx exposed
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        25h
nginx        NodePort    10.111.4.125   <none>        80:30407/TCP   13s

访问应用

[root@master ~]# curl http://192.168.240.30:30407  #端口号随机生成,通过getsvc获取
<!DOCTYPE html>
<html>
<head lang="en">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>把美女带回家应用案例</title>
        <meta name="description" content="把美女带回家应用案例">
        <meta name="keywords" content="index">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
·····省略部分······
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值