Sub topic 9
Deploying with kubectl
This chapter covers the deploy step itself: how the workflow tells the cluster to run the new image. It assumes you can read GitHub Actions YAML, know what a Deployment is, and already have a pipeline that pushes an image tagged with the commit SHA and a kubeconfig available to the job.
Two ways to roll out a change
A push-based pipeline has two common ways to change what runs in the cluster.
kubectl set imagechanges only the container image of an existing Deployment.kubectl apply -fsends full manifests, so the cluster state matches your files.
set image is small and quick. It suits a Deployment that already exists and whose only change is the image. apply suits changes to replicas, environment variables, probes, or new objects. Many teams use apply for everything and substitute the image tag first.
Updating the image
The workflow from the earlier chapters ends with this step:
- name: Update image
run: kubectl set image deployment/web web="$IMAGE:$GITHUB_SHA" -n shopThe argument web="$IMAGE:$GITHUB_SHA" has the form container-name=image. The first web is the container name inside the pod template, not the Deployment name. If it does not match, kubectl fails with an error saying the container was not found.
Because the tag is a new commit SHA each time, the pod template changes. Kubernetes sees the change and starts a rolling update. If you reused a tag such as latest, the template would not change and nothing would roll out.
Applying manifests
When you keep manifests in the repository, apply them with a fixed tag substituted in. A plain sed works for small cases:
- uses: actions/checkout@v4
- name: Apply manifests
run: |
sed "s|IMAGE_PLACEHOLDER|$IMAGE:$GITHUB_SHA|" k8s/deployment.yaml | kubectl apply -n shop -f -The deploy job in the earlier example had no checkout step. Any job that reads files from the repository needs actions/checkout, because a fresh runner starts empty.
Before applying, kubectl diff -f shows what would change, and kubectl apply --dry-run=server -f asks the API server to validate the manifests without saving them. Both catch mistakes early.
Common errors
| Symptom | Likely cause |
|---|---|
| `error: unable to recognize` or `connection refused` | The kubeconfig is empty, malformed, or points to the wrong server |
| `Forbidden` | The deploy identity lacks the verb or resource in its Role |
| `unable to find container named "web"` | The container name in `set image` does not match the pod template |
| Rollout never starts | The tag did not change, so the pod template is identical |
| `namespaces "shop" not found` | The namespace is missing or the `-n` flag was left out |
Always pass -n explicitly. Relying on the kubeconfig's default namespace makes a workflow depend on hidden state.
Exercises
Exercise
A workflow runs kubectl set image deployment/web web=ghcr.io/acme/shop:latest -n shop on every push. The first run works, but later runs change nothing in the cluster. Why, and what is the fix?
Show answer
The image string is identical each time, so the pod template does not change and the Deployment controller starts no rollout. Tag each build with a unique value such as the commit SHA ($GITHUB_SHA) and use that tag in the set image command.
Exercise
Your kubectl apply step fails with Forbidden: cannot patch resource "deployments". Where do you look first?
Show answer
Look at the Role bound to the deploy identity. It must list the apps API group, the deployments resource, and the patch and update verbs in the target namespace, and a RoleBinding must connect it to the ServiceAccount the kubeconfig uses.