How to Use the Command 'doctl kubernetes cluster' (with examples)
doctl kubernetes cluster
is a command-line interface utility for managing Kubernetes clusters on DigitalOcean. This tool allows users to effortlessly create, manage, and scale clusters while encompassing various actions such as upgrading and deleting clusters. By providing a versatile way to handle clusters, doctl
ensures developers and operations teams can have a streamlined workflow for their infrastructure.
Create a Kubernetes cluster
Code:
doctl kubernetes cluster create --count 3 --region nyc1 --size s-1vcpu-2gb --version latest cluster_name
Motivation:
Creating a Kubernetes cluster is often the first step in deploying cloud-native applications. With DigitalOcean’s CLI, provisioning a cluster is made simple and efficient. By initiating a new cluster, developers can quickly start deploying applications, testing environments, or setting up a staging area.
Explanation:
create
: Initiates the creation of a new Kubernetes cluster.--count 3
: Specifies the number of nodes, providing three nodes for high availability and resilience.--region nyc1
: Sets the geographical location for the cluster, with NYC1 being a popular location due to its low latency for many users in North America.--size s-1vcpu-2gb
: Determines the size of each node in the cluster, offering a configuration with 1 virtual CPU and 2GB of RAM for balanced performance.--version latest
: Ensures that the cluster uses the latest stable Kubernetes version, safeguarding access to recent features and updates.cluster_name
: The desired name for the new Kubernetes cluster, which aids in identification and management.
Example output:
Notice: Cluster created successfully
ID Name Region Version Auto Upgrade
abcdef123456 cluster_name nyc1 v1.19.4-do.2 false
List all Kubernetes clusters
Code:
doctl kubernetes cluster list
Motivation:
Listing all Kubernetes clusters provides a panoramic view of all active clusters in the environment, helping administrators track and manage multiple Kubernetes deployments. It is particularly useful when maintaining environments with various stages like development, testing, and production.
Explanation:
list
: Retrieves and displays a list of all existing Kubernetes clusters on the user’s DigitalOcean account.
Example output:
ID Name Region Version
abcdef123456 cluster1 nyc1 v1.19.4-do.2
ghijkl789012 cluster2 sfo2 v1.18.6-do.1
Fetch and save the kubeconfig
Code:
doctl kubernetes cluster kubeconfig save cluster_name
Motivation:
Fetching and saving the kubeconfig is crucial for developers and operations teams to interact with their Kubernetes cluster securely. It enables commands to be executed against the cluster and is a key step post-cluster creation, ensuring proper access control to manage resources.
Explanation:
kubeconfig save
: Obtains the Kubernetes config file for the specified cluster and saves it to the user’s device, typically to be accessed withkubectl
.cluster_name
: Specifies the name of the cluster for which the kubeconfig is fetched, ensuring the right configuration for the intended cluster.
Example output:
Notice: Saved kubeconfig to ~/.kube/config
Check for available upgrades
Code:
doctl kubernetes cluster get-upgrades cluster_name
Motivation:
Checking for available upgrades regularly ensures that the cluster is up-to-date with the latest Kubernetes features, security patches, and performance improvements. Staying current safeguards applications and infrastructure from vulnerabilities.
Explanation:
get-upgrades
: Checks for any available version upgrades for the given Kubernetes cluster.cluster_name
: Specifies the cluster for which the upgrade information is being checked, keeping scope limited to the relevant cluster.
Example output:
Version Slug
v1.20.0 v1.20.0-do.0
Upgrade a cluster to a new Kubernetes version
Code:
doctl kubernetes cluster upgrade cluster_name
Motivation:
Upgrading a Kubernetes cluster to a new version ensures compatibility with new software features and benefits from enhancements and bug fixes. Upgrades are a pivotal maintenance activity that helps optimize cluster performance and reliability.
Explanation:
upgrade
: Initiates the upgrade process for the specified cluster.cluster_name
: Identifies the specific cluster that needs to be upgraded, ensuring targeted action.
Example output:
Notice: Upgrade initiated for cluster `cluster_name` to version v1.20.0-do.0
Delete a cluster
Code:
doctl kubernetes cluster delete cluster_name
Motivation:
Deleting a cluster is necessary when resources are no longer needed, or when restructuring the infrastructure. This action helps in managing costs and resources efficiently by removing unused or test clusters.
Explanation:
delete
: Removes the specified Kubernetes cluster permanently.cluster_name
: The cluster targeted for deletion, ensuring the correct resource is removed.
Example output:
? Are you sure you want to delete cluster cluster_name? (y/N) y
Notice: Cluster cluster_name deleted
Conclusion
Using the doctl kubernetes cluster
command, developers and IT administrators can easily manage their Kubernetes clusters on DigitalOcean. From creation to deletion, this tool offers a suite of commands to efficiently control every aspect of Kubernetes lifecycle management, optimizing resource use and boosting deployment agility.