Kubernetes集群——(k8s)资源清单
一、资源清单简介二、自主式Pod资源清单删除之前pod[root@server2 manifest]# kubectl delete svc myapp[root@server2 manifest]# kubectl delete deployments.apps myapp[root@server2 manifest]# kubectl get allNAMETYPECLUSTER-IPEXTE
一、资源清单简介
二、自主式Pod资源清单
删除之前pod
[root@server2 manifest]# kubectl delete svc myapp
[root@server2 manifest]# kubectl delete deployments.apps myapp
[root@server2 manifest]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 45h
创建资源清单的参考文章
参考地址:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#deployment-v1-apps
kubectl explain pod //查询帮助文档
在server2主机上
mkdir manifest
cd manifest/
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# vim pod.yml
apiVersion: v1 指明api资源属于哪个群组和版本,同一个组可以有多个版本
kind: Pod 标记创建的资源类型
metadata: 元数据
name: myapp 对象名称自定义
spec: 定义目标资源的期望状态
containers: 容器
- name: myapp 名称自定义
image: myapp:v1 定义镜像
[root@server2 manifest]# kubectl create -f pod.yml 创建
pod/myapp created
[root@server2 manifest]# kubectl get pod 查看
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 26s
[root@server2 manifest]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 35s 10.244.2.9 server4 <none> <none>
[root@server2 manifest]# curl 10.244.2.9 访问
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
自主式资源清单更换镜像
[root@server2 manifest]# kubectl delete -f pod.yml 先删除之前创建的
pod "myapp" deleted
[root@server2 manifest]# vim pod.yml 更改镜像
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myapp
image: myapp:v2 改为v2
[root@server2 manifest]# kubectl create -f pod.yml 建立
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 4s 10.244.1.11 server3 <none> <none>
[root@server2 manifest]# curl 10.244.1.11 测试访问
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
不删除pod直接更换镜像
[root@server2 manifest]# \vi pod2.yml
[root@server2 manifest]# vim pod2.yml
[root@server2 manifest]# cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 3 副本三个
selector: 选择器
matchLabels:
app: nginx 标签
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: myapp:v1 指定镜像
更换镜像
[root@server2 manifest]# vim pod2.yml
[root@server2 manifest]# cat pod2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 5 副本改为五个
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: myapp:v2 镜像换v2
三、交互式进入pod内部
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
- name: demo
image: busyboxplus 运行后会有交互式shell
tty: true 打开一个终端
stdin: true
stdinOnce: true
[root@server2 manifest]# kubectl apply -f pod.yml 创建pod的两个容器
pod/myapp created
[root@server2 manifest]# kubectl get pod 查看两个都在运行
NAME READY STATUS RESTARTS AGE
myapp 2/2 Running 0 4s
访问含有交互式shell的容器
[root@server2 manifest]# kubectl attach myapp -c demo -it
这两个容器会共享网络;容器内部是可以互相访问的,进入busyboxplusd的交互式shell可以访问到myapp:v1的页面
访问没有交互式shell的容器
[root@server2 manifest]# kubectl exec -it myapp -c myappv1 -- sh
别名设定
/etc/nginx/conf.d # vi default.conf
/etc/nginx/conf.d #
四、pod镜像拉取策略和容器的端口映射
4.1默认拉取策略:
kubectl explain pod.spec.containers 查看拉取策略
总是拉取最新的镜像
自定义镜像拉取策略
Always:意思是每次都尝试重新拉取镜像
Nerver: 表示仅适用本地镜像源
ifNotPresent: 表示如果本地有就用本地的,没有就拉取在线镜像
以上三个都不定义:默认是第一个Always:每次都重新拉取镜像
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
- name: demo
image: busyboxplus
tty: true
stdin: true
stdinOnce: true
imagePullPolicy: ifNotPresent
4.2容器的端口映射
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
ports:
- name: http
containerPort: 80 80端口映射到80
hostPort: 80
4.3定义使用主机网络模式
容器使用主机网络模式就是在使用宿主机的网络IP,但是只能有一个不能有副本,因为主机上的80端口只有一个
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
hostNetwork: true 使用主机网络模式
[root@server2 manifest]# kubectl delete -f pod.yml
pod "myapp" deleted
[root@server2 manifest]#
[root@server2 manifest]# kubectl apply -f pod.yml
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide IP已经变为主机IP
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 9s 172.25.254.3 server3 <none> <none>
[root@server2 manifest]# curl 172.25.254.3 直接访问主机IP
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
五、定义pod的重启策略
三个参数:
Always:Pod一旦终止运行,则无论是容器如何终止的,kubelet服务都将重启它;
OnFailuer:只有pod以非零退出码终止时,kubelet才会重启它,如果正常结束(退出码为0),则kubelet将不会重启它;
Nerver:Pod终止后,kubelet将退出码告诉给master;不会重启pod。
kubectl run demo --image=busyboxplus -it --restart=Never 退出之后不重启服务
[root@server2 manifest]# kubectl delete pod demo
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
restartPolicy: Never 退出之后不重启,写在清单中所有服务都是
六、限制pod资源
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
resources: 指定资源限制和资源请求的值
requests: 指定容器启动和调度时的限制设置
memory: 100Mi 内存请求容器启动时初始化的可用数量
cpu: 0.1 CPU请求容器启动时初始化的可用数量
limits: 指定容器运行时资源的运行上限
memory: 200Mi 指定内存限制单位为MiB和GiB
cpu: 0.2 指定CPU限制
[root@server2 manifest]# kubectl apply -f pod.yml
pod/myapp created
[root@server2 manifest]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 5s
[root@server2 manifest]# kubectl describe pod myapp 查看详细信息
七、定义Node的label过滤标签,以key:value格式指定(节点选择器)
[root@server2 manifest]# vim pod.yml
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
spec:
containers:
- name: myappv1
image: myapp:v1
resources:
requests:
memory: 100Mi
cpu: 0.1
limits:
memory: 200Mi
cpu: 0.2
nodeSelector:
kubernetes.io/hostname: server3 自定义节点选择器,之前在server4
[root@server2 manifest]# kubectl apply -f pod.yml
pod/myapp created
[root@server2 manifest]# kubectl get pod -o wide 运行在server上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 9s 10.244.1.27 server3 <none> <none>
八、定义pod的标签
kubectl get pod --show-labels //查看标签
kubectl get pod -l app //过滤包含app的标签
kubectl label pod demo version=v1 //打标签
kubectl label pod demo app=nginx --overwrite //更改标签
[root@server2 manifest]# cat pod.yml
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels: 标签
app: myapp 定义标签自定义
spec:
containers:
- name: myappv1
image: myapp:v1
resources:
requests:
memory: 100Mi
cpu: 0.1
limits:
memory: 200Mi
cpu: 0.2
nodeSelector:
kubernetes.io/hostname: server3
更多推荐
所有评论(0)