目录
http://k8s.driverzeng.com/v1.19/
mysql
[root@master-1 mysql]# cat mysql.yaml
apiVersion: "v1"
kind: "Pod"
metadata:
name: mysql57 // 资源清单叫mysql57
spec:
nodeName: node-1 // 指定启动到node-1节点
volumes:
- name: mysql-data // 挂载点名称
hostPath:
path: /data/mysql // 挂载点宿主机上的位置
containers:
- name: mysql-container // 容器名称
image: mysql:5.7 // 容器使用的镜像
imagePullPolicy: IfNotPresent // 拉取镜像规则,有就不拉取,没有择拉取
env: // 环境变量
- name: MYSQL_ROOT_PASSWORD
value: '123'
- name: MYSQL_DATABASE
value: 'wordpress'
- name: MYSQL_USER
value: 'wp_user'
- name: MYSQL_PASSWORD
value: '123'
args: // 传参
- --character-set-server=utf8mb4
- --collation-server=utf8mb4_unicode_ci
volumeMounts:
- name: mysql-data // 使用哪个挂载点
mountPath: /var/lib/mysql // 容器内的目录位置
# 启动
[root@master-1 mysql]# kubectl apply -f mysql.yaml
ports:
- containerPort: 3306
hostPort: 3306
根据你提供的信息,这是一个 Kubernetes YAML 文件中定义的端口映射部分,用于将容器内的端口映射到主机上。
在这个例子中:
containerPort: 3306 定义了容器内的端口号为 3306,这通常是 MySQL 数据库服务器的默认端口。
hostPort: 3306 定义了主机上映射的端口号也为 3306,这意味着通过主机上的该端口可以访问到容器内的 MySQL 服务。
## 通过这样的端口映射配置,你可以将主机上的请求转发到容器内运行的 MySQL 服务进行处理。这样,你可以通过主机的 3306 端口来连接和访问 MySQL 数据库。
请注意,在使用端口映射时要确保主机上的对应端口未被占用,并且允许访问该端口的网络规则已正确配置。
wordpress
[root@master-1 wordpress]# cat mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: wordpress
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: dbport
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
volumeMounts:
- name: db
mountPath: /var/lib/mysql
resources: {}
volumes:
- name: db
hostPath:
path: /var/lib/mysql
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wordpress
spec:
selector:
app: mysql
ports:
- name: mysqlport
protocol: TCP
port: 3306
targetPort: dbport
# 启动
kubectl apply -f mysql.yaml
# 查看数据库的ip
[root@master-1 wordpress]# kubectl get pod -n wordpress -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-d45bfb8c4-vlqpj 1/1 Running 0 20m 10.2.1.22 node-1 <none> <none>
[root@master-1 wordpress]# cat wordpress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: wdport
env:
- name: WORDPRESS_DB_HOST
value: 10.2.1.22:3306 // 这个换成自己的数据库的ip
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
type: NodePort
selector:
app: wordpress
ports:
- name: wordpressport
protocol: TCP
port: 80
targetPort: wdport
# 启动
kubectl apply -f wordpress.yaml
# 查看端口
[root@master-1 wordpress]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.1.111.44 <none> 3306/TCP 26m
wordpress NodePort 10.1.234.121 <none> 80:32727/TCP 22m
# 访问10.0.0.110:32727
用node节点ip
标签:name,app,MySQL,value,3306,wordpress,mysql,清单
From: https://www.cnblogs.com/xiutai/p/17749234.html