Skip to content

博客

Golang排序

官方package: sort, 提供了对int,float,string的排序

  • sort.Ints
  • sort.Floats
  • sort.Strings
ints := []int{5, 6, 1, 2, 3} // unsorted
sort.Ints(ints)
fmt.Println(ints)
sort.Sort(sort.Reverse(sort.IntSlice(ints))) // sorted descending
fmt.Println(ints)
floats := []float64{1.3, 3.2, 2.2} // unsorted
sort.Float64s(floats) // sorted ascending
fmt.Println(floats)
sort.Sort(sort.Reverse(sort.Float64Slice(floats))) // sorted descending
fmt.Println(floats)
strings := []string{"b", "a", "b1", "1"} // unsorted
sort.Strings(strings)
fmt.Println(strings)
sort.Sort(sort.Reverse(sort.StringSlice(strings))) // sorted descending
fmt.Println(strings)
func SliceStable(slice interface{}, less func(i, j int) bool)
func Slice(slice interface{}, less func(i, j int) bool)

sort.Slice使用function less(i, j int) bool 来对slice进行排序。

sort.SclieStabl使用function less(i, j int)bool对slice进行排序,当遇到遇到相等的元素时,将会保持原来的顺序不变。

people := []struct {
Name string
Age int
}{
{"Alice", 25},
{"Elizabeth", 75},
{"Alice", 75},
{"Bob", 75},
{"Alice", 75},
{"Bob", 25},
{"Colin", 25},
{"Elizabeth", 25},
}
// Sort by name, preserving original order
sort.SliceStable(people, func(i, j int) bool { return people[i].Name < people[j].Name })
fmt.Println("By name:", people)
// Sort by age preserving name order
sort.SliceStable(people, func(i, j int) bool { return people[i].Age < people[j].Age })
fmt.Println("By age,name:", people)
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}

如:

type Person struct {
Name string
Age int
}
// ByAge implements sort.Interface based on the Age field.
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func main() {
family := []Person{
{"Alice", 23},
{"Eve", 2},
{"Bob", 25},
}
sort.Sort(ByAge(family))
fmt.Println(family) // [{Eve 2} {Alice 23} {Bob 25}]
}

更多:

https://golang.org/pkg/sort/

Nginx配置https

在centos上,直接使用yum安装Nginx

Terminal window
yum install nginx -y

完成安装后,使用nginx命令启动Nginx:

Terminal window
ningx
# 或者
systemctl start nginx

此时,使用http://ip地址或者域名 可以看到Nginx的测试页面

无法访问,可以尝试重启nginx: ningx -s reload

可以在阿里云或者腾讯云申请免费证书, 免费期限一年, 更长期限或者其他类型,可能需要购买了。

将申请到的证书,下载到本地(key文件和crt文件)。

将证书上传到服务器/etc/nginx目录:

Terminal window
scp ssl.key root@xx.xx.xx.xx:/usr/nginx
scp pen.crt root@xx.xx.xx.xx:/usr/nginx

Nginx 配置目录在 /etc/nginx/conf.d,在该目录创建 ssl.conf

/etc/nginx/conf.d/ssl.conf配置:

server {
listen 443;
server_name www.example.com; # 改为绑定证书的域名
# ssl 配置
ssl on;
# 证书的地址, 如果证书在/etc/nginx, 则可以只写文件名称
ssl_certificate pem.crt; # 改为自己申请得到的 crt 文件的名称
ssl_certificate_key ssl.key; # 改为自己申请得到的 key 文件的名称
# ssl 相关配置
ssl_session_timeout 5m; # 缓存有效期
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 安全链接可选的安全协议
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #加密算法
ssl_prefer_server_ciphers on; # 使用服务端的首选算法
location / {
root html;
index index.html index.htm;
}
}

将http重定向到https

server {
listen 80;
server_name www.example.com; # 改为证书绑定的域名
return 301 https://$server_name$request_uri;
}

一次额外的重定向可能会增加数百毫秒访问延迟,这不利于用户体验,并可能最终影响到网站上的业务,此外重定向通常会触发与额外域名建立连接。因为这里是同一个域名的重定向,所以可以使用rewrite避免重定向

使用rewrite避免重定向

server {
listen 80;
server_name www.example.com; # 改为证书绑定的域名
rewrite ^(.*)$ https://$server_name$request_uri;
}

4、重启Nginx

验证nginx配置是否正确

Terminal window
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx, 重新加载配置使其生效:

Terminal window
nginx -s reload

在浏览器通过https访问域名来测试HTTPS是否成功启动

Kubernetes出现pods服务一直处于ContainerCreating状态

创建单机Kubenetes集群环境后,Pod一直处于ContainerCreating状态:

Terminal window
[root@VM_0_3_centos]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myweb-pgn80 0/1 ContainerCreating 0 10m

查看Pod的Event记录

Terminal window
[root@VM_0_3_centos]# kubectl describe pod myweb-pgn80

无法创建的原因提示:

Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request. details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

查找缺失的文件,显示是一个软连接,链接目标是/etc/rhsm:

Terminal window
[root@VM_0_3_centos conf.d]# ll /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt
lrwxrwxrwx 1 root root 27 Jun 10 11:37 /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt -> /etc/rhsm/ca/redhat-uep.pem

尝试手动拉取镜像

Terminal window
[root@VM_0_3_centos]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

提示:

open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory

依据google的解决办法,尝试使用

Terminal window
[root@VM_0_3_centos]# yum install *rhsm*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package python-rhsm-1.19.10-1.el7_4.x86_64 is obsoleted by subscription-manager-rhsm-1.20.11-1.el7.centos.x86_64 which is already installed
Package subscription-manager-rhsm-1.20.11-1.el7.centos.x86_64 already installed and latest version
Package python-rhsm-certificates-1.19.10-1.el7_4.x86_64 is obsoleted by subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_64 which is already installed
Package subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_64 already installed and latest version
Nothing to do

问题出在目标的两个包被subscription-manager-rhsm-certificates-1.20.11-1.el7.centos.x86_64 和subscription-manager-rhsm-1.20.11-1.el7.centos.x86_64替代。

删除已经安装的这两个包,手动安装目标包

Terminal window
[root@VM_0_3_centos]# yum remove *rhsm*

下载目标包,注意版本

Terminal window
#wget ftp://ftp.icm.edu.pl/vol/rzm6/linux-scientificlinux/7.4/x86_64/os/Packages/python-rhsm-certificates-1.19.9-1.el7.x86_64.rpm
#wget ftp://ftp.icm.edu.pl/vol/rzm6/linux-scientificlinux/7.4/x86_64/os/Packages/python-rhsm-1.19.9-1.el7.x86_64.rpm

然后执行安装命令

Terminal window
[root@VM_0_3_centos centos]# rpm -ivh *.rpm
warning: python-rhsm-1.19.9-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 192a7d7d: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:python-rhsm-certificates-1.19.9-1################################# [ 50%]
2:python-rhsm-1.19.9-1.el7 ################################# [100%]

重新拉取镜像

[root@VM_0_3_centos centos]#docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest

查看POD状态

Terminal window
[root@VM_0_3_centos centos]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-m4n1g 1/1 Running 0 9h
myweb-pgn80 1/1 Running 0 9h
myweb-z7q4v 1/1 Running 0 9h

解决问题

Kuberneter无法创建pod

腾讯云CentOS7.3

使用yum安装etcd和Kubernetes软件

Terminal window
yum install -y ectd kubernetes

按顺序启动所有的服务

Terminal window
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy

完成一个单机版的Kubernetes集群环境。

创建mysql-rc.yaml

Terminal window
kubectl create -f mysql-rc.yaml

查看创建的服务

Terminal window
kubectl get rc # 反馈正常

查看pds,显示『no resource found』

Terminal window
kubectl get pods
  • $ vim /etc/kubernetes/apiserver
  • 找到这一行 “KUBE_ADMISSION_CONTROL=“–admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota”,去掉ServiceAccount,保存退出。
  • 重启kube-apiserver服务: $systemctl restart kube-apiserver

Pyton生成标定板

做一个和Opencv相关的项目调研,这里留下些记录….

使用Opencv进行图摄像头标定时,要用到标定板。下面用Python开始生成一个标定板:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 21 16:21:49 2018
@author: xiaoxiwang
"""
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 宽450像素
width = 450
# 高350像素
height = 350
# 50 x 50 一个单元格
length = 50
img = np.zeros((width, height), dtype=np.uint8)
for j in range(height):
for i in range(width):
if ((int)(i / length) + (int)(j / length)) % 2:
img[i, j] = 255
# 将标定板保存为图checkboard.jpg
cv2.imwrite('checkerborad.jpg', img)
# 使用matplotlib查看生成的标定板
plt.subplot(111), plt.imshow(img, cmap='gray'), plt.title('Output')
plt.show()