简单易懂的Kubernetes(K8S)之kubectl管理
文章目录1、帮助信息2、创建kubectl run 命令3、发布nginxservice提供负载均衡的功能4、查看资源对象简写5、查看关联后端的节点6、网络状态详细信息7、服务暴露的端口8、在两台node节点安装ipvs9、在node02操作 同样安装ipvsadmin工具查看10、在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件)11、更新nginx 为
·
文章目录
- 1、帮助信息
- 2、创建 kubectl run 命令
- 3、发布nginx service提供负载均衡的功能
- 4、查看资源对象简写
- 5、查看关联后端的节点
- 6、网络状态详细信息
- 7、服务暴露的端口
- 8、在两台node节点安装ipvs
- 9、在node02操作 同样安装ipvsadmin工具查看
- 10、在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件)
- 11、更新nginx 为1.14版本
- 12、获取修改模板
- 13、回滚nginx
- 14、查看历史版本
- 15、执行回滚
- 16、检查回滚状态
- 17、查看deployment
- 18、删除nginx
- 19、删除服务SVC
- 20、查看具体资源的详细信息
- 21、查看deployment资源
- 22、进入pod
Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
1、帮助信息
[root@master1 ~]# 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 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
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
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替换一个资源
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 的版本信息
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).
#项目的生命周期,创建-----> 发布------> 更新------> 回滚------> 删除
2、创建 kubectl run 命令
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas]
[--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
示例:
[root@master1 ~]# kubectl get pod #检查存在的资源
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-fskqq 1/1 Running 1 21h
[root@master1 ~]# kubectl delete pod nginx-dbddb74b8-fskqq #删除存在的pod
pod "nginx-dbddb74b8-fskqq" deleted
[root@master1 ~]# kubectl get pod #发现删除过后又自动创建了新的pod
NAME READY STATUS RESTARTS AGE
nginx-dbdd b74b8-zrlwc 1/1 Running 0 99s
[root@master1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-dbddb74b8-zrlwc 1/1 Running 0 6m35s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 47h
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1 1 1 1 22h
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dbddb74b8 1 1 1 22h
[root@master1 ~]# kubectl delete deployment/nginx #删除nginx控制器
deployment.extensions "nginx" deleted
[root@master1 ~]# kubectl get pod #此时资源就销毁了
No resources found.
[root@master1 ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d
--------开始创建新的资源-------
[root@master1 ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
#创建名为nginx的资源,镜像为nginx最新版,使用replicas=3:3副本机制
[root@master1 ~]# kubectl get pods #查看资源是否创建成功
NAME READY STATUS RESTARTS AGE
nginx-7697996758-gmgpn 1/1 Running 0 95s
nginx-7697996758-jbq5f 1/1 Running 0 95s
nginx-7697996758-sk7xd 1/1 Running 0 95s
[root@master1 ~]# kubectl get pods -o wide #查看3个资源的ip地址
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-7697996758-gmgpn 1/1 Running 0 2m11s 172.17.86.3 192.168.200.60 <none>
nginx-7697996758-jbq5f 1/1 Running 0 2m11s 172.17.63.2 192.168.200.40 <none>
nginx-7697996758-sk7xd 1/1 Running 0 2m11s 172.17.86.4 192.168.200.60 <none>
[root@master1 ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-gmgpn 1/1 Running 0 5m34s
pod/nginx-7697996758-jbq5f 1/1 Running 0 5m34s
pod/nginx-7697996758-sk7xd 1/1 Running 0 5m34s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3 3 3 3 5m34s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-7697996758 3 3 3 5m34s
[root@master1 ~]# kubectl get pods,deployment,replicaset
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-gmgpn 1/1 Running 0 33m
pod/nginx-7697996758-jbq5f 1/1 Running 0 33m
pod/nginx-7697996758-sk7xd 1/1 Running 0 33m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3 3 3 3 33m
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-7697996758 3 3 3 33m
3、发布nginx service提供负载均衡的功能
格式:
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP]
[--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
[options]
[root@master1 ~]# kubectl get svc #先查看已经存在的资源
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d
#发布nginx service
[root@master1 ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
4、查看资源对象简写
[root@master1 ~]# kubectl api-resources
5、查看关联后端的节点
[root@master1 ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.200.10:6443,192.168.200.20:6443 2d2h
nginx-service 172.17.63.2:80,172.17.86.3:80,172.17.86.4:80 20m
6、网络状态详细信息
[root@master1 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-7697996758-gmgpn 1/1 Running 0 118m 172.17.86.3 192.168.200.60 <none>
nginx-7697996758-jbq5f 1/1 Running 0 118m 172.17.63.2 192.168.200.40 <none>
nginx-7697996758-sk7xd 1/1 Running 0 118m 172.17.86.4 192.168.200.60 <none>
7、服务暴露的端口
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d2h
nginx-service NodePort 10.0.0.242 <none> 80:35988/TCP 22m
此时就可以在外部访问pod资源了
8、在两台node节点安装ipvs
注意:kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式
yum install ipvsadm -y
[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.17.63.1:30001 rr
-> 172.17.86.2:8443 Masq 1 0 0
TCP 172.17.63.1:35988 rr
-> 172.17.63.2:80 Masq 1 0 0
-> 172.17.86.3:80 Masq 1 0 0
-> 172.17.86.4:80 Masq 1 0 0
TCP 192.168.122.1:30001 rr
-> 172.17.86.2:8443 Masq 1 0 0
TCP 192.168.122.1:35988 rr
-> 172.17.63.2:80 Masq 1 0 0
-> 172.17.86.3:80 Masq 1 0 0
-> 172.17.86.4:80 Masq 1 0 0
9、在node02操作 同样安装ipvsadmin工具查看
10、在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件)
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-gmgpn 1/1 Running 0 124m
nginx-7697996758-jbq5f 1/1 Running 0 124m
nginx-7697996758-sk7xd 1/1 Running 0 124m
[root@master1 ~]# kubectl logs nginx-7697996758-gmgpn
[root@master1 ~]# kubectl logs nginx-7697996758-jbq5f
[root@master1 ~]# kubectl logs nginx-7697996758-sk7xd
11、更新nginx 为1.14版本
谷歌浏览器重新加载刷新页面查看nginx版本信息
[root@master1 ~]# kubectl set --help
Configure application resources
These commands help you make changes to existing application resources.
Available Commands:
env Update environment variables on a pod template
image 更新一个 pod template 的镜像
resources 在对象的 pod templates 上更新资源的 requests/limits
selector 设置 resource 的 selector
serviceaccount Update ServiceAccount of a resource
subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding
Usage:
kubectl set SUBCOMMAND [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).
12、获取修改模板
[root@master1 ~]# kubectl set image deployment/nginx nginx=nginx:1.14
#处于动态监听状态
[root@master1 ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-6ff7c89c7c-cskmk 1/1 Running 0 52s
nginx-6ff7c89c7c-t5pt7 0/1 ContainerCreating 0 2s
nginx-6ff7c89c7c-vmkcn 1/1 Running 0 26s
nginx-7697996758-gmgpn 1/1 Running 0 140m
nginx-7697996758-jbq5f 0/1 Terminating 0
//Ctrl+c中断监听,更新速度快
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6ff7c89c7c-cskmk 1/1 Running 0 98s
nginx-6ff7c89c7c-t5pt7 1/1 Running 0 48s
nginx-6ff7c89c7c-vmkcn 1/1 Running 0 72s
版本更新成功
13、回滚nginx
[root@master1 ~]# kubectl rollout --help
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history 显示 rollout 历史
pause 标记提供的 resource 为中止状态
resume 继续一个停止的 resource
status 显示 rollout 的状态
undo 撤销上一次的 rollout
Usage:
kubectl rollout SUBCOMMAND [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).
14、查看历史版本
[root@master1 ~]# kubectl rollout history deployment/nginx deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
15、执行回滚
[root@master1 ~]# kubectl rollout undo deployment/nginx
16、检查回滚状态
[root@master1 ~]# kubectl rollout status deployment/nginx
回滚成功
17、查看deployment
[root@master1 ~]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 147m
18、删除nginx
[root@master1 ~]# kubectl delete deployment/nginx
[root@master1 ~]# kubectl get deploy
No resources found.
[root@master1 ~]# kubectl get pods
No resources found.
19、删除服务SVC
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d2h
nginx-service NodePort 10.0.0.242 <none> 80:35988/TCP 52m
[root@master1 ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 2d2h
20、查看具体资源的详细信息
[root@master1 ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx created
[root@master1 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-2pvdr 1/1 Running 0 24s
nginx-7697996758-php62 0/1 ContainerCreating 0 24s
nginx-7697996758-pk7gs 0/1 ContainerCreating 0 24s
[root@master1 ~]# kubectl describe pod nginx-7697996758-2pvdr
Name: nginx-7697996758-2pvdr
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: 192.168.200.60/192.168.200.60
Start Time: Thu, 15 Apr 2021 19:58:05 +0800
Labels: pod-template-hash=7697996758
run=nginx
Annotations: <none>
Status: Running
IP: 172.17.86.3
Controlled By: ReplicaSet/nginx-7697996758
Containers:
nginx:
Container ID: docker://3c530259a879188cb78e0e458a59296772c5564542e6b75a423ebdf84b0f4cac
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:75a55d33ecc73c2a242450a9f1cc858499d468f077ea942867e662c247b5e412
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 15 Apr 2021 19:58:21 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-697gb (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-697gb:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-697gb
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:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 51s kubelet, 192.168.200.60 pulling image "nginx:latest"
Normal Scheduled 50s default-scheduler Successfully assigned default/nginx-7697996758-2pvdr to 192.168.200.60
Normal Pulled 35s kubelet, 192.168.200.60 Successfully pulled image "nginx:latest"
Normal Created 35s kubelet, 192.168.200.60 Created container
Normal Started 35s kubelet, 192.168.200.60 Started container
21、查看deployment资源
[root@master1 ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Thu, 15 Apr 2021 19:58:06 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-7697996758 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 106s deployment-controller Scaled up replica set nginx-7697996758 to 3
22、进入pod
[root@master1 ~]# kubectl exec -it nginx-7697996758-2pvdr bash
root@nginx-7697996758-2pvdr:/#
更多推荐
已为社区贡献11条内容
所有评论(0)