mongo.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import * as k8s from '@pulumi/kubernetes'
  2. import * as pulumi from '@pulumi/pulumi'
  3. /**
  4. * ServiceDeployment is an example abstraction that uses a class to fold together the common pattern of a
  5. * Kubernetes Deployment and its associated Service object.
  6. * This class delpoys a Mongo DB instance on a Persistent Volume
  7. */
  8. export class MongoDBServiceDeployment extends pulumi.ComponentResource {
  9. public readonly deployment: k8s.apps.v1.Deployment
  10. public readonly service: k8s.core.v1.Service
  11. constructor(name: string, args: ServiceDeploymentArgs, opts?: pulumi.ComponentResourceOptions) {
  12. super('mongodb:service:PostgresServiceDeployment', name, {}, opts)
  13. const databaseLabels = { app: name }
  14. const pvcName = `${name}-pvc`
  15. const pvc = new k8s.core.v1.PersistentVolumeClaim(
  16. pvcName,
  17. {
  18. metadata: {
  19. labels: databaseLabels,
  20. namespace: args.namespaceName,
  21. name: pvcName,
  22. },
  23. spec: {
  24. accessModes: ['ReadWriteOnce'],
  25. resources: {
  26. requests: {
  27. storage: `${args.storage}Gi`,
  28. },
  29. },
  30. },
  31. },
  32. { parent: this }
  33. )
  34. this.deployment = new k8s.apps.v1.Deployment(
  35. name,
  36. {
  37. metadata: {
  38. namespace: args.namespaceName,
  39. labels: databaseLabels,
  40. },
  41. spec: {
  42. selector: { matchLabels: databaseLabels },
  43. template: {
  44. metadata: { labels: databaseLabels },
  45. spec: {
  46. containers: [
  47. {
  48. name: 'mongo-db',
  49. image: 'library/mongo:4.4',
  50. volumeMounts: [
  51. {
  52. name: 'mongo-data',
  53. mountPath: '/data/db',
  54. subPath: 'mongo',
  55. },
  56. ],
  57. },
  58. ],
  59. volumes: [
  60. {
  61. name: 'mongo-data',
  62. persistentVolumeClaim: {
  63. claimName: pvcName,
  64. },
  65. },
  66. ],
  67. },
  68. },
  69. },
  70. },
  71. { parent: this }
  72. )
  73. this.service = new k8s.core.v1.Service(
  74. name,
  75. {
  76. metadata: {
  77. namespace: args.namespaceName,
  78. labels: this.deployment.metadata.labels,
  79. name: name,
  80. },
  81. spec: {
  82. ports: [{ port: 27017 }],
  83. selector: this.deployment.spec.template.metadata.labels,
  84. },
  85. },
  86. { parent: this }
  87. )
  88. }
  89. }
  90. export interface ServiceDeploymentArgs {
  91. namespaceName: pulumi.Output<string>
  92. storage: Number
  93. }