对于k8s-pod-01的学习
检查k8s集群是否正常
1. 从node节中检查
ps -ef | grep kubelet # 查看服务是否启动
2. 查看网络插件是否开启
[root@k8s-node-12 ~]# docker ps | grep flannel
020ba15a9d76 f3729ca97827 "/opt/bin/flanneld -…" 42 minutes ago Up 42 minutes k8s_kube-flannel_kube-flannel-ds-qwcxb_kube-flannel_222ea8e4-25a4-407d-9d0a-3188311fce84_1
f199ca98f29a registry.aliyuncs.com/google_containers/pause:3.2 "/pause" 42 minutes ago Up 42 minutes k8s_POD_kube-flannel-ds-qwcxb_kube-flannel_222ea8e4-25a4-407d-9d0a-3188311fce84_2
格式化命令格式
# -o wide 显示详细完整的信息
[root@k8s-10-master ~]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-10-master Ready master 18h v1.19.3 10.0.0.10 <none> CentOS Linux 7 (Core) 3.10.0-1160.71.1.el7.x86_64 docker://19.3.15
k8s-node-11 Ready <none> 18h v1.19.3 10.0.0.11 <none> CentOS Linux 7 (Core) 3.10.0-1160.71.1.el7.x86_64 docker://19.3.15
k8s-node-12 Ready <none> 18h v1.19.3 10.0.0.12 <none> CentOS Linux 7 (Core) 3.10.0-1160.71.1.el7.x86_64 docker://19.3.15
# 简洁的输出
[root@k8s-10-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-10-master Ready master 18h v1.19.3
k8s-node-11 Ready <none> 18h v1.19.3
k8s-node-12 Ready <none> 18h v1.19.3
- flannel 是一个网络插件,每一个node上都要运行,以容器的形式,以pod的方式运行
- pod
基于namespace 查询资源
基于声明yaml,创建pod资源
[root@k8s-master-10 ~]#cat linux0224-pod-2.yml
apiVersion: v1
kind: Pod
metadata:
name: linux0224-pod-2-nginx
namespace: linux0224
spec:
containers:
- image: nginx:latest
imagePullPolicy: IfNotPresent
name: test-nginx-2
- namespace这里的作用是资源组,对资源单独的创建一个环境去管理
- 默认有一个default的资源组,namespace,kubelet不指定默认就是
[root@k8s-10-master ~]# kubectl get namespaces
NAME STATUS AGE
calico-apiserver Active 17h
calico-system Active 17h
default Active 18h
kube-flannel Active 18h
kube-node-lease Active 18h
kube-public Active 18h
kube-system Active 18h
tigera-operator Active 17h
# 查看flannel插件
[root@k8s-10-master ~]# kubectl -n kube-flannel get po
NAME READY STATUS RESTARTS AGE
kube-flannel-ds-dljbj 1/1 Running 1 18h
kube-flannel-ds-nwqn4 1/1 Running 0 18h
kube-flannel-ds-qwcxb 1/1 Running 1 18h
编写yaml,声明式,获取,创建资源描述清单的流程
创建linux0224名称空间,去运行你的pod
[root@k8s-master-10 ~]#kubectl create namespace linux0224
namespace/linux0224 created
[root@k8s-master-10 ~]#
[root@k8s-master-10 ~]#
[root@k8s-master-10 ~]#kubectl get namespaces
NAME STATUS AGE
default Active 22h
kube-flannel Active 21h
kube-node-lease Active 22h
kube-public Active 22h
kube-system Active 22h
linux0224 Active 3s
[root@k8s-master-10 ~]#
[root@k8s-master-10 ~]#
[root@k8s-master-10 ~]#kubectl create -f linux0224-pod-2.yml
pod/linux0224-pod-2-nginx created
# 查看创建的pod
[root@k8s-10-master ~]# kubectl -n linux0224 get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
linux0224-pod-2-nginx 1/1 Running 0 49s 10.2.1.2 k8s-node-12 <none> <none>
创建一个nginx-pod 查看容器命名规则
[root@k8s-master-10 /all-k8s-yaml]#cat nginx-pod-3.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-3
namespace: linux0224
spec:
containers:
- image: nginx:1.21.1
name: t3-nginx
# 生成和查看
[root@k8s-10-master all-k8s-yaml]# kubectl create -f nginx-pod-3.yml
pod/nginx-3 created
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get pod
NAME READY STATUS RESTARTS AGE
linux0224-pod-2-nginx 1/1 Running 0 24m
nginx-3 1/1 Running 0 31s
# 记录后的更新状态
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get po -o wide -w
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m15s default-scheduler Successfully assigned linux0224/nginx-3 to k8s-node-11
Normal Pulling 4m15s kubelet Pulling image "nginx:1.21.1"
Normal Pulled 3m55s kubelet Successfully pulled image "nginx:1.21.1" in 19.234136512s
Normal Created 3m55s kubelet Created container t3-nginx
Normal Started 3m55s kubelet Started container t3-nginx
[root@k8s-master-10 /all-k8s-yaml]#
[root@k8s-master-10 /all-k8s-yaml]#
[root@k8s-master-10 /all-k8s-yaml]#kubectl -n linux0224 describe pod nginx-3
=今天开始=查看官网的yaml资料=====
这里的官网资料是华为云的,自己可以自己去找找看。。
今天任务简单点,学完昨日脑图知识点即可
主要学习目标
如何学习k8s最核心的对象(k8s集群内资源)
k8s的一些抽象的理念,pod,控制器,service,代码层面的逻辑概念。
集群外资源?node资源,k8s所处的机器,cpu,内存等资源
- pod如何部署pod控制器
- pod如何对外提供访问,学服务发现
- 学习pod数据的持久化
- 可有可无,优化pod配置文件管理,配置文件管理资源
Kuberneters对象的描述
kubernetes中资源可以使用yaml描述(如果您对于yaml格式不了解,可以参考yaml的语法,)以下的内容分为4个部分
- typeMeta : 对象类型元数据,声明对象后使用哪个API的版本,哪个类型的对象。
- objectMeta : 对象的元数据,包括对象的名称,使用的标签
- status : 对象的实际状态,只能在对象创建后看到,创建对象无需指定
最终的yaml
# 、给pod控制器,放入到具体ns下
[root@k8s-master-10 ~]#kubectl create namespace linux0224
namespace/linux0224 created
[root@k8s-10-master ~]# kubectl get namespace
NAME STATUS AGE
calico-apiserver Active 26h
calico-system Active 26h
default Active 27h
kube-flannel Active 27h
kube-node-lease Active 27h
kube-public Active 27h
kube-system Active 27h
linux0224 Active 8h
tigera-operator Active 26h
[root@k8s-10-master ~]#
# 修改yaml
apiVersion: apps/v1
kind: Deployment
# 给这个资源,创建到xx名称空间下 ,添加名称空间的字段是? 如何写?写到哪?
metadata:
name: nginx
labels:
app: nginx
namespace: linux0224
spec:
selector:
matchLabels:
app: nginx
replicas: 6
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-linux0224
image: nginx:1.15.1
# 当前这个yaml,没有描述,NodeSelector,节点选择器,以后说,自动分配到某个Node节点上的
创建声明式yml的k8s对象
[root@k8s-10-master all-k8s-yaml]# kubectl create -f t1-nginx-deployment.yml
deployment.apps/nginx created
# 检查一下
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 6/6 6 6 113s
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get pods
NAME READY STATUS RESTARTS AGE
linux0224-pod-2-nginx 1/1 Running 1 8h
nginx-3 1/1 Running 1 8h
nginx-84d9b94bd7-2tp6m 1/1 Running 0 3m56s
nginx-84d9b94bd7-9qkn5 1/1 Running 0 3m56s
nginx-84d9b94bd7-cfbhp 1/1 Running 0 3m56s
nginx-84d9b94bd7-kvc7f 1/1 Running 0 3m56s
nginx-84d9b94bd7-q9nq9 1/1 Running 0 3m56s
nginx-84d9b94bd7-qvqjg 1/1 Running 0 3m56s
查看所有的pod的信息
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get pods -l app=nginx -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-84d9b94bd7-2tp6m 1/1 Running 0 6m4s 10.2.1.6 k8s-node-12 <none> <none>
nginx-84d9b94bd7-9qkn5 1/1 Running 0 6m4s 10.2.1.8 k8s-node-12 <none> <none>
nginx-84d9b94bd7-cfbhp 1/1 Running 0 6m4s 10.2.2.11 k8s-node-11 <none> <none>
nginx-84d9b94bd7-kvc7f 1/1 Running 0 6m4s 10.2.2.13 k8s-node-11 <none> <none>
nginx-84d9b94bd7-q9nq9 1/1 Running 0 6m4s 10.2.2.12 k8s-node-11 <none> <none>
nginx-84d9b94bd7-qvqjg 1/1 Running 0 6m4s 10.2.1.7 k8s-node-12 <none> <none>
修改namespace下pod的信息
kubectl exec nginx-84d9b94bd7-qvqjg --bash -c 'echo "sleep my yzk" ' > /usr/share/nginx/html/index.html
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 exec nginx-84d9b94bd7-qvqjg -- bash -c 'echo "sleep yi hui ">/usr/share/nginx/html/index.html'
[root@k8s-10-master all-k8s-yaml]# curl 10.2.1.7
sleep yi hui
静态POD玩法流程
docker 调度的是容器,在k8s集群中,最小的调度单元pod(豆荚)
为什么引入Pod
- 容器引擎解耦
- 多容器共享网络的存储功能,进程,空间,支持的业务也更加的灵活
创建pod
所有都加上namespace来理解
先查看当前机器上的所有的pod,详细信息
创建 nginx pod
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 run --image=nginx:1.21.1 my-pod-1-nginx
pod/my-pod-1-nginx created
创建一个 mysql pod
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 run --image=mysql:5.7 my-pod-2-mysql
pod/my-pod-2-mysql created
# 检查
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
linux0224-pod-2-nginx 1/1 Running 1 9h 10.2.1.5 k8s-node-12 <none> <none>
my-pod-1-nginx 0/1 ContainerCreating 0 22s <none> k8s-node-11 <none> <none>
my-pod-2-mysql 0/1 ContainerCreating 0 14s <none> k8s-node-12 <none> <none>
nginx-3 1/1 Running 1 8h 10.2.1.4 k8s-node-12 <none> <none>
我们发现mysql-pod还在chuagn
查看pod日志,找出故障原因
# 检查my-mysql01 pod这个的日志信息
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 logs my-mysql01
2025-06-15 13:10:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.44-1.el7 started.
2025-06-15 13:10:02+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2025-06-15 13:10:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.44-1.el7 started.
2025-06-15 13:10:02+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following as an environment variable:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
pod创建成共,容器运行参数有问题,导致挂了
kubectl run 弊端
得编辑修改pod信息即可
运行一个可访问的mysql5.7 声明yaml去运行
# yaml怎么写?
# 基于现有资源,修改即可
[root@k8s-master-10 ~]#kubectl -n linux0224 get po my-pod-2-mysql -oyaml > /all-k8s-yml/my-pod-2-mysql.yml
# 修改如下
[root@k8s-master-10 ~]#cat /all-k8s-yml/my-pod-2-mysql.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: my-pod-2-mysql
name: my-pod-2-mysql
namespace: linux0224
spec:
containers:
- image: mysql:5.7
imagePullPolicy: IfNotPresent
name: my-pod-2-mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: 'linux0224'
# 查看pod, 以及显示pod的标签信息
# 删除旧的pod
kubectl -n linux0224 delete pod my-pod-2-mysql
#再新建新的pod
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 get pods -l run=my-pod-2-mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-pod-2-mysql 1/1 Running 0 39s 10.2.1.60 k8s-node-11 <none> <none>
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]## 到这都看懂111
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]## 用一个临时pod,访问这个mysql-pod,然后退出自动删除自己
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#
# 可以临时开启一个pod,去链接mysql-pod服务端
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 run test-mysql --rm -it --image=mysql:5.7 -- bash
# 查看mysql-pod本身的输数据信息
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 exec my-pod-2-mysql -- bash -c 'mysql -uroot -plinux0224 -e "show databases;"'
mysql: [Warning] Using a password on the command line interface can be insecure.
Database
information_schema
linux0224666
mysql
performance_schema
sys
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]## kandong 1111111
导出pod配置为yaml清单
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 get pods my-pod-2-mysql -o yaml > /tmp/latest_mysql57.yml
删除创建的pod
静态pod
控制器下的pod区别
如kubectl run创建的
如 yaml中创建的是 KIND: pod类型
删除了就没了,不会自建
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 delete pod my-pod-1-nginx
pod "my-pod-1-nginx" deleted
[root@k8s-master-10 /all-k8s-yml]#
# 查看pod资源的标签
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 get pods -owide --show-labels
# 基于标签删除pod
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 delete pods -l run=my-pod-2-mysql
pod "my-pod-2-mysql" deleted
# 删除deployment控制器下的nginx
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 describe pods nginx-84d9b94bd7-5pdqx
# 删不掉,副本保障,重建6个nginx-pod
查看deployment和pod 两个资源
deployment 就是部署
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get pods
NAME READY STATUS RESTARTS AGE
linux0224-pod-2-nginx 1/1 Running 1 10h
my-pod-1-nginx 1/1 Running 0 51m
my-pod-2-mysql 1/1 Running 0 13m
nginx-3 1/1 Running 1 9h
nginx-84d9b94bd7-2tp6m 1/1 Running 0 94m
nginx-84d9b94bd7-9qkn5 1/1 Running 0 94m
nginx-84d9b94bd7-cfbhp 1/1 Running 0 94m
nginx-84d9b94bd7-kvc7f 1/1 Running 0 94m
nginx-84d9b94bd7-q9nq9 1/1 Running 0 94m
nginx-84d9b94bd7-qvqjg 1/1 Running 0 94m
[root@k8s-10-master all-k8s-yaml]# kubectl -n linux0224 get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 6/6 6 6 95m
label玩法—学习kubectl命令
- node的查看,node标签管理,,给机器加上一个标签 k-v
[root@k8s-master-10 /all-k8s-yml]#kubectl get nodes -owide --show-labels
# 修改node节点的 label信息
[root@k8s-master-10 /all-k8s-yml]#kubectl label nodes k8s-node-12 diskType=sansumssd --overwrite
node/k8s-node-12 labeled
# 查看标签
[root@k8s-master-10 /all-k8s-yml]#kubectl get nodes -owide --show-labels -l diskType=sansumssd
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME LABELS
k8s-node-12 Ready <none> 10d v1.19.3 10.0.0.12 <none> CentOS Linux 7 (Core) 3.10.0-862.el7.x86_64 docker://19.3.15 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,diskType=sansumssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-12,kubernetes.io/os=linux
# 给11机器加一个标签
[root@k8s-master-10 /all-k8s-yml]#kubectl label nodes k8s-node-11 cpuType=interl
node/k8s-node-11 labeled
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#kubectl label nodes k8s-node-11 cpuType=intel --overwrite
node/k8s-node-11 labeled
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#
[root@k8s-master-10 /all-k8s-yml]#kubectl get nodes -owide --show-labels -l cpuType=intel
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME LABELS
k8s-node-11 Ready <none> 10d v1.19.3 10.0.0.11 <none> CentOS Linux 7 (Core) 3.10.0-862.el7.x86_64 docker://19.3.15 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,cpuType=intel,daemon=need,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node-11,kubernetes.io/os=linux
[root@k8s-master-10 /all-k8s-yml]#
- 静态pod创建,命令模式,查看,编辑,描述,删除
没有yaml的形式哦,不推荐使用
kubectl run xxxx
kubectl get pods xxxx
kubectl edit pods xxxx
kubectl describe pods xxxx
kubectl delete pods xxxx
- 静态pod的 yaml模式,声明式定义,增删改查
yaml语法,删除资源
kubectl explain 资源.字段.字段.xxxxxx
kubectl create -f xx.yml
kubectl delete -f xx.yml
- pod打标签,增删改查
[root@k8s-master-10 /all-k8s-yml]#kubectl -n linux0224 get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
my-pod-2-mysql 1/1 Running 0 16s run=my-pod-2-mysql
增
删
改
查
- 初体验控制器deployment玩法
、