Deploying to Kubernetes
Why Kubernetes
-
We can learn one way to deploy applications and re-use those skills on other projects. So rather than learning the Google, Heroku, AWS way to deploy applications we learn the Kubernetes way of deploying applications. We will be cloud agnostic.
-
Kubernetes will handle just about every deployment scenario you can think of.
-
We can test our deployments on our local machines using a tool called
kind
. -
The Kubernetes way of application deployment will be a useful skills for the next decade or more.
-
For some projects we need to deploy On Prem and more and more companies are providing Kubernetes as a way to deploy applications in house.
Setting up a local cluster with Kind
Kind Will create a tiny Kubernetes cluster in our docker environment. We've pre-installed kind
in our devcontainer
so let's create a cluster.
$ kind get clusters No kind clusters found.
Create a temporary file called config.yaml.
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 networking: # If we don't do this, then we can't connect on linux apiServerAddress: "0.0.0.0" kubeadmConfigPatchesJSON6902: - group: kubeadm.k8s.io version: v1beta3 kind: ClusterConfiguration patch: | - op: add path: /apiServer/certSANs/- value: host.docker.internal
Normally kind
is easier to use than this but because we are in a devcontainer
we have to use some special config.
kind create cluster --name nails-cluster --config=config.yaml
kind create cluster --name nails-cluster Creating cluster "nails-cluster" ... â Ensuring node image (kindest/node:v1.25.3) đŧ â Preparing nodes đĻ â Writing configuration đ â Starting control-plane đšī¸ â Installing CNI đ â Installing StorageClass đž Set kubectl context to "kind-nails-cluster" You can now use your cluster with: kubectl cluster-info --context kind-nails-cluster Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community đ
Interacting with our cluster (Windows and MacOs)
Kubernetes is administered with a command called kubectl
let's configure kubectl
so that it can access our cluster.
$ kind export kubeconfig --name nails-cluster Set kubectl context to "kind-nails-cluster"
We need to do a little trick so that kubectl
can see the cluster when running inside our devcontainer
. Run the following.
sed -i 's/0.0.0.0/host.docker.internal/g' $HOME/.kube/config
And now we can use kubectl
to see what pods
we have in our cluster.
$ kubectl get pods No resources found in default namespace.
Interacting with our cluster in Linux
Add the following to .devcontainer/docker-compose.yml
extra_hosts: - "host.docker.internal:host-gateway"
And rebuild your devcontainer
$ kind export kubeconfig --name nails-cluster Set kubectl context to "kind-nails-cluster"
We need to do a little trick so that kubectl
can see the cluster when running inside our devcontainer
. Run the following.
sed -i 's/0.0.0.0/host.docker.internal/g' $HOME/.kube/config
And now we can use kubectl
to see what pods
we have in our cluster.
$ kubectl get pods No resources found in default namespace.