部署一个简单的Nginx服务:
deployment-yaml:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-web spec: replicas: 3 selector: matchLabels: name: nginx-web template: metadata: labels: name: nginx-web spec: containers: - name: nginx-web image: nginx
service-yaml
apiVersion: v1 kind: Service metadata: name: niginx-service spec: selector: name: nginx-web type: NodePort ports: - port: 80 protocol: TCP targetPort: 80 name: http nodePort: 30080
部署一个基于NFS持久化的Mysql服务:
--- # mysql-pv apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv spec: accessModes: - ReadWriteMany capacity: storage: 1Gi persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: /nfs-data/mysql-pv server: 172.16.65.200 --- # mysql-pvc apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi storageClassName: nfs --- # mysql deployment apiVersion: apps/v1 kind: Deployment metadata: name: mysql spec: selector: matchLabels: name: mysql template: metadata: labels: name: mysql spec: containers: - image: mysql name: mysql env: - name: MYSQL_ROOT_PASSWORD value: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pvc --- # mysql-service apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 selector: name: mysql
持久化测试:
# 运行一个mysql 客户端,通过DNS服务连接到 mysql service # kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword mysql> use mysql; mysql> create table my_user(name char(10)); mysql> insert my_user values("alex"); mysql> select * from my_user; +------+ | name | +------+ | alex | +------+ 1 row in set (0.00 sec)