There are two ways to deploy applications (nginx) in Kubernetes, which one do you prefer?

There are two ways to publish applications in k8s:

  • kubernetes-dashboard
  • kubectl command line

1. Dashboard method

Configuration and deployment: It includes the application name, container number, pod number, and service. It is very convenient. If you don’t want to set up the configuration yaml, you can deploy it very conveniently.

Click Deploy to successfully deploy the k8s application. After deployment, you can see the corresponding status and information of deployment, pod, service, etc.

  • Deployment

  • Pod

  • Service

  • monitor

2. Command line method

1. Create namespace

vim nginx-namespace.yaml
apiVersion: v1 #类型为Namespace
kind: Namespace  #类型为Namespace
metadata:
  name: ns-test  #命名空间名称
  labels:
    name: label-test  #pod标签

implement

#创建
kubectl create -f nginx-namespace.yaml
#查询
kubectl get namespace

2. Create pod

Generally, pods are not created directly, but are created through the controller. Deployment is one of the controllers

vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ns-test
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

implement

#创建
kubectl create -f nginx-deployment.yaml
#查询。查询时需要等待一会,此时会下载镜像需要时间
kubectl get deployment  -n ns-test
#或
kubectl get pods -n ns-test

You can see "replicas: 3", so there are 3 pods and each pod has only one container. All start normally.

Next, let’s see how to access and view the accessed port:

kubectl get pods -o wide -n ns-test   #-o wide 展开的意思

At this time, it is already accessible, which is to access the port through the virtual IP.

curl 10.244.1.43

curl 10.244.1.44

curl 10.244.1.45

As shown below, as long as the machines within the cluster can be accessed directly through IP

Here comes the problem. With so many virtual IPs, they will be regenerated every time the pod is rebuilt. So what should we do? ? ?

Look at the servcie below

3. Create service

vim nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: ns-test
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

implement

kubectl apply -f nginx-service.yaml
kubectl get svc nginx-service -o wide  -n ns-test

You can see that there is a cluster-ip. Through this port + port, you can load nginx of the previous three nodes.

Of course, cluster-ip can only be accessed by machines within the cluster. Combined with nginx or domain name, load balancing access can be provided to the outside world.

It can be seen that although cluster-ip has the load balancing function, it still cannot access the applications deployed by k8s from the external network. So how can the applications deployed by k8s be directly accessed from the external network?

Set the service to nodeport mode, as follows:

apiVersion: v1
kind: Service
metadata:
  namespace: ns-test
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - nodePort: 30000
  	protocol: TCP
    port: 80
    targetPort: 80
  type: NodePort

Among them, type: NodePort. If you want to specify the port, you can also add nodePort: 30000. In this way, the application deployed by k8s can be accessed through the host ip+nodePort.

implement

kubectl apply -f nginx-service.yaml
kubectl get svc nginx-service -o wide  -n ns-test

You can see that I am using the ip+nodePort of my host and can directly access nginx.

3. Supplementary knowledge

1、Service

There are four types of Kubernetes services: ClusterIP, NodePort, LoadBalancer, and ExternalName. The type attribute in the service spec determines how the service is exposed to the network.

  • Services of type ClusterIP will be exposed inside the cluster and assigned a cluster IP address. Pods can access the service through this IP address. ClusterIP type services are suitable for services that need to be accessed within the cluster, such as database services.
  • A service of type NodePort will expose a port on each node and be assigned a cluster IP address. External clients can access the service through <NodeIP>:<NodePort>. NodePort type services are suitable for services that need to be accessed outside the cluster, such as web services.
  • Services of type LoadBalancer will use a load balancer outside the cluster to expose the service. External clients can access the service through the load balancer's IP address. LoadBalancer type services are suitable for services that need to be accessed outside the cluster and require high availability. Generally, LoadBalancer is provided for a fee.
  • Services of type ExternalName will point the service to an external host or domain name. Pods can access the service through this host or domain name. ExternalName type services are suitable for scenarios that require access to external services.


If the article is helpful to you, please follow + like it! !

Guess you like

Origin blog.csdn.net/citywu123/article/details/132868884