K8s容器部署两个项目(主节点操作)
1、创建yaml文件
master节点上创建todo-list.yaml文件,两个jar包设置了两个副本,vue设置了一个副本,端口号固定配置。
当前文件目录为:/home/docker
在这里插入图片描述
创建yaml文件命令为:sudo vim todo-list.yaml

文件内容如下,保存文件方法同上:

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-todo
namespace: default
labels:
app: web-todo
spec:
replicas: 1
selector:
matchLabels:
app: web-todo
template:
metadata:
labels:
app: web-todo
spec:
imagePullSecrets:
- name: harbor-secret
containers:
- name: web-todo
image: 192.168.3.200/library/web-todo:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9000
name: web-todo
resources:
requests:
memory: 300Mi
cpu: 0.4
limits:
memory: 500Mi
cpu: 1

kind: Service
apiVersion: v1
metadata:
name: web-todo
namespace: default
labels:
app: web-todo
spec:
type: NodePort
ports:

  • port: 9000
    nodePort: 30260
    selector:
    app: web-todo

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: web-todo
spec:
rules:

  • host: ling.hapo.com
    http:
    paths:
    • path:
      backend:
      serviceName: web-todo
      servicePort: 9000

apiVersion: v1
kind: Service
metadata:
name: dsm-k8s
namespace: default
labels:
app: dsm-k8s
spec:
type: NodePort
ports:

  • port: 9093
    nodePort: 32351
    selector:
    app: dsm-k8s

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: dsm-k8s
name: dsm-k8s
spec:
replicas: 2
selector:
matchLabels:
app: dsm-k8s
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: dsm-k8s
spec:
containers:
- image: 192.168.3.200/library/dsm-k8s:v1
name: dsm-k8s
imagePullPolicy: Never
resources: {}
status: {}

apiVersion: v1
kind: Service
metadata:
name: note-k8s
namespace: default
labels:
app: note-k8s
spec:
type: NodePort
ports:

  • port: 8094
    nodePort: 32081
    selector:
    app: note-k8s

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: note-k8s
name: note-k8s
spec:
replicas: 2
selector:
matchLabels:
app: note-k8s
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: note-k8s
spec:
containers:
- image: 192.168.3.200/library/note-k8s:v1
name: note-k8s
imagePullPolicy: Never
resources: {}
status: {}

2、部署
部署命令同样要在todo-list.yaml文件同级目录下进行。

部署命令:

kubectl create -f todo-list.yaml

查看pod:

kubectl get pods -o wide
在这里插入图片描述
如果所有pod都在running状态则表示项目都起好了。

查看deployment和service:

kubectl get deployment

kubectl get svc
在这里插入图片描述
查看pod日志:

kubectl logs

删除pod,deployment,service

kubectl delete svc note-k8s

kubectl delete deployment note-k8s

kubectl delete pods

3、查看部署好的页面
浏览器访问:192.168.3.200:30260/DLHYXT/dsmMgr/login

在这里插入图片描述
至此,前后端均已部署完成。
常用命令
查看所有运行的进程

docker ps -a

停止进程
docker stop id

删除承载进程的容器

docker rm id

删除镜像

docker rmi <镜像ID>

我们生成的镜像一般都是打过标签的,会出现同一个镜像ID对应两个镜像的情况,这时候删除命令为:

docker rmi [REPOSITORY]:[TAG]

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐