说在最前
Pod作为Kubernetes的一等公民,是K8S调度的最小单元,也是使用Kubernetes的人打交道最多的K8S模型。既然Pod这么重要,搞清楚Pod的各种功能及操作就非常重要了,理解了Pod以后,各种Pod的控制器组件,例如:Deployment,StatefulSet,DaemonSet, Job,ReplicaSet理解起来才能事半功倍。
正篇
阅读官方文档对Pod的描述,其阐述了Pod在集群中的两种主要使用方式:
- 一个Pod运行一个容器
- 一个Pod运行多个需要在一起的容器
由于前面的实验,我们已经使用过Deployment部署简单的nginx容器,这种就属于第一种,一个Pod运行一个容器的模型,所以本篇文章我们来实战一下,一个Pod运行多个相互关联的容器。
实战
场景说明
本文以常用blog应用wordpress+mysql为例,将wordpress与mysql运行在同一个Pod内,并实现同Pod的相互访问。
本文实验所有的源码保存在:
https://github.com/zrbcool/blog-public/tree/master/k8s-hands-on/lab04
创建wordpress的Pod
查看Pod的yaml定义:
lab04 git:(master) cat 01-wordpress-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: wordpress
name: wordpress
spec:
containers:
- image: wordpress:latest
imagePullPolicy: IfNotPresent
name: wordpress
部署该Pod,并查看运行状态
lab04 kubectl apply -f 01-wordpress-pod.yaml
pod/wordpress create
lab04 kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress 1/1 Running 0 2m21s 10.244.0.53 worker01 <none> <none>
lab04 http 10.244.0.53
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 10 Jun 2019 05:37:54 GMT
Keep-Alive: timeout=5, max=100
Location: http://10.244.0.53/wp-admin/setup-config.php
Server: Apache/2.4.25 (Debian)
X-Powered-By: PHP/7.3.6
为了方便访问,我们再创建一个类型为NodePort的Service(这块不用纠结,后面会专门讲到),
lab04 git:(master) cat 02-wordpress-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-svc
labels:
app: wordpress
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: wordpress
type: NodePort
lab04 kubectl apply -f 02-wordpress-svc.yaml
service/wordpress-svc created
lab04 kubectl get svc wordpress-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress-svc NodePort 10.98.40.11 <none> 80:30466/TCP 108s
然后我们用节点IP:30466就可以进行访问了,试试
加入mysql的Pod
上面的步骤,我们成功使用Pod资源模型部署了wordpress,但是wordpress是基于mysql的应用,我们还需要一个mysql,针对这个实验,我们将mysql与wordpress部署在一个pod,这样他们就可以基于localhost进行通信了,来试试看。
修改Pod定义,加入mysql
修改后的yaml请参考:
lab04 git:(master) cat 03-wordpress-mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
app: wordpress
name: wordpress
spec:
containers:
- image: wordpress:latest
imagePullPolicy: IfNotPresent
name: wordpress
- image: mysql:5.7.26
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "passw0rd"
执行部署,
lab04 kubectl delete -f 03-wordpress-mysql-pod.yaml
pod "wordpress" deleted
lab04 kubectl create -f 03-wordpress-mysql-pod.yaml
pod/wordpress created
lab04 kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress 2/2 Running 0 25s 10.244.0.55 worker01 <none> <none>
lab04 telnet 10.244.0.55 3306
Trying 10.244.0.55...
Connected to 10.244.0.55.
Escape character is '^]'.
lab04 http 10.244.0.55
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 10 Jun 2019 06:14:58 GMT
Keep-Alive: timeout=5, max=100
Location: http://10.244.0.55/wp-admin/setup-config.php
Server: Apache/2.4.25 (Debian)
X-Powered-By: PHP/7.3.6
可见两个容器已经在同一个Pod内启动,并且mysql的3306端口也是通的,wordpress的80也是OK的
进入wordpress设置界面进行配置
下一步
上图说了,缺数据库,来手工创建一个库
lab04 kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress 2/2 Running 0 5m40s 10.244.0.57 worker01 <none> <none>
lab04 docker run -it --network host --rm mysql mysql -h10.244.0.57 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> create database wordpress;
Query OK, 1 row affected (0.00 sec)
再来一次,成功了!
通过标签过滤,查看下本次实验我们创建的所有资源:
lab04 git:(master) kubectl get all -l app=wordpress
NAME READY STATUS RESTARTS AGE
pod/wordpress 2/2 Running 0 29m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/wordpress-svc NodePort 10.98.40.11 <none> 80:30466/TCP 67m
总结
我们通过定义Pod,将两个容器定义在一个Pod当中,启动了wordpress+mysql的一个Pod实例,并使用NodePort类型的Service对wordpress进行了设置及访问,成功实践了一Pod多容器的使用方式。
或许你已经想到了当前这个实验结果的问题:
- 1,mysql的数据是状态化数据需要保存
- 2,直接定义的Pod是无法进行修改,扩容的,只能删除重建
这些问题,我们后面使用到的StatefulSet及Deployment等可以解决。