---
title: "How to Set a Proxy in a Kubernetes Pod? Secret and CronJob"
description: "Give a pod its proxy through an https_proxy variable read from a Secret. Cluster addresses in no_proxy, CIDR support, CronJob and an exit IP check."
url: https://proxynet.io/blog/kubernetes-egress-proxy
date: 2026-12-02
author: "Acar Diveroli"
category: "Integration"
lang: en
---

# How to Set a Proxy in a Kubernetes Pod? Secret and CronJob

A proxy in a Kubernetes pod is set with the `HTTPS_PROXY`, `HTTP_PROXY` and `NO_PROXY` environment variables passed to the container. Kubernetes does not use these variables itself; the application inside the container reads them. The trap is the `NO_PROXY` list. If cluster addresses are missing from it, service-to-service requests also go to the proxy and fail. This post covers keeping the password in a Secret, the `NO_PROXY` list, a CronJob setup and checking the exit IP.

> **Note: Short answer**
>
> Put the proxy URL in a Secret and pass it to the container as `HTTPS_PROXY` with `env[].valueFrom.secretKeyRef`. Add `.svc`, `.cluster.local`, the service and pod ranges and the API server IP to `NO_PROXY`.

## The proxy URL in a Secret

The proxy URL carries a username and password, so it does not go into the manifest as plain text. A plain `value:` shows up in the Git repository, in `kubectl describe pod` output and in terminal logs. For a variable read from a Secret, describe shows only the source: which key of which Secret.

Create the Secret with one command. If the password contains `@`, write it as `%40` inside the URL:

```bash
kubectl create secret generic proxynet-proxy \
  --from-literal=url='http://user:pass%40123@pr.proxynet.io:8000'
```

The single quotes matter. According to the Kubernetes docs, characters such as `$`, `\`, `*`, `=` and `!` are interpreted by the shell if left unquoted. `kubectl describe secret proxynet-proxy` does not show the value; it prints only the byte count of the `url` key.

A Secret is not a vault. By default the value is stored unencrypted in etcd. The base64 in Secret output is an encoding, not encryption. The docs note that anyone who can create a pod in a namespace can also read the Secrets in that namespace. Narrow access with RBAC and keep the proxy Secret only in the namespace of the workloads that need it.

## Environment variables in a Deployment

The container binds a key from the Secret to an environment variable with `secretKeyRef`. The Deployment below gives the same URL to both the uppercase and lowercase names:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: data-job
spec:
  replicas: 1
  selector:
    matchLabels:
      app: data-job
  template:
    metadata:
      labels:
        app: data-job
    spec:
      containers:
        - name: app
          image: registry.example.com/data-job:1.0
          env:
            - name: HTTPS_PROXY
              valueFrom:
                secretKeyRef:
                  name: proxynet-proxy
                  key: url
            - name: HTTP_PROXY
              valueFrom:
                secretKeyRef:
                  name: proxynet-proxy
                  key: url
            # Lowercase copies: derived from the earlier variable
            - name: https_proxy
              value: "$(HTTPS_PROXY)"
            - name: http_proxy
              value: "$(HTTP_PROXY)"
            - name: NO_PROXY
              value: "localhost,127.0.0.1,.svc,.cluster.local,kubernetes.default,10.96.0.0/12,10.244.0.0/16,10.96.0.1,169.254.169.254"
            - name: no_proxy
              value: "$(NO_PROXY)"
```

The `$(HTTPS_PROXY)` syntax copies the value of a variable defined earlier in the list. Order matters. If the copy comes before its source, Kubernetes does not resolve it and the variable keeps the literal text `$(HTTPS_PROXY)`. This way the password enters the manifest only once.

You need both cases because Linux treats variable names as case-sensitive. curl reads `http_proxy` only in lowercase, and its docs say the lowercase form wins when both are set. Go's `httpproxy` package reads the uppercase name first. Give both the same value and precedence stops mattering. If the application does not read the variable in its language at all, that is a different problem; the symptoms are in [why the HTTP_PROXY variable is not working](/blog/http-proxy-env-not-applied).

Updating the Secret does not change a running pod. According to the docs, a Secret consumed as an environment variable is not refreshed until the container restarts. After changing the password, run `kubectl rollout restart deployment/data-job`.

## Cluster addresses in NO_PROXY

`NO_PROXY` tells the client which destinations skip the proxy. An application in a pod often talks to both external sites and services inside the cluster. If the list is incomplete, a request to the database or another service also goes to the external proxy. The proxy does not know that internal name and returns an error.

| List entry | What it covers |
|---|---|
| `.svc`, `.cluster.local` | Services addressed by full name (`api.default.svc.cluster.local`) |
| `kubernetes.default`, short service names | Names without a suffix (`api`, `redis`) |
| Service range | Virtual IPs of services; kubeadm default `10.96.0.0/12` |
| Pod range | Pod IPs; depends on the network plugin |
| API server IP | The `KUBERNETES_SERVICE_HOST` value |
| `169.254.169.254` | The cloud provider's metadata service |

Short names are a separate trap. When code calls the `redis` service in the same namespace as `http://redis:6379`, `NO_PROXY` sees only the name `redis`. The `.svc` suffix does not match it. Write the full service name in code or add the short name to the list.

Take the ranges from your own cluster. The `10.244.0.0/16` in the manifest above is only an example; the pod range varies from install to install. Inside a pod, `echo $KUBERNETES_SERVICE_HOST` shows the API server IP.

## CIDR support depends on the client

Not every client understands a CIDR entry such as `10.96.0.0/12`. We tried the same `NO_PROXY` list in five clients. A ✓ means the request stayed off the proxy:

| Client | CIDR (`10.96.0.10`) | Suffix (`api.default.svc`) | Short name (`api`) |
|---|---|---|---|
| curl 8.22.0 | ✓ | ✓ | ✗ |
| Python Requests 2.34.2 | ✓ | ✓ | ✗ |
| Python urllib (3.13) | ✗ | ✓ | ✗ |
| Go 1.26 net/http | ✓ | ✓ | ✗ |
| Node 24 fetch | ✗ | ✓ | ✗ |

urllib and Node sent the `10.96.0.10` target to the proxy. In Python, Requests matched the same list correctly while the standard library did not. The short name `api` and `kubernetes.default` matched the `.svc` suffix in no client. The full name stayed off the proxy in all five.

The takeaway is simple. Do not rely on CIDR; list critical IPs one by one. The API server IP comes first. Clients written in Go connect to the API server through this IP. A tool built on Node or urllib may send the same request to the proxy.

## Image pulls use a separate proxy

The variables you give a pod affect only the application inside the container. The image is pulled by the container runtime on the node (containerd or CRI-O), not by the container. The runtime reads its proxy from its own process environment. That environment usually lives in the systemd service definition on the node.

This split shortens debugging. If a pod is stuck in `ImagePullBackOff`, the `HTTPS_PROXY` in the manifest cannot help, because the container has not started yet. The problem is on the node and belongs to the cluster admin. The daemon-side logic is the same in Docker; details are in [Docker proxy settings](/blog/docker-proxy-settings).

## Scheduled jobs with a CronJob

A daily data pull runs as a CronJob. The proxy comes from the same `secretKeyRef` block. If the job only talks to an external site, a short `NO_PROXY` list is enough:

```yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: price-fetch
spec:
  schedule: "0 6 * * *"
  timeZone: "Europe/Istanbul"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      backoffLimit: 2
      template:
        spec:
          restartPolicy: OnFailure
          containers:
            - name: job
              image: registry.example.com/price-fetch:1.0
              env:
                - name: HTTPS_PROXY
                  valueFrom:
                    secretKeyRef:
                      name: proxynet-proxy
                      key: url
                - name: https_proxy
                  value: "$(HTTPS_PROXY)"
                - name: NO_PROXY
                  value: "localhost,127.0.0.1,.svc,.cluster.local"
                - name: no_proxy
                  value: "$(NO_PROXY)"
```

Three fields protect the job. `timeZone` ties the schedule to Türkiye time. Without it, the schedule follows the local time of kube-controller-manager. `concurrencyPolicy: Forbid` does not start a new run before the previous one ends, so two jobs never hit the same target at once. `backoffLimit` caps failed attempts.

To test the job without waiting, create a Job from the CronJob by hand: `kubectl create job --from=cronjob/price-fetch price-fetch-test`. Follow the target site's terms and its robots.txt, and keep the request rate low. If you want a different exit IP on every run, [Residential Proxy](https://proxynet.io/residential-proxy) rotation does that at the gateway.

## Checking the exit IP

You know the setup works from the exit IP, not from the absence of errors. Send a request to an IP echo service from inside the pod:

```bash
kubectl exec deploy/data-job -- curl -s https://api.ipify.org
```

If the image has no curl, use the application's language:

```bash
kubectl exec deploy/data-job -- python -c \
  "import urllib.request; print(urllib.request.urlopen('https://api.ipify.org').read().decode())"
```

If the address is the node's public IP, the application is not reading the variable. If it is the proxy's IP, the setup is correct. The response formats of different services are in [how to check your proxy's exit IP](/blog/proxy-egress-ip-check-api).

`kubectl exec deploy/data-job -- env` prints the variables together with the password. Use it for diagnosis, but do not paste the output into a log or a chat.

## Troubleshooting

The symptom usually tells you which layer the problem is in:

| Symptom | Likely cause |
|---|---|
| `ImagePullBackOff` | The container runtime on the node does not know the proxy |
| `CreateContainerConfigError` | The Secret or the `url` key is missing |
| Service-to-service request returns 502 or times out | The target is not in `NO_PROXY` |
| Exit IP is the node's IP | The application does not read the variable |
| `407 Proxy Authentication Required` | Wrong credentials or an unencoded `@` |
| Password changed, old password still in use | The pod was not restarted |

According to the docs, if a required Secret is missing, the pod's containers do not start. If the Secret exists but the key is missing, the pod fails at startup. In both cases, check the events section of `kubectl describe pod`.

## How we tested

We tested `NO_PROXY` matching on December 2, 2026, on Windows 11. We used curl 8.22.0, Python 3.13.17 with Requests 2.34.2 and urllib, Go 1.26.10 net/http and Node 24.22.2 fetch (with `--use-env-proxy`). We ran a local proxy on `127.0.0.1` that logged every incoming request. Targets were unresolvable names imitating cluster names and IPs such as `10.96.0.10`. A logged request counted as proxied; a request ending in a DNS error or timeout counted as direct. We parsed the manifests offline with the kubectl 1.37.1 client but did not apply them to a real cluster. The Secret, CronJob and image pull behavior is based on the Kubernetes docs.

## The bottom line

Keep the proxy URL in a Secret, define both letter cases together and list critical IPs one by one in `NO_PROXY`. Above all, confirm the setup with the exit IP from inside the pod. If the target site uses an IP allowlist, [ISP Proxy](https://proxynet.io/static-isp-residential-proxy) gives you a fixed address.

## Sources

- [Kubernetes docs, Distribute Credentials Securely Using Secrets: kubernetes.io](https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/)
- [Kubernetes docs, CronJob: kubernetes.io](https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/)
- [Go `httpproxy` package, Config and FromEnvironment: pkg.go.dev](https://pkg.go.dev/golang.org/x/net/http/httpproxy)
- [curl man page, ENVIRONMENT section: curl.se](https://curl.se/docs/manpage.html)

## Frequently asked questions

### How do you give a pod a proxy in Kubernetes?

With environment variables. Add `HTTPS_PROXY` and `HTTP_PROXY` to the `env` list in the container definition and read the value from a Secret with `secretKeyRef`. Kubernetes does not use these variables itself; the application inside the container reads them. So if the application's library ignores environment variables, you also have to set the proxy in code. Check the result with the exit IP from inside the pod.

### What should go in the NO_PROXY list?

`localhost`, `127.0.0.1`, `.svc`, `.cluster.local`, the service and pod IP ranges, the API server IP and, in the cloud, `169.254.169.254`. If you use short service names, add them too, because the `.svc` suffix does not match a name like `redis`. Ranges differ between clusters, so take the values from your own setup. Give the lowercase `no_proxy` variable the same list.

### Does CIDR notation in NO_PROXY work everywhere?

No. In our test, curl, Requests and Go matched the `10.96.0.0/12` entry. Python's standard `urllib` module and Node fetch did not, and the request went to the proxy. Test with whatever library your application uses. If you are unsure, list critical IPs such as the API server one by one. All five clients matched a single-IP entry.

### Does a pod pick up the new proxy password when the Secret changes?

No. A Secret consumed as an environment variable is not updated until the container restarts. The variable is written once, when the container starts. After changing the password, refresh the pods with `kubectl rollout restart deployment/<name>`. In a CronJob, every run starts a new pod, so the next run picks up the new value on its own.

### Does HTTPS_PROXY in a pod affect image pulls?

No. The container runtime on the node pulls the image, not the application in the pod. The runtime reads its proxy from its own process environment, which usually lives in the systemd service definition on the node. If a pod is stuck in `ImagePullBackOff`, check the runtime settings on the node, not the manifest. That is usually a job for the cluster admin.

### Does the proxy password show up in kubectl describe output?

Not if you use a Secret. `kubectl describe pod` shows only the source for a variable that comes from a Secret, and `kubectl describe secret` prints only the byte count. If you write the value as a plain `value:`, it appears in clear text in describe output. The `env` output printed through `kubectl exec` shows the password in every case.
