首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

K8s Kubectl基础命令的使用、滚动更新、以及回滚操作

  • 25-02-17 11:21
  • 2712
  • 11107
blog.csdn.net

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:

diff Diff live version against would-be applied version

apply 通过文件名或标准输入流(stdin)对资源进行配置

patch 使用 strategic merge patch 更新一个资源的 field(s)

replace 通过 filename 或者 stdin替换一个资源

wait Experimental: Wait for a specific condition on one or many resources.

convert 在不同的 API versions 转换配置文件

Settings Commands:

label 更新在这个资源上的 labels

annotate 更新一个资源的注解

completion Output shell completion code for the specified shell (bash or zsh)

Other Commands:

alpha Commands for features in alpha

api-resources Print the supported API resources on the server

api-versions Print the supported API versions on the server, in the form of “group/version”

config 修改 kubeconfig 文件

plugin Provides utilities for interacting with plugins.

version 输出 client 和 server 的版本信息

Kubectl常用命令的使用

=============================================================================

explain


获取资源

用法:

kubectl explain RESOURCE

//获取资源及其字段的文档

[root@master ~]# kubectl explain pods

KIND: Pod

VERSION: v1

DESCRIPTION:

Pod is a collection of containers that can run on a host. This resource is

created by clients and scheduled onto hosts.

FIELDS:

apiVersion

APIVersion defines the versioned schema of this representation of an

object. Servers should convert recognized schemas to the latest internal

value, and may reject unrecognized values. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

kind

Kind is a string value representing the REST resource this object

represents. Servers may infer this from the endpoint the client submits

requests to. Cannot be updated. In CamelCase. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

metadata

Standard object’s metadata. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

spec

Specification of the desired behavior of the pod. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

status

Most recently observed status of the pod. This data may not be up to date.

Populated by the system. Read-only. More info:

https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

edit


编辑资源

//运行一个pod类型的nginx,名字叫nginx,内型是deployment

[root@master ~]# kubectl create deployment nginx --image nginx

deployment.apps/nginx created

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 0/1 ContainerCreating 0 10s

//使用edit命令编辑

[root@master ~]# kubectl edit deployment nginx

…

kind: Deployment

metadata:

annotations:

deployment.kubernetes.io/revision: “1”

creationTimestamp: “2021-12-20T10:33:49Z”

generation: 1

labels:

app: nginx

name: jjyy #添加name: jjyy

…

//查看

[root@master ~]# kubectl describe deploy nginx

Name: nginx

Namespace: default

CreationTimestamp: Mon, 20 Dec 2021 18:33:49 +0800

Labels: app=nginx

name=jjyy

Annotations: deployment.kubernetes.io/revision: 1

Selector: app=nginx

Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable

StrategyType: RollingUpdate

MinReadySeconds: 0

RollingUpdateStrategy: 25% max unavailable, 25% max surge

Pod Template:

…

scale


动态扩展

//使用scale扩展

[root@master ~]# kubectl scale deployment/nginx --replicas 3

deployment.apps/nginx scaled

//扩展后查看多了几个相同类型的pod

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 37s

nginx-6799fc88d8-cpbg4 1/1 Running 0 15m

nginx-6799fc88d8-t6vpw 1/1 Running 0 37s

//如果只需要2个deployment类型的nginx的pod

[root@master ~]# kubectl scale deployment/nginx --replicas 2

deployment.apps/nginx scaled

//查看发现只有两个(删除是随机的)

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 3m22s

nginx-6799fc88d8-cpbg4 1/1 Running 0 18m

autoscale


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

//最少3个,当不满足条件时会自动创建

[root@master ~]# kubectl autoscale deployment/nginx --min 3 --max=5

horizontalpodautoscaler.autoscaling/nginx autoscaled

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-8bnn8 1/1 Running 0 8m1s

nginx-6799fc88d8-cpbg4 1/1 Running 0 23m

nginx-6799fc88d8-ctjmt 0/1 ContainerCreating 0 4s

//当大于5个时,先创建,之后也会随机删除,直到满足条件五个

[root@master ~]# kubectl scale deployment/nginx --replicas 7

deployment.apps/nginx scaled

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-84mfw 1/1 Running 0 52s

nginx-6799fc88d8-8bnn8 1/1 Running 0 20m

nginx-6799fc88d8-cpbg4 1/1 Running 0 35m

nginx-6799fc88d8-ctjmt 1/1 Running 0 12m

nginx-6799fc88d8-rf74m 1/1 Running 0 52s

cluster-info


显示集群信息

[root@master ~]# kubectl cluster-info

Kubernetes control plane is running at https://192.168.129.250:6443

KubeDNS is running at https://192.168.129.250:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

cordon


可调度

[root@master ~]# kubectl cordon node2

node/node2 already cordoned

[root@master ~]# kubectl get node

NAME STATUS ROLES AGE VERSION

master Ready control-plane,master 2d4h v1.20.0

node1 NotReady 2d4h v1.20.0

node2 Ready,SchedulingDisabled 2d4h v1.20.0

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-cpbg4 1/1 Running 0 132m 10.244.1.24 node1

nginx-6799fc88d8-plvhf 1/1 Running 0 95m 10.244.1.31 node1

nginx-6799fc88d8-rf74m 1/1 Running 1 97m 10.244.2.4 node2

[root@master ~]# kubectl scale deployment/nginx --replicas 2

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-9wk4h 0/1 Pending 0 3m46s

nginx-6799fc88d8-rf74m 1/1 Running 1 103m 10.244.2.4 node2

nginx-6799fc88d8-snm2v 0/1 Pending 0 16s

uncordon


不可调度

[root@master ~]# kubectl uncordon node2

node/node2 uncordoned

[root@master ~]# kubectl get node

NAME STATUS ROLES AGE VERSION

master Ready control-plane,master 2d4h v1.20.0

node1 NotReady 2d4h v1.20.0

node2 Ready 2d4h v1.20.0

[root@master ~]# kubectl create deployment nginx2 --image nginx

deployment.apps/nginx2 created

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-9wk4h 1/1 Running 0 8m22s

nginx-6799fc88d8-rf74m 1/1 Running 1 108m

nginx-6799fc88d8-snm2v 1/1 Running 0 4m52s

nginx2-5fc4444698-bf76p 1/1 Running 0 25s

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-6799fc88d8-9wk4h 1/1 Running 0 8m59s 10.244.2.5 node2

nginx-6799fc88d8-rf74m 1/1 Running 1 109m 10.244.2.4 node2

nginx-6799fc88d8-snm2v 1/1 Running 0 5m29s 10.244.2.6 node2

nginx2-5fc4444698-bf76p 1/1 Running 0 62s 10.244.2.7 node2

describe


显示指定pod的详细信息

[root@master ~]# kubectl describe pod nginx

Name: nginx-6799fc88d8-cpbg4

Namespace: default

Priority: 0

Node: node1/192.168.129.135

Start Time: Mon, 20 Dec 2021 18:33:49 +0800

Labels: app=nginx

pod-template-hash=6799fc88d8

Annotations:

Status: Running

IP: 10.244.1.24

…

logs


查看日志

[root@master ~]# kubectl logs deployment/nginx

…

2021/12/20 10:34:05 [notice] 1#1: start worker processes

2021/12/20 10:34:05 [notice] 1#1: start worker process 32

2021/12/20 10:34:05 [notice] 1#1: start worker process 33

/docker-entrypoint.sh: Configuration complete; ready for start up

[root@master ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 443/TCP 2d4h

nginx ClusterIP 10.102.213.63 8080/TCP 24h

[root@master ~]# curl 10.102.213.63:8080

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

[root@master ~]# kubectl logs deployment/nginx

…

2021/12/20 12:44:49 [notice] 1#1: start worker processes

2021/12/20 12:44:49 [notice] 1#1: start worker process 31

2021/12/20 12:44:49 [notice] 1#1: start worker process 32

10.244.0.0 - - [20/Dec/2021:13:04:14 +0000] “GET / HTTP/1.1” 200 615 “-” “curl/7.61.1” “-”

10.244.0.0 - - [20/Dec/2021:13:05:26 +0000] “GET / HTTP/1.1” 200 615 “-” “curl/7.61.1” “-”

attach


附加在一个容器里

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Running 0 43m

nginx-6799fc88d8-plvhf 1/1 Running 0 6m38s

nginx-6799fc88d8-rf74m 1/1 Running 0 8m57s

[root@master ~]# kubectl attach nginx-6799fc88d8-cpbg4

Defaulting container name to nginx.

Use ‘kubectl describe pod/nginx-6799fc88d8-cpbg4 -n default’ to see all of the containers in this pod.

If you don’t see a command prompt, try pressing enter.

exec


执行容器命令

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Running 0 45m

nginx-6799fc88d8-plvhf 1/1 Running 0 8m2s

nginx-6799fc88d8-rf74m 1/1 Running 0 10m

[root@master ~]# kubectl exec nginx-6799fc88d8-cpbg4 – date

Mon Dec 20 11:20:02 UTC 2021

[root@master ~]# kubectl exec -it nginx-6799fc88d8-cpbg4 – /bin/sh

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

exit

[root@master ~]#

prot-forward


转发一个或多个端口到pod里面去

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

web-96d5df5c8-8fzdv 1/1 Running 0 17s

[root@master ~]# kubectl port-forward deployment/web 80 #可以变成’:80’(随机一个端口号,只能本机访问)或者变成指定的端口号’8080:80’(本机访问用80,其它主机访问就可以用8080)

Forwarding from 127.0.0.1:80 -> 80

Forwarding from [::1]:80 -> 80

[root@master ~]# ss -anlt

State Recv-Q Send-Q Local Address:Port Peer Address:Port

… 0.0.0.0:*

LISTEN 0 128 127.0.0.1:2381 0.0.0.0:*

LISTEN 0 128 127.0.0.1:80 0.0.0.0:*

[root@master ~]# curl 127.0.0.1

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

[root@master ~]# kubectl port-forward --address 0.0.0.0 deployment/web 80

Forwarding from 0.0.0.0:80 -> 80

//允许所有IP访问80端口

[root@master ~]# curl 192.168.129.250

Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.

For online documentation and support please refer to

nginx.org.

Commercial support is available at

nginx.com.

Thank you for using nginx.

在这里插入图片描述

cp


复制

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

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE

nginx-6799fc88d8-cpbg4 1/1 Terminating 0 3h42m

nginx-6799fc88d8-plvhf 1/1 Terminating 0 3h5m

web-96d5df5c8-8fzdv 1/1 Running 0 5m38s

[root@master ~]# ls

anaconda-ks.cfg init kube-flannel.yml

[root@master ~]# kubectl cp /root/anaconda-ks.cfg web-96d5df5c8-8fzdv:/usr

[root@master ~]# kubectl exec web-96d5df5c8-8fzdv – ls -l /usr/

total 4

-rw------- 1 root root 1184 Dec 20 14:17 anaconda-ks.cfg

drwxr-xr-x 1 root root 261 Dec 2 10:59 bin

drwxr-xr-x 2 root root 6 Oct 3 09:15 games

…略

label


标签

给资源设置、更新标签

[root@master ~]# kubectl run nginx --image nginx

pod/nginx created

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: run=nginx

…

//追加标签

[root@master ~]# kubectl label pods nginx app=test

pod/nginx labeled

//查看标签

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: app=test

run=nginx

…

//更改标签

[root@master ~]# kubectl label --overwrite pods nginx app=hehe

pod/nginx labeled

//查看是否更改成功

[root@master ~]# kubectl describe pod nginx

Name: nginx

Namespace: default

Priority: 0

Node: node2/192.168.129.136

Start Time: Mon, 20 Dec 2021 22:34:38 +0800

Labels: app=hehe

run=nginx

…

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

…

api-versions


API版本

[root@master ~]# kubectl api-versions

admissionregistration.k8s.io/v1

admissionregistration.k8s.io/v1beta1

apiextensions.k8s.io/v1

apiextensions.k8s.io/v1beta1

apiregistration.k8s.io/v1

apiregistration.k8s.io/v1beta1

apps/v1

authentication.k8s.io/v1

authentication.k8s.io/v1beta1

authorization.k8s.io/v1

authorization.k8s.io/v1beta1

autoscaling/v1

autoscaling/v2beta1

autoscaling/v2beta2

batch/v1

batch/v1beta1

certificates.k8s.io/v1

certificates.k8s.io/v1beta1

coordination.k8s.io/v1

coordination.k8s.io/v1beta1

discovery.k8s.io/v1beta1

events.k8s.io/v1

events.k8s.io/v1beta1

extensions/v1beta1

flowcontrol.apiserver.k8s.io/v1beta1

networking.k8s.io/v1

networking.k8s.io/v1beta1

node.k8s.io/v1

node.k8s.io/v1beta1

policy/v1beta1

rbac.authorization.k8s.io/v1

rbac.authorization.k8s.io/v1beta1

scheduling.k8s.io/v1

scheduling.k8s.io/v1beta1

storage.k8s.io/v1

storage.k8s.io/v1beta1

v1

version


查看版本

[root@master ~]# kubectl version

Client Version: version.Info{Major:“1”, Minor:“20”, GitVersion:“v1.20.0”, GitCommit:“af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38”, GitTreeState:“clean”, BuildDate:“2020-12-08T17:59:43Z”, GoVersion:“go1.15.5”, Compiler:“gc”, Platform:“linux/amd64”}

Server Version: version.Info{Major:“1”, Minor:“20”, GitVersion:“v1.20.0”, GitCommit:“af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38”, GitTreeState:“clean”, BuildDate:“2020-12-08T17:51:19Z”, GoVersion:“go1.15.5”, Compiler:“gc”, Platform:“linux/amd64”}

使用deployment控制器部署镜像


[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-5fd5cdd7d4-4sjb7 1/1 Running 0 3m30s

nginx-5fd5cdd7d4-nq9bb 1/1 Running 0 3m30s

nginx-5fd5cdd7d4-p97tp 1/1 Running 0 3m45s

[root@master ~]# kubectl get deploy,pods

NAME READY UP-TO-DATE AVAILABLE AGE

deployment.apps/nginx 1/3 3 1 38s

NAME READY STATUS RESTARTS AGE

pod/nginx-5fd5cdd7d4-4sjb7 1/1 Running 0 3m59s

pod/nginx-5fd5cdd7d4-nq9bb 1/1 Running 0 3m59s

pod/nginx-5fd5cdd7d4-p97tp 1/1 Running 0 4m14s

//使用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 443/TCP 2d6h

nginx NodePort 10.111.209.122 80:32603/TCP 8s

//访问

[root@master ~]# curl 192.168.129.250:32603

把美女带回家应用案例

添加美女

今晚翻盘哪个

滚动更新、回滚

======================================================================

用Dockerfile编写两个镜像


[root@master ~]# mkdir httpd

[root@master ~]# cd httpd/

[root@master ~]# ls

anaconda-ks.cfg httpd init kube-flannel.yml

//制作第一版镜像

[root@master ~]# vim httpd/Dockerfile

FROM busybox

RUN mkdir /data && \

echo “test page on jjyy” > /data/index.html

ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]

[root@master ~]# docker build -t 1314444/httpd:v0.1 httpd

Sending build context to Docker daemon 2.048kB

Step 1/3 : FROM busybox

—> ffe9d497c324

Step 2/3 : RUN mkdir /data && echo “test page on jjyy” > /data/index.html

—> Using cache

—> 2329b553343b

Step 3/3 : ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]

—> Using cache

—> 17a2afe17b15

Successfully built 17a2afe17b15

Successfully tagged 1314444/httpd:v0.1

//制作第二版镜像

[root@master ~]# cat httpd/Dockerfile

FROM busybox

RUN mkdir /data && \

echo “test page on 666” > /data/index.html

ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]

[root@master ~]# docker build -t 1314444/httpd:v0.2 httpd

Sending build context to Docker daemon 2.048kB

Step 1/3 : FROM busybox

—> ffe9d497c324

Step 2/3 : RUN mkdir /data && echo “test page on 666” > /data/index.html

—> Using cache

—> 1674557401b0

Step 3/3 : ENTRYPOINT [“/bin/httpd”,“-f”,“-h”,“/data”]

—> Using cache

—> e83d0d9638e2

Successfully built e83d0d9638e2

Successfully tagged 1314444/httpd:v0.2

//上传镜像

[root@master httpd]# docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.

Username: 1314444

Password:

WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store

注:本文转载自blog.csdn.net的2401_89694015的文章"https://blog.csdn.net/2401_89694015/article/details/145100807"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

115
云原生
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top