通过deployment的方式部署
创建一个deployment文件
[root@k8s-master1 pods]# cat app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: dsfnamespace: applabels:app: dsf
spec:replicas: 1selector:matchLabels:app: dsftemplate:metadata:labels:app: dsfspec:imagePullSecrets:- name: registry-pull-secretcontainers:- name: nginximage: 192.168.21.121:5000/app/nginx@sha256:a42a428525996f3a84d466ee628a074cac568e0e8c99b5d6f7398be342337039imagePullPolicy: IfNotPresentports:- containerPort: 80resources:requests:cpu: "0.1"memory: "200Mi"limits:cpu: "0.1"memory: "200Mi"- 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"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-servicenamespace: app
spec:selector:app: dsfports:- name: nginx-service-portport: 80targetPort: 80- name: tomcat-service-portport: 8080targetPort: 8080type: ClusterIP