Category: kubernetes

  • Install Minikube with docker driver on Ubuntu

    Install Minikube with docker driver on Ubuntu

    Minikube is a Kubernetes implementation for development and testing. By default, it uses Virtualbox. The newer version support docker driver, this allows you to use docker to run your minikube Kubernetes cluster. You can find documentation for minikube at

    https://minikube.sigs.k8s.io/docs/start/

    First of all, we need to install Docker, for this, run the command.

    You must be user root, if you are not root, run command “sudo su” to become user root.

    wget -qO- https://get.docker.com/ | sh
    

    Minikube only allows us to run as a normal user, so we need to add the user we will be using in group docker.

    usermod -aG docker YOUR_USER_NAME_HERE
    

    Next, install minikube with

    wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    mv minikube-linux-amd64 /usr/local/bin/minikube
    chmod 755 /usr/local/bin/minikube
    

    We have minikube and docker installed. To continue, we need to switch to a normal user, that is in docker group.

    su - YOUR_USER_NAME_HERE
    

    To delete older minikube installation, run

    minikube delete --all --purge
    

    Now create a new minikube cluster with the command

    minikube start --driver=docker 
    

    minikube docker provider

    To see status

    minikube status
    

    Now you will be able to run all normal kubectl commands. For minikube installation, instead of kubectl, you need to use command “minikube kubectl –“. It is better create an alias, so you don’t need to type “minikube kubectl” every time you need to run kubectl command.

    https://minikube.sigs.k8s.io/docs/handbook/kubectl/

    vi ~/.bashrc
    

    At end of the file, add

    alias kubectl='minikube kubectl --'
    

    Activate it with command

    source ~/.bashrc
    

    To get nodes, pods, and service, you can use commands

    kubectl get nodes
    kubectl get pods
    kubectl get service
    

    See MiniKube

  • Google Kubernetes Engine Node Pools

    In Google Kubernetes Engine, Nodes are Google Complete Engine (Virtual Machiens). Pods run inside Nodes. A Kubernetes cluster can contain multiple node pools. A node pool contain multiple nodes of same instance type.

    To create a Node Pool in Google Kubernetes Engine, run

    gcloud container node-pools create POOL_NAME \
      --cluster=CLUSER_NAME \
      --machine-type=e2-standard-2 \
      --num-nodes=1 \
      --zone=ZONE_NAME
    

    To delete a node pool, run

    gcloud container node-pools delete POOL_NAME --cluster CLUSER_NAME --zone ZONE_NAME
    

    To resize a node pool, run

    gcloud container clusters resize CLUSER_NAME --node-pool POOL_NAME \
        --num-nodes 3 --zone ZONE_NAME
    

    See Google Kubernetes Engine

  • Install Minikube in Ubuntu

    Minikube is an easy-to-use Kubernetes that run on one machine. It is used for testing purposes.

    https://github.com/kubernetes/minikube
    Install Minikube with docker driver on Ubuntu

    To install minikube, run

    wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    sudo mv minikube-linux-amd64 /usr/local/bin/minikube
    sudo chmod 755 /usr/local/bin/minikube
    

    Before you can start minikube, you need to install VirtualBox, this can be done with the command

    sudo apt install virtualbox
    

    To start minikube, run

    minikube start
    

    To stop

    minikube stop
    

    To connect to minikube node

    minikube ssh
    

    See minikube logs

    minikube logs
    

    Delete minikube

    minikube delete
    

    Mount a file system

    minikube mount $(pwd):/var/www/html/
    

    See Kubernetes

  • Deploy MySQL Server in Kubernetes using Helm

    To install MySQL server in Kubernetes, run

    helm install stable/mysql
    

    Once install is completed, you will get something like

    NOTES:
    MySQL can be accessed via port 3306 on the following DNS name from within your cluster:
    incendiary-monkey-mysql.default.svc.cluster.local
    
    To get your root password run:
    
        MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default incendiary-monkey-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo)
    
    To connect to your database:
    
    1. Run an Ubuntu pod that you can use as a client:
    
        kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il
    
    2. Install the mysql client:
    
        $ apt-get update && apt-get install mysql-client -y
    
    3. Connect using the mysql cli, then provide your password:
        $ mysql -h incendiary-monkey-mysql -p
    
    To connect to your database directly from outside the K8s cluster:
        MYSQL_HOST=127.0.0.1
        MYSQL_PORT=3306
    
        # Execute the following command to route the connection:
        kubectl port-forward svc/incendiary-monkey-mysql 3306
    
        mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
    

    To connect to this MySQL, you need to create a temporary Ubuntu server in Kubernetes as MySQL is only available inside the cluster.

    To create a server, run

    kubectl run -i --tty ubuntu --image=ubuntu:18.04 --restart=Never -- bash -il
    

    Install MySQL client inside this server with

    apt update
    apt install mariadb-client -y
    

    To connect to MySQL, use the command provided after install, in my case

    mysql -h incendiary-monkey-mysql -u root -p
    

    You can get password by running

    kubectl get secret --namespace default incendiary-monkey-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
    

    You can list packages installed using helm with

    boby@sok-01:~$ helm list
    NAME             	REVISION	UPDATED                 	STATUS  	CHART       	APP VERSION	NAMESPACE
    incendiary-monkey	1       	Tue Feb 26 22:24:13 2019	DEPLOYED	mysql-0.15.0	5.7.14     	default  
    boby@sok-01:~$ 
    

    To delete, run

    boby@sok-01:~$ helm delete incendiary-monkey
    release "incendiary-monkey" deleted
    boby@sok-01:~$ 
    
  • Install WordPress in Kubernetes

    To install WordPress in Kubernetes, you need to install helm package manager.

    Now run

    helm install stable/wordpress
    

    After the helm chart is run, you will get commands to get login for WordPress admin area.

    Here is services it create

    boby@sok-01:~$ kubectl get service
    NAME                             TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                      AGE
    kubernetes                       ClusterIP      10.39.240.1              443/TCP                      34m
    whopping-labradoodle-mariadb     ClusterIP      10.39.248.140            3306/TCP                     2m
    whopping-labradoodle-wordpress   LoadBalancer   10.39.253.160   35.193.33.14   80:32054/TCP,443:30939/TCP   2m
    boby@sok-01:~$
    

    List of all pods

    boby@sok-01:~$ kubectl get pod
    NAME                                             READY   STATUS      RESTARTS   AGE
    ubuntu                                           0/1     Completed   0          21m
    whopping-labradoodle-mariadb-0                   1/1     Running     0          3m
    whopping-labradoodle-wordpress-8bddb5fdf-2l24d   1/1     Running     0          3m
    boby@sok-01:~$ 
    

    Deployments

    boby@sok-01:~$ kubectl get deployment
    NAME                             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    whopping-labradoodle-wordpress   1         1         1            1           4m
    boby@sok-01:~$ 
    

    To scale the deployment, run

    boby@sok-01:~$ kubectl scale deployment/whopping-labradoodle-wordpress --replicas=2
    deployment.extensions/whopping-labradoodle-wordpress scaled
    boby@sok-01:~$ 
    

    See Helm Kubernetes Package Manager, Kubernetes

  • Helm Kubernetes Package Manager

    Helm is Package Manager for Kubernetes. Helm packages are called “Charts”. Charts allow you to deploy various software on Kubernetes cluster.

    https://helm.sh/

    To install helm, run

    curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh
    bash get_helm.sh
    

    If you are on Ubuntu, you can install from snap

    snap install helm --classic
    

    To update helm repos, run

    helm repo update
    

    To deploy MySQL chart to Kubernetes, run

    helm install stable/mysql
    

    Install WordPress in Kubernetes
    Deploy MySQL Server in Kubernetes using Helm

    See Kubernetes

  • Google Kubernetes Engine

    To list all Kubernetes clusters, run

    gcloud container clusters list
    

    To create a Kubernetes cluster, run

    gcloud container clusters create CLUSTER_NAME
    

    Get credential for kubectl

    gcloud container clusters get-credentials CLUSTER_NAME
    

    List all nodes (GCP instances)

    kubectl get nodes