Deployments are just configurations that relies on a Pod. So before diving in, make sure you read the Pod Documentation
$container = K8s::container();
$container
->setName('mysql')
->setImage('mysql', '5.7')
->setPorts([
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
]);
$pod = K8s::pod()
->setName('mysql')
->setLabels(['tier' => 'backend'])
->setContainers([$mysql]);
$dep = K8s::deployment()
->onCluster($this->cluster)
->setName('mysql')
->setSelectors(['matchLabels' => ['tier' => 'backend']])
->setReplicas(1)
->setTemplate($pod);
Deployments support annotations, as well as labels:
$dep->setAnnotations([
'nginx.kubernetes.io/tls' => 'true',
]);
$dep->setLabels([
'matchesLabel' => ['app' => 'backend'],
]);
While the Deployment kind has spec
, you can avoid writing this:
$dep = K8s::deployment($cluster)
->setAttribute('spec.template', [...]);
And use the setSpec()
method:
$dep = K8s::deployment($cluster)
->setSpec('template', [...]);
Dot notation is supported:
$dep = K8s::deployment($cluster)
->setSpec('some.nested.path', [...]);
$dep = K8s::deployment($cluster)
->whereName('mysql')
->get();
$template = $dep->getTemplate();
Retrieving the spec attributes can be done like the setSpec()
method:
$dep->getSpec('template', []);
The second value for the getSpec()
method is the default value, in case the found path is not existent.
Dot notation is supported:
$dep->getSpec('some.nested.path', []);
Deployments rely on pods, so you can get the pod template as K8sPod
class:
$template = $dep->getTemplate();
$podName = $template->getName();
To retrieve the pod template as an array, pass false
to the retrieval method:
$pod = $dep->getTemplate(false);
$podName = $template['name'];