Kubernetes应用快速入门

@admin  July 11, 2020

查看节点详细信息

查看node节点

[root@k8s-master ~]# kubectl describe node k8s-node-1
Name:               k8s-node-1
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=k8s-node-1
                    kubernetes.io/os=linux
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"8e:68:0b:16:80:fd"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.3.6
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Thu, 09 Jul 2020 11:58:53 +0800
Taints:             <none> # 是否有污点
Unschedulable:      false # 是否不可以调度
Lease:
  HolderIdentity:  k8s-node-1
  AcquireTime:     <unset>
  RenewTime:       Sat, 11 Jul 2020 09:35:43 +0800
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Thu, 09 Jul 2020 11:59:50 +0800   Thu, 09 Jul 2020 11:59:50 +0800   FlannelIsUp                  Flannel is running on this node
  MemoryPressure       False   Sat, 11 Jul 2020 09:32:45 +0800   Thu, 09 Jul 2020 11:58:54 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Sat, 11 Jul 2020 09:32:45 +0800   Thu, 09 Jul 2020 11:58:54 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Sat, 11 Jul 2020 09:32:45 +0800   Thu, 09 Jul 2020 11:58:54 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Sat, 11 Jul 2020 09:32:45 +0800   Thu, 09 Jul 2020 11:59:54 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.3.6
  Hostname:    k8s-node-1
Capacity:
  cpu:                2
  ephemeral-storage:  51354116Ki
  hugepages-2Mi:      0
  memory:             2048612Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  47327953228
  hugepages-2Mi:      0
  memory:             1946212Ki
  pods:               110
System Info:
  Machine ID:                 f043c8e64c244fd1a710c92393cfc6eb
  System UUID:                6E444D56-9F8F-2907-4FE1-31ED22560FAE
  Boot ID:                    0bd3876d-5748-4c79-a13d-c323c2e0700b
  Kernel Version:             3.10.0-693.el7.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://19.3.12
  Kubelet Version:            v1.18.5
  Kube-Proxy Version:         v1.18.5
PodCIDR:                      10.244.1.0/24
PodCIDRs:                     10.244.1.0/24
Non-terminated Pods:          (2 in total)
  Namespace                   Name                           CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                           ------------  ----------  ---------------  -------------  ---
  kube-system                 kube-flannel-ds-amd64-64j2m    100m (5%)     100m (5%)   50Mi (2%)        50Mi (2%)      45h
  kube-system                 kube-proxy-hdh9n               0 (0%)        0 (0%)      0 (0%)           0 (0%)         45h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests   Limits
  --------           --------   ------
  cpu                100m (5%)  100m (5%)
  memory             50Mi (2%)  50Mi (2%)
  ephemeral-storage  0 (0%)     0 (0%)
  hugepages-2Mi      0 (0%)     0 (0%)
Events:              <none>

查看master节点

[root@k8s-master ~]# kubectl describe node k8s-master
Name:               k8s-master
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=k8s-master
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/master=
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"32:5b:8b:0d:06:a2"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.3.5
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Thu, 09 Jul 2020 11:32:39 +0800
Taints:             node-role.kubernetes.io/master:NoSchedule # master是有污点的,不可以调度。
Unschedulable:      false
Lease:
  HolderIdentity:  k8s-master
  AcquireTime:     <unset>
  RenewTime:       Sat, 11 Jul 2020 09:39:06 +0800
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Thu, 09 Jul 2020 11:44:34 +0800   Thu, 09 Jul 2020 11:44:34 +0800   FlannelIsUp                  Flannel is running on this node
  MemoryPressure       False   Sat, 11 Jul 2020 09:37:55 +0800   Thu, 09 Jul 2020 11:32:33 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Sat, 11 Jul 2020 09:37:55 +0800   Thu, 09 Jul 2020 11:32:33 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Sat, 11 Jul 2020 09:37:55 +0800   Thu, 09 Jul 2020 11:32:33 +0800   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Sat, 11 Jul 2020 09:37:55 +0800   Thu, 09 Jul 2020 11:44:41 +0800   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.3.5
  Hostname:    k8s-master
Capacity:
  cpu:                2
  ephemeral-storage:  51354116Ki
  hugepages-2Mi:      0
  memory:             2048612Ki
  pods:               110
Allocatable:
  cpu:                2
  ephemeral-storage:  47327953228
  hugepages-2Mi:      0
  memory:             1946212Ki
  pods:               110
System Info:
  Machine ID:                 ad96724432424945be1afc247362540d
  System UUID:                E12E4D56-6255-0FE8-731C-3A330593CDAE
  Boot ID:                    1f2ed42c-2bfc-45b5-863b-ce6aa5c52780
  Kernel Version:             3.10.0-693.el7.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://19.3.12
  Kubelet Version:            v1.18.5
  Kube-Proxy Version:         v1.18.5
PodCIDR:                      10.244.0.0/24
PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (8 in total)
  Namespace                   Name                                  CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                                  ------------  ----------  ---------------  -------------  ---
  kube-system                 coredns-66bff467f8-jwrl5              100m (5%)     0 (0%)      70Mi (3%)        170Mi (8%)     46h
  kube-system                 coredns-66bff467f8-s4jbd              100m (5%)     0 (0%)      70Mi (3%)        170Mi (8%)     46h
  kube-system                 etcd-k8s-master                       0 (0%)        0 (0%)      0 (0%)           0 (0%)         46h
  kube-system                 kube-apiserver-k8s-master             250m (12%)    0 (0%)      0 (0%)           0 (0%)         46h
  kube-system                 kube-controller-manager-k8s-master    200m (10%)    0 (0%)      0 (0%)           0 (0%)         46h
  kube-system                 kube-flannel-ds-amd64-sfqlj           100m (5%)     100m (5%)   50Mi (2%)        50Mi (2%)      45h
  kube-system                 kube-proxy-nx7nr                      0 (0%)        0 (0%)      0 (0%)           0 (0%)         46h
  kube-system                 kube-scheduler-k8s-master             100m (5%)     0 (0%)      0 (0%)           0 (0%)         46h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                850m (42%)  100m (5%)
  memory             190Mi (9%)  390Mi (20%)
  ephemeral-storage  0 (0%)      0 (0%)
  hugepages-2Mi      0 (0%)      0 (0%)
Events:              <none>

查看k8s版本

[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:47:41Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:39:24Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

查看集群状态

[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.3.5:6443
KubeDNS is running at https://192.168.3.5:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

创建应用

在k8s中,创建一个应用可以使用kubectl run命令来操作。

[root@k8s-master ~]# kubectl run --help
Create and run a particular image in a pod.

Examples:
  # Start a nginx pod.
  kubectl run nginx --image=nginx

  # Start a replicated instance of nginx
  kubectl run nginx --image=nginx --replicas=5 # 设置5个副本,1.18.0以后废弃
  
  # Start a hazelcast pod and let the container expose port 5701.
  # 启动单个hazelcast实例,让容器暴露端口5701。
  kubectl run hazelcast --image=hazelcast/hazelcast --port=5701
  
  # Start a hazelcast pod and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the
container.
  kubectl run hazelcast --image=hazelcast/hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
  
  # Start a hazelcast pod and set labels "app=hazelcast" and "env=prod" in the container.
  kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"
  
  # Dry run. Print the corresponding API objects without creating them.
  kubectl run nginx --image=nginx --dry-run=client # --dry-run 单跑模式,只运行。
  
  # Start a nginx pod, but overload the spec with a partial set of values parsed from JSON.
  # 启动单个nginx实例,但使用从JSON解析的部分值集合重载部署规范。
  kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
  
  # Start a busybox pod and keep it in the foreground, don't restart it if it exits.
  # 启动busybox的pod并将其保留在前台,如果退出则不要重启.
  kubectl run -i -t busybox --image=busybox --restart=Never
  
  # Start the nginx pod using the default command, but use custom arguments (arg1 .. argN) for that command.
  # 使用default命令启动nginx容器,但为该命令使用自定义参数(arg1 .. argN)
  kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
  
  # Start the nginx pod using a different command and custom arguments.
  # 使用不同的命令和自定义参数启动nginx容器。
  kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

  # Start the cron job to compute π to 2000 places and print it out every 5 minutes.
  # 启动cron作业以计算π到2000个位置并每5分钟打印一次。
  kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
  ....

dry-run

kubectl  run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 --dry-run=true
Flag --replicas has been deprecated, has no effect and will be removed in the future.
W0711 09:58:29.670673   24920 helpers.go:549] --dry-run=true is deprecated (boolean value) and can be replaced with --dry-run=client.
pod/nginx-deploy created (dry run)

创建

[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1
Flag --replicas has been deprecated, has no effect and will be removed in the future. # 1.18已经不支持replicas了
pod/nginx-deploy created

[root@k8s-master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
nginx                           1/1     Running   0          119s

创建service

[root@k8s-master ~]# kubectl expose deployment nginx-deploy --name=nginx  --port=80 --target-port=80 --protocol=TCP
service/nginx exposed

查看pod

[root@k8s-master ~]# kubectl get pod -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE         NOMINATED NODE   READINESS GATES
nginx                           1/1     Running   0          3m25s   10.244.3.2   k8s-node-1   <none>           <none>
nginx-deploy-784f598b6c-bh647   1/1     Running   0          21s     10.244.2.4   k8s-node-2   <none>           <none>
[root@k8s-master ~]# kubectl  run client --image=busybox  -it --restart=Never
If you don't see a command prompt, try pressing enter.
/ # 

PS:持续更新中


添加新评论