In the process of learning K8S, it is inevitable to do experiments by ourselves. Usually k8S requires three machines, one as master node and the other two as node nodes. When I did not know there was minikube, three virtual machines were opened each time, which has high requirements on computers and is not convenient. It is also more difficult and time-consuming to set up a cluster of three machines. It is recommended that you use Minikube to do experiments when learning K8S. Istio, Knative and Kubevela can all run on Minikube.

Note: Both K8S and Minikube assume docker is already installed

Minikube installation

Install kubectl

Install dependent software first

apt update
apt install -y apt-transport-https
Copy the code

Download and add keys

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
Copy the code

$curl = $curl = $curl = $curl = $curl = $curl = $curl = $curl = $curl = $curl = $curl

Add APT source, where Kubernetes-Xenial xenial and their own operating system kernel to keep consistent

cat > /etc/apt/sources.list.d/kubernetes.list << EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
Copy the code

Start the installation

apt update
apt install kubectl
Copy the code

Download minikube

Go to Github and search for minikube. The first one is to enter the Release page and find the version you need to download. The download link is as follows (this is version 1.16.0) :

Github.com/kubernetes/…

Once downloaded, unpack, add permissions, and move to the bin directory

chmod u+x minikube
mv minikube /usr/local/bin/
Copy the code

The configuration has been downloaded here

Start and download the image

minikube start --image-mirror-country=cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers - kubernetes - version = v1.18.3 - vm - driver = noneCopy the code
  • minikube startStart a minikube
  • --image-mirror-country=cnSpecify the mirror source country,cnSaid the Chinese
  • --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containersSpecify the mirror source, set here is Ali cloud
  • - kubernetes - version = v1.18.3Minikube v1.16.0 supports K8S v1.18.3
  • --vm-driver=noneDo not create a VM
  • --memory=2048mbSpecifies how much memory to use

After executing the command, it takes a long time for Minikube to download the image required by K8S, kubelet and kubeadm. We specify the source of the image, otherwise we cannot download the image of GCR warehouse in China.

When Done! Is displayed on the terminal kubectl is now configured to use … The next time you run minikube start, it will be very quick

Note: To specify –vm-driver= None, conntrack must be installed in the operating system. Otherwise, error: requires Conntrack

apt install conntrack
Copy the code

Relevant command

The common commands of minikube are as follows:

The command instructions
minikube -h See the minikube command help
minikube start Start the
minikube stop Stop. After starting, you can run this command to stop it
minikube delete delete
minikube dashboard Start Dashboard. In K8S, you need to install Dashboard by helm. In Minikube, you can use a command

After the minikube dashboard command is used to start the dashboard, it is not accessible outside the local machine. You need to use kubectl proxy to set the proxy as follows:

Kubectl proxy --port=8001 --address=' server IP' --accept-hosts='^.*' &Copy the code

Access to the dashboard: http:// server IP: 8001 / API/v1 / namespaces/kubernetes – dashboard/services/HTTP: kubernetes – dashboard: # / proxy / / the overview? namespace=default

kubectl

Kubectl is the most commonly used command to manage K8s, so it is important to know how to use this command before you experience minikube. There are many subcommands in this command

get

View all services and resources

The command instructions
kubectl get ns View all namespaces
kubectl get all To view all services in the specified namespace, pass-nSpecify a namespace, such askubectl get all -n kube-system, the following can be passed-nTo specify the namespace
kubectl get po View all pods in the specified namespace. Default is the default namespace-wTo listen for changes, usually used to see if a new POD has been successfully started--show-labelsView the labels of each POD
kubectl get svc Look at the service
kubectl get cm Look at the config map

All view commands can directly specify the specific service name (resource name), and specify -o to output details, as follows:

Kubectl get po-n kube-system XXX -o yaml kubectl get po-n kube-system XXX -o yamlCopy the code

apply

Used to apply YAML files

kubectl apply -f xxx.yml
Copy the code

You can define a variety of resources in a YAML file, such as namespace, configmap, service, Deployment, replicaset, etc. You can simply execute the apply subcommand for such a YAML file instead of creating each one yourself

create

Used to create services and resources

The command instructions
kubectl create ns xxx Created toxxxThe namespace of the name
kubectl create cm xxx –from-file=yyy Create ConfigMap,--from-file=yyyIf this parameter is specified, yyy can be a file or a directory. If yyy is a directory, all configuration files in the directory are loaded. The file name is key, and the file content is the value
kubectl create secret tls xxx Create Secret, as inkubectl create secret tls xxx --key xxx.key --cert xxx.crtWhere TLS is the type of secret and XXX is the name of secret

edit

Use to edit existing services and resources

The command instructions
kubectl edit cm xxx Edit the ConfigMap in XXX

delete

Delete existing services and resources

The command instructions
kubectl delete ns xxx Delete the XXX namespace
kubectl delete po xxx Delete pod XXX, which can be passed-nSpecifying a namespace
kubectl delete po –all Delete all pods in the specified namespace, others such as SVC, Deployment, RS, etc. –all delete all
kubectl delete svc xxx Remove the service
kubectl delete rs xxx Delete the ReplicaSet
kubectl delete -f xxx.yml Use the YAML file to delete files. This method is most commonly used. When using K8S, make sure that the YAML file is well backed up

Delete subcommands specify what type of service to delete, and then specify the name and namespace

describe

View detailed information, often used when pod startup fails to see the cause, such as image download failure

kubectl describe po -n kube-system xxx
Copy the code

logs

You can view the pod logs or the logs of a specified container in the POD to find specific problems, such as network failure and program output logs

kubectl logs -n kube-system xxx
kubectl logs -n kube-system xxx -f
Copy the code

To specify a container, use -c to specify the name of the container in pod. C stands for container

kubectl logs -n kube-system xxx -f -c yyy
Copy the code

First experience deploying Nginx

Now it’s time to implement an Nginx deployment and experience the very long YAML

Here is the YAML file for nginx, which requires a Deployment and a Service

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v1
spec:
  replicas: 1
  selector:
    matchLabels: This attribute is required in the new version to match the metadata.labels defined in the template below
      name: nginx
      version: v1
  template:
    metadata:
      Name =nginx, version=v1
      labels:
        name: nginx
        version: v1
    spec:
      containers:
      - name: nginx # container name
        image: nginx # image
        imagePullPolicy: IfNotPresent IfNotPresent indicates that a new image is pulled from the repository if it is not locally available
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  The type of Service
  type: ClusterIP
  Select pod by selector
  selector:
    name: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
Copy the code

The above yamL file defines a Deployment and a Service, where Deployment is responsible for deploying pod. Spec. template is the definition of pod, and spec.replicas requires several Nginx pods to be deployed

Service spec. Selector is used to select pod (s) from Deployment Deployment. Name =nginx (s) in the pod tag is selected. However, the effect of load balancing is not good, and it will be replaced by other schemes in the future

By default, the YAML file is installed in the default namespace. You can specify a namespace under metadata in the YAML file

kubectl apply -f xxx.yml
Copy the code

You can then view it using the kubectl command

kubectl get po
kubectl get deployment
kubectl get svc
Copy the code

Kubectl get SVC: curl nginx: kubectl get SVC: kubectl get

Kubectl get SVC NAME TYPE cluster-ip external-ip PORT(S) AGE Kubernetes ClusterIP 10.96.0.1 < None > 443/TCP 171D nginx ClusterIP 10.108.164.139 < None > 80/TCP 9m40s curl 10.108.164.139Copy the code

Curl displays the welcome page of nginx, which is successfully deployed

Delete nginx deployment:

kubectl delete -f xxx.yml
Copy the code

Once again, all yamL files are saved in K8S. Both startup and deletion can be done through YAML files

Docker – compose. Yml transformation

K8s’s yamL file is very long, usually own a lot of docker-comemess. yml file, if you can have a tool to docker-comemess. yml into K8S yamL is very convenient, K8S provides such a tool, called kompose

Install kompose

From the making of kompose download, link is https://github.com/kubernetes/kompose/releases, download package for your operating system, decompression after an executable file, it will be moved to the bin directory

mv kompose-linux-amd64 /usr/local/bin/
Copy the code

To do the transformation, prepare a simple docker-comemage.yml

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 80: 80
Copy the code

Go to the directory where the docker-comemess. yml file resides and perform the conversion

kompose-linux-amd64 convert
Copy the code

After successful conversion, you will get some YAML files of K8S, which are much faster than your own handwriting