<p>I need to set up a kubernetes pod to create demo environments for clients of my web application, with a 20 day trial duration. After these 20 days, the pod should be automatically deleted, how can I make the pod self-destruct after 20 days?
I use Rancher to deploy my pods.</p>

Luis Manuel Cortés Tirado
<p>You can achieve this using <strong>two</strong> ways, write your own code and run on K8s to check status which will delete the deployment (POD) after 20 days</p>
<p>Reference github : <a href="https://github.com/dignajar/clean-pods" rel="nofollow noreferrer">https://github.com/dignajar/clean-pods</a></p>
<p>There is no option for your pod to get auto-deleted.</p>
<p>Either you run <strong>cronjob</strong> at an interval of <strong>20</strong> <strong>days</strong> which will delete specific deployment but again in this case you have to <strong>pass</strong> deployment or pod <strong>name</strong> so cronjob has that variable.</p>
<p><strong>Example : 1</strong></p>
<p>use <a href="https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md" rel="nofollow noreferrer">delete_namespaced_pod</a></p>
<pre><code>    from kubernetes import client, config
    from kubernetes.client.rest import ApiException
    config.load_incluster_config() # if running inside k8s cluster config.load_kube_config()
    
    configuration = client.Configuration()
    
    with client.ApiClient(configuration) as api_client:
        api_instance = client.CoreV1Api(api_client)
        
        namespace = '&lt;Namespace name&gt;'
        name = '&lt;POD name&gt;'  
api_instance.list_namespaced_pod(namespace)
        
        try:
            api_response = api_instance.delete_namespaced_pod(name, namespace)
            print(api_response)
        except ApiException as e:
            print(&quot;Exception when calling CoreV1Api-&gt;delete_namespaced_pod: %s\n&quot; % e) 
</code></pre>
<p><strong>Example : 2</strong></p>
<p>cronjob</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cleanup
spec:
  schedule: &quot;30 1 1,20 * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: kubectl-container
            image: bitnami/kubectl:latest
            command: [&quot;sh&quot;, &quot;-c&quot;, &quot;kubectl delete pod &lt;POD name or add variable here&gt;&quot;]
          restartPolicy: Never
</code></pre>
<p><strong>Extra</strong></p>
<p>You can also write <strong>shell script</strong> which run <strong>daily</strong> run few command to check the <strong>AGE</strong> of <strong>POD</strong> and delete if equal to <strong>20</strong> days</p>
<pre><code>kubectl get pods --field-selector=status.phase=Pending --sort-by=.metadata.creationTimestamp | awk 'match($5,/[20-9]d|[0-9][0-9]d|[0-9][0-9][0-9]d/) {print $0}'
</code></pre>
<p><strong>Update</strong></p>
<p>If you face any error for forbidden do create the service account and use that with cronjob</p>
<pre><code>apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-name
  namespace: default

---
 
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: sa-role
rules:
- apiGroups: [&quot;*&quot;]
  resources: [&quot;*&quot;]
  verbs: [&quot;list&quot;, &quot;delete&quot;]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: sa-rolebinding
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: sa-role
subjects:
- kind: ServiceAccount
  name: sa-name
  namespace: default

---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: jobs
spec:
  schedule: &quot;*/30 * * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-role
</code></pre>
