k8s 安装:https://www.cnblogs.com/jhxxb/p/14321352.html
yaml 文件说明:https://kubernetes.io/zh/docs/concepts/overview/working-with-objects/kubernetes-objects
MySQL
apiVersion: v1 kind: PersistentVolume metadata: name: nfs-mysql spec: capacity: storage: 5Gi accessModes: - ReadWriteMany nfs: server: 10.74.2.71 path: "/nfs/mysql" --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-mysql namespace: default spec: accessModes: - ReadWriteMany storageClassName: "" resources: requests: storage: 5Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: mysql-deployment spec: selector: matchLabels: app: mysql-label replicas: 1 template: metadata: labels: app: mysql-label spec: containers: - name: mysql image: mysql:latest env: - name: MYSQL_ROOT_PASSWORD value: root - name: MYSQL_ROOT_HOST value: '%' - name: TZ value: Asia/Shanghai ports: - containerPort: 3306 volumeMounts: - name: mysql-data mountPath : /var/lib/mysql volumes: - name: mysql-data persistentVolumeClaim: claimName: nfs-mysql --- apiVersion: v1 kind: Service metadata: name: mysql-service spec: selector: app: mysql-label type: NodePort ports: - port: 3306 targetPort: 3306
sudo mkdir /nfs/mysql
kubectl apply -f mysql.yaml
Oracle
apiVersion: v1 kind: PersistentVolume metadata: name: nfs-oracle spec: capacity: storage: 10Gi accessModes: - ReadWriteMany nfs: server: 10.74.2.71 path: "/nfs/oracle" --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-oracle namespace: default spec: accessModes: - ReadWriteMany storageClassName: "" resources: requests: storage: 10Gi --- apiVersion: apps/v1 kind: Deployment metadata: name: oracle-deployment spec: selector: matchLabels: app: oracle-label replicas: 1 template: metadata: labels: app: oracle-label spec: containers: - name: oracle-11g image: loliconneko/oracle-ee-11g:latest env: - name: WEB_CONSOLE value: 'false' - name: DBCA_TOTAL_MEMORY value: '4096' ports: - containerPort: 1521 volumeMounts: - name: oracle-data mountPath : /u01/app/oracle volumes: - name: oracle-data persistentVolumeClaim: claimName: nfs-oracle --- apiVersion: v1 kind: Service metadata: name: oracle-service spec: selector: app: oracle-label type: NodePort ports: - port: 1521 targetPort: 1521 nodePort: 1521
sudo mkdir /nfs/oracle
kubectl apply -f oracle.yaml
Redis
注意这里 resources 项限制了 CPU 和 Memory 资源
apiVersion: apps/v1 kind: Deployment metadata: name: redis-deployment spec: selector: matchLabels: app: redis-label replicas: 1 template: metadata: labels: app: redis-label spec: containers: - name: redis image: redis:alpine resources: limits: cpu: 1 memory: 512Mi requests: cpu: 0.5 memory: 128Mi ports: - containerPort: 6379 --- apiVersion: v1 kind: Service metadata: name: redis-service spec: selector: app: redis-label type: NodePort ports: - port: 6379 targetPort: 6379 nodePort: 6379
kubectl apply -f redis.yaml
部署应用到指定 Node 节点
两种方式:NodeName 和 NodeSelector
一、NodeName
以部署 RabbitMQ 为例,注意 nodeName 属性值为节点名称
apiVersion: apps/v1 kind: Deployment metadata: name: rabbitmq-deployment spec: selector: matchLabels: app: rabbitmq-label replicas: 1 template: metadata: labels: app: rabbitmq-label spec: nodeName: k8s-node-8 containers: - name: rabbitmq image: rabbitmq:management-alpine resources: limits: cpu: 1 memory: 512Mi requests: cpu: 0.5 memory: 128Mi env: - name: RABBITMQ_DEFAULT_VHOST value: my_vhost - name: RABBITMQ_DEFAULT_USER value: guest - name: RABBITMQ_DEFAULT_PASS value: guest ports: - containerPort: 5672 - containerPort: 15672 --- apiVersion: v1 kind: Service metadata: name: rabbitmq-service spec: selector: app: rabbitmq-label type: NodePort ports: - name: app port: 5672 targetPort: 5672 nodePort: 5672 - name: ui port: 15672 targetPort: 15672 nodePort: 15672
部署查看
kubectl apply -f rabbitmq.yaml
kubectl get pods -o wide
二、NodeSelector
给 Node 设置标签
kubectl label nodes k8s-node-8 deploy=hello kubectl get nodes --show-labels
已部署 MQ 为例,注意 nodeSelector 属性值为标签属性值
apiVersion: apps/v1 kind: Deployment metadata: name: rabbitmq-deployment spec: selector: matchLabels: app: rabbitmq-label replicas: 1 template: metadata: labels: app: rabbitmq-label spec: nodeSelector: deploy: hello containers: - name: rabbitmq image: rabbitmq:management-alpine resources: limits: cpu: 1 memory: 512Mi requests: cpu: 0.5 memory: 128Mi env: - name: RABBITMQ_DEFAULT_VHOST value: my_vhost - name: RABBITMQ_DEFAULT_USER value: guest - name: RABBITMQ_DEFAULT_PASS value: guest ports: - containerPort: 5672 - containerPort: 15672 --- apiVersion: v1 kind: Service metadata: name: rabbitmq-service spec: selector: app: rabbitmq-label type: NodePort ports: - name: app port: 5672 targetPort: 5672 nodePort: 5672 - name: ui port: 15672 targetPort: 15672 nodePort: 15672