More questions coming :slightly_smiling_face:. Is ...
# kubernetes
w
More questions coming 🙂. Is there a way to a service name that doesn't add the extra unique characters to the string? Making a POC in .NET and here I would like to set the Uri for the service in the
appsettings.json
file in the frontend app, but that doesn't work since pulumi adds some characters. I guess one way could be to get the name from the environment somehow instead.
To clarify. If I try to deploy services named
frontend
and
backend
. From
frontend
I should now be able to reference the
backend
using
<https://backend>
since they are in the same namespace (at least I think so). With pulumi I have to use
backend-xxxxxx
where I doesn't know what
xxxxx
is before I've deployed
backend
once. For other resources I've seen that you can override the name, but I can't find that for the
Deployment
or
Service
resources.
a
It isn't Pulumi that is naming your pods and ReplicaSets that way. It is Kubernetes. Each pod needs to be unique in the cluster, so this is the default (and only) kubernetes behaviour afaik when using Deployments. Your services shouldn't have this kind of unique name in the cluster. This pulumi TypeScript fragment will create a service, attached to pods with the postgresLabels, and give the service the DNS entry in the cluster of
postgres-svc
In the same namespace, you can use the Url or hostname of
postgres-svc
to access that pod, regardless of the pods name.
Copy code
const postgresServiceName = "postgres-svc";
const postgresService = new k8s.core.v1.Service(postgresServiceName, 
    {
        metadata: {
            name: postgresServiceName,
            labels: postgresLabels,
        },
        spec: {
            ports: [{ port: 5432, protocol: "TCP"}],
            selector: postgresLabels,
        },
    }, {provider: k8sProvider});
This is the k8s service resource that is created with that pulumi fragment
If you know the
metadata: { name: "insert-value-here" }
ahead of time, you can put that in your
appsettings.json
you can also override your appsettings.json with an env var that you put in your deployment resource description in Pulumi.
Copy code
const postgresDepName = "postgres-dep";
const postgresDeployment = new k8s.apps.v1.Deployment(postgresDepName, {
    metadata: { 
        name: postgresDepName, 
        labels: postgresLabels
    },
    spec: {
        selector: { matchLabels: postgresLabels },
        replicas: 1,
        revisionHistoryLimit: 2,
        template: {
            metadata: { labels: postgresLabels },
            spec: {
                containers: [{
                    name: "postgres",
                    image: "postgres:alpine",
                    volumeMounts: [{
                        mountPath: "/var/lib/postgresql/data",
                        name: "volume"
                    }],
                    resources: {
                        requests: {
                            cpu: "100m",
                            memory: "100Mi",
                        },
                    },
                    ports: [{ containerPort: 5432 }],
                    env: [
                        {name: "POSTGRES_USER", value: "admin"},
                        {name: "POSTGRES_PASSWORD", value: "P@ssw0rd!"},
                        {name: "POSTGRES_DB", value: "identity"},
                        {name: "PGDATA", value: "/mnt/data/pgdata"}
                    ]
                }],
                volumes:[{
                    name: "volume",
                    persistentVolumeClaim: {
                        claimName: postgresPVClaimName
                    }
                }]
            },
        },
    },
}, {provider: k8sProvider});
Note the
env: []
section in the container spec
c
pulumi does add a suffix to resources. this is so that if you change an immutable field it can orchestrate a replacement. to avoid this just manually specify .metadata.name.
g
If you’re referring to the random suffix on the resource name, see https://www.pulumi.com/docs/intro/concepts/programming-model/#autonaming for more details. Short answer is that it won’t be autonamed if specify
.metadata.name
of any k8s resource
w
Got it, thanks.