Kubernetes源码分析之CPU Manager

背景 Kubelet默认使用CFS Quota/Share的方式来实现Pod的CPU层面约束,而对于cpuset的支持,通过很长一段时间的讨论([Issue] Determine if we should support cpuset-cpus and cpuset-mem)后,最终通过CPU Manager来实现。 CPU Manager作为alpha版本从v1.8开始出现,自v1.10开始作为beta版本默认开启。 使用方式 v1.10版本前需要开启feature-gate。 --feature-gates=CPUManager=true 开启cpuset还需要一些cpumanager的参数设置 --cpu-manager-policy=static --cpu-manager-reconcile-period=10s / Optional, default same as `--node-status-update-frequency` 还需要设置cpu reservation,可以通过 --kube-reserved // or --system-reserved 源码分析 Start CPU Manager 在kubelet启动之时,cpuManager会被生成,并Start。此时,cpuManager已经获取了所在宿主机的cpu拓扑结构,并且另起goroutine每隔reconcilePeriod时间,对宿主机上所有的activePods做一次reconcile。 kubelet.go // initializeModules will initialize internal modules that do not require the container runtime to be up. // Note that the modules here must not depend on modules that are not initialized here. [Read More]

Core Dump File Size Limit in Docker

Background 工作中遇到这样一个问题,运行环境为Docker容器,由于设置了容器的磁盘大小,但是没有设置core dump file的大小(默认为unlimited),当程序收到SIGABRT信号退出时,磁盘容易被撑满。 所以决定不让其产生core dump file。那么问题来了,在Docker容器环境下,该如何实现? 以下记录了围绕这个目的所做的一些尝试与遇到的问题。 Try and try Method 1: 使用systemd [failed] Systemd’s default behavior is to generate core dumps for all processes in /var/lib/systemd/coredump. This behavior can be overridden by creating a configuration snippet in the /etc/systemd/coredump.conf.d/ directory with the following content. /etc/systemd/coredump.conf.d/custom.conf [Coredump] Storage=none 然后重载systemd的配置 systemctl daemon-reload 但是,结果是我们会得到一个诸如Failed to get D-Bus connection: Operation not permitted的error。根据14年的issue Failed to get D-Bus connection: No connection to service manager - CentOS 7 #7459,我们并不能在容器中直接使用systemctl,而是需要用我们自己的process manager(supervisor)来管理进程。 [Read More]
docker 

Coroutine With Gevent

背景 工作中用到gevent。 gevent是一个基于libev的并发库。它为各种并发和网络相关的任务提供了整洁的API。 使用过程中,带着某些问题阅读了一部分源码,现在做一下总结与分享。 协程 Python通过yield与generator,能实现coroutine。举个栗子(更多更详细的例子请 参考this awesome presentation): >>> def grep(pattern): ... print "Looking for %s" % pattern ... while True: ... line = (yield) ... if pattern in line: ... print line ... >>> g = grep("python") >>> g.next() Looking for python >>> g.send("hello world") >>> g.send("python generators rock!") python generators rock! >>> 原本以为gevent会是对yield一些封装,了解后知道,在gevent里面,上下文切换通过 yielding来完成的,但其用到的主要模式是Greenlet, Greenlet是以C扩展模块形式接入Python的轻量级协程。Greenlet全部运行在主程序操作系统 的内部,但它们被协作式地调度。在任何时刻,只有一个协程在运行。 Greenlet 对于Greenlet,暂且不多说, 通过阅读官网的API,我们知道其主要是通过switch这个 方法来实现跳转的,switch如何实现的暂不做讨论,先贴上官网的例子混个脸熟: from greenlet import greenlet def test1(): print 12 gr2. [Read More]

Python Yield Keyword

Yield keyword is an important feature in Python language, in order to understand coroutine in Python, we need to understand yield and generator first. However, this feature also makes the Python newbies like me confused a lot, as there’s no similar thing in Java or C language. So in this post, I’ll try to explain and conclude what the yield keyword does. In fact, the same question was posed on stackoverflow, which gains 5951 votes. [Read More]
python 

A Little Taste on React

I have some experience on Android development, and recently for some reason I want to try React Native. As the RN official tutorial said: So to understand the basic structure of a React Native app, you need to understand some of the basic React concepts, like JSX, components, state, and props. “Maybe I should listen to this guy’s suggestion”, I said to myself, so I spent a week to understand what is React and how to use it. [Read More]
react 

Simple Commit-Msg for Git Hook

Although I am the only contributor of my own Github project, I still want my commit message to be tidier. I wish all the commit messages will follow the pattern like: [Example]: This is an Example. [Exercise]: This ia an Exercise. [Problem]: A problem solved. [Other]: Maybe a merge commit. However, I usually remember to follow this pattern after the commit, that’s really disappointed. So I decided to write a simple hook to prevent this kind of Amnesia. [Read More]
git  shell 

Basic Shell Commands

公司某个产品的测试环境比较复杂,它需要在 OpenStack 上创建3个节点,其中2个节点安装产品并实现高可用(HA),第3个节点(TestAgent节点)与产品节点通过接收与发送数据包进行测试。

整个部署与测试流程都需要用Jenkins做自动化测试,而我的任务是将TestAgent节点移植到Docker容器中。用时一个多月,从熟悉源代码的部署与测试流程,到OpenStack的UI界面和CLI命令的基本使用,到OpenStack网络通信的基本理解,到Python的学习与使用(如使用pexpect通过ssh执行shell命令和脚本), 到shell脚本的熟悉与使用,这个过程中爬过了许多大大小小的坑,也收获了很多东西,这里主要总结一下shell脚本的常用命令。

if

if TEST-COMMANDS; then CONSEQUENT-COMMANDS; fi

其中TEST-COMMANDS一般有3种情形:

  • shell command 如果它的返回状态为0,则执行CONSEQUENT-COMMANDS
  • bracket []
  • double bracket [[]] 在方括号内都会做一些判断,如判断文件是否存在,[ -f FILE ]等等,而其两者的区别在于[[]][]的拓展,只支持与bash、zsh等几种shell,所以可移植性要差一些,不过如果不考虑移植性的问题,[[]]会更加简洁与可读,具体可以参考 StackOverFlow上的回答以及这篇文档

有时简单的逻辑关系可以直接使用&&代替。

grep

grep -rl “pattern” file 返回含有“pattern”的文件名,grep常与awk或者cut一起使用,例如

  • grep "foo" file.txt | awk '{print $1}'
  • grep "/bin/bash" /etc/passwd | cut -d':' -f1,6

sed

  • sed -i 'pattern' file 直接操作文件的内容而不是stdout
  • sed 's/hello/bonjour/' greetings.txt 基本用法
  • sed '/is beautiful/i Life' input 在之前插入一行
  • sed '/Hello/a World' input 在之后增加一行
  • sed '/^\s*$/d' 删除空白行

echo with color

  • RED='\033[0;31m'
  • NC='\033[0m' # No Color
  • echo -e "I ${RED}love${NC} Stack Overflow\n"
shell 

A Ping Question

Recently, I focus on OpenStack things where an interesting network question comes out: If a host machine has multiple network interfaces, is it possible to ping all of them from another host? To be clearer, we assume that Host A has a unique network interface 192.168.11.0/24, Host B has 2 interfaces: 192.168.125.0/24 and 192.168.126.0/24. Meanwhile, the 3 interfaces are connected by a router, as the graph illustrated. If we look at the routing table of Host B, we’ll find something like [Read More]

Kubernetes on Coreos

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. I just follow the guide of CoreOS + Kubernetes Step By Step to deploy Kubernetes cluster on CoreOS. Although this guide is detailed, there’s still something that will cause misunderstanding and should be paid close attention to. So this post is to help you better follow the guide and setup Kubernetes. First of all, according to CoreOS cluster architectures, the CoreOS + Kubernetes Step By Step guide is for Easy development/testing cluster or Production cluster with central services, however, what I have is a Small cluster which was set up in the way Setup CoreOS Cluster with Static IPs, that will make some difference to ETCD_ENDPOINTS environment variable. [Read More]
coreos  docker  k8s 

Setup Coreos Cluster With Static Ip

We have talked about how to setup a CoreOS cluster in my previous post: Setup CoreOS Cluster Manually with VirtualBox. However, as we setup the cluster in VirtualBox, which uses DHCP as default, the etcd2 may not work when VM’s IP changed. So in this article I’ll tell you how to upgrade your cluster and enable the VMs use static IPs instead of DHCP. Setup Static IP First of all, make sure your cluster node VM uses the bridge connection type (as default), that will enable the connection between outer world, as we are not going to set IP tables. [Read More]