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

2023-12-18 05:42:25

通过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:
        - name: registry-pull-secret
      containers:
      - name: nginx
        image: 192.168.21.121:5000/app/nginx@sha256:a42a428525996f3a84d466ee628a074cac568e0e8c99b5d6f7398be342337039
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: "0.1"
            memory: "200Mi"
          limits:
            cpu: "0.1"
            memory: "200Mi"
      - 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"
      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

文章来源:https://blog.csdn.net/qq_40657367/article/details/135051820
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。