AI DevOps Tools

Working with Kubernetes and EKS

Introduction

In this article, we will cover various Kubernetes commands and configurations, as well as their usage within Amazon EKS (Elastic Kubernetes Service). Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. EKS is a managed Kubernetes service provided by AWS, making it easier to run Kubernetes without needing to maintain the control plane yourself.

Basic Kubernetes Commands

Deploying an Application

To deploy an application in Kubernetes, you typically start with a deployment YAML file (my-deployment.yaml). This file defines the desired state of your application, including the number of replicas, the container image to use, and other configurations.

sudo kubectl create -f my-deployment.yaml

This command creates the deployment as specified in the YAML file. The -f flag specifies the file to use.

sudo kubectl rollout status deployment/my-deployment

This command checks the status of the deployment rollout, ensuring that all replicas are up and running.

sudo kubectl rollout history deployment/my-deployment

This command shows the revision history of the deployment, which is useful for tracking changes and debugging issues.

sudo kubectl delete deployment my-deployment

This command deletes the specified deployment, removing all associated resources.

Recording Deployment Details

To keep a history of the changes made to a deployment, you can use the --record flag.

sudo kubectl create -f my-deployment.yaml --record

This command creates the deployment and records the current command in the resource’s annotation.

Applying Changes to a Deployment

sudo kubectl apply -f my-deployment.yaml

This command updates an existing deployment to match the configuration in the YAML file. If the deployment does not exist, it will be created.

sudo kubectl describe deployment

This command provides detailed information about the deployment, including the current state of the replicas, events, and more.

sudo kubectl get nodes

This command lists all nodes in the Kubernetes cluster, providing their status and other details.

Updating a Deployment

sudo kubectl set image deployment/my-deployment nginx-container=nginx:1.12

This command updates the container image used in the deployment to a new version.

Rolling Back a Deployment

sudo kubectl rollout undo deployment/my-deployment

This command rolls back the deployment to a previous revision.

Viewing Pods and Deployments

sudo kubectl get deployment

This command lists all deployments in the current namespace.

sudo kubectl get pods

This command lists all pods in the current namespace.

EKS Cluster Configuration

Basic Cluster Configuration

The following is a basic EKS cluster configuration YAML (cluster.yaml):

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: eks-cluster
  region: us-east-2

nodeGroups:
  - name: ng-1
    instanceType: t2.small
    desiredCapacity: 3
    ssh:
      publicKeyName: eks-key
  - name: ng-mixed
    minSize: 3
    maxSize: 5
    instancesDistribution:
      maxPrice: 0.2
      instanceTypes: ["t2.small", "t3.small"]
      onDemandBaseCapacity: 0
      onDemandPercentageAboveBaseCapacity: 50
    ssh:
      publicKeyName: eks-key

Advanced Cluster Configuration

A more advanced EKS cluster configuration might look like this:

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: eks-cluster
  region: us-east-2

nodeGroups:
  - name: scale-east2a
    instanceType: t2.small
    desiredCapacity: 1
    maxSize: 10
    availabilityZones: ["us-east-2a"]
    iam:
      withAddonPolicies:
        autoScaler: true
    labels:
      nodegroup-type: stateful-east2a
      instance-type: onDemand
    ssh:
      publicKeyName: eks-key
  - name: scale-east2b
    instanceType: t2.small
    desiredCapacity: 1
    maxSize: 10
    availabilityZones: ["us-east-2b"]
    iam:
      withAddonPolicies:
        autoScaler: true
    labels:
      nodegroup-type: stateful-east2b
      instance-type: onDemand
    ssh:
      publicKeyName: eks-key
  - name: scale-spot
    desiredCapacity: 1
    maxSize: 10
    instancesDistribution:
      instanceTypes: ["t2.small", "t3.small"]
      onDemandBaseCapacity: 0
      onDemandPercentageAboveBaseCapacity: 0
    availabilityZones: ["us-east-2a", "us-east-2b"]
    iam:
      withAddonPolicies:
        autoScaler: true
    labels:
      nodegroup-type: stateless-workload
      instance-type: spot
    ssh:
      publicKeyName: eks-key
availabilityZones: ["us-east-2a", "us-east-2b"]

cloudWatch:
  clusterLogging:
    enableTypes: ["api", "audit", "authenticator"]

Deployment Configuration Example

Here is an example deployment YAML file (deployment.yaml) for Kubernetes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-autoscaler
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        service: nginx
        app: nginx
    spec:
      containers:
      - image: nginx
        name: test-autoscaler
        resources:
          limits:
            cpu: 300m
            memory: 512Mi
          requests:
            cpu: 300m
            memory: 512Mi
      nodeSelector:
        instance-type: spot

Role-Based Access Control (RBAC) Configuration

RBAC in Kubernetes allows you to control who can access your cluster and what actions they can perform. Here is an example Role YAML file (role.yaml):

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  namespace: production
  name: prod-viewer-role
rules:
- apiGroups: ["", "extensions", "apps"]
  resources: ["*"]  # can be further limited, e.g. ["deployments", "replicasets", "pods"]
  verbs: ["get", "list", "watch"]

And here is the corresponding RoleBinding YAML file (rolebinding.yaml):

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: prod-viewer-binding
  namespace: production
subjects:
- kind: User
  name: testproduser
  apiGroup: ""
roleRef:
  kind: Role
  name: testproduser-role
  apiGroup: ""

Conclusion

In this guide, we have covered basic Kubernetes commands for managing deployments, updating images, rolling back deployments, and viewing resources. We also explored how to configure an EKS cluster and set up Role-Based Access Control (RBAC) to secure your cluster. By understanding these concepts and commands, you will be well-equipped to manage and maintain Kubernetes clusters in a production environment.

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply