本文主要内容:
1.安装k3s
2.在idea中,编辑yml文件创建pod
3.在k3d中pod基本操作
4.在k3d中Labeles标签基本操作
5.在k3d中容器的基本操作(k3s的操作与k8s操作不同处在于,k3s每次执行命令,前面需要加上k3s)
文章目录
- K3S安装
- idea编辑ymal
- pod基本操作
- 查看默认命名空间的 pod,可以通过k3s kubectl get pods
- 创建pod
- 删除pod
- 查看pod日志
- 查看pod描述信息
- 创建pod.yml,运行多个容器
- 进入pod中的容器
- 标签(Labeles)基本操作
- 查看Labeles标签
- 添加labeles标签
- 修改labeles标签
- #删除labels标签
- 筛选labels标签
- 容器基本操作
- 查看容器生命周期及阶段
- 容器生命周期回调
K3S简介 与 K3S官方文档链接
K3s是一个轻量级的、专为容器化应用和Kubernetes集群设计的开源Kubernetes发行版。K3s的目标是提供一个更小、更简单、更易于部署和维护的Kubernetes集群。
K3S安装
K3S安装脚本获取,脚本地址:https://get.k3s.io/
curl -sfL https://get.k3s.io | sh -
如果觉得官方源太慢,可以指定国内源进行安装:
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
修改配置
装好之后根据需要,可以调整下 K3s 的服务配置文件,以将 K3s 的默认容器引擎从 Containerd 切换到 Docker。
修改 K3S 服务的配置文件:
vi /etc/systemd/system/multi-user.target.wants/k3s.service
需要修改 ExecStart 的值:
# 原值
# ExecStart=/usr/local/bin/k3s server
# 新值
ExecStart=/usr/local/bin/k3s server --docker --no-deploy traefik
之后保存退出,执行命令重新加载新的服务配置文件:
systemctl daemon-reload
service k3s restart
最后,可以通过命令确认 K3s 服务是否就绪:
k3s kubectl get node
如果你看到类似下面的输出,表示 K3s 已经安装完成并且正常运行了:
idea编辑ymal
在idea中编写yaml并发布到kubernetes
idea安装kubernetes插件,首先进入idea->File->Settings->Plugins->搜索kubernetes->安装->重启,
在项目src下创建一个文件夹命名为k8s,之后创建文件name.yaml,进入输入界面:
pod文件自带配置
pod基本操作
查看默认命名空间的 pod,可以通过k3s kubectl get pods
k3s kubectl get pods
创建pod
#创建pod
[root@localhost tmp]# k3s kubectl run nginx --image=nginx:1.19
pod/nginx created
#查看pod
[root@localhost tmp]# k3s kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 16s
通过创建nginx-pod.yml,创建容器
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
将内容复制粘贴到k3s服务器上即可
touch nginx-pod.yml #创建文件
```bash
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
通过apply创建pod
k3s kubectl apply -f nginx-pod.yml
删除pod
方式一、k3s kubectl delete pod pod名称
[root@localhost tmp]# k3s kubectl delete pod nginx
pod "nginx" deleted
方式二、k3s kubectl delete -f pod.yml
[root@localhost tmp]# k3s kubectl delete -f mypod.yml
pod "mypod" deleted
查看pod日志
# 注意: 查看 pod 中第一个容器日志
kubectl logs -f(可选,实时) nginx(pod 名称)
# 注意: 查看 pod 中指定容器的日志
kubect logs -f pod名称 -c 容器名称
查看pod描述信息
kubectl describe pod nginx(pod名称)
创建pod.yml,运行多个容器
vi mypod.yaml
#创建多个pod的yml文件,包含nginx和reids
apiVersion: v1
kind: Pod
metadata:
name: mypod
labels:
app: mypod
spec:
containers:
- name: nginx
image: nginx:1.19
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
- name: redis
image: redis:5.0.10
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
restartPolicy: Always
执行命令k3s kubectl apply -f mypod.yml
创建pod
[root@localhost tmp]# k3s kubectl apply -f mypod.yml
pod/mypod created
进入pod中的容器
#注意: 这种方式进入容器默认只会进入 pod 中第一个容器
k3s kubectl exec -it pod名称 --(固定写死) bash(执行命令)
# 注意: 进入指定 pod 中指定容器
kubectl exec -it pod名称 -c 容器名称 --(固定写死) bash(执行命令)
例如:
k3s kubectl exec -it mypod -c nginx -- sh
标签(Labeles)基本操作
# 查看标签
kubectl get pods --show-labels
# kubectl label pod pod名称 标签键值对
kubectl label pod myapp env=prod
# 覆盖标签 --overwrite
kubectl label --overwrite pod myapp env=test
# 删除标签 -号代表删除标签
kubectl label pod myapp env-
# 根据标签筛选 env=test/env > = <
kubectl get po -l env=test
kubectl get po -l env
kubectl get po -l '!env' # 不包含的 pod
查看Labeles标签
查看labeles标签:k3s kubectl get pods --show-labels
[root@localhost tmp]# k3s kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 26m run=nginx
mypod 2/2 Running 0 8m40s app=mypod
添加labeles标签
添加标签:给mypod 添加 env=dev的标签
#添加标签
[root@localhost tmp]# k3s kubectl label pod mypod env=dev
pod/mypod labeled
[root@localhost tmp]# k3s kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 33m run=nginx
mypod 2/2 Running 0 15m app=mypod,env=dev
修改labeles标签
#添加错了,如何修改标签,将dev修改成test
[root@localhost tmp]# k3s kubectl label --overwrite pod mypod env=test
pod/mypod labeled
[root@localhost tmp]# k3s kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 36m run=nginx
mypod 2/2 Running 0 19m app=mypod,env=test
#删除labels标签
#删除labels,如env-即可删除
[root@localhost tmp]# k3s kubectl label pod mypod env-
pod/mypod unlabeled
[root@localhost tmp]# k3s kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 38m run=nginx
mypod 2/2 Running 0 21m app=mypod
筛选labels标签
[root@localhost tmp]# k3s kubectl get pod -l env=dev
NAME READY STATUS RESTARTS AGE
mypod 2/2 Running 0 25m
容器基本操作
查看容器生命周期及阶段
在执行k3s kubectl apply -f mypod.yml命令生成容器后,即可命令k3s kubectl describe pod mypod监控该容器生命周期状态
[root@localhost ~]# k3s kubectl describe pod mypod
Name: mypod
Namespace: default
Priority: 0
Service Account: default
Node: localhost.localdomain/192.168.31.222
Start Time: Wed, 08 Nov 2023 11:22:59 +0800
Labels: app=mypod
env=dev
Annotations: <none>
Status: Running
IP: 10.42.0.11
IPs:
IP: 10.42.0.11
Containers:
nginx:
Container ID: containerd://9fb499466b68ed9f9d7892b3f073de6b74d9b0d1f3e2ff26b40e69db02d19040
Image: nginx:1.19
Image ID: docker.io/library/nginx@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 08 Nov 2023 11:23:11 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qg4m9 (ro)
redis:
Container ID: containerd://50f827458fc848859715c85962814ef6f52b841d20e3cad4b46c7d6abba6db2f
Image: redis:5.0.10
Image ID: docker.io/library/redis@sha256:2236b827820ab65836a7aa5b494e03c3ede6e8801a853a0e24c9d6afbcfe0461
Port: 6379/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 08 Nov 2023 11:24:50 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-qg4m9 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-qg4m9:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none> #生命周期未显示出来
容器生命周期回调
执行命令 vi lifecycle.yml
创建yml文件,复制以下内容(注意点: ”command:“后面一定要加空格不然会报错):
apiVersion: v1
kind: Pod
metadata:
name: lifecycle
labels:
app: lifecycle
spec:
containers:
- name: nginx
image: nginx:1.19
lifecycle:
postStart:
exec:
command: ["/bin/bash","-c","echo postStart >> /start.txt"]
preStop:
exec:
command: ["/bin/bash","-c","echo postStart >> /stop.txt && sleep 30"]
imagePullPolicy: IfNotPresent
restartPolicy: Always
执行命令 k3s kubectl apply -f lifecycle.yml
创建容器
[root@localhost tmp]# k3s kubectl apply -f lifecycle.yml
pod/lifecycle created
执行以下命令,验证PostStart回调在容器创建后,是否被立即执行
[root@localhost tmp]# k3s kubectl get pods
NAME READY STATUS RESTARTS AGE
lifecycle 1/1 Running 0 78s
[root@localhost tmp]# k3s kubectl exec -it lifecycle -c nginx -- bash
root@lifecycle:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv sys usr
boot docker-entrypoint.d etc lib media opt root sbin start.txt tmp var
执行以下命令查看PreStop 回调被执行之前即开始计数, 所以无论回调函数的执行结果如何,容器最终都会在 Pod 的终止宽限期内被终止
[root@localhost tmp]# k3s kubectl apply -f lifecycle.yml
pod/lifecycle created
[root@localhost tmp]# k3s kubectl get pods
NAME READY STATUS RESTARTS AGE
lifecycle 1/1 Running 0 3m8s
#再开一个小窗口,查看删除容器后,是否生成stop文件
[root@localhost tmp]# k3s kubectl delete -f lifecycle.yml
pod "lifecycle" deleted
[root@localhost ~]# k3s kubectl exec -it lifecycle -c nginx -- bash
root@lifecycle:/# ls
bin dev docker-entrypoint.sh home lib64
mnt proc run srv stop.txt tmp var #已生成