Deploy-an-application
This guide outlines the steps to deploy an application to a Kubernetes cluster managed by Utho, along with example outputs for each command to help verify execution.
Prerequisites
To follow this guide, you must:
- Install docker, the docker command-line tool
- Install kubectl, the Kubernetes command-line tool.
1. Access the Kubernetes Cluster
Download the
kubeconfigfile from the cluster management section on Utho’s platform.Use the following command to configure your local system to use the Kubernetes cluster:
export KUBECONFIG=/path/to/kubeconfigVerify the cluster connection:
kubectl cluster-infoSample Output :
Kubernetes control plane is running at https://123.45.67.89:6443 CoreDNS is running at https://123.45.67.89:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
2. Prepare Your Application for Deployment
Ensure your application is containerized. Build and push your application image to a container registry. Example Docker commands:
docker build -t <your-image-name>:<tag> . docker push <registry-url>/<your-image-name>:<tag>Sample Output :
Sending build context to Docker daemon 10.24kB Step 1/4 : FROM nginx ---> 4bb46517cac3 Step 2/4 : COPY . /usr/share/nginx/html ---> Using cache Step 3/4 : EXPOSE 80 ---> Using cache Step 4/4 : CMD ["nginx", "-g", "daemon off;"] ---> Using cache Successfully built d9f2e4d3a356 Successfully tagged <registry-url>/<your-image-name>:<tag> The push refers to repository [<registry-url>/<your-image-name>] abc1234: Pushed latest: digest: sha256:abcdef1234567890 size: 1573
3. Create Kubernetes Deployment YAML
- Prepare a YAML file for your application’s deployment and service.
deployment.yamlapiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app-container image: <registry-url>/<your-image-name>:<tag> ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
4. Apply the YAML File
Deploy your application using the YAML file:
kubectl apply -f deployment.yamlSample Output :
deployment.apps/my-app created service/my-app-service createdVerify the deployment and service:
kubectl get deploymentsSample Output :
NAME READY UP-TO-DATE AVAILABLE AGE my-app 3/3 3 3 2mkubectl get podsSample Output :
NAME READY STATUS RESTARTS AGE my-app-658f89f469-abcde 1/1 Running 0 2m my-app-658f89f469-fghij 1/1 Running 0 2m my-app-658f89f469-klmno 1/1 Running 0 2mkubectl get servicesSample Output :
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service LoadBalancer 10.96.245.195 123.45.67.89 80:30382/TCP 2m
5. Monitor the Deployment
Check the logs of your application pods to ensure everything is working correctly:
kubectl logs -f <pod-name>Sample Output :
Starting server on port 80 Server running at http://0.0.0.0:80Monitor the health of your application:
kubectl get pods --watchSample Output :
NAME READY STATUS RESTARTS AGE my-app-658f89f469-abcde 1/1 Running 0 5m my-app-658f89f469-fghij 1/1 Running 0 5m my-app-658f89f469-klmno 1/1 Running 0 5m
6. Access Your Application
Note the external IP of the LoadBalancer service:
kubectl get service my-app-serviceSample Output :
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service LoadBalancer 10.96.245.195 123.45.67.89 80:30382/TCP 2mTest the application:
curl http://123.45.67.89Sample Output :
Welcome to My App!
7. Scale the Application (Optional)
To scale the number of replicas:
kubectl scale deployment my-app --replicas=5Sample Output :
deployment.apps/my-app scaledVerify the scaling:
kubectl get podsSample Output :
NAME READY STATUS RESTARTS AGE my-app-658f89f469-abcde 1/1 Running 0 2m my-app-658f89f469-fghij 1/1 Running 0 2m my-app-658f89f469-klmno 1/1 Running 0 2m my-app-658f89f469-pqrst 1/1 Running 0 30s my-app-658f89f469-uvwxy 1/1 Running 0 30s
8. Update the Application (Optional)
Update the application with a new container image:
kubectl set image deployment/my-app my-app-container=<new-image-url>Sample Output :
deployment.apps/my-app image updatedMonitor the rollout status:
kubectl rollout status deployment/my-appSample Output :
deployment "my-app" successfully rolled out
9. Clean Up Resources
To delete the deployment and service:
kubectl delete -f deployment.yamlSample Output :
deployment.apps "my-app" deleted service "my-app-service" deletedVerify resource deletion:
kubectl get allSample Output :
No resources found in default namespace.
By following these steps and verifying with the sample outputs, you can successfully deploy your application on Utho’s Kubernetes cluster.