Pod, replicast, deployment, label, service of Kubernetes

Pod:

Pod is a set of closely related containers, which share PID, IPC, Network and UTS namespace. It is the basic unit of Kubernetes scheduling. The design concept of pod is to support multiple containers to share the Network and file system in one pod. The services can be combined in a simple and efficient way of inter process communication and file sharing

Disadvantages: it does not support high concurrency and high availability. When the Pod crashes, it cannot be recovered automatically

1. Create Pod

# vi pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: demo
spec:
  containers:
  - image: httpd
    name: httpd
    imagePullPolicy: Always

# kubectl create -f pod.yaml

2. View Pod

# kubectl get pods

NAME    READY     STATUS    RESTARTS   AGE
demo    1/1       Running      0       8d

# kubectl describe pods

3. Delete Pod

# kubectl delete pod demo

Replicaset:

While inheriting all the features of Pod, replicast can use the pre created template to define and automatically control the number of replicas. It can expand and shrink the capacity of Pod by changing the number of replicas of Pod

Disadvantages: the template template cannot be modified, so the new image version cannot be published

1. Create replicast

# vi replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: demo-rc
  labels:
    app: demo-rc
spec:
  replicas: 2
  selector:
    matchLabels:
      app: demo-rc
  template:
    metadata:
      labels:
        app: demo-rc
    spec:
      containers:
      - name: httpd
        image: httpd
        imagePullPolicy: Always

# kubectl create -f replicaset.yaml

2. View replicaset

# kubectl get replicaset

NAME      READY     STATUS    RESTARTS   AGE
demo-rc    1/1       Running      0       8d

# kubectl describe replicaset

3. Delete replicaset

# kubectl delete replicaset demo-rc

Deployment

While inheriting all the features of Pod and replicast, Deployment can realize real-time rolling update of template template and has the feature of our online Application life circle

1. Create Deployment

# vi deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
  labels:
    app: httpd-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: httpd-demo
  template:
    metadata:
      labels:
        app: httpd-demo
    spec:
      containers:
      - name: httpd
        image: httpd
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        env:
        - name: VERSION
          value: "v1"

# kubectl create -f deployment.yaml

2. View Deployment

# kubectl get deployment

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
httpd-deployment   2         2         2            2           8d

# kubectl get pods -o wide

NAME                               READY     STATUS    RESTARTS   AGE       IP            NODE
httpd-deployment-956697567-8mqch   1/1       Running   0          8d        10.244.0.36   kube-master
httpd-deployment-956697567-wcbs6   1/1       Running   0          8d        10.244.0.37   kube-master

# kubectl describe deployment

3. Update deployment

With this command, you can call out the vi editor to edit the template

# kubectl edit -f deployment.yaml

Use this command to make the current editing result effective

# kubectl apply -f deployment.yaml

Check again to see that the old version of deployment has been taken off the shelf and the new version has taken effect

# kubectl get deployment

NAME                          DESIRED   CURRENT   READY     AGE
httpd-deployment-6b98d94474   0         0         0         1m
httpd-deployment-956697567    2         2         2         7m

4. Expansion and contraction

You can modify the assignment of replicas to expand and shrink the deployment

# kubectl scale deployment/httpd-deployment --replicas=1

5. Delete deployment

# kubectl delete deployment httpd-deployment

Lable

Label is a pair of key / value pairs from attach to Pod, which is used to pass user-defined attributes. For example, you may create a "tier" and "app" tag, mark the front-end Pod container with label (tier=frontend, app=myapp), and mark the background Pod with label (tier=backend, app=myapp). Then, you can use Selectors to select the Pod with a specific label, and let a specific Pod or Deployment use a Service to achieve a specific network configuration

Service

Service is the abstraction of application services. It provides load balancing and service discovery for applications through labels. The Pod IP and port list of matching labels form endpoints, and Kube proxy is responsible for balancing the service IP load to these endpoints. Each Service will automatically assign a cluster IP (a virtual address accessible only within the cluster) and DNS name. Other containers can access the Service through this address or DNS without knowing the operation of the back-end container.

1. Change NodePort restrictions

By default, the external NodePort limit range of Kubernetes is 30000-32767. If you want to use some common ports (80, 8080, 443), you need to enlarge this range

# vi /etc/kubernetes/manifests/kube-apiserver.yaml

Add the following node port configuration between -- service cluster IP range and secure port

...
- --service-cluster-ip-range=10.96.0.0/12
- --service-node-port-range=0-32767
- --insecure-port=0
....

Restart service

# systemctl restart kubelet

2. Create Service

# vi svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: demo
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 80
  selector:
    app: httpd-demo

# kubectl create -f svc.yaml

Tip: if you want to add an external access port to a Pod or deployment, the key value of the selector added by the service must correspond to it

3. View open ports

# kubectl get svc demo

NAME      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
demo      NodePort   10.100.96.157   <none>        80:80/TCP   1h

# kubectl describe service demo

Name:                     demo
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=httpd-demo
Type:                     NodePort
IP:                       10.100.96.157
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  80/TCP
Endpoints:                10.244.0.36:80,10.244.0.37:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Next: Build a private Docker warehouse in Harbor →

Added by akuji36 on Sat, 25 Dec 2021 11:05:45 +0200