Java双端队列Deque使用详解

Deque是一个双端队列接口,继承自Queue接口,Deque的实现类是LinkedList、ArrayDeque、LinkedBlockingDeque,其中LinkedList是最常用的。

关于Queue的介绍可以看上一篇文章:Java队列Queue使用详解

Deque有三种用途:

普通队列(一端进另一端出):
Queue queue = new LinkedList()或Deque deque = new LinkedList()
双端队列(两端都可进出)
Deque deque = new LinkedList()
堆栈
Deque deque = new LinkedList()

注意:Java堆栈Stack类已经过时,Java官方推荐使用Deque替代Stack使用。Deque堆栈操作方法:push()、pop()、peek()。

Deque是一个线性collection,支持在两端插入和移除元素。名称 deque 是“double ended queue(双端队列)”的缩写,通常读为“deck”。大多数 Deque 实现对于它们能够包含的元素数没有固定限制,但此接口既支持有容量限制的双端队列,也支持没有固定大小限制的双端队列。

此接口定义在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式:一种形式在操作失败时抛出异常,另一种形式返回一个特殊值(null 或 false,具体取决于操作)。插入操作的后一种形式是专为使用有容量限制的 Deque 实现设计的;在大多数实现中,插入操作不能失败。

下表总结了上述 12 种方法:
第一个元素 (头部) 最后一个元素 (尾部)
抛出异常 特殊值 抛出异常 特殊值
插入 addFirst(e) offerFirst(e) addLast(e) offerLast(e)
删除 removeFirst() pollFirst() removeLast() pollLast()
检查 getFirst() peekFirst() getLast() peekLast()

Deque接口扩展(继承)了 Queue 接口。在将双端队列用作队列时,将得到 FIFO(先进先出)行为。将元素添加到双端队列的末尾,从双端队列的开头移除元素。从 Queue 接口继承的方法完全等效于 Deque 方法,如下表所示:
Queue方法 等效Deque方法
add add(e) addLast(e)
offer(e) offerLast(e)
remove() removeFirst()
poll() pollFirst()
element() getFirst()
peek() peekFirst()

双端队列也可用作 LIFO(后进先出)堆栈。应优先使用此接口而不是遗留 Stack 类。在将双端队列用作堆栈时,元素被推入双端队列的开头并从双端队列开头弹出。堆栈方法完全等效于 Deque 方法,如下表所示:
堆栈方法 等效Deque方法
push(e) addFirst(e)
pop() removeFirst()
peek() peekFirst()

k8s污点Taints与容忍详解Toleration

污点状态:

  • NoSchedule:如果 Node 上带有污点 effect 为 NoSchedule,而 Node 上不带相应容忍,Kubernetes 就不会调度 Pod 到这台 Node 上。
  • PreferNoShedule:如果 Node 上带有污点 effect 为 PreferNoShedule,这时候 Kubernetes 会努力不要调度这个 Pod 到这个 Node 上。
  • NoExecute:如果 Node 上带有污点 effect 为 NoExecute,这个已经在 Node 上运行的 Pod 会从 Node 上驱逐掉。没有运行在 Node 的 Pod 不能被调度到这个 Node 上。

污点值:

  • 污点 value 的值可以为 NoSchedule、PreferNoSchedule 或 NoExecute

污点属性:

  • 污点是k8s集群的pod中的一种属性
  • 污点属性分为以上三种

污点组成:

  • key、value 及一个 effect 三个元素
<key>=<value>:<effect>

1、设置单污点及单容忍度

kubectl taint nodes master1 node-role.kubernetes.io/master=:NoSchedule     

kubectl taint node node1 key1=value1:NoSchedule       # 设置value值

kubectl taint node master1 key2=:PreferNoSchedule     # 不设置value值

2、设置多污点及多容忍度

kubectl taint nodes node1 key1=value1:NoSchedule  

kubectl taint nodes node1 key1=value1:NoExecute  

kubectl taint nodes node1 key2=value2:NoSchedule

3、查看pod中的污点状态

[root@master1 ~]# kubectl describe nodes master1
Name:               master1
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=master1
                    kubernetes.io/os=linux
                    node-role.kubernetes.io/master=
Annotations:        flannel.alpha.coreos.com/backend-data: {"VNI":1,"VtepMAC":"36:51:e1:31:e5:9e"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.200.3
                    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, 13 Jan 2021 06:04:10 -0500
Taints:             node-role.kubernetes.io/master:NoSchedule              # 污点状态及容忍度
Unschedulable:      false
Lease:
  HolderIdentity:  master1
  AcquireTime:     <unset>
  RenewTime:       Thu, 14 Jan 2021 01:14:07 -0500
Conditions:
  Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----                 ------  -----------------                 ------------------                ------                       -------
  NetworkUnavailable   False   Wed, 13 Jan 2021 06:12:43 -0500   Wed, 13 Jan 2021 06:12:43 -0500   FlannelIsUp                  Flannel is running on this node
  MemoryPressure       False   Thu, 14 Jan 2021 01:11:17 -0500   Wed, 13 Jan 2021 06:50:32 -0500   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure         False   Thu, 14 Jan 2021 01:11:17 -0500   Wed, 13 Jan 2021 06:50:32 -0500   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure          False   Thu, 14 Jan 2021 01:11:17 -0500   Wed, 13 Jan 2021 06:50:32 -0500   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready                True    Thu, 14 Jan 2021 01:11:17 -0500   Wed, 13 Jan 2021 06:50:32 -0500   KubeletReady                 kubelet is posting ready status
Addresses:
  InternalIP:  192.168.200.3
  Hostname:    master1
Capacity:
  cpu:                4
  ephemeral-storage:  17394Mi
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             2897500Ki
  pods:               110
Allocatable:
  cpu:                4
  ephemeral-storage:  16415037823
  hugepages-1Gi:      0
  hugepages-2Mi:      0
  memory:             2795100Ki
  pods:               110
System Info:
  Machine ID:                 feb4edfea2404d3c8ad028ca4593bb32
  System UUID:                C6F44D56-0F24-6114-23E7-8DF6CD4E4CFE
  Boot ID:                    afcc0ef6-d767-4b97-9a7b-9b2500757f2e
  Kernel Version:             3.10.0-862.el7.x86_64
  OS Image:                   CentOS Linux 7 (Core)
  Operating System:           linux
  Architecture:               amd64
  Container Runtime Version:  docker://19.3.0
  Kubelet Version:            v1.18.2
  Kube-Proxy Version:         v1.18.2
PodCIDR:                      10.244.0.0/24
PodCIDRs:                     10.244.0.0/24
Non-terminated Pods:          (6 in total)
  Namespace                   Name                               CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                   ----                               ------------  ----------  ---------------  -------------  ---
  kube-system                 etcd-master1                       0 (0%)        0 (0%)      0 (0%)           0 (0%)         19h
  kube-system                 kube-apiserver-master1             250m (6%)     0 (0%)      0 (0%)           0 (0%)         19h
  kube-system                 kube-controller-manager-master1    200m (5%)     0 (0%)      0 (0%)           0 (0%)         19h
  kube-system                 kube-flannel-ds-wzf7w              100m (2%)     100m (2%)   50Mi (1%)        50Mi (1%)      19h
  kube-system                 kube-proxy-7h5sb                   0 (0%)        0 (0%)      0 (0%)           0 (0%)         19h
  kube-system                 kube-scheduler-master1             100m (2%)     0 (0%)      0 (0%)           0 (0%)         19h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests    Limits
  --------           --------    ------
  cpu                650m (16%)  100m (2%)
  memory             50Mi (1%)   50Mi (1%)
  ephemeral-storage  0 (0%)      0 (0%)
  hugepages-1Gi      0 (0%)      0 (0%)
  hugepages-2Mi      0 (0%)      0 (0%)
Events:              <none>

4、过滤出有几台节点存在污和容忍度是什么

[root@master1 ~]# kubectl describe node master1 | grep Taints
Taints:             node-role.kubernetes.io/master:NoSchedule

[root@master1 ~]# kubectl describe node master2 | grep Taints
Taints:             node-role.kubernetes.io/master:NoSchedule

[root@master1 ~]# kubectl describe node master3 | grep Taints
Taints:             node-role.kubernetes.io/master:NoSchedule

5、有无污点返回的结果

Taints:             node-role.kubernetes.io/master:NoSchedule     # 有污点

Taints:             <none>                                        # 没污点

6、删除污点使其pod能够调度和使用

kubectl taint node master1 node-role.kubernetes.io/master:NoSchedule-

kubectl taint nodes master1 key:NoSchedule-

shell三种引号的区别

Linux Shell中有三种引号,分别为双引号(” “)、单引号(‘ ‘)以及反引号(` `)。

其中双引号对字符串中出现的$、”、`和\进行替换;单引号不进行替换,将字符串中所有字符作为普通字符输出,而反引号中字符串作为shell命令执行,并返回执行结果。具体含义如下:

双引号(” “):在双引号中,除了$, ”, `和\以外所有的字符都解释成字符本身。

单引号(’ ‘):在单引号中所有的字符包括特殊字符($,”,`和\)都将解释成字符本身而成为普通字符。

反引号(` `):在反引号中的字符串将解释成shell命令来执行。

类似-Xms、-Xmn这些参数的含义:

类似-Xms、-Xmn这些参数的含义:

答:
堆内存分配:
JVM初始分配的内存由-Xms指定,默认是物理内存的1/64
JVM最大分配的内存由-Xmx指定,默认是物理内存的1/4
默认空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制;空余堆内存大于70%时,JVM会减少堆直到 -Xms的最小限制。
因此服务器一般设置-Xms、-Xmx相等以避免在每次GC 后调整堆的大小。对象的堆内存由称为垃圾回收器的自动内存管理系统回收。
非堆内存分配:
JVM使用-XX:PermSize设置非堆内存初始值,默认是物理内存的1/64;
由XX:MaxPermSize设置最大非堆内存的大小,默认是物理内存的1/4。
-Xmn2G:设置年轻代大小为2G。
-XX:SurvivorRatio,设置年轻代中Eden区与Survivor区的比值。

Docker之/etc/profile不生效得问题

配置案例

export JAVA_HOME=/usr/local/jdk/
export JRE_HOME=/usr/local/jdk/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export M2_HOME=/usr/local/maven
export MYSQL_HOME=/usr/local/mysql

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$M2_HOME/bin:$MYSQL_HOME/bin

 

可以将环境变量放到 /root/.bashrc 这个里面,下次启动容器也生效

kubectl create 一直处于 ContainerCreating 状态

使用命令查看 pods 状态,发现过去很久还是没有启动成功。

guoqingsongmbp:k8s guo$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 15s

1
2
3

继续查看详情

guoqingsongmbp:k8s guo$ kubectl describe pod nginx
Name: nginx
Namespace: default
Node: minikube/192.168.99.105
Start Time: Tue, 25 Dec 2018 17:45:28 +0800
Labels: app=nginx
Annotations: <none>
Status: Pending
IP:
Containers:
nginx:
Container ID:
Image: nginx
Image ID:
Port: 80/TCP
State: Waiting
Reason: ContainerCreating
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5bz7m (ro)
Conditions:
Type Status
Initialized True
Ready False
PodScheduled True
Volumes:
default-token-5bz7m:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5bz7m
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: <none>
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal Scheduled 14s default-scheduler Successfully assigned nginx to minikube
Normal SuccessfulMountVolume 14s kubelet, minikube MountVolume.SetUp succeeded for volume “default-token-5bz7m”
Warning FailedCreatePodSandBox 0s kubelet, minikube Failed create pod sandbox.

 

发现最后出错了,Warning FailedCreatePodSandBox 0s kubelet, minikube Failed create pod sandbox.。

进入到 minikube 节点里面进行排查问题。

guoqingsongmbp:k8s guo$ minikube ssh
_ _
_ _ ( ) ( )
___ ___ (_) ___ (_)| |/’) _ _ | |_ __
/’ _ ` _ `\| |/’ _ `\| || , < ( ) ( )| ‘_`\ /’__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/’`\____)

# 查看日志
$ journalctl -xe

1
2
3
4
5
6
7
8
9
10

发现有这么一个错误,如下:

Dec 25 09:40:03 minikube dockerd[2468]: time=”2018-12-25T09:40:03.283646463Z” level=info msg=”Attempting next endpoint for pull after error: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)”
Dec 25 09:40:03 minikube dockerd[2468]: time=”2018-12-25T09:40:03.283664032Z” level=error msg=”Handler for POST /v1.31/images/create returned error: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)”
Dec 25 09:40:03 minikube localkube[3258]: E1225 09:40:03.284457 3258 remote_runtime.go:92] RunPodSandbox from runtime service failed: rpc error: code = Unknown desc = failed pulling image “gcr.io/google_containers/pause-amd64:3.0”: Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

1
2
3

这里是因为会使用地址 gcr.io/google_containers/pause-amd64:3.0 进行拉取镜像,但是这个地址被墙了,所以不通,这里的解决办事是去 docker hub 上拉取完毕之后然后再进行更改 tag。如下:

$ docker pull docker.io/kubernetes/pause
Using default tag: latest
latest: Pulling from kubernetes/pause
a3ed95caeb02: Pull complete
f72a00a23f01: Pull complete
Digest: sha256:2088df8eb02f10aae012e6d4bc212cabb0ada93cb05f09e504af0c9811e0ca14
Status: Downloaded newer image for kubernetes/pause:latest

$ docker tag kubernetes/pause:latest gcr.io/google_containers/pause-amd64:3.0

1
2
3
4
5
6
7
8
9

最后把原来的 pod 删除掉,再重新启动即可。

guoqingsongmbp:k8s guo$ kubectl delete -f pod_nginx.yml
pod “nginx” deleted

guoqingsongmbp:k8s guo$ kubectl create -f pod_nginx.yml
pod “nginx” created

guoqingsongmbp:k8s guo$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 14m

guoqingsongmbp:k8s guo$ kubectl describe pod nginx
Name: nginx
Namespace: default
Node: minikube/192.168.99.105
Start Time: Tue, 25 Dec 2018 18:00:56 +0800
Labels: app=nginx
Annotations: <none>
Status: Running
IP: 172.17.0.4
Containers:
nginx:
Container ID: docker://cf22052ba5626cf6d99fbdb3867fa545a20c16d6f02c7eb9d9ad25b6ce6500ad
Image: nginx
Image ID: docker-pullable://nginx@sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba
Port: 80/TCP
State: Running
Started: Tue, 25 Dec 2018 18:01:26 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5bz7m (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-5bz7m:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5bz7m
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: <none>
Events:
Type Reason Age From Message
—- —— —- —- ——-
Normal Scheduled 14m default-scheduler Successfully assigned nginx to minikube
Normal SuccessfulMountVolume 14m kubelet, minikube MountVolume.SetUp succeeded for volume “default-token-5bz7m”
Normal Pulling 14m kubelet, minikube pulling image “nginx”
Normal Pulled 14m kubelet, minikube Successfully pulled image “nginx”
Normal Created 14m kubelet, minikube Created container
Normal Started 14m kubelet, minikube Started container