当前位置: 首页 > news >正文

K8S的一个pod中运行多个容器

通过deployment的方式部署

创建一个deployment文件

[root@k8s-master1 pods]# cat app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: dsfnamespace: applabels:app: dsf
spec:replicas: 1                                            #实例的个数selector:matchLabels:app: dsftemplate:metadata:labels:app: dsfspec:imagePullSecrets:                                  #k8s中的imagepullsecrets是用于在拉取容器镜像时提供认证凭据的功能。它允许我们在部署应用程序时,将私有镜像仓库的认证信息配置到Kubernetes集群中- name: registry-pull-secretcontainers:                                        #容器的相关配置- name: nginx           image: 192.168.21.121:5000/app/nginx@sha256:a42a428525996f3a84d466ee628a074cac568e0e8c99b5d6f7398be342337039imagePullPolicy: IfNotPresent                    #Pod 镜像拉取策略(Always总是拉取镜像,ifNotPresent 本地有则使用本地镜像,不拉取。Never 只使用本地镜像,从不拉取,即使本地没有)ports:                                           # 容器端口- containerPort: 80resources:                                       #定义容器的CPU和内存requests:cpu: "0.1"memory: "200Mi"limits:cpu: "0.1"memory: "200Mi"volumeMounts:                                    #数据卷的配置            - name: nginx-volumesmountPath: "/usr/share/nginx/html/"            #会删除文件夹下的其他文件readOnly: true- name: hostpath-filemountPath: "/data/k8s"livenessProbe:                                   #存活探针tcpSocket:port: 80initialDelaySeconds: 15periodSeconds: 20readinessProbe:                                  #就绪探针tcpSocket:port: 80initialDelaySeconds: 15periodSeconds: 10- name: tomcatimage: 192.168.21.121:5000/app/tomcat@sha256:6a1163fd0c216d0baf5020fb63198d8fddfd466c8449f6a9bcc2aa7ab387a9e9imagePullPolicy: IfNotPresentports:- containerPort: 8080resources:requests:cpu: "0.1"memory: "200Mi"limits:cpu: "0.1"memory: "200Mi"volumeMounts:- name: tomcat-volumesmountPath: "/usr/local/tomcat/conf/context.xml"subPath: "usr/local/tomcat/conf/context.xml"              #subPath方式挂载文件不会删除文件夹下的其他文件- name: hostpath-filemountPath: "/data/k8s"livenessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 20readinessProbe:tcpSocket:port: 8080initialDelaySeconds: 15periodSeconds: 10volumes:                                                           #数据卷- name: nginx-volumesconfigMap:                                                       #基于configmap配置数据卷name: nginx-confitems:- key: "index.html"path: "index.html"- name: tomcat-volumesconfigMap:name: tomcat-confitems:- key: "context.xml"path: "usr/local/tomcat/conf/context.xml"- name: hostpath-filehostPath:                                                          #通过hostPath的方式配置数据卷path: /data/k8stype: DirectoryOrCreate                                          #目录存在就使用,不存在就先创建后使用,Directory目录必须存在,FileOrCreate文件存在就使用, 不存在就先创建后使用,File文件必须存在affinity:                                                            #亲和性配置podAffinity:                                                       #容器的亲和性requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: appoperator: Invalues:- dsftopologyKey: apppodAntiAffinity:                                                    #容器的反亲和配置preferredDuringSchedulingIgnoredDuringExecution:- weight: 100podAffinityTerm:labelSelector:matchExpressions:- key: appoperator: Invalues:- tomcattopologyKey: apprestartPolicy: Always

创建服务

[root@k8s-master1 pods]# kubectl apply -f app.yaml
deployment.apps/dsf created

检查服务启动的情况

[root@k8s-master1 pods]# kubectl get pods -n app -o wide
NAME                                        READY   STATUS    RESTARTS        AGE    IP               NODE          NOMINATED NODE   READINESS GATES
dsf-95fb75697-7tn59                         2/2     Running   0               9s     10.10.135.243    k8s-master3   <none>           <none>

验证服务是否正常

[root@k8s-master1 pods]# kubectl get pods -n app -o wide
NAME                                        READY   STATUS    RESTARTS        AGE    IP               NODE          NOMINATED NODE   READINESS GATES
dsf-95fb75697-7tn59                         2/2     Running   0               9s     10.10.135.243    k8s-master3   <none>           <none>
#验证一下服务是否可用
[root@k8s-master3 ~]# curl http://10.10.135.212:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@k8s-master3 ~]# curl http://10.10.135.212:8080<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Apache Tomcat/8.5.34</title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head>

ngixn和tomcat都能正常访问

Service的配置

[root@k8s-master1 pods]# cat app-service.yaml
apiVersion: v1
kind: Service
metadata:name: dsf-servicenamespace: app
spec:selector:app: dsfports:- name: nginx-service-portport: 80targetPort: 80- name: tomcat-service-portport: 8080targetPort: 8080type: ClusterIP
http://www.lryc.cn/news/263403.html

相关文章:

  • 《每天一分钟学习C语言·一》
  • zookeeper:启动后占用8080端口问题解决
  • 深度学习中的高斯分布
  • 【已解决】Atlas 导入 Hive 元数据,执行 import-hive.sh 报错
  • 在 Windows PC 上轻松下载并安装 FFmpeg
  • 21.Servlet 技术
  • 【Hive】——DDL(PARTITION)
  • SpringBoot 源码解析4:事件监听器
  • 使用 FastAPI 和 Vue.js 实现前后端分离
  • 算法基础之SPFA判断负环
  • 一些常用的Linux命令及其简要说明(持续更新)
  • 开发企业展示小程序的关键步骤和技巧
  • Python-Selenium-使用 pywinauto 实现 Input 上传文件
  • Go语言运行时与自家平台对比后认识
  • leetcode 450. 删除二叉搜索树中的节点
  • 小红书可观测 Metrics 架构演进,如何实现数十倍性能提升?
  • selenium学习
  • 前端开发新趋势:Web3、区块链和虚拟现实
  • 如何安装运行Wagtail并结合cpolar内网穿透实现公网访问网站界面
  • 【>D:\10\Debug\RCa00828(34): fatal error RC1022: expected ‘#endif‘】
  • 使用vite搭建项目时,在启动vite后,浏览器显示页面:找不到localhost的网页
  • libp2p 快速开始
  • 【数据结构】——排序算法简答题模板
  • vue3.0基础
  • Kafka本地安装⭐️(Windows)并测试生产消息以及消费消息的可用性
  • 生产环境_Spark解析JSON字符串并插入到MySQL数据库
  • WEB渗透—PHP反序列化(四)
  • LVS-DR模式部署
  • Oracle的学习心得和知识总结(三十)| OLTP 应用程序的合成工作负载生成器Lauca论文翻译及学习
  • HarmonyOS4.0从零开始的开发教程18后台代理提醒