database.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Postgres instance on a Persistent Volume
  7. */
  8. export class PostgresServiceDeployment 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('postgres: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: 'postgres-db',
  49. image: 'postgres:12',
  50. env: args.env,
  51. ports: [{ containerPort: 5432 }],
  52. volumeMounts: [
  53. {
  54. name: 'postgres-data',
  55. mountPath: '/var/lib/postgresql/data',
  56. subPath: 'postgres',
  57. },
  58. ],
  59. },
  60. ],
  61. volumes: [
  62. {
  63. name: 'postgres-data',
  64. persistentVolumeClaim: {
  65. claimName: pvcName,
  66. },
  67. },
  68. ],
  69. },
  70. },
  71. },
  72. },
  73. { parent: this }
  74. )
  75. this.service = new k8s.core.v1.Service(
  76. name,
  77. {
  78. metadata: {
  79. namespace: args.namespaceName,
  80. labels: this.deployment.metadata.labels,
  81. name: name,
  82. },
  83. spec: {
  84. ports: [{ port: 5432 }],
  85. selector: this.deployment.spec.template.metadata.labels,
  86. },
  87. },
  88. { parent: this }
  89. )
  90. }
  91. }
  92. interface Environment {
  93. name: string
  94. value: string
  95. }
  96. export interface ServiceDeploymentArgs {
  97. namespaceName: pulumi.Output<string>
  98. env?: Environment[]
  99. storage: Number
  100. isMinikube?: boolean
  101. }