Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use k8s in docker

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use k8s in docker, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A k8s cluster with one master and two nodes was previously installed:

[root@k8s-master chenzx] # kubectl get nodesNAME STATUS ROLES AGE VERSIONk8s-master Ready master 17h v1.11.2k8s-node1 Ready 14h v1.11.2k8s-node2 Ready 13h v1.11.2

The kubectl command is the client tool of apiserver, which can add, delete, modify and query nodes resources.

Kubectl describe

Function: describe the information of a node

[root@k8s-master chenzx] # kubectl describe node k8s-node1Name: k8s-node1Taints: # # Taints indicates whether there is a stain Allocated resources: (Total limits may be over 100percent, i.e., overcommitted.) Resource Requests Limits-cpu 100m (10%) 100m (10%) memory 50Mi (2%) 50Mi (2%) Events: kubectl cluster-info

Function: look at k8s cluster information

[root@k8s-master chenzx] # kubectl cluster-infoKubernetes master is running at https://172.16.1.100:6443KubeDNS is running at https://172.16.1.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy creates and runs an image

For example: create a nginx image

[root@k8s-master chenzx] # kubectl run nginx-deploy-image=nginx:1.14-alpine-port=80-replicas=1-dry-run=true

Description:

-- image: indicates the image name of the official website of docker

-- replicas: indicates that several Pod are created and started. If you don't write, the default is 1.

-- dry-run: indicates that it is not really executed. If you remove this choice, you will actually execute it.

[root@k8s-master chenzx] # kubectl run nginx-deploy-image=nginx:1.14-alpine-port=80-replicas=1 deployment.apps/nginx-deploy created [root@k8s-master chenzx] # kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEnginx-deploy 1 1 1 12s

Note: the AVAILABLE item should be guaranteed to be 1, indicating that it is available. When you start running kubectl get deployment, the AVAILABLE may be 0, and then you are doing a ready-to-do probe, and after a while it will become 1 selected indicating that several copies are expected, and current indicating that there are actually several copies.

[root@master] # kubectl get podNAME READY STATUS RESTARTS AGEnginx-deploy-5b595999-65rff 1 Running 1 Running 0 1m

Note: 1 pod 1 indicates that there is a container in the box and it is all ready.

RESTARTs=0 says it hasn't been restarted.

[root@master] # kubectl get pod-o wideNAME READY STATUS RESTARTS AGE IP NODEnginx-deploy-5b595999-65rff 1 Running 0 23m 10.244.2.2 node2

You can display more information through-o wide, for example, you can see that the container nginx is running on the node2 node, and the ip address of this pod is 10.244.2.2. This pod uses the address of the cni0 on the node2 node (checked with ifconfig on node2).

Visit the nginx page:

[root@node2 ~] # curl 10.244.2.2 Welcome to nginx!

You can see the words welcom to nginx.

The address 10.244.2.2 of pod can only be used within K8s, and cannot be used outside K8s.

We know that there are two types of pod clients

The first category: other pod

The second category: cluster external clients.

Delete pod [root@master ~] # kubectl get podNAME READY STATUS RESTARTS AGEnginx-deploy-5b595999- 65rff 1 Running 0 40m [root@master ~] # kubectl delete pods nginx-deploy-5b595999- 65rffpod "nginx-deploy-5b595999- 65rff" deleted [root@master ~] # kubectl get podNAME READY STATUS RESTARTS AGEnginx-deploy-5b595999-fzwvq 1 / 1 Running 0 21s [root@master ~] # kubectl get pod-o wideNAME READY STATUS RESTARTS AGE IP NODEnginx-deploy-5b595999-fzwvq 1 Running 0 3m 10.244.1.2 node1

Pod is managed by the controller. If we delete the running pod, the controller will immediately create a new pod, the new pod is the same as the original, and change the node node, as above, from node2 to node1, and the ip address has changed. This is because a certain number of pod copies must be kept online.

Publish Port

[root@master] # kubectl expose deployment nginx-deploy-name=nginx-port=80-target-port=80-protocol=TCP

-- name: the name is random. It is the service name we want to create.

The above command means to create a serivice for nginx-deploy pod under the deployment controller, while the port of service named nginx,service (--port) is 80, and the port mapped to nginx-deploy pod (--target-port) is 80.

View the details of service:

[root@master] # kubectl get svc # is serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 2dnginx ClusterIP 10.100.47.128 80/TCP 2d [root@master] # kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkubernetes ClusterIP 10. 96.0.1 443/TCP 2dnginx ClusterIP 10.100.47.128 80/TCP 2d View label

The above clusterip is service ip, but this ip can only be accessed inside the cluster, not outside the cluster. Nginx (service name) generates iptables access rules and dispatches the address and port corresponding to cluster ip to each pod associated with the tag selector. You can view the details of nginx as a service resource through kubectl describe svc nginx:

[root@master ~] # kubectl describe svc nginxName: nginxNamespace: defaultLabels: run=nginx-deploy # # this is the tag selector, and the pod corresponding to this tag can be seen in the Endpoints below. Annotations: Selector: run=nginx-deployType: ClusterIPIP: 10.100.47.128Port: 80/TCPTargetPort: 80/TCPEndpoints: 10.244.1.3 NoneEvents 80 # through this, you can see that nginx service is associated with 10.244.1.3 run=nginx-deployType 80 this podSession Affinity: NoneEvents:

You can see the label of each pod with the following command:

[root@master ~] # kubectl get pods-- show-labelsNAME READY STATUS RESTARTS AGE LABELSclient 1 Running 0 42m run=clientnginx-deploy-5b595999-2hbgh 1 Running 0 1h pod-template-hash=16151555,run=nginx-deploy View coredns

In addition, we can access it through service ip or serivce name, but the service name is parsed through the coredns of K8s. The cordns address can be viewed as follows:

[root@master] # kubectl get pods-n kube-system-o wideNAME READY STATUS RESTARTS AGE IP NODEcoredns-78fcdf6894-2l2cf 1 Running 0 2d 10.244.0.2 mastercoredns-78fcdf6894-dkkfq 1 Running 0 2d 10.244.0.3 master

Above we see that the pod addresses of coredns are 10.244.0.2 and 10.244.0.3, respectively. However, we generally do not directly use the address of pod, but use service ip to access coredns pod:

[root@master] # kubectl get svc-n kube-systemNAME TYPE CLUSTER-IP EXTERNAL-IP PORT (S) AGEkube-dns ClusterIP 10.96.0.10 53 Universe UDP

[root@master ~] # yum-y install bind-utils

[root@master ~] # dig-t A nginx @ 10.96.0.10 # means to resolve the domain name nginx address with dns10.96.0.10, A represents A record; DiG 9.9.4-RedHat-9.9.4-61.el7_5.1-t A nginx @ 10.96.0.10; global options: + cmd;; Got answer:;;-> > HEADER

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to create a custom layout using Android AS

    How to use Android AS to create a custom layout, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way. First create a title.xml

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report