Kubernetes(k8s)1.6.0部署 WordPress以及hpa和滚动更新测试

部署WordPress以前已经完成Kubernetes1.6.0集群环境部署和初步测试,这里以部署WordPress为例,整理部署一般应用的流程。

1. 创建单独的命名空间

为应用创建一个单独的命名空间wordpress

kubectl create namespace wordpress

2. 创建mysql

2.1 创建mysql对应的PV

创建pv以前,通过nfs创建共享目录。如果没有安装nfs则需要先安装nfs服务。

yum install nfs-utils -y

创建目录:

mkdir -p /root/data/nfs/pvmysql

添加到nfs

#vim /etc/exports

/root/data/nfs/pvmysql 192.168.17.0/8(rw,no_root_squash)

#systemctl restart nfs

准备脚本mysql-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqlpv
  namespace: wordpress
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /root/data/nfs/pvmysql
    server: 192.168.17.81

选择nfs,path就是刚才创建的目录/root/data/nfs/pvmysql,

这里的nfs server选择的是192.168.17.81

创建pv

# kubectl apply -f mysql-pv.yaml 
persistentvolume/mysqlpv created


# kubectl get pv -n wordpress
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mysqlpv   1Gi        RWO            Recycle          Available                                   39s

可以看到pv创建成功了。

2.2 创建mysql对应的PVC

vi mysql-pvc.yaml

脚本内容: 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqlpvc
  namespace: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  volumeName: mysqlpv
  resources:
    requests:
      storage: 1Gi

执行创建

# kubectl apply -f mysql-pvc.yaml 
persistentvolumeclaim/mysqlpvc created


# kubectl get pvc -n wordpress
NAME       STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysqlpvc   Bound    mysqlpv   1Gi        RWO                           52s

可以看到pvc创建成功。

2.3 创建mysql deployment

准备脚本文件

vim mysql-deploy.yaml

脚本内容:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deploy
  namespace: wordpress
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deploy
  namespace: wordpress
  labels:
    apps: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
          name: dbport
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: rootPassWord
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress
        volumeMounts:
        - name: db-pv
          mountPath: /var/lib/mysql
      volumes:
      - name: db-pv
        persistentVolumeClaim:
          claimName: mysqlpvc

创建mysql deploy:

# kubectl apply -f mysql-deploy.yaml
deployment.apps/mysql-deploy created


# kubectl get deploy -n wordpress
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
mysql-deploy   1/1     1            1           78s

可以看到,已经部署成功。

2.4 创建mysql service

准备脚本文件:

vim mysql-service.yaml

文件内容:

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: wordpress
spec:
  selector:
    app: mysql
  ports:
  - name: mysqlport
    protocol: TCP
    port: 3306
    targetPort: 3306

执行部署:

# kubectl apply -f mysql-service.yaml 
service/mysql created

[root@k8s-master wordpress]# kubectl get service -n wordpress
NAME    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
mysql   ClusterIP   10.10.92.175   <none>        3306/TCP   13s

至此,mysql已经创建完毕。

3. 创建wordpress

3.1 创建wordpress deploy

创建脚本

vim wordpressdeploy.yaml

脚本内容:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deploy
  namespace: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: wdport
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql:3306
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD

执行部署:

# kubectl apply -f wordpressdeploy.yaml 
deployment.apps/wordpress-deploy created


# kubectl get deploy -n wordpress
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
wordpress-deploy   1/1     1            1           3m50s

3.2 创建wordpress对应的service

创建脚本:

vim wordpress-service.yaml

文件内容:

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  namespace: wordpress
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
  - name: wordpressport
    protocol: TCP
    port: 80
    targetPort: wdport

执行创建:

# kubectl apply -f wordpress-service.yaml 
service/wordpress created

# kubectl get svc -n wordpress
NAME        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
wordpress   NodePort    10.10.156.2    <none>        80:32474/TCP   13s

至此wordpress部署成功

4. 基本测试

4.1 浏览器访问测试

http://192.168.17.85:32474/

可以使用集群的任一节点ip进行访问。

出现配置界面:

 4.2 查看共享的nfs目录

# ls /root/data/nfs/pvmysql
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  wordpress

可以看到mysql创建的文件。

5. HPA创建和测试

hpa是为了在请求数量多导致节点负载增加时,可以自动启动更多的节点来满足业务需求,当业务请求减少的时候,又可以自动地降低节点数来释放资源。

HPA依赖于metrics-server,只有正确部署了metrics-server,HPA才能正常工作。

# kubectl autoscale deployment wordpress-deploy --cpu-percent=10 --min=1 --max=10 -n wordpress
horizontalpodautoscaler.autoscaling/wordpress-deploy autoscaled


# kubectl get hpa -n wordpress -w
NAME               REFERENCE                     TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
wordpress-deploy   Deployment/wordpress-deploy   1%/10%    1         10        1          3m40s

5.1 模拟压力测试

创建一个容器来进行模拟测试。

#启动一个用于测试的容器
kubectl run v1 -it --image=busybox /bin/sh

#登录到容器执行如下命令
while true; do wget -q -O- http://192.168.17.81:32474/; done

可以看到容器的持续创建过程:

# kubectl get pod -n wordpress
NAME                                READY   STATUS              RESTARTS   AGE
mysql-deploy-777bfff47c-fq966       1/1     Running             0          174m
wordpress-deploy-668c7b5484-528js   0/1     ContainerCreating   0          45s
wordpress-deploy-668c7b5484-5mdbn   0/1     ContainerCreating   0          75s
wordpress-deploy-668c7b5484-6x5jt   1/1     Running             0          60s
wordpress-deploy-668c7b5484-8bsm7   1/1     Running             0          148m
wordpress-deploy-668c7b5484-8rbgn   1/1     Running             0          75s
wordpress-deploy-668c7b5484-9ddxc   1/1     Running             0          60s
wordpress-deploy-668c7b5484-dlw64   0/1     ContainerCreating   0          60s
wordpress-deploy-668c7b5484-jjsmh   0/1     ContainerCreating   0          60s
wordpress-deploy-668c7b5484-t685t   0/1     ContainerCreating   0          2m46s
wordpress-deploy-668c7b5484-w2j87   1/1     Running             0          45s

从图形界面也可以看到创建的实例:

5.2 停止压力测试

停止压力测试一段时间后,可以看到多余的节点在逐步释放。

# kubectl get pod -n wordpress
NAME                                READY   STATUS        RESTARTS   AGE
mysql-deploy-777bfff47c-fq966       1/1     Running       0          3h7m
wordpress-deploy-668c7b5484-6x5jt   1/1     Terminating   0          13m
wordpress-deploy-668c7b5484-8bsm7   1/1     Running       0          161m
wordpress-deploy-668c7b5484-8rbgn   1/1     Terminating   0          13m
wordpress-deploy-668c7b5484-9ddxc   1/1     Terminating   0          13m
wordpress-deploy-668c7b5484-w2j87   1/1     Terminating   0          12m

最后只有一个pod节点。

# kubectl get pod -n wordpress
NAME                                READY   STATUS    RESTARTS   AGE
mysql-deploy-777bfff47c-fq966       1/1     Running   0          3h7m
wordpress-deploy-668c7b5484-8bsm7   1/1     Running   0          161m

6. 滚动更新测试

修改wordpressdeploy.yaml文件,为镜像增加版本。

image: wordpress:5.8

重新执行部署。

kubectl apply -f wordpressdeploy.yaml

可以查看更新状态

# kubectl rollout status deploy wordpress-deploy -n wordpress
Waiting for deployment "wordpress-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "wordpress-deploy" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "wordpress-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "wordpress-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "wordpress-deploy" successfully rolled out

在更新过程中,web页面可以正常访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿20

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值