写在前面
本文一起看一个基于k8s的实战小栗子,在这篇文章 中我们基于docker搭建了一个WordPress网站。本文就通过k8s再来实现一遍。架构图如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/9c73ac0c183a429a8f4b1a2feb363527.png
从上图可以看到和docker方式在应用封装和网络环境上都有所不同,应用封装使用了pod,网络环境是k8s内部的私有网络环境。
1:MariaDB
我们使用ConfigMap来配置MariaDB相关的配置参数,定义yaml如下:
apiVersion: v1
kind: ConfigMap
metadata:
name: maria-cm
data:
DATABASE: 'db'
USER: 'wp'
PASSWORD: '123'
ROOT_PASSWORD: '123'
创建:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl apply -f k8sMariaDB.yml
configmap/maria-cm created
然后定义MariaDB POD 的yaml,如下:
apiVersion: v1
kind: Pod
metadata:
name: maria-pod
labels:
app: wordpress
role: database
spec:
containers:
- image: mariadb:10
name: maria
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
envFrom:
- prefix: 'MARIADB_'
configMapRef:
name: maria-cm
这里使用了配置项envFrom
,会根据configMapRef
从指定的configmap中读取配置,并增加prefix
前缀作为最终env的key,如USER: 'wp'
,最终设置的env结果就是MARIADB_USER='wp'
,这样MariaDB在运行时,就能从环境变量中读取了,应用如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl apply -f k8sRealMariaDB.yml
...
如下可以查看pod在k8s私有网络里生成的IP地址:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
maria-pod 1/1 Running 0 18m 172.17.0.5 minikube <none> <none>
IP为172.17.0.5
,这里记下,后面需要用到,我们这里也可以进入到MariaDB容器查看:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl exec -it maria-pod -- mysql -u wp -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.10.2-MariaDB-1:10.10.2+maria~ubu2204 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
这里因为pod里只有一个容器所以就没有使用-c参数来指定具体是哪个容器了,其实在实际的应用中我们一般也只会在一个pod中设置一个容器,所以一般可以认为pod和容器的对应关系是1:1
。
2:编排WordPress
首先配置WordPress使用的ConfigMap配置对象,yaml如下:
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
HOST: '172.17.0.5'
USER: 'wp'
PASSWORD: '123'
NAME: 'db'
注意这里的HOST修改为MariaDB对应的POD IP地址。应用如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl apply -f aa.yml
configmap/wp-cm created
WordPress yaml如下:
apiVersion: v1
kind: Pod
metadata:
name: wp-pod
labels:
app: wordpress
role: website
spec:
containers:
- image: wordpress:5
name: wp-pod
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
envFrom:
- prefix: 'WORDPRESS_DB_'
configMapRef:
name: wp-cm
应用如下:
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl apply -f aa.yml
pod/wp-pod created
dongyunqi@dongyunqi-virtual-machine:~/test$ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
maria-pod 1/1 Running 0 109m 172.17.0.5 minikube <none> <none>
wp-pod 1/1 Running 0 4m21s 172.17.0.6 minikube <none> <none>
因为WordPress是在k8s的私有网络里,宿主机是无法直接访问的,所以需要映射WordPress的80端口到宿主机,这里映射到宿主机的8080端口,使用命令kubectl port-forward
,如下:
unqi@dongyunqi-virtual-machine:~/test$ kubectl port-forward wp-pod 8080:80 &
[1] 2814520
dongyunqi@dongyunqi-virtual-machine:~/test$ Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
注意此时虽然宿主机8080端口已经映射到WordPress pod的80端口,但是直接访问8080端口还是不通的,我也不知道为什么,必须通过80才行,所以我们还需要在宿主机使用Nginx代理80端口。
3:宿主机Nginx
Nginx配置文件如下:
dongyunqi@dongyunqi-virtual-machine:/tmp$ tee /tmp/proxy.yml <<-'EOF'
> server {
> listen 80;
> default_type text/html;
>
> location / {
> proxy_http_version 1.1;
> proxy_set_header Host $host;
> proxy_pass http://127.0.0.1:8080;
> }
> }
> EOF
server {
listen 80;
default_type text/html;
location / {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
使用docker 启动:
dongyunqi@dongyunqi-virtual-machine:/tmp$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
975cbe00fd24 nginx:alpine "/docker-entrypoint.…" 9 seconds ago Up 9 seconds epic_villani
...
然后访问宿主机80端口就可以了,我本地是http://192.168.64.131/
:
4:使用dashboard查看
执行minikube dashboard
打开dashboard:
dongyunqi@dongyunqi-virtual-machine:~$ minikube dashboard
🤔 Verifying dashboard health ...
🚀 Launching proxy ...
🤔 Verifying proxy health ...
🎉 Opening http://127.0.0.1:45395/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
Gtk-Message: 12:23:35.970: Not loading module "atk-bridge": The functionality is provided by GTK natively. Please try to not load it.
查看当pod:
点击某个pod:
查看本文的ConfigMap:
写在后面
参考文章列表:
k8s之基本环境准备 。