Kubernetes集群之熟悉kubectl基础命令

管理K8S资源的三种基本方法

  • 陈述式管理方法——命令行CLI工具
  • 声明式管理方法——统一资源配置清单(mainfest)
  • GUI式管理方法——web页面(dashboard)

kubectl命令行

  • kubernetes集群管理集群资源的唯一入口式通过相应的方法调用apiserver的接口
  • kubectl是官方的CLI命令行工具,用于与apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理K8S各种资源的一种有效途径

陈述式资源管理方法

基础管理

  • 查看名称空间
1
2
kubectl get namespaces
kubectl get ns
  • 查看名称空间内的资源
1
kubectl get all [-n default]
  • 创建名称空间
1
2
kubectl create namespaces app_name
kubectl create ns app_name
  • 删除名称空间
1
2
kubectl delete namespaces app_name
kubectl delete ns app_name
  • 创建deployment
1
kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.15.2 -n kube-public
  • 查看deployment
1
2
kubectl get deployments -n kube-public -o wide
kubectl get deploy -n kube-public -o wide
  • 详细查看
1
kubectl describe deployment nginx-dp -n kube-public
  • 查看pod资源
1
2
3
kubectl get pods -A   # 查看所有pod资源
kubectl get pods -o wide # 查看pod详细信息
kubectl get pods --show-labels # 查看pod标签
  • 进入pod资源
1
kubectl exec  -it -n kube-public nginx-dp-5dfc645654-3jsg5 -- /bin/bash
  • 缩放副本数
1
2
kubectl scale deploy nginx-dp --replicas=2 -n kube-public
kubecrl edit depoly nginx-dp
  • 删除pod资源(重启)
1
kubectl delete pod -n kube-public nginx-dp-5dfc689474-4vgz9 [--force --grace-period=0]
  • 删除deployment
1
kubectl delete deployment -n kube-public nginx-dp
  • node打标签
1
2
3
kubectl label node k8s-master1.boysec.cn node-role.kubernetes.io/slave=
# 删除
kubectl label node k8s-master1.boysec.cn node-role.kubernetes.io/slave-
  • pod打标签
1
2
3
kubectl label pod nginx-55d498bc69-6kwg4 hello=666
# 删除
kubectl label pod nginx-55d498bc69-6kwg4 hello-

Service资源管理

ClusterIP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 创建容器
kubectl create deploy nginx --image=harbor.od.com/public/nginx:v1
# 增加
kubectl scale deploy nginx --replicas=3

# 查看标签
[root@k8s-master1 ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-55d498bc69-6kwg4 1/1 Running 0 2m3s app=nginx,pod-template-hash=55d498bc69
nginx-55d498bc69-mdm88 1/1 Running 0 4m16s app=nginx,pod-template-hash=55d498bc69
nginx-55d498bc69-skrm7 1/1 Running 0 2m3s app=nginx,pod-template-hash=55d498bc69

# 暴露nginx服务
[root@k8s-master1 ~]# kubectl expose deploy nginx --port=8888 --target-port=80
service/nginx exposed

# 查看svc
[root@k8s-master1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP 192.168.1.211 <none> 8888/TCP 16
[root@k8s-master1 ~]# curl 192.168.1.211:8888

NodePort

1
2
3
4
[root@k8s-master1 ~]# kubectl expose deploy nginx --port=8888 --target-port=80 --type=NodePort
[root@k8s-master1 ~]# kubectl get svc nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 192.168.139.106 <none> 8888:16269/TCP 27s

注意:集群外通过外部IP+16269端口访问,集群内通过8888端口访问

滚动升级

1
2
3
4
5
# 应用升级:nginx:v1.12 \nginx:v1.15
# kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

kubectl set image deploy/nginx nginx=harbor.od.com/public/nginx:v2
# 可以携带--record参数记录变更。

回滚记录

1
2
3
4
5
6
[root@k8s-master1 ~]# kubectl rollout history deploy/nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 kubectl set image deploy/nginx nginx=harbor.od.com/public/nginx:v3 --record=true

回滚到指定版本

1
2
kubectl rollout undo deploy/nginx --to-revision=1 
kubectl rollout undo deploy nginx --to-revision=1