• 标题
    • MySQL
      • 创建deployment
      • 创建SVC
    • Wordpress
      • 创建deployment
      • 创建SVC
    • 删除

    标题

    MySQL

    创建deployment

    1. apiVersion: extensions/v1beta1
    2. kind: Deployment # 定义一个deployment
    3. metadata:
    4. name: mysql # deployment名称,全局唯一
    5. labels:
    6. app: mysql123
    7. release: stable
    8. spec:
    9. replicas: 1
    10. selector:
    11. matchLabels:
    12. app: mysql123 # deployment的POD标签选择器,即:监控和管理拥有这些标签的POD实例,确保当前集群中有且只有replicas个POD实例在运行
    13. template:
    14. metadata:
    15. labels: # 指定该POD的标签
    16. app: mysql123 # POD副本拥有的标签,需要与deployment的selector一致
    17. spec:
    18. containers:
    19. - name: mysql
    20. image: mysql
    21. ports:
    22. - containerPort: 3306
    23. env:
    24. - name: MYSQL_ROOT_PASSWORD
    25. value: "123456"

    创建SVC

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: mysql # Service名称,全局唯一
    5. labels:
    6. app: mysql123
    7. spec:
    8. ports:
    9. - port: 3306 # Service提供服务的端口号
    10. selector:
    11. app: mysql123 # 选择器

    Wordpress

    创建deployment

    1. apiVersion: extensions/v1beta1
    2. kind: Deployment
    3. metadata:
    4. name: wordpress
    5. labels:
    6. app: wordpress
    7. spec:
    8. replicas: 1
    9. selector:
    10. matchLabels:
    11. app: wordpress
    12. template:
    13. metadata:
    14. labels:
    15. app: wordpress
    16. spec:
    17. containers:
    18. - name: wordpress
    19. image: wordpress:4.8-apache
    20. ports:
    21. - containerPort: 80
    22. env:
    23. - name: WORDPRESS_DB_HOST
    24. value: "mysql"
    25. - name: WORDPRESS_DB_PASSWORD
    26. value: "123456"

    创建SVC

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: wordpress
    5. labels:
    6. app: wordpress
    7. spec:
    8. type: NodePort # 为该Service开启NodePort方式的外网访问模式
    9. ports:
    10. - port: 80 # Service提供服务的端口号
    11. targetPort: 80 # 将Service的80端口转发到Pod中容器的80端口上
    12. nodePort: 32001 # 在k8s集群外访问的端口,如果设置了NodePort类型,但没设置nodePort,将会随机映射一个端口,可使用kubectl get svc wordpress看到
    13. selector:
    14. app: wordpress

    删除

    1. kubectl delete deployment,svc -l app=mysql123
    2. kubectl delete deployment,svc -l app=wordpress