序号 | 主机 | 主机名 | 角色 | 备注 |
1 | 192.168.3.114 | master | k8s-master | k8s 1.25.0基于containerd部署 |
2 | 192.168.3.115 | node1 | k8s-node | |
3 | 192.168.3.116 | node2 | k8s-node |
一、基本操作
1、基础命令
kubectl get nodes
kubectl get node -o wide
kubectl describe node
kubectl get node k8s-master -o yaml
kubectl explain Node.apiVersion
kubectl api-versions
2、创建namespace
kubectl get namespace
kubectl get ns
kubectl create namespace myns
kubectl delete namespace myns
3、Label
# 查看Label
kubectl get nodes --show-labels
# 增加Label
kubectl label nodes node01 node-role.kubernetes.io/work=node
# 删除Label
kubectl label nodes node01 node-role.kubernetes.io/work-
# 添加Annotation
kubectl annotate nodes node1 work=node
# 删除Annotation
kubectl annotate nodes node1 work-
# 查看
kubectl describe nodes node1
4、Pod
1) 命令创建pod
kubectl run --image=nginx mynginx
kubectl get pods
2) yaml创建pod
vim mybusybox.yaml
kind: Pod
apiVersion: v1
metadata:
name: mybusybox
labels:
app: busybox
spec:
containers:
- name: mybusybox
image: busybox
args:
- /bin/sh
- -c
- sleep 30000
kubectl apply -f mybusybox.yaml
kubectl get pods
kubectl get pods -owide
# 进入容器
kubectl exec -it mybusybox -- /bin/sh
kubectl exec -it mynginx /bin/bash
# 输出Pod信息
kubectl get pods mynginx -o yaml > mynginx.yaml
cat mynginx.yaml
# 修改pod
kubectl edit pod mybusybox
# yaml编写
kubectl explain pod
多容器Pod
vim mynginxtomcat.yaml
apiVersion: v1
kind: Pod
metadata:
name: mynginxtomcat
spec:
containers:
- name: pod-nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: pod-tomcat
image: tomcat
imagePullPolicy: IfNotPresent
kubectl apply -f mynginxtomcat.yaml
kubectl get pods
# 进入多容器Pod
kubectl exec -it -c pod-tomcat mynginxtomcat -- /bin/bash
kubectl exec -it -c pod-nginx mynginxtomcat -- /bin/bash
静态pod
Static Pod的存放路径默认为:/etc/kubernetes/manifests/
vim /var/lib/kubelet/config.yaml 修改目录
二、控制器
1、replicaset
ReplicaSet是kubernetes中的一种副本控制器,简称 rs ,主要作用是控制由其管理的 pod,使 pod pod副本的 数量始终维持在预设个。它主要作用就是保证一定Pod能够在集群中正常运行,它会持续监听这些 Pod的运行状态,在Pod发生故障时重启 pod ,pod数量减少时重新运行Pod副本。
vim myreplicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myreplicaset
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: pod-nginx
template:
metadata:
labels:
app: pod-nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
kubectl apply -f myreplicaset.yaml
kubectl get replicasets
或
kubectl get rs
kubectl get pods
kubectl get pods -o wide
删除rs
kubectl delete -f myreplicaset.yaml
删除pod 会重建
调整 replicas 数量
修改yaml文件 改replicas: 3 为replicas: 2
kubectl apply -f myreplicaset.yaml
或
kubectl scale replicaset --replicas 2 myreplicaset
2、Deployment
Deployment是 kubernetes中最常用的资源对象,通过定义一个 Deployment控制器会创建一个新的 ReplicaSet控制器,通过ReplicaSet创建pod,删除Deployment控制器,也会删除Deployment控制器下对应的ReplicaSet控制器和 pod pod资源
vim mydeploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydeployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
kubectl apply -f mydeploy.yaml
kubectl get deployments.apps
kubectl get pods
kubectl get pods -owide
调整副本数量
kubectl scale --replicas 2 deployment mydeployment
或
更改yaml文件
replicas 2
2) 升级nginx
查看版本:kubectl describe pod mydeployment-7fb96c846b-g4vjc
修改yaml文件 mydeploy.yaml
改image: nginx:1.14.2 为 image: nginx:latest
kubectl apply -f mydeploy.yaml
3) 回滚
# 查看历史记录
kubectl rollout history deployment mydeployment
# 查看某一个记录的详细信息
kubectl rollout history deployment mydeployment --revision=1
# 执行回滚操作
kubectl rollout undo deployment mydeployment --to-revision=1
3、DaemonSet
DaemonSet 控制器能够确保 k8s 集群所有的节点都运行一个相同的 pod 副本