Connecting to Kubernetes on Digital Ocean. 5 simple steps

by Evgenii Studitskikh
2 minutes read

Prerequisites

  1. Install the required tools:
  • kubectl (Kubernetes command-line tool)
  • doctl (Digital Ocean command-line tool)

Step 1: Configure Digital Ocean Access

# Install doctl if you haven't already
brew install doctl  # For MacOS
# Or download from Digital Ocean website for other OS

# Authenticate with Digital Ocean
doctl auth init
# Enter your API token when prompted
Bash

Step 2: Connect to Your Cluster

# List all kubernetes clusters
doctl kubernetes cluster list

# Download cluster configuration
doctl kubernetes cluster kubeconfig save <cluster-name>
Bash

Step 3: Verify Connection

# Test connection by getting cluster info
kubectl cluster-info

# View all nodes
kubectl get nodes

# View all pods
kubectl get pods --all-namespaces
Bash

Step 4: Accessing Resources

Connect to a Pod:

# List pods in default namespace
kubectl get pods

# List pods in specific namespace
kubectl get pods -n <namespace>

# Connect to a specific pod
kubectl exec -it <pod-name> -- /bin/bash
# Or for a specific container in a pod:
kubectl exec -it <pod-name> -c <container-name> -- /bin/bash
Bash

View Pod Logs:

# View pod logs
kubectl logs <pod-name>

# Follow pod logs in real-time
kubectl logs -f <pod-name>
Bash

Port Forwarding:

# Forward local port to pod port
kubectl port-forward <pod-name> 8080:80
Bash

Step 5: Namespace Operations

# List all namespaces
kubectl get namespaces

# Set default namespace
kubectl config set-context --current --namespace=<namespace>
Bash

Common Troubleshooting Commands

Check Pod Status:

# Get detailed pod information
kubectl describe pod <pod-name>

# Get pod status with wide output
kubectl get pods -o wide
Bash

Check Node Status:

# Get detailed node information
kubectl describe node <node-name>

# Check node resources
kubectl top node
Bash

Check Cluster Health:

# View cluster events
kubectl get events --sort-by='.metadata.creationTimestamp'

# Check component status
kubectl get componentstatuses
Bash

Security Note:

  • Always use RBAC (Role-Based Access Control) to manage access
  • Regularly rotate your Digital Ocean API tokens
  • Be cautious when executing commands in production environments
  • Consider using network policies to restrict pod-to-pod communication

You may also like