前面的文章,咱們使用Pod完成了wordpress+mysql的部署,並將兩個容器放在了一個Pod當中,本篇文章,咱們將使用Deployment完成前面的部署工做java
本文實驗全部的源碼保存在: github.com/zrbcool/blo…mysql
查看deployment定義git
➜ lab05 git:(master) ✗ cat 01-wordpress-mysql-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: wordpress
name: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress:latest
imagePullPolicy: IfNotPresent
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: "127.0.0.1"
- name: WORDPRESS_DB_USER
value: "root"
- name: WORDPRESS_DB_PASSWORD
value: "passw0rd"
- image: mysql:5.7.26
imagePullPolicy: IfNotPresent
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: "passw0rd"
- name: MYSQL_DATABASE
value: "wordpress"
複製代碼
執行部署操做github
➜ lab05 git:(master) ✗ kubectl apply -f 01-wordpress-mysql-deployment.yaml
deployment.extensions/wordpress created
➜ lab05 git:(master) ✗ kubectl apply -f 02-wordpress-svc.yaml
service/wordpress-svc created
➜ lab05 git:(master) ✗ kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
wordpress 1/1 1 1 2m15s wordpress,mysql wordpress:latest,mysql:5.7.26 app=wordpress
➜ lab05 git:(master) ✗ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress-5844bb9d6b-dbfb4 2/2 Running 0 2m23s 10.244.0.62 worker01 <none> <none>
➜ lab05 git:(master) ✗ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR wordpress-svc NodePort 10.98.150.68 <none> 80:30611/TCP 2m23s app=wordpress
複製代碼
訪問咱們的服務, web
修改咱們的deployment定義,將replicas修改成2,查看效果sql
➜ lab05 git:(master) ✗ kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
wordpress 2/2 2 2 7m43s wordpress,mysql wordpress:latest,mysql:5.7.26 app=wordpress
➜ lab05 git:(master) ✗ kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wordpress-5844bb9d6b-dbfb4 2/2 Running 0 7m47s 10.244.0.62 worker01 <none> <none>
wordpress-5844bb9d6b-ttkvm 2/2 Running 0 11s 10.244.0.63 worker01 <none> <none>
複製代碼
屢次訪問網頁,發現網頁進入了網站初始化界面,初始化網站爲mywebsite2,發現網站常常在website1與website2間切換,這是由於咱們的兩個副本從應用到數據庫徹底是相同的兩份,而Service負載均衡,會分配請求到兩套實例上,可是mysql是有狀態應用,就形成了咱們使用上的問題。數據庫
實際上,Deployment就是用來部署無狀態應用的,它期待你的副本之間是沒有區別的,那麼在咱們的場景下,mysql使用Deployment來部署就不是一個好的實踐,咱們這裏只是學習使用,後續咱們會用StatefulSet來部署有狀態應用解決當前這個問題。api