An overview of Kubernetes

Working with Kubernetes is quite easy, using either a Command Line Interface (CLI) or API (RESTful). This section will describe Kubernetes control by CLI. The CLI we use in this chapter is version 1.10.2.

After you install Kubernetes master, you can run a kubectl command as follows. It shows the kubectl and Kubernetes master versions (both the API Server and CLI are v1.10.2):

$ kubectl version --short
Client Version: v1.10.2
Server Version: v1.10.2

kubectl connects the Kubernetes API server using the RESTful API. By default, it attempts to access the localhost if .kube/config is not configured, otherwise you need to specify the API server address using the --server parameter. Therefore, it is recommended to use kubectl on the API server machine for practice. 

If you use kubectl over the network, you need to consider authentication and authorization for the API server. See Chapter 7, Building Kubernetes on GCP.

kubectl is the only command for Kubernetes clusters, and it controls the Kubernetes cluster manager. Find more information at http://kubernetes.io/docs/user-guide/kubectl-overview/. Any container, or Kubernetes cluster operation, can be performed by a kubectl command. 

In addition, kubectl allows the inputting of information via either the command line's optional arguments or a file (use the -f option); it is highly recommended to use a file, because you can maintain Kubernetes configuration as code. This will be described in detail in this chapter.

Here is a typical kubectl command-line argument: 

kubectl [command] [TYPE] [NAME] [flags]

The attributes of the preceding command are as follows: 

  • command: Specifies the operation that you want to perform on one or more resources. 
  • TYPE: Specifies the resource type. Resource types are case-sensitive and you can specify the singular, plural, or abbreviated forms. 
  • NAME: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed. 
  • flags: Specifies optional flags. 

For example, if you want to launch nginx, you can use either the kubectl run command or the kubectl create -f command with the YAML file as follows:

  1. Use the run command:
$ kubectl run my-first-nginx --image=nginx "my-first-nginx"
  1. Use the create -f command with the YAML file:
$ cat nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-first-nginx
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx

//specify -f (filename)
$ kubectl create -f nginx.yaml
deployment.apps "my-first-nginx" created
  1. If you want to see the status of the Deployment, type the kubectl get command as follows: 
$ kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-first-nginx 1 1 1 1 4s
  1. If you also want the support abbreviation, type the following: 
$ kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
my-first-nginx 1 1 1 1 38s
  1. If you want to delete these resources, type the kubectl delete command as follows: 
$ kubectl delete deploy my-first-nginx
deployment.extensions "my-first-nginx" deleted
  1. The kubectl command supports many kinds of sub-commands; use the -h option to see the details, for example:
//display whole sub command options 
$ kubectl -h


//display sub command "get" options
$ kubectl get -h


//display sub command "run" options
$ kubectl run -h

This section describes how to use the kubectl command to control the Kubernetes cluster. The following recipes describe how to set up Kubernetes components: 

  • Setting up a Kubernetes cluster on macOS using minikube and Set up a Kubernetes cluster on Windows using minikube in Chapter 1, Building Your Own Kubernetes Cluster
  • Setting up a Kubernetes cluster on Linux using kubeadm in Chapter 1, Building Your Own Kubernetes Cluster
  • Setting up a Kubernetes cluster on Linux using kubespray (Ansible) in Chapter 1, Building Your Own Kubernetes Cluster
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222