Kubernetes Handbook (Start & Pod)

使用minikube构建本地单节点k8s集群 minikube ssh kubectl cluster-info kubectl get nodes #查看节点信息 kubectl describe node minikube #详细信息 多节点k8s集群,使用Google K8s Engine 构建方式看GKE官网即可 k8s初步使用 kubectl run kubia –image=derios/kubia –port=8080 –generator=run/v1 --image=derios/kubia代表要运行的容器镜像 这里的--generator会被废弃,其含义指代的是创建一个ReplicationController而不是Deployment。 kubectl apply -f 更常用 kubectl get pods kubectl get pods -o wide 显示pod ip和pod的节点 如果使用GWE,可以访问集群的dashborad: kubectl clusert-info获取地址 gcloud container clusters describe kubia | grep -E “(username|password):“获取用户名和密码 如果仅仅使用minikube,则如下不需要任何凭证即可访问: 1 minikube dashboard Namespace相关操作 1 kubectl config set-context --current --namespace=my-namespace 创建服务对象,访问Web应用 如果使用minikube或者kubeadm等自定义k8s,loadbalancer是没有集成的,需要AWS或者Google Cloud。最好使用NodePort或者Ingress Controller。如果真要用minikube, 可以使用minikube tunnel解决, 或者minikube service kubia-http ...

March 31, 2021 · 6 min · 1100 words · Me

Docker Cheat Sheet

Books Docker in Action (English ver.) Docker入门到实践(中文) 速查 Docker Cheat Sheet 全量CLI 容器管理CLI 查看容器CLI 容器交互CLI 镜像管理CLI 镜像传输CLI DOCKERFILE主要命令 Dockerfile 基底 1 FROM ruby:2.2.2 变量 1 2 ENV APP_HOME/myapp RUN mkdir $APP_HOME 初始化 1 RUN bundle install 1 WORKDIR /myapp 1 2 VOLUME ["/data"] # Specification for mount point 1 2 ADD file.xyz /file.xyz COPY --chown=user:group host_file.xyz /path/container_file.xyz Onbuild 1 2 ONBUILD RUN bundle install # when used with another file 命令 1 2 EXPOSE 5900 CMD ["bundle", "exec", "rails", "server"] Entrypoint 1 ENTRYPOINT exec top -b Metadata 1 LABEL version="1.0" 1 2 LABEL "com.example.vendor"="ACME Incorporated" LABEL com.example.label-with-value="foo" 1 2 LABEL description="This text illustrates \ that label-values can span multiple lines." Docker Compose 基本用法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # docker-compose.yml version: '2' services: web: build: . # build from Dockerfile context: ./Path dockerfile: Dockerfile ports: - "5000:5000" volumes: - .:/code redis: image: redis 指令 1 2 docker-compose start docker-compose stop 1 2 docker-compose pause docker-compose unpause 1 2 3 docker-compose ps docker-compose up docker-compose down Reference(例子) 构建 1 2 3 web: # build from Dockerfile build: . 1 2 3 4 # build from custom Dockerfile build: context: ./dir dockerfile: Dockerfile.dev 1 2 3 4 5 6 # build from image image: ubuntu image: ubuntu:14.04 image: tutum/influxdb image: example-registry:4000/postgresql image: a4bc65fd 端口 1 2 3 ports: - "3000" - "8000:80" # guest:host 1 2 # expose ports to linked services (not to host) expose: ["3000"] 指令 1 2 3 # command to execute command: bundle exec thin -p 3000 command: [bundle, exec, thin, -p, 3000] 1 2 3 # override the entrypoint entrypoint: /app/start.sh entrypoint: [php, -d, vendor/bin/phpunit] 环境变量 1 2 3 4 5 # environment vars environment: RACK_ENV: development environment: - RACK_ENV=development 1 2 3 # environment vars from file env_file: .env env_file: [.env, .development.env] 依赖 1 2 3 4 5 # makes the `db` service available as the hostname `database` # (implies depends_on) links: - db:database - redis 1 2 3 # make sure `db` is alive before starting depends_on: - db 其他选项 1 2 3 4 # make this service extend another extends: file: common.yml # optional service: webapp 1 2 3 volumes: - /var/lib/mysql - ./_data:/var/lib/mysql 高级特性 打标签 1 2 3 4 services: web: labels: com.example.description: "Accounting web app" DNS服务器 1 2 3 4 5 6 services: web: dns: 8.8.8.8 dns: - 8.8.8.8 - 8.8.4.4 设备绑定 1 2 3 4 services: web: devices: - "/dev/ttyUSB0:/dev/ttyUSB0" 外部链接 1 2 3 4 5 services: web: external_links: - redis_1 - project_db_1:mysql 主机设置 1 2 3 4 services: web: extra_hosts: - "somehost:192.168.1.100" Services 1 2 3 4 5 6 7 8 9 10 11 # To view list of all the services runnning in swarm docker service ls # To see all running services docker stack services stack_name # to see all services logs docker service logs stack_name service_name # To scale services quickly across qualified node docker service scale stack_name_service_name=replicas Clean up 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # To clean or prune unused (dangling) images docker image prune # To remove all images which are not in use containers , add - a docker image prune -a # To Purne your entire system docker system prune # To leave swarm docker swarm leave # To remove swarm ( deletes all volume data and database info) docker stack rm stack_name # To kill all running containers docker kill $(docekr ps -q )

March 30, 2021 · 3 min · 584 words · Me

Life of an HTTP request in a Go server

这篇文章的启发是我在阅读Go的http源码时获得的,之前对这块缺乏深入的了解,这篇文章会结合源码讨论包括典型http request的路由,还会涉及到一些并发和中间件的issue。 我们先从一个简单的go server谈起,下面的代码从https://gobyexample.com/http-servers 截取: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package main import ( "fmt" "net/http" ) func hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } func headers(w http.ResponseWriter, req *http.Request) { for name, headers := range req.Header { for _, h := range headers { fmt.Fprintf(w, "%v: %v\n", name, h) } } } func main() { http.HandleFunc("/hello", hello) http.HandleFunc("/headers", headers) http.ListenAndServe(":8090", nil) } 追踪请求的生命周期我们从http.ListenAndServe这个方法开始,下面的图示说明了这一层的调用关系: 这里实际上inlined了一些代码,因为初始的代码有很多其他的细节不好追踪。 ...

February 20, 2021 · 3 min · 639 words · Me

IaaS vs PaaS vs SaaS

如果你在考虑将你的在线电子业务转移到云上,下面三个名词会经常遇到: IaaS PaaS SaaS 这些是云计算领域的三个主要类别 你很可能早就听说过它们,在各种各样的业务转移到云上的趋势下,它们各自占据的不同的比例: SaaS占据大概24%的企业负载 IaaS在12%左右波动 PaaS是近年来最流行的模型,大概占据32%,且当下和未来会有更多的增长 On-Premise, SaaS, PaaS, IaaS的关键区别 on-premise这里理解成本地部署的含义,比如企业内部部署的CRM软件系统,其反义词即为云端 不久以前,所有公司的IT系统都是on-premise的,云的概念比较模糊和遥远。 SaaS, PaaS, IaaS只是三种简单描述你如何为你的业务使用云设施的方式: IaaS: cloud-based services, 为存储、网络以及虚拟化等服务提供pay-as-you-go PaaS: 通过internet提供硬件和软件工具 SaaS: 完全由第三方提供的软件服务 On-Premise: 企业内部部署 下面是一个图解: SaaS, PaaS, IaaS 举例 大多数的业务一般使用SaaS以及IaaS云计算服务模型的组合,此外有很多业务会鼓励开发者使用PaaS去构建应用。 SaaS举例: BigCommerce, Google Apps, Salesforce, Dropbox, MailChimp, ZenDesk, DocuSign, Slack, Hubspot. PaaS举例: AWS Elastic Beanstalk, Heroku, Windows Azure (mostly used as PaaS), Force.com, OpenShift, Apache Stratos, Magento Commerce Cloud. IaaS举例: AWS EC2, Rackspace, Google Compute Engine (GCE), Digital Ocean, Magento 1 Enterprise Edition*. ...

February 16, 2021 · 2 min · 410 words · Me

Go编程模式:Pipeline

概述 这篇文章介绍Go编程里的Pipeline模式。如果是对Unix/Linux命令行熟悉的人会知道,Pipeline其实就是把每个命令拼接起来完成一个组合功能的技术。当下诸如流式处理,函数式编程,以及应用Gateway对微服务进行简单API编排,其实都受pipeline技术方式的影响。换句话说,这种技术能够很容易得把代码按照单一职责的原则拆分成多个高内聚低耦合的小模块,然后拼装起来去完成比较复杂的功能。 ​

February 3, 2021 · 1 min · 3 words · Me