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

通过deployment的方式部署

创建一个deployment文件

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

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值