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