Linux学习-kubernetes学习之kubectl命令(二)

kubernetes版本1.22.3

问题1:

Error from server: Get "https://192.168.88.103:10250/containerLogs/default/client/client": dial tcp 192.168.88.103:10250: connect: no route to host

解决方法

#经查看因为192.168.88.103上面firewalld服务处于开启状态,需关闭,并设置为开机不启动
[root@node2 ~]# systemctl stop firewalld
[root@node2 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
kubectl 命令
#查看节点的详细信息
[root@master bak]# kubectl describe node node1
Name:               node1
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=node1
                    kubernetes.io/os=linux
Annotations:        flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"72:c5:c4:bc:31:ff"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.88.102
                    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:  Wed, 03 Nov 2021 10:45:38 -0400
Taints:             <none>
Unschedulable:      false
Lease:
  HolderIdentity:  node1
  AcquireTime:     <unset>
  RenewTime:       Wed, 03 Nov 2021 11:12:26 -0400
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Wed, 03 Nov 2021 10:45:46 -0400   Wed, 03 Nov 2021 10:45:46 -0400   FlannelIsUp                  Flannel is running on this node
  MemoryPressure       False   Wed, 03 Nov 2021 11:11:04 -0400   Wed, 03 Nov 2021 10:45:38 -0400   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Wed, 03 Nov 2021 11:11:04 -0400   Wed, 03 Nov 2021 10:45:38 -0400   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Wed, 03 Nov 2021 11:11:04 -0400   Wed, 03 Nov 2021 10:45:38 -0400   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Wed, 03 Nov 2021 11:11:04 -0400   Wed, 03 Nov 2021 10:45:48 -0400   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.88.102
  Hostname:    node1
Capacity:
  cpu:                4
  ephemeral-storage:  38815216Ki
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             7992344Ki
  pods:               110
Allocatable:
  cpu:                4
  ephemeral-storage:  35772103007
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             7889944Ki
  pods:               110
System Info:
  Machine ID:                 53c69389ae554f4f83ed809bd51dddca
  System UUID:                BF9C4D56-0FE6-5083-0565-16792C78EDF6
  Boot ID:                    d4fd0a95-b88f-42af-8822-696d59b63542
  Kernel Version:             3.10.0-862.el7.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://20.10.10
  Kubelet Version:            v1.22.3
  Kube-Proxy Version:         v1.22.3
PodCIDR:                      192.168.2.0/24
PodCIDRs:                     192.168.2.0/24
Non-terminated Pods:          (2 in total)
  Namespace                   Name                     CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age
  ---------                   ----                     ------------  ----------  ---------------  -------------  ---
  kube-system                 kube-flannel-ds-dfvsb    100m (2%)     100m (2%)   50Mi (0%)        50Mi (0%)      35m
  kube-system                 kube-proxy-gn9nj         0 (0%)        0 (0%)      0 (0%)           0 (0%)         35m
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests   Limits
  --------           --------   ------
  cpu                100m (2%)  100m (2%)
  memory             50Mi (0%)  50Mi (0%)
  ephemeral-storage  0 (0%)     0 (0%)
  hugepages-1Gi      0 (0%)     0 (0%)
  hugepages-2Mi      0 (0%)     0 (0%)
Events:
  Type    Reason                   Age                From        Message
  ----    ------                   ----               ----        -------
  Normal  Starting                 26m                kube-proxy  
  Normal  Starting                 34m                kube-proxy  
  Normal  NodeHasSufficientMemory  35m                kubelet     Node node1 status is now: NodeHasSufficientMemory
  Normal  NodeHasNoDiskPressure    35m                kubelet     Node node1 status is now: NodeHasNoDiskPressure
  Normal  NodeHasSufficientPID     35m                kubelet     Node node1 status is now: NodeHasSufficientPID
  Normal  NodeAllocatableEnforced  35m                kubelet     Updated Node Allocatable limit across pods
  Normal  Starting                 35m                kubelet     Starting kubelet.
  Normal  NodeReady                34m                kubelet     Node node1 status is now: NodeReady
  Normal  Starting                 26m                kubelet     Starting kubelet.
  Normal  NodeHasNoDiskPressure    26m (x2 over 26m)  kubelet     Node node1 status is now: NodeHasNoDiskPressure
  Normal  NodeHasSufficientPID     26m (x2 over 26m)  kubelet     Node node1 status is now: NodeHasSufficientPID
  Normal  NodeAllocatableEnforced  26m                kubelet     Updated Node Allocatable limit across pods
  Normal  NodeHasSufficientMemory  26m (x2 over 26m)  kubelet     Node node1 status is now: NodeHasSufficientMemory
  Normal  NodeReady                26m                kubelet     Node node1 status is now: NodeReady
#version查看kubectl版本信息
[root@master bak]# kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"clean", BuildDate:"2021-10-27T18:41:28Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"clean", BuildDate:"2021-10-27T18:35:25Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"}
#cluster-info获得集群信息
[root@master bak]# kubectl cluster-info
Kubernetes control plane is running at https://192.168.88.101:6443
CoreDNS is running at https://192.168.88.101:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
#部署nginx应用
#--dry-run程序并没有真正执行
[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --dry-run=true
W1103 11:25:31.051890  100284 helpers.go:569] --dry-run=true is deprecated (boolean value) and can be replaced with --dry-run=client.
pod/nginx-deploy created (dry run)
#创建deployment ,--replicas表示使用两个副本部署nginx
[root@master ~]# kubectl create deployment nginx-deploy --image=nginx:1.14-alpine --replicas=2
deployment.apps/nginx-deploy created
#通过service暴露资源
[root@master ~]# kubectl expose deployment nginx-deploy  --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
#获取pod列表
[root@master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS      AGE
nginx                           1/1     Running   1 (74m ago)   14h
nginx-deploy-7c948bcff4-jclqg   1/1     Running   0             26m
nginx-deploy-7c948bcff4-mczxp   1/1     Running   0             26m
#获取service列表,service创建后通过SELECTOR(标签选择器)关联至后端的Pod
[root@master ~]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   24h   <none>
nginx        ClusterIP   10.105.125.19   <none>        80/TCP    28m   app=nginx-deploy
#通过curl访问service的地址,可以查看nginx服务的页面
[root@master ~]# curl 10.105.125.19
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#查看kube的服务dns
[root@master ~]# kubectl get service -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   23h
#创建一个client deployment
[root@master ~]# kubectl create deployment client --image=busybox --replicas=1
deployment.apps/client created
#运行名为client的deployment
[root@master ~]# kubectl run client -it --image=busybox --restart=Never
If you don't see a command prompt, try pressing enter.
/ # cat /etc/resolv.conf
#DNS地址
nameserver 10.96.0.10
#kubernetes本地的Pod资源,default指的名称空间的名字,search表示搜索域
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
#在pod中可以直接解析service
/ # wget -O - -q nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#解析nginx的A记录的service地址
[root@master ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.10 

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.7 <<>> -t A nginx.default.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31486
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;nginx.default.svc.cluster.local. IN	A

;; ANSWER SECTION:
nginx.default.svc.cluster.local. 30 IN	A	10.105.125.19

;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Sun Nov 07 09:54:21 EST 2021
;; MSG SIZE  rcvd: 107

[root@master ~]# kubectl get services -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   23h   <none>
nginx        ClusterIP   10.105.125.19   <none>        80/TCP    24m   app=nginx-deploy
#查看service的信息
[root@master ~]# kubectl describe service nginx
Name:              nginx
Namespace:         default
Labels:            app=nginx-deploy
Annotations:       <none>
#选择器
Selector:          app=nginx-deploy
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.105.125.19
IPs:               10.105.125.19
Port:              <unset>  80/TCP
TargetPort:        80/TCP
#对应的Pod的地址
Endpoints:         10.244.1.4:80,10.244.3.3:80
Session Affinity:  None
Events:            <none>
[root@master ~]# kubectl get pods --show-labels
NAME                            READY   STATUS             RESTARTS       AGE   LABELS
client                          1/1     Running            0              13m   run=client
client-7c75c79d7c-h9994         0/1     CrashLoopBackOff   8 (58s ago)    16m   app=client,pod-template-hash=7c75c79d7c
nginx                           1/1     Running            1 (103m ago)   14h   run=nginx
nginx-deploy-7c948bcff4-jclqg   1/1     Running            0              55m   app=nginx-deploy,pod-template-hash=7c948bcff4
nginx-deploy-7c948bcff4-mczxp   1/1     Running            0              55m   app=nginx-deploy,pod-template-hash=7c948bcff4
#动态扩展副本数量(增加/减少)
[root@master ~]# kubectl scale --replicas=3 deployment nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master ~]# kubectl get pods
NAME                            READY   STATUS             RESTARTS       AGE
client                          1/1     Running            0              23m
client-7c75c79d7c-h9994         0/1     CrashLoopBackOff   10 (33s ago)   26m
nginx                           1/1     Running            1 (113m ago)   14h
nginx-deploy-7c948bcff4-jclqg   1/1     Running            0              65m
nginx-deploy-7c948bcff4-lhq2q   1/1     Running            0              7s
nginx-deploy-7c948bcff4-mczxp   1/1     Running            0              65m
[root@master ~]# kubectl scale --replicas=2 deployment nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master ~]# kubectl get pods
NAME                            READY   STATUS             RESTARTS       AGE
client                          1/1     Running            0              24m
client-7c75c79d7c-h9994         0/1     CrashLoopBackOff   10 (84s ago)   27m
nginx                           1/1     Running            1 (114m ago)   14h
nginx-deploy-7c948bcff4-jclqg   1/1     Running            0              66m
nginx-deploy-7c948bcff4-mczxp   1/1     Running            0              66m
#修改service类型让其它机器可以访问Pod资源
[root@master ~]# kubectl edit service nginx
#修改  type: NodePort
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        24h
nginx        NodePort    10.105.125.19   <none>        80:31526/TCP   49m
kubectl 命令补充
#创建一个deployment名称为app,使用ikubernetes/myapp:v1镜像,replicas=2表示建立两个副本
[root@master ~]# kubectl create deployment myapp --image=ikubernetes/myapp:v1 --replicas=2
[root@master ~]# kubectl get pods 
NAME                            READY   STATUS    RESTARTS   AGE
client                          1/1     Running   0          11m
myapp-7b595df7fc-5nccs          1/1     Running   1          13h
myapp-7b595df7fc-gt7tb          1/1     Running   0          5m39s
#启动一个busybox来测试
/ # wget -O - -q myapp
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
#通过set image--修改镜像来升级程序
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.extensions/myapp image updated
#启动一个busybox来测试升级情况
/ # wget -O - -q myapp
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
#查看资源升级情况
[root@k8s-master k8syaml]# kubectl rollout status deployment myapp
deployment "myapp" successfully rolled out
#查看资源的历史版本
[root@k8s-master k8syaml]# kubectl rollout history deployment myapp
deployment.apps/myapp 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

#回滚镜像,不指定版本,回滚至上一个版本
[root@master ~]# kubectl rollout undo deployment myapp
deployment.extensions/myapp rolled back
#回滚镜像至指定版本--to-revision
[root@k8s-master k8syaml]# kubectl rollout undo deployment web --to-revision=2
#弹性伸缩
[root@k8s-master k8syaml]# kubectl scale deployment web --replicas=3
deployment.apps/web scaled


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值