nfsVolume.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as k8s from '@pulumi/kubernetes'
  2. import * as k8stypes from '@pulumi/kubernetes/types/input'
  3. import * as pulumi from '@pulumi/pulumi'
  4. /**
  5. * NFSServiceDeployment is an abstraction uses the cloud resources to create a PVC
  6. * which is then used by an NFS container, enabling users to then use this NFS server
  7. * as a shared file system without depending on creating custom cloud resources
  8. */
  9. export class NFSServiceDeployment extends pulumi.ComponentResource {
  10. public readonly deployment: k8s.apps.v1.Deployment
  11. public readonly service: k8s.core.v1.Service
  12. public readonly pvc: k8s.core.v1.PersistentVolumeClaim
  13. constructor(name: string, args: ServiceDeploymentArgs, opts?: pulumi.ComponentResourceOptions) {
  14. super('k8sjs:service:NFSServiceDeployment', name, {}, opts)
  15. const nfsLabels = { role: 'nfs-server' }
  16. const claimName = 'pvcfornfs'
  17. // Deploys a cloud block storage which will be used as base storage for NFS server
  18. const pvcNFS = new k8s.core.v1.PersistentVolumeClaim(
  19. claimName,
  20. {
  21. metadata: {
  22. labels: nfsLabels,
  23. namespace: args.namespace,
  24. name: claimName,
  25. },
  26. spec: {
  27. accessModes: ['ReadWriteOnce'],
  28. resources: {
  29. requests: {
  30. storage: `1Gi`,
  31. },
  32. },
  33. },
  34. },
  35. { parent: this }
  36. )
  37. const container: k8stypes.core.v1.Container = {
  38. name: 'nfs-server',
  39. image: 'gcr.io/google_containers/volume-nfs:0.8',
  40. ports: [
  41. { name: 'nfs', containerPort: 2049 },
  42. { name: 'mountd', containerPort: 20048 },
  43. { name: 'rpcbind', containerPort: 111 },
  44. ],
  45. command: ['/bin/sh', '-c'],
  46. args: ['chmod 777 /exports && /usr/local/bin/run_nfs.sh /exports'],
  47. securityContext: { 'privileged': true },
  48. volumeMounts: [
  49. {
  50. name: 'nfsstore',
  51. mountPath: '/exports',
  52. },
  53. ],
  54. }
  55. this.deployment = new k8s.apps.v1.Deployment(
  56. `nfs-server`,
  57. {
  58. metadata: {
  59. namespace: args.namespace,
  60. labels: nfsLabels,
  61. name: 'nfs-server',
  62. },
  63. spec: {
  64. replicas: 1,
  65. selector: { matchLabels: nfsLabels },
  66. template: {
  67. metadata: {
  68. labels: nfsLabels,
  69. },
  70. spec: {
  71. containers: [container],
  72. volumes: [
  73. {
  74. name: 'nfsstore',
  75. persistentVolumeClaim: {
  76. claimName,
  77. },
  78. },
  79. ],
  80. },
  81. },
  82. },
  83. },
  84. { parent: this }
  85. )
  86. this.service = new k8s.core.v1.Service(
  87. 'nfs-server',
  88. {
  89. metadata: {
  90. namespace: args.namespace,
  91. name: 'nfs-server',
  92. },
  93. spec: {
  94. ports: [
  95. { name: 'nfs', port: 2049 },
  96. { name: 'mountd', port: 20048 },
  97. { name: 'rpcbind', port: 111 },
  98. ],
  99. selector: nfsLabels,
  100. },
  101. },
  102. { parent: this }
  103. )
  104. const ip = this.service.spec.apply((v) => v.clusterIP)
  105. const pv = new k8s.core.v1.PersistentVolume(
  106. `${name}-pv`,
  107. {
  108. metadata: {
  109. labels: nfsLabels,
  110. namespace: args.namespace,
  111. name: `${name}-pvc`,
  112. },
  113. spec: {
  114. accessModes: ['ReadWriteMany'],
  115. capacity: {
  116. storage: `1Gi`,
  117. },
  118. nfs: {
  119. server: ip, //pulumi.interpolate`nfs-server.${namespaceName}.svc.cluster.local`,
  120. path: '/',
  121. },
  122. },
  123. },
  124. { parent: this, dependsOn: this.service }
  125. )
  126. this.pvc = new k8s.core.v1.PersistentVolumeClaim(
  127. `${name}-pvc`,
  128. {
  129. metadata: {
  130. namespace: args.namespace,
  131. name: `${name}-pvc`,
  132. },
  133. spec: {
  134. accessModes: ['ReadWriteMany'],
  135. resources: {
  136. requests: {
  137. storage: `1Gi`,
  138. },
  139. },
  140. storageClassName: '',
  141. selector: { matchLabels: nfsLabels },
  142. },
  143. },
  144. { parent: this, dependsOn: pv }
  145. )
  146. }
  147. }
  148. export interface ServiceDeploymentArgs {
  149. namespace: pulumi.Output<string>
  150. }