Aller au contenu

Lab 01: Deploy an OKS Cluster

Learn how to deploy a Kubernetes cluster with OKS and manage your resources…

In this lab, you will learn how to deploy a Kubernetes cluster using our managed solution. You will explore and manage this cluster via the command line using oks-cli. Finally, you will deploy a simple application on Kubernetes.

In this section, you will learn how to:

  • Configure the oks-cli tool to interact with and manage your Kubernetes cluster.
  • Configure the kubectl tool to manage the cluster.
  • Deploy a simple application on the Kubernetes cluster.
  • Manage your OKS profiles.
  • Creating the Virtual Environment

To avoid conflicts with system packages, it is recommended to use a Python virtual environment to install oks-cli. Here’s how:

Terminal window
python3 -m venv .venv

This will create a .venv directory where the virtual environment will be stored.

  • Activating the Virtual Environment

Then, activate the virtual environment depending on your operating system:

  • On Linux or macOS:
Terminal window
source .venv/bin/activate
  • On Windows (cmd.exe):

    .venv\Scripts\activate.bat

  • On Windows (PowerShell):

    .venv\Scripts\Activate.ps1

Once activated, your command prompt will change to display the environment name (e.g., .venv), indicating that you are now working inside this virtual environment.

Once the virtual environment is activated, you can install oks-cli:

Terminal window
pip install https://docs.eu-west-2.oks.outscale.com/oks-cli/oks_cli-latest.zip
  • Verifying the Installation

To verify that oks-cli was installed successfully, run:

Terminal window
oks-cli version

This should display the version of the oks-cli tool.

  • Viewing all available options

To see all available options:

Terminal window
oks-cli fullhelp

To generate the autocompletion file for oks-cli, run:

Terminal window
mkdir -p $HOME/.oks_cli/completions/
_OKS_CLI_COMPLETE=bash_source oks-cli > $HOME/.oks_cli/completions/oks-cli.bash
. $HOME/.oks_cli/completions/oks-cli.bash
echo '. $HOME/.oks_cli/completions/oks-cli.bash' >> $HOME/.bashrc

This will create a file containing Bash-specific autocompletion rules.

Once the virtual environment is set up and oks-cli is installed, you can install kubectl to manage your Kubernetes cluster.

  • System prerequisites

You must use a version of kubectl that differs by no more than one minor version from your Kubernetes cluster version.

  • Installing Kubectl on Linux

To install the latest version of kubectl, use:

Terminal window
curl -LO https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl

If you want to install a specific version, replace $(curl -Ls https://dl.k8s.io/release/stable.txt) with the desired version number.

For example, to install version 1.32.0:

Terminal window
curl -LO https://dl.k8s.io/release/v1.32.0/bin/linux/amd64/kubectl
  • Making the binary executable

Once downloaded, make the binary executable:

Terminal window
chmod +x ./kubectl
  • Moving the binary to your PATH

Move the executable file to a directory in your PATH:

Terminal window
sudo mv ./kubectl /usr/local/bin/kubectl
  • Verifying the installation

Check that kubectl is installed correctly:

Terminal window
kubectl version --client
  • Other installation methods

You can also refer to the official Kubernetes documentation for additional installation methods:

https://kubernetes.io/docs/tasks/tools/install-kubectl/

To make kubectl easier to use, you can configure aliases and autocompletion.

  • Configuring an alias for kubectl

Add the following alias to your .bashrc or .zshrc:

Terminal window
alias k='kubectl'

Then reload your configuration file:

Terminal window
source ~/.bashrc # or source ~/.zshrc for Zsh

You can now use k instead of kubectl.

  • Enabling kubectl autocompletion

To enable autocompletion:

  1. Install the autocompletion script:

    Terminal window
    $ kubectl completion bash > ~/.kubectl-completion
    $ source ~/.kubectl-completion
  2. Add this line to your .bashrc or .zshrc:

    source <(kubectl completion bash)

Reload your configuration:

Terminal window
source ~/.bashrc # or source ~/.zshrc for Zsh

Autocompletion is now enabled.

Access to your Kubernetes clusters via oks-cli is organized through profiles. An OKS profile corresponds to an Outscale account associated with projects and clusters, allowing simplified resource management.

  • List all available profiles:
Terminal window
oks-cli profile list

Example output:

Profile: default Account type: ak/sk Region: eu-west-2 Endpoint: https://api.eu-west-2.oks.outscale.com/api/v2/
Profile: my-second-profile Account type: username/password Region: eu-west-2 Endpoint:https://api.eu-west-2.oks.outscale.com/api/v2/
  • Add an existing profile:
Terminal window
oks-cli profile add --profile-name "my-second-profile" --access-key <ACCESS_KEY> --secret-key <SECRET_KEY> --region eu-west-2

You can also add a profile using access and secret keys:

Terminal window
oks-cli profile add --profile-name "my-third-profile" --access-key MY_ACCESS_KEY --secret-key MY_SECRET_KEY --region eu-west-2

Profile names must be unique. If omitted, the default profile will be updated.

  • Update a profile with a new region and endpoint:
Terminal window
oks-cli profile update --profile-name my-third-profile --region cloudgouv-eu-west-1
  • List available projects:
Terminal window
oks-cli --profile my-profile project list
  • Create a new project:
Terminal window
oks-cli --profile my-profile project create --project-name my-project --description 'Lab project'
  • Get detailed project information:
Terminal window
oks-cli --profile my-profile project get --project-name my-project

Each cluster in OKS must be associated with a project.

  • List clusters:
Terminal window
oks-cli --profile my-profile cluster list --project-name my-project
  • Create a cluster:
Terminal window
oks-cli --profile my-profile cluster create --project-name my-project --cluster-name my-cluster --admin "xx.xx.xx.xx/xx" --control-plane "cp.mono.master" --version 1.30
  • --cluster-name: Cluster name (unique within the project)
  • --project-name: Target project
  • --admin: Allowed IP(s)
  • --control-plane: Control plane size
  • --version: Kubernetes version

Get your public IP:

Terminal window
curl -4 ifconfig.me
  • Check cluster status:
Terminal window
oks-cli --profile my-profile cluster list --project-name my-project
  • Get cluster details:
Terminal window
oks-cli --profile my-profile cluster get --project-name my-project --cluster-name my-cluster
  • Access the cluster:
Terminal window
oks-cli --profile my-profile cluster kubeconfig --cluster-name my-cluster --project-name my-project > kubeconfig.yaml
export KUBECONFIG=./kubeconfig.yaml

Test access:

Terminal window
kubectl get ns
Terminal window
oks-cli cluster nodepool --cluster-name my-cluster --project-name my-project create --nodepool-name my-nodepool --type "tinav6.c4r6p2" --count 3
apiVersion: oks.dev/v1beta2
kind: NodePool
metadata:
name: nodepool-01
spec:
desiredNodes: 2
nodeType: tinav7.c2r4p2
zones:
- eu-west-2a
upgradeStrategy:
maxUnavailable: 1
maxSurge: 0
autoUpgradeEnabled: true
autoUpgradeMaintenance:
durationHours: 1
startHour: 12
weekDay: Tue
autoHealing: true

Apply:

Terminal window
kubectl apply -f nodepool.yaml

Verify:

Terminal window
kubectl get nodepools
Terminal window
kubectl get nodes
kubectl describe node <node-name>

To avoid specifying the project name every time:

Terminal window
oks-cli project login <project-name>