One, foreword

  • Pod: k8S minimum unit
  • Service: Pod automatically allocates different IP addresses each time it is dynamically created, so Service (i.e. registration and discovery of services) is introduced.
  • Namespace: The namespace used for isolation

2. Deployment steps

1. Create a namespace

nginx-namespace.yaml

Metadata: name: ns-test # Name_labels: name: name_labels: name: name_labels: name: name_labels: name_labels: name_labels: name_labels: name_labels: name_labels: name_labels: name_labels: name_labels: name_labels The label - the test # pod labelsCopy the code

perform

Kubectl create -f nginx-namespace. Yaml kubectl get namespaceCopy the code

2. Create pod

Instead of creating a pod directly, you create a pod through a Controller. Deployment is one of the controllers

nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ns-test
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
Copy the code

perform

Yaml # kubectl get deployment # kubectl get pods -n ns-test = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = $kubectl create -f nginx - deployment. Yaml # nginx - deployment deployment of 2 $kubectl pod full success get pods -o wide -n ns-test NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES Nginx-deployment-54f57cf6bf-57g8l 1/1 Running 0 23m 10.244.1.10 shuifeng < None > <none> nginx-deployment-54f57CF6bf-ltDF7 1/1 Running 0 23m 10.244.1.11 shuifeng <none> <none> # READY 2/2 $kubectl get deploy nginx-deployment NAME READY up-to-date AVAILABLE AGE nginx-deployment 2/2 2 2 23m # At this time access with the automatically generated IP # such as curl http://10.244.1.10Copy the code

3. Create service

vim nginx-service.yaml

apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 $ kubectl create -f nginx-service.yaml $ kubectl get svc nginx-service -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR nginx-service ClusterIP 10.108.9.49 < None > 80/TCP 11m app=nginx curl http://10.108.9.49Copy the code

Technical notes

  • If the host IP is used if access

Kubectl get SVC nginx-service -o wide kubectl get SVC nginx-service -o wide

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 8000
Copy the code
  • Kubectl create kubectl apply

When YAML changes, you can use Apply to make the changes take effect

  • This is an introduction to the command-line approach, which can be easily created using the Dashboard user interface, although yamL is still required