K8S自己动手系列 - 2.2 - Deployment

前言

前面的文章,我们使用Pod完成了wordpress+mysql的部署,并将两个容器放在了一个Pod当中,本篇文章,我们将使用Deployment完成前面的部署工作

本文实验所有的源码保存在:
https://github.com/zrbcool/blog-public/tree/master/k8s-hands-on/lab05

实战

查看deployment定义

  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"

执行部署操作

  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

访问我们的服务,
K8S自己动手系列 - 2.2 - Deployment

Deployment扩缩容尝试

修改我们的deployment定义,将replicas修改为2,查看效果

  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来部署有状态应用解决当前这个问题。

上一篇:K8S避坑指南 - Deployment更新POD内容器无法收到SIGTERM信号


下一篇:K8S自己动手系列 - 1.3 - Taint & Affinity