K8s博客
K8s博客
1. 将MYSQL运行为Pod
[root@centos8 ~]#vim mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-mysql
labels:
app: mysql
release: test
spec:
containers:
- name: wp-mysql
image: mysql:latest
ports:
- containerPort: 3306
hostPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
#运行为Pod
[root@centos8 ~]#kubectl apply -f mysql.yaml
#查看Pod
[root@centos8 ~]#kubectl get pods
2.将WordPress运行为Pod,且能将数据存储于MYSQL Pod中
#编写配置清单
[root@centos8 ~]#vim wp-mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-mysql
labels:
app: mysql
release: test
spec:
containers:
- name: wp-mysql
image: mysql:latest
ports:
- containerPort: 3306
hostPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
[root@centos8 ~]#cat wp-wordpress.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-wordpress
labels:
app: wp-wordpress
release: test
spec:
containers:
- name: wp-wordpress
image: wordpress:latest
ports:
- containerPort: 80
hostPort: 8080
#将MYSQL和wordpress运行为容器
[root@centos8 ~]#kubectl apply -f wp-mysql.yaml
pod/wp-mysql created
[root@centos8 ~]#kubectl apply -f wp-wordpress.yaml
pod/wp-wordpress created
#进入容器连接mysql,创建用户,授权,指定密码
[root@centos8 ~]#kubectl exec -it wp-mysql -- /bin/bash
root@node2:/# mysql -uroot -p
Enter password:
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> mysql> create user word10.0.0.%';
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'10.0.0.%';
Query OK, 0 rows affected (0.01 sec)
mysql> SET PASSWORD FOR 'wordpress'@'10.0.0.%' = '123456';
Query OK, 0 rows affected (0.06 sec)
mysql> \q
Bye
root@node2:/# exit
exit
[root@centos8 ~]#
#查看容器拉起,确定MYSQL数据库IP
[root@centos8 ~]#kubectl describe pods wp-mysqlName: wp-mysql
Namespace: default
Priority: 0
Node: node2.magedu.com/10.0.0.98
Start Time: Thu, 03 Dec 2020 20:56:52 +0800
Labels: app=mysql
release=test
Annotations: <none>
Status: Running
IP: 10.244.2.6
IPs:
IP: 10.244.2.6
........
#访问http://10.0.0.98:8080,用户名worpress ,wordpress,密码:123456,数据库主机:10.0.0.98 ,如下图