takeaway

While previous articles have covered the concept of K8S and simple application deployment using k8S, today I’ll cover the basics of using K8S. Since k8S resources can be described using YAML files, use of YAML is also mentioned.

YAML description

YAML Syntax YAML is a recursive abbreviation of “YAML Ain’t a Markup Language” (YAML is not a Markup Language). The syntax is similar to other high-level languages, and data forms such as lists, hash tables, and scalars can be expressed simply. The use of whitespace indentation and a lot of appearance – dependent features are particularly suitable for expressing or editing data structures, various configuration files, etc. YAML configuration files have a.yml suffix, such as runoob.yml.

1. Basic grammar

The number of Spaces allowed for indentation does not matter, as long as elements of the same level are left aligned. ‘#’ indicates a comment

2. Data types

Object: a collection of key-value pairs, also called mapping/hashes/dictionary array: a group of values arranged in order, also called sequence/list scalars: single, non-divisible values

3, objects,

Object key-value pairs use the colon structure for key: value, followed by a space.

You can also use key:{key1: value1, key2: value2… }

You can also use indentation: [note: Indentation must be two Spaces]

key:
  key1: value1
  key2: value2
Copy the code

4, arrays,

Lines beginning with – form an array:

Containers:
- name: A
  age: 20
- name: B
Copy the code

5, scalar

A scalar is a basic, non-divisible value, including: string, Boolean, integer, floating point, Null, time, date

boolean: 
    - TRUE  # true, true
    - FALSE  #false, false will do
float:
    - 3.14
    - 6.8523015 e+5  Scientific notation can be used
int:
    - 123
    - 0b1010_0111_0100_1010_1110    # binary representation
null:
    nodeName: 'node'
    parent: ~  # use ~ to indicate null
string:
    - 'Hello world'  You can use double or single quotation marks around special characters
    - newline
      newline2    The # string can be broken into multiple lines, and each line is converted to a space
date:
    - 2018-02-17    The date must be in ISO 8601 format, yyyY-MM-DD
datetime: 
    -  2018-02-17T15:02:31+08:00    The time is in ISO 8601 format, with T concatenation between time and date, and + for time zone at the end
Copy the code

6, references,

& anchor points and * aliases, which can be used to reference:

person: &person
  hand: true
  leg: true
tom:
  < < : *person
Copy the code

The above amounts to the following:

person: &person
  hand: true
  leg: true
tom:
  hand: true
  leg: true
Copy the code

Add and remove check Namespace

1. Create a Namespace

Namespace names follow the following res:

[a-z0-9]([-a-z0-9]*[a-z0-9])?
Copy the code

(1) Create a vm on the CLI

kubectl create namespace liusy-01
Copy the code

(2) Use yamL file to create

Create a yaml file named liusy-02-ns.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: liusy-02
Copy the code

Then run the following command to create it

Yaml kubectl apply-f liusy-02-ns.yaml kubectl apply-f liusy-02-ns.yaml kubectl apply-f liusy-02-ns.yamlCopy the code

2. View the Namespace

kubectl get ns
or
kubectl get namespace
Copy the code

3. Delete the Namespace

(1) Delete using pure command line

kubectl delete ns/liusy-01
Copy the code

(2) Use YAML file deletion

If a Namespace is created using a YAML file, you can also delete the Namespace using a YAML file

kubectl delete -f liusy-02-ns.yaml
Copy the code

Add and remove check Pod

Create a Pod

Create a YAML called test_pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-test
  namespace: liusy   # specify the namespace
  labels:
    name: pod-test
spec:
  containers:
  - name: pod-test
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
Copy the code

Then use the following command to create:

kubectl create -f test_pod.yaml
Copy the code

2. Check the Pod

If Pod is specified in a namespace, the namespace must be specified otherwise it cannot be viewed

View the Pod for the default namespace

kubectl get pods
Copy the code

View pods for a specific Namespace, such as pods with a Namespace of Liusy

Kubectl get Pods -n liusy or kubectl get Pods --namespace liusyCopy the code

View all pods:

kubectl get pods -A
Copy the code

Kubectl supports multiple viewing methods:

Such as:

Displays more information about Pod, status, IP, which node it is running on, and so on

kubectl get pods pod-name -o=wide
Copy the code

Display pod details in YAML format

kubectl get pods podname -o yaml
Copy the code

Displays Pod information with custom column names

kubectl get pods podname -o=custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion
Copy the code

File-based custom column name output

kubectl get pods podname -o=custom-columns-file=template.txt

vim template.txt
NAME             RSRC
.metadata.name   .metadata.resourceVersion
Copy the code

3. Delete the Pod

(1) Delete using pure command

kubectl delete pods/liusy
Copy the code

If Pod is in a namespace, you also need to specify the -n argument

(2) Use YAML files

If a Pod is created using a YAMl file, it can also be deleted using a YAML file

kubectl delete -f test_pod.yaml
Copy the code

(3) Delete all pods

kubectl delete pods --all
Copy the code

4. Run the container command

(1) To execute a pod command, use the first POD container to execute it by default

kubectl exec <pod-name> command
Copy the code

(2) Specify a container of pod to execute the command

kubectl exec <pod-name> -c <container-name> date
Copy the code

(3) Enter the container

kubectl exec -it <pod-name> -c <container-name> /bin/bash
Copy the code

Add and remove check the Controller

1. Create Replication Controller

(1) Create the Deployment controller using command

kubectl run nginx --image=nginx:latest --imagePullPolicy=IfNotPresent --replicas=3
Copy the code

(2) Create ReplicationController using the YAML file

Create a YAML file

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  replicas: 1
  selector:
    name: nginx
  template:
    metadata:
      name: nginx
      labels:
        name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
Copy the code

Then use the following command to create:

kubectl create -f nginx.yaml
Copy the code

2. View the controller

If you are looking at the Deployment controller, use the following command

kubectl get deployment
Copy the code

To view Replication Controller, run the following command

Kubectl get RC or kubectl get ReplicationControllerCopy the code

If the namespace is specified, you need to add the -n parameter or –namespace to specify the namespace

3, delete,

(1) Delete using pure command

kubectl delete rc/nginx
Copy the code

(2) If it is created using yamL file, it can also be deleted using YAML file

kubectl delete -f nginx.yaml
Copy the code

Add and remove check Service

1. Create Service

(1) Create a vm using the CLI

kubectl expose rc rc-name --type=ClusterIP --target-port=80 --port=80
Copy the code

(2) Use yamL file to create

Create a YAML file

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30002
  selector:
    name: nginx
Copy the code

Then use the following command to create:

kubectl create -f nginx-service.yaml
Copy the code

2, view

Kubectl get SVC or kubectl get ServiceCopy the code

If a namespace is specified, the -n or –namespace argument specifies the namespace

3, delete,

(1) Delete pure command

kubectl delete svc/nginx
Copy the code

(2) If you use yamL file to create, you can also use YAML file to delete

kubectl delete -f nginx-service.yaml
Copy the code

general

1. View details about a resource

Kubectl Describe Resource type Resource name or Kubectl Describe resource type/resource nameCopy the code

2. View the logs of a resource

Kubectl logs Resource type Resource name or Kubectl logs Resource type/resource nameCopy the code

To trace the container logs, run the tail -f command

kubectl logs -f <pod-name> -c <container-name>
Copy the code

3. Delete the resource that has a Label

Kubectl delete resource type -l name=<label-name>Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

I’m Liusy, a programmer who likes to work out.

Welcome to pay attention to wechat public number [Liusy01], exchange Java technology and fitness, get more dry goods, get Java advanced dry goods, get the latest factory interview materials, become a Java god together.

Here we go. Keep an eye on it.