Kubernetes 入门:运行有状态服务

本文以 WordPress 为例,演示在 Kubernetes 集群中运行有状态服务,以及相关命令和概念。

为 MYSQL 密码创建 Secret

Secret 是一个对象,存储一系列的敏感信息,比如 password 和 key。

创建 Secret 对象

kubectl create secret generic mysql-pass --from-literal=password=YOUR-PASSWORD

YOUR-PASSWORD 替换为你想实际应用的密码

查看 Secrets

kubectl get secrets
NAME                  TYPE                                  DATA      AGE
default-token-jdn58   kubernetes.io/service-account-token   3         1d
mysql-pass            Opaque                                1         6s

部署 MySQL

在 k8s 中,PersistentVolumeClaims(简称 PVC) 和 PersistentVolumes(简称PV) 用于数据的持久化存储。

  • PV:持久化卷,是集群的一部分。就像 Node 一样,属于集群中的资源。
  • PVC:用户对存储的请求。它与 Pod 相似,Pod 消耗 Node 资源,PVC 消耗 PV 资源。

1、新建 MySQL 部署文件 mysql-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

2、部署 MySQL

kubectl create -f mysql-deployment.yaml

3、确认 PersistentVolume 是动态配置的

kubectl get pvc
NAME             STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pv-claim   Bound     pvc-22264d66-543c-11e8-993e-080027736407   10Gi       RWO            standard       1m

4、确认 Pod 正常运行

kubectl get pod
NAME                               READY     STATUS    RESTARTS   AGE
wordpress-mysql-7ddd5d59dd-njv2b   1/1       Running   0          5m

部署 WordPress

和 MySQL 类似,WordPress 也需要创建 Deployment 和 Service ,也需要 PVC 做持久化存储、Secret 做密码保密,另外,WordPress 需要暴露给集群外部的流量使用,还需要配置 type: NodePort

1、创建 WordPress 部署文件 wordpress-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: registry.docker-cn.com/library/wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

2、部署 WordPress

kubectl create -f wordpress-deployment.yaml

3、稍作等待,查看整个 WordPress 服务,可以看到 MySQL 和 WordPress 均正常工作

NAME                                   READY     STATUS    RESTARTS   AGE
pod/wordpress-5d7d6dfb58-mxr9k         1/1       Running   0          4m
pod/wordpress-mysql-7ddd5d59dd-njv2b   1/1       Running   0          24m

NAME                      TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes        ClusterIP      10.96.0.1               443/TCP        1d
service/wordpress         LoadBalancer   10.107.47.102        80:30435/TCP   4m
service/wordpress-mysql   ClusterIP      None                    3306/TCP       24m

NAME                              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/wordpress         1         1         1            1           4m
deployment.apps/wordpress-mysql   1         1         1            1           24m

NAME                                         DESIRED   CURRENT   READY     AGE
replicaset.apps/wordpress-5d7d6dfb58         1         1         1         4m
replicaset.apps/wordpress-mysql-7ddd5d59dd   1         1         1         24m

提醒: Minikube 只能通过 NodePort 暴露服务. EXTERNAL-IP 永远是 .

4、访问 WordPress

minikube service wordpress
Kubernetes 入门:运行有状态服务_第1张图片

移除 WordPress

删除 Secret

kubectl delete secret mysql-pass

删除 Deployment

kubectl delete deployment -l app=wordpress

删除 Service

kubectl delete service -l app=wordpress

删除 PVC

kubectl delete pvc -l app=wordpress

-l, --selector='': 选择器过滤, 不包含那些未初始化的.

你可能感兴趣的:(Kubernetes 入门:运行有状态服务)