服务部署与迁移示例

2022-09-15 21:55:47 浏览数 (3)

一、服务部署与迁移示例

1、简单的部署 wordpress

通过 mysql 与 wordpress 镜像包部署持久化的 wordpress 博客网站

1.1、创建 PV
代码语言:javascript复制
cat lnmp-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv001
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-1
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv002
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-2
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv003
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-3
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv004
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-4
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv005
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-5
    server: 192.168.1.169
1.2、部署 mysql
代码语言:javascript复制
cat wp-mysql.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
spec:
  type: ClusterIP
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    app: wp-mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  labels:
    app: wp-mysql
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp-mysql
spec:
  selector:
    matchLabels:
      app: wp-mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: wp-mysql
    spec:
      containers:
      - name: wp-mysql
        image: docker.io/mysql:5.7
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        - name: MYSQL_DATABASE
          value: "wordpress"
        - name: MYSQL_USER
          value: "wordpress"
        - name: MYSQL_PASSWORD
          value: "wordpress"
        volumeMounts:
        - name: mysql
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql
        persistentVolumeClaim:
          claimName: mysql-pvc
1.3、部署 wordpress
代码语言:javascript复制
cat wp-wordpress

apiVersion: v1
kind: Service
metadata:
   name: wordpress
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30001
  selector:
     app: wordpress
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wp-wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
  replicas: 1
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wp-wordpress
        image: docker.io/wordpress
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: "mysql-svc"
        - name: WORDPRESS_DB_USER
          value: "wordpress"
        - name: WORDPRESS_DB_PASSWORD
          value: "wordpress"
        volumeMounts:
        - name: wordpress
          mountPath: /var/www/html
      volumes:
      - name: wordpress
        persistentVolumeClaim:
          claimName: wordpress-pvc

2、部署 LNMP 环境

通过部署 LNMP 环境 部署持久化的 wordpress

2.1、部署 PV
代码语言:javascript复制
cat lnmp-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv001
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-1
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv002
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-2
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv003
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-3
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv004
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-4
    server: 192.168.1.169
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv005
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /nfs-5
    server: 192.168.1.169
2.2、部署 nginx php
代码语言:javascript复制
cat lnmp-nginx-php.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-php-svc
  labels:
    app: lnmp-nginx-php
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30001
  selector:
    app: lnmp-nginx-php
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: lnmp-web-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lnmp-nginx-php
spec:
  selector:
    matchLabels:
      app: lnmp-nginx-php
  replicas: 1
  template:
    metadata:
      labels:
        app: lnmp-nginx-php
    spec:
      containers:
      - name: lnmp-nginx
        image: richarvey/nginx-php-fpm
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        - containerPort: 9000
        volumeMounts:
        - name: nginx-data
          mountPath: /var/www/html/
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: lnmp-web-pvc
2.3、部署 MYSQL
代码语言:javascript复制
cat lnmp-mysql.yaml

apiVersion: v1
kind: Service
metadata:
  name: lnmp-mysql-svc
spec:
  type: ClusterIP
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    app: lnmp-mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: lnmp-mysql-pvc
  labels:
    app: lnmp-mysql
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: lnmp-mysql
spec:
  selector:
    matchLabels:
      app: lnmp-mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: lnmp-mysql
    spec:
      containers:
      - name: lnmp-mysql
        image: docker.io/mysql:5.7
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        - name: MYSQL_DATABASE
          value: "wordpress"
        - name: MYSQL_USER
          value: "wordpress"
        - name: MYSQL_PASSWORD
          value: "wordpress"
        volumeMounts:
        - name: mysql
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql
        persistentVolumeClaim:
          claimName: lnmp-mysql-pvc
2.4、部署 wordpress
代码语言:javascript复制
下载 wordpress 的安装包

wordpress官网:https://cn.wordpress.org 

将解压后的 wordpress 目录内的所有的内容 上传/复制/移动 到 /nfs-x 目录内

打开浏览器,输入 IP 端口 回车后即可看到如下 wordpress 的安装界面

点击 “现在就开始”

输入我们在 lnmp-mysql.yaml 文件内定义的 用户名、密码、数据库名、以及我们的 mysql 的 svc 的名称

如果数据库信息填写正确,点击 “提交” 后可以看到如下提示,我们点击 “现在安装”

设置我们站点的相关信息

3、springboot-web

​ 当前,Spring毫无疑问已经成为java后台对象管理标准框架,除了通过IOC能够管理我们的自定义对象的生命周期之外还提供了众多功能繁复的可配置功能模块。但同时带来了复杂的配置项,这对初学者而言简直是一种灾难。于是SpringBoot应运而生,Springboot的出现大大简化了配置,主要表现在消除了web.xml和依赖注入配置的整合,处处遵循规约大于配置的思想,将初学者在繁杂的配置项中解放出来,专注于业务的实现,而不需要去关注太底层的实现。

代码语言:javascript复制
cat dockerfile 

FROM openjdk:8-jdk 

COPY target/springboot-web-demo-1.0-SNAPSHOT.jar /springboot-web.jar

ENTRYPOINT ["java", "-jar", "/springboot-web.jar"]

docker build -t spring-web:v1 .

docker tag  www.zutuanxue.com/library/spring-web:v1

docker tag spring-web:v1  www.zutuanxue.com/library/spring-web:v1

cat spring-web.yaml

apiVersion: v1
kind: Service
metadata:
  name: spring-web-svc
spec:
  type: NodePort
  ports:
    - port: 8181
      targetPort: 8080
      nodePort: 30600
  selector:
    app: spring-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-web
spec:
  selector:
    matchLabels:
      app: spring-web
  replicas: 3
  template:
    metadata:
      labels:
        app: spring-web
    spec:
      containers:
        - name: spring-web
          image: www.zutuanxue.com/library/spring-web:v1
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 8080
          
kubectl apply -f spring-web.yaml

kubectl get pod

kubectl get svc

打开浏览器输入 IP:30600/hello?name=zutuanxue

4、JAVA

代码语言:javascript复制
cat dockerfile

FROM tomcat

COPY jspdemo_war.war  /usr/local/tomcat/webapps/jspdemo_war.war

ENTRYPOINT ["catalina.sh","run"]

cat tomcat-java.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat-java-svc
spec:
  type: NodePort
  ports:
    - port: 8282
      targetPort: 8080
      nodePort: 30700
  selector:
    app: tomcat-java
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-java
spec:
  selector:
    matchLabels:
      app: tomcat-java
  replicas: 3
  template:
    metadata:
      labels:
        app: tomcat-java
    spec:
      containers:
        - name: tomcat-java
          image: www.zutuanxue.com/library/tomcat-java:v1
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 8080
          
打开浏览器进入链接 http://IP:30700/jspdemo_war (项目名)   用户名与密码为:lisi

0 人点赞