Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
#帮助信息
[root@localhost ~]# 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).
#项目的生命周期:
创建→发布→更新→回滚→删除
#创建
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@localhost ~]# kubectl run nginx-deployment --image=nginx --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-deployment created
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-dbddb74b8-whwhl 1/1 Running 0 2d17h
nginx-deployment-5477945587-b8r6m 1/1 Running 0 70s
nginx-deployment-5477945587-dz8hb 1/1 Running 0 70s
nginx-deployment-5477945587-wd82l 1/1 Running 0 70s
[root@localhost ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-dbddb74b8-whwhl 1/1 Running 0 2d17h
pod/nginx-deployment-5477945587-b8r6m 1/1 Running 0 3m9s
pod/nginx-deployment-5477945587-dz8hb 1/1 Running 0 3m9s
pod/nginx-deployment-5477945587-wd82l 1/1 Running 0 3m9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1 1 1 1 4d23h
deployment.apps/nginx-deployment 3 3 3 3 3m9s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-dbddb74b8 1 1 1 4d23h
replicaset.apps/nginx-deployment-5477945587 3 3 3 3m9s
#删除
[root@localhost ~]# kubectl delete deploy/nginx
deployment.extensions "nginx" deleted
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5477945587-b8r6m 1/1 Running 0 12m
nginx-deployment-5477945587-dz8hb 1/1 Running 0 12m
nginx-deployment-5477945587-wd82l 1/1 Running 0 12m
[root@localhost bin]# kubectl delete deploy/nginx-deployment
[root@localhost ~]# kubectl get pods
No resources found.
#项目周期
1:创建nginx
[root@localhost bin]# 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@localhost ~]# kubectl get pods,deployment
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-jbln5 1/1 Running 0 47s
pod/nginx-7697996758-xgxzd 1/1 Running 0 47s
pod/nginx-7697996758-xjdlz 1/1 Running 0 47s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3 3 3 3 47s
[root@localhost ~]# kubectl get pods,deployment,replicaset
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-jbln5 1/1 Running 0 87s
pod/nginx-7697996758-xgxzd 1/1 Running 0 87s
pod/nginx-7697996758-xjdlz 1/1 Running 0 87s
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.extensions/nginx 3 3 3 3 87s
NAME DESIRED CURRENT READY AGE
replicaset.extensions/nginx-7697996758 3 3 3 87s
2:发布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@localhost ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@localhost ~]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-7697996758-jbln5 1/1 Running 0 10m
pod/nginx-7697996758-xgxzd 1/1 Running 0 10m
pod/nginx-7697996758-xjdlz 1/1 Running 0 10m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
service/nginx-service NodePort 10.0.0.247 <none> 80:38804/TCP 2m35s
#查看资源对象简写
[root@localhost ~]# kubectl api-resources
#查看关联后端的节点
[root@localhost ~]# kubectl get endpoints
#查看网络状态详细信息
[root@localhost ~]# kubectl get pods -o wide
#查看服务暴露的端口
[root@localhost ~]# kubectl get svc
//在node操作,查看负载均衡端口
[root@localhost ~]# yum install ipvsadm -y
[root@localhost ~]# ipvsadm -L -n
(kubernetes里kube-proxy支持三种模式,在v1.8之前我们使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式)
3.更新nginx 版本
#谷歌浏览器重新加载刷新页面查看nginx版本信息:
[root@localhost ~]# kubectl set --help
(省略部分)
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]
(省略部分)
#获取修改模板
[root@localhost ~]# kubectl set image --help
(省略部分)
Examples:
# Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox
container image to 'busybox'.
kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
(省略部分)
#更新nginx版本为1.17
[root@localhost ~]# kubectl set image deployment/nginx nginx=nginx:1.17
kubectl set image deployment/nginx nginx=nginx:1.17
#动态监听状态
[root@localhost ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-664bd6cf57-xq7b9 0/1 ContainerCreating 0 10s
nginx-6c94d899fd-rhp76 1/1 Running 0 102s
nginx-664bd6cf57-xq7b9 1/1 Running 0 26s
nginx-6c94d899fd-rhp76 1/1 Terminating 0 119s
nginx-6c94d899fd-rhp76 0/1 Terminating 0 119s
[root@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-664bd6cf57-xq7b9 1/1 Running 0 2m17s
(有时可能会因为版本问题出现更新失败的情况)
4.回滚nginx
#帮助信息
[root@localhost ~]# 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).
#查看历史版本
[root@localhost ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
7 <none>
10 <none>
11 <none>
#回滚
[root@localhost ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx
#检查回滚状态
[root@localhost ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
#查看deployment
[root@localhost ~]# kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 1 1 1 1 15d
#删除nginx
[root@localhost ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@localhost ~]# kubectl get deploy
No resources found.
[root@localhost ~]# kubectl get pods
No resources found.
#删除svc服务
[root@localhost ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 16d
nginx-service NodePort 10.0.0.5 <none> 80:31713/TCP 9d
[root@localhost ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@localhost ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 16d
#查看具体资源的详细信息
[root@localhost ~]# 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@localhost ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-k8mxb 1/1 Running 0 36s
nginx-7697996758-mdqlf 1/1 Running 0 36s
nginx-7697996758-z5l4j 1/1 Running 0 36s
[root@localhost ~]# kubectl describe pod nginx-7697996758-k8mxb
Name: nginx-7697996758-k8mxb
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: 20.0.0.33/20.0.0.33
Start Time: Thu, 10 Dec 2020 09:49:04 +0800
Labels: pod-template-hash=7697996758
run=nginx
Annotations: <none>
Status: Running
IP: 172.17.37.3
Controlled By: ReplicaSet/nginx-7697996758
Containers:
nginx:
Container ID: docker://fa229350c504c258f96481c68abf534ee5d6ec03291167c0d64efc2a9a539b8b
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 10 Dec 2020 09:49:15 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-qh76n (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-qh76n:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-qh76n
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 Scheduled 93s default-scheduler Successfully assigned default/nginx-7697996758-k8mxb to 20.0.0.33
Normal Pulling 90s kubelet, 20.0.0.33 pulling image "nginx:latest"
Normal Pulled 80s kubelet, 20.0.0.33 Successfully pulled image "nginx:latest"
Normal Created 80s kubelet, 20.0.0.33 Created container
Normal Started 80s kubelet, 20.0.0.33 Started container
#查看deployment资源
[root@localhost ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Thu, 10 Dec 2020 09:49:02 +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 2m30s deployment-controller Scaled up replica set nginx-7697996758 to 3
#进入pod
[root@localhost ~]# kubectl exec -it nginx-7697996758-k8mxb bash
root@nginx-7697996758-k8mxb:/# 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
#退出pod
root@nginx-7697996758-k8mxb:/# exit
exit