赞
踩
数据库环境准备是应用的前置准备工作
先在 node 节点上安装 mysql
sudo yum install mysql-server -y
安装sudo systemctl start mysqld
启动sudo systemctl enable mysqld
设置开启启动sudo mysql_secure_installation
设置安全选项mysql --version
查看版本我们在 node1 这个 work 节点部署,准备 app-mysql.yaml
apiVersion: v1 kind: Service metadata: name: mysql-nodeport spec: ports: - port: 3306 nodePort: 30306 selector: app: mysql type: NodePort --- apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: serviceName: "mysql" replicas: 1 # 虽然您要求不要副本,但StatefulSet通常需要至少一个副本 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:8.0.36 env: - name: MYSQL_ROOT_PASSWORD value: "password" ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-config mountPath: /etc/mysql/conf.d - name: mysql-data mountPath: /var/lib/mysql volumes: - name: mysql-config hostPath: path: /tmp/mysql/conf.d type: DirectoryOrCreate - name: mysql-data hostPath: path: /tmp/mysql/data type: DirectoryOrCreate
$ kubectl create -f app-mysql.yaml
创建
service/mysql-nodeport created
statefulset.apps/mysql created
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/mysql-0 1/1 Running 0 27s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 35h
service/mysql-nodeport NodePort 10.1.183.195 <none> 3306:30306/TCP 27s
NAME READY AGE
statefulset.apps/mysql 1/1 27s
$ mysql -u root -P 30306 -h node1.k8s -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.36 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
注意,在低版本的一些 mysql 中的配置会有不同,而且可能需要加上如下,不会意外退出
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
现在,我们可以创建一个新的数据库了
$ create database blogDBTest DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected, 2 warnings (0.06 sec)
查看 $ show databases
+--------------------+
| Database |
+--------------------+
| blogDBTest |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.03 sec)
可见,mysql数据库环境准备好了,这里只做了单实例的
注意,这个配置有很多的优化点,比如:集群副本,存储,密码加密等
仅供参考,可关注我之前的博文以及持续关注 …
1 ) 先说明下博客应用的底层配置
spring:
thymeleaf:
mode: HTML
profiles:
active: dev
comment.avater: /images/avatar.png
server:
port: 5000
FROM openjdk:8-jdk-alpine3.7
VOLUME /tmp
ADD target/blog.jar /blog.jar
EXPOSE 5000
ENTRYPOINT ["java", "-jar", "/blog.jar"]
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${MYSQL_SERVER:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB_NAME_TEST:blogDB}?useUnicode=true&characterEncoding=utf-8
username: ${MYSQL_USER_TEST: root}
password: ${MYSQL_PASSWORD_TEST: password}
2 )现在进入正题,应用程序的配置
$ vi app-blog.yaml
这个是博客应用程序的yaml文件
apiVersion: v1 kind: Service metadata: name: k8sblog spec: selector: app: k8sblog type: NodePort ports: - port: 5000 targetPort: 5000 nodePort: 30002 --- apiVersion: apps/v1 kind: Deployment metadata: name: k8sblog spec: selector: matchLabels: app: k8sblog template: metadata: labels: app: k8sblog spec: hostAliases: - ip: "10.211.10.2" hostnames: - "art.local" # 这里是我们的私有镜像中心 containers: - name: k8sblog image: 10.211.10.2:8081/docker-local/k8sblog:1.1 ports: - containerPort: 5000 env: - name: MYSQL_PORT value: "30306" - name: MYSQL_SERVER value: "node1.k8s" - name: MYSQL_DB_NAME_TEST value: blogDB - name: MYSQL_USER_TEST value: "root" - name: MYSQL_PASSWORD_TEST valueFrom: secretKeyRef: name: mysql-password-test key: MYSQL_PASSWORD_TEST imagePullSecrets: - name: regcred-local
这里的密码加密的生成,这里上面的可以进行参考,之前博文也有其他方式,请持续关注
kubectl create secret generic mysql-password-test --from-literal=MYSQL_PASSWORD_TEST=password
$ kubectl create -f app-blog.yaml
创建
$ kubectl get po
查看app成功运行
访问: http://node1.k8s:30002
以上仅仅是一种演示,仅供参考
1 ) 拉取
image: 10.211.10.2:8081/docker-local/k8sblog:1.1
这个配置2 )上传同步
mvn package
docker build -t 10.211.10.2:8081/docker-local/k8sblog:1.1 .
docker push 10.211.10.2:8081/docker-local/k8sblog:1.1
3 )关于设置镜像拉取密码
从上面可以看到, 有如下配置
imagePullSecrets:
- name: regcred-local
这个就是在私有镜像中心设置的拉取镜像时的密码
$ kubectl create secret docker-registry regcred-local --docker-server=10.211.10.2:8081 --docker-username=admin --docker-password=password --docker-email=xxx@qq.com
regcred-local
就代表你的用户名和密码加密后的codekubectl describe secret regcred-local
可以看到加密了和之前的其他secret 一样的效果1 ) 关于空间隔离
kubectl get ns
查看所有命名空间kubectl create ns test
创建 test 命名空间2 )创建相关环境的秘钥
kubectl create secret generic mysql-password-test --from-literal=MYSQL_PASSWORD_TEST=password -n test
kubectl create secret docker-registry regcred-local --docker-server=10.211.10.2:8081 --docker-username=admin --docker-password=password --docker-email=xxx@qq.com -n test
metadata:
namespace: test
kubectl get secret -n test
查看test空间下创建的所有秘钥10.211.10.2:8081/docker-local/k8sblog:1.1
10.211.10.2:8081/docker-local-test/k8sblog:1.1
或10.211.10.2:8081/docker-local-prod/k8sblog:1.1
10.211.10.2:8081/docker/k8sblog:1.1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。