1. 编写配置文件yaml
编写configmap配置文件
shell[root@k8s-master java-demo]# vim configmap.yaml [root@k8s-master java-demo]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: java-demo-config # 应用卷的名字 data: application.yml: | server: port: 8080 spring: datasource: url: jdbc:mysql://121.4.77.138:3306/java_demo?characterEncoding=utf-8 username: java_demo password: Aa@9953013 driver-class-name: com.mysql.jdbc.Driver freemarker: allow-request-override: false cache: true check-template-location: true charset: UTF-8 content-type: text/html; charset=utf-8 expose-request-attributes: false expose-session-attributes: false expose-spring-macro-helpers: false suffix: .ftl template-loader-path: - classpath:/templates/
[root@k8s-master java-demo]# vim configmap.yaml [root@k8s-master java-demo]# cat configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: java-demo-config # 应用卷的名字 data: application.yml: | server: port: 8080 spring: datasource: url: jdbc:mysql://121.4.77.138:3306/java_demo?characterEncoding=utf-8 username: java_demo password: Aa@9953013 driver-class-name: com.mysql.jdbc.Driver freemarker: allow-request-override: false cache: true check-template-location: true charset: UTF-8 content-type: text/html; charset=utf-8 expose-request-attributes: false expose-session-attributes: false expose-spring-macro-helpers: false suffix: .ftl template-loader-path: - classpath:/templates/
应用配置文件
yaml[root@k8s-master java-demo]# kubectl apply -f configmap.yaml configmap/java-demo-config created
[root@k8s-master java-demo]# kubectl apply -f configmap.yaml configmap/java-demo-config created
2. 创建configmap
编写应用配置文件yaml
shell[root@k8s-master java-demo]# vim java_demo.yaml [root@k8s-master java-demo]# cat java_demo.yaml
[root@k8s-master java-demo]# vim java_demo.yaml [root@k8s-master java-demo]# cat java_demo.yaml
yamlapiVersion: apps/v1 kind: Deployment metadata: name: java-demo namespace: default labels: app: java-demo annotations: # 记录回滚参数 kubernetes.io/change-cause: "java-demo:v1" #记录到revision中的内容,记录版本号 spec: replicas: 3 # Pod副本预期数量 revisionHistoryLimit: 10 # RS历史版本保存数量 selector: matchLabels: app: java-demo strategy: rollingUpdate: maxSurge: 25% # 滚动更新过程最大pod副本数 maxUnavailable: 25% # 滚动更新过程中最大不可用pod副本数, type: RollingUpdate template: metadata: labels: app: java-demo # Pod副本的标签 spec: imagePullSecrets: - name: harbor-auth containers: - name: java-demo image: harbor.ikubernetes.net/hsuing/java:v1 # 获取容器 ports: - containerPort: 8080 resources: requests: # 启动容器最小资源 memory: "256Mi" cpu: "500m" limits: # 容器的最大资源限制 memory: "512Mi" # 容器的最大资源内存限制128M cpu: "1000m" # 容器的最大资源cpu的核数限制,500m=0.5核数 readinessProbe: # 就绪检查,失败就会剔除 service httpGet: port: 8080 path: / initialDelaySeconds: 20 #启动容器后多少秒健康检查 periodSeconds: 20 #以后间隔多少秒检查一次 livenessProbe: # 存活检查,如果失败,将杀死容器,来重启 httpGet: port: 8080 path: / volumeMounts: - name: config # 指定共享卷的名称 mountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml" # 指定挂载目录下 subPath: application.yml # 指定文件名称 volumes: - name: config configMap: name: java-demo-config # 指定configmap的卷名 items: - key: "application.yml" # 指定的configmap的data里的保存的文件名 path: "application.yml" # 指定configmap的data文件
apiVersion: apps/v1 kind: Deployment metadata: name: java-demo namespace: default labels: app: java-demo annotations: # 记录回滚参数 kubernetes.io/change-cause: "java-demo:v1" #记录到revision中的内容,记录版本号 spec: replicas: 3 # Pod副本预期数量 revisionHistoryLimit: 10 # RS历史版本保存数量 selector: matchLabels: app: java-demo strategy: rollingUpdate: maxSurge: 25% # 滚动更新过程最大pod副本数 maxUnavailable: 25% # 滚动更新过程中最大不可用pod副本数, type: RollingUpdate template: metadata: labels: app: java-demo # Pod副本的标签 spec: imagePullSecrets: - name: harbor-auth containers: - name: java-demo image: harbor.ikubernetes.net/hsuing/java:v1 # 获取容器 ports: - containerPort: 8080 resources: requests: # 启动容器最小资源 memory: "256Mi" cpu: "500m" limits: # 容器的最大资源限制 memory: "512Mi" # 容器的最大资源内存限制128M cpu: "1000m" # 容器的最大资源cpu的核数限制,500m=0.5核数 readinessProbe: # 就绪检查,失败就会剔除 service httpGet: port: 8080 path: / initialDelaySeconds: 20 #启动容器后多少秒健康检查 periodSeconds: 20 #以后间隔多少秒检查一次 livenessProbe: # 存活检查,如果失败,将杀死容器,来重启 httpGet: port: 8080 path: / volumeMounts: - name: config # 指定共享卷的名称 mountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml" # 指定挂载目录下 subPath: application.yml # 指定文件名称 volumes: - name: config configMap: name: java-demo-config # 指定configmap的卷名 items: - key: "application.yml" # 指定的configmap的data里的保存的文件名 path: "application.yml" # 指定configmap的data文件
启动配置文件
bash[root@k8s-master java-demo]# kubectl apply -f java_demo.yaml deployment.apps/java-demo configured
[root@k8s-master java-demo]# kubectl apply -f java_demo.yaml deployment.apps/java-demo configured