阿里云容器化产品使用

第一期k8s环境部署

创建无状态应用

在集群详情选择无状态菜单,选择镜像创建,填写应用名称、镜像地址和tag,设置资源配置和端口,在高级配置中选择服务(Service)右边的创建,填写服务的名称、类型、以及服务端口和容器端口,然后点创建,完成后到应用详情查看应用状态

测试应用

在容器首页查看网络下面的服务有对应的公网ip,在浏览器访问公网ip则可以访问到上面部署的无状态服务

prometheus监控

在容器首页点击运维管理下的prometheus的监控菜单,里面会有容器的各种监控指标

第二期容器环境docker安装

  1. docker环境安装

1.1 安装依赖库

yum install -y yum-utils device-mapper-persistent-data lvm2

1.2 添加docker镜像源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1.3 安装docker ce
yum makecache fast && yum -y install docker-ce

1.4 配置docker镜像加速

tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF

1.5 启动docker

  1. 构建docker本地镜像以及运行镜像

2.1 创建工作空间同时编写hello world代码

cat > /tmp/demo/main.go << EOF
package main

import (

    "fmt"
    "net/http"

)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello! World\n")
    })

    fmt.Println("start to serve...")
    http.ListenAndServe(":80", nil)

}
EOF

2.2 创建dockerfile文件
在项目根目录创建dockerfile文件
cat > /tmp/demo/Dockerfile << EOF
FROM golang:1.12-alpine

change current working dir

WORKDIR /go/src/app

copy main.go into /go/src/app

COPY . .

go build and install the app

RUN go install -v ./...

run the app by default

CMD ["app"]
EOF
2.3 开始构建本地镜像

docker build . -t demo:v1
.: 代表构建本地路径
-t: 代表tag

2.4 运行本地镜像
docker run -d -p 9000:80 demo:v1

-d:后台运行
-p 指定端口映射
demo:v1: 运行镜像的tag

第三期chaosblade体验

  1. 创建集群

1.1 创建kubernetes集群

1.2 部署应用资源

apiVersion: apps/v1
kind: Deployment
metadata:
name: nacos-server
spec:
selector:

matchLabels:
  app: nacos-server

template:

metadata:
  labels:
    app: nacos-server
spec:
  containers:
    - name: nacos-standalone
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/nacos:1.0.0
      ports:
        - containerPort: 8848
      env:
        - name: PREFER_HOST_MODE
          value: "hostname"
        - name: MODE
          value: "standalone"
      resources:
        limits:
          cpu: 1
          memory: 2048Mi
        requests:
          cpu: 200m
          memory: 512Mi

apiVersion: v1
kind: Service
metadata:
name: nacos-server
spec:
type: ClusterIP
selector:

app: nacos-server

ports:

- name: http
  port: 8848
  targetPort: 8848

apiVersion: apps/v1
kind: Deployment
metadata:
name: cart-redis
spec:
selector:

matchLabels:
  app: cart-redis

replicas: 1
template:

metadata:
  labels:
    app: cart-redis
spec:
  containers:
    - name: cart-redis
      image: redis:alpine
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 6379
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: v1
kind: Service
metadata:
labels:

app: cart-redis

name: cart-redis
spec:
ports:

- port: 6379
  targetPort: 6379

selector:

app: cart-redis

apiVersion: apps/v1
kind: Deployment
metadata:
name: cartservice
spec:
selector:

matchLabels:
  app: cartservice

template:

metadata:
  labels:
    app: cartservice
spec:
  containers:
    - name: cartservice
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/cartservice:1.0.0
      imagePullPolicy: Always
      env:
        - name: dubbo.registry.address
          value: "nacos://nacos-server:8848"
        - name: spring.cloud.nacos.discovery.server-addr
          value: "nacos-server:8848"
        - name: spring.cloud.nacos.config.server-addr
          value: "nacos-server:8848"
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: apps/v1
kind: Deployment
metadata:
name: recommendationservice
spec:
selector:

matchLabels:
  app: recommendationservice

template:

metadata:
  labels:
    app: recommendationservice
    version: 1.0.0-SNAPSHOT
spec:
  containers:
    - name: recommendationservice
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/recomendationservice:1.0.0
      # imagePullPolicy: Always
      env:
        - name: dubbo.registry.address
          value: "nacos://nacos-server:8848"
        - name: spring.cloud.nacos.discovery.server-addr
          value: "nacos-server:8848"
        - name: spring.cloud.nacos.config.server-addr
          value: "nacos-server:8848"
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: apps/v1
kind: Deployment
metadata:
name: product-mysql
spec:
selector:

matchLabels:
  app: product-mysql

replicas: 1
strategy:

type: Recreate

template:

metadata:
  labels:
    app: product-mysql
spec:
  containers:
    - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
      env:
        - name: MYSQL_DATABASE
          value: product
        - name: MYSQL_ROOT_PASSWORD
          value: productservice
      image: mysql:5.6
      name: product-mysql
      ports:
        - containerPort: 3306
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: v1
kind: Service
metadata:
labels:

app: product-mysql

name: product-mysql
spec:
ports:

- port: 3306
  targetPort: 3306

selector:

app: product-mysql

apiVersion: apps/v1
kind: Deployment
metadata:
name: productservice
spec:
selector:

matchLabels:
  app: productservice

template:

metadata:
  labels:
    app: productservice
    version: 1.0.0-SNAPSHOT
spec:
  containers:
    - name: productservice
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/productservice:1.0.0
      imagePullPolicy: Always
      env:
        - name: dubbo.registry.address
          value: "nacos://nacos-server:8848"
        - name: spring.cloud.nacos.discovery.server-addr
          value: "nacos-server:8848"
        - name: spring.cloud.nacos.config.server-addr
          value: "nacos-server:8848"
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: apps/v1
kind: Deployment
metadata:
name: checkout-mysql
spec:
selector:

matchLabels:
  app: checkout-mysql

replicas: 1
strategy:

type: Recreate

template:

metadata:
  labels:
    app: checkout-mysql
spec:
  containers:
    - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
      env:
        - name: MYSQL_DATABASE
          value: checkout
        - name: MYSQL_ROOT_PASSWORD
          value: checkoutservice
      image: mysql:5.6
      name: checkout-mysql
      ports:
        - containerPort: 3306
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi

apiVersion: v1
kind: Service
metadata:
labels:

app: checkout-mysql

name: checkout-mysql
spec:
ports:

- port: 3306
  targetPort: 3306

selector:

app: checkout-mysql

apiVersion: apps/v1
kind: Deployment
metadata:
name: checkoutservice
spec:
selector:

matchLabels:
  app: checkoutservice

template:

metadata:
  labels:
    app: checkoutservice
spec:
  containers:
    - name: checkoutservice
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/checkoutservice:health
      imagePullPolicy: Always
      ports:
        - name: liveness-port
          containerPort: 8080
          protocol: TCP
      env:
        - name: dubbo.registry.address
          value: "nacos://nacos-server:8848"
        - name: spring.cloud.nacos.discovery.server-addr
          value: "nacos-server:8848"
        - name: spring.cloud.nacos.config.server-addr
          value: "nacos-server:8848"
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi
      livenessProbe:
        failureThreshold: 3
        httpGet:
          path: /health
          port: liveness-port
          scheme: HTTP
        initialDelaySeconds: 5
        periodSeconds: 10
        successThreshold: 1
        timeoutSeconds: 1
      startupProbe:
        failureThreshold: 3
        httpGet:
          path: /health
          port: liveness-port
          scheme: HTTP
        initialDelaySeconds: 40
        periodSeconds: 5
        successThreshold: 1

apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
selector:

matchLabels:
  app: frontend

template:

metadata:
  labels:
    app: frontend
spec:
  containers:
    - name: frontend
      image: registry.cn-beijing.aliyuncs.com/ahas_demo/frontend:async-test
      imagePullPolicy: Always
      ports:
        - name: liveness-port
          containerPort: 8080
          protocol: TCP
      env:
        - name: dubbo.registry.address
          value: "nacos://nacos-server:8848"
        - name: spring.cloud.nacos.discovery.server-addr
          value: "nacos-server:8848"
        - name: spring.cloud.nacos.config.server-addr
          value: "nacos-server:8848"
      resources:
        limits:
          cpu: 1
          memory: 512Mi
        requests:
          cpu: 200m
          memory: 128Mi
      livenessProbe:
        failureThreshold: 3
        httpGet:
          path: /health
          port: liveness-port
          scheme: HTTP
        initialDelaySeconds: 5
        periodSeconds: 10
        successThreshold: 1
        timeoutSeconds: 1
      startupProbe:
        failureThreshold: 3
        httpGet:
          path: /health
          port: liveness-port
          scheme: HTTP
        initialDelaySeconds: 60
        periodSeconds: 5
        successThreshold: 1

apiVersion: v1
kind: Service
metadata:
name: frontend
spec:
type: ClusterIP
selector:

app: frontend

ports:

- name: http
  port: 8080
  targetPort: 8080

apiVersion: v1
kind: Service
metadata:
name: frontend-external
spec:
type: LoadBalancer
selector:

app: frontend

ports:

- name: http
  port: 8080
  targetPort: 8080



创建后大概3-5分钟后在无状态菜单中看到部署的服务,如下图

通过前端应用frontend的详情frontend-external获取前端访问地址,至此容器部署完成

  1. 部署探针

在容器服务-Kubernetes首页的应用中心菜单找到集群,进入集群详情,选择左侧应用菜单下的helm创建ack-ahas-pilot探针服务,安装完成后会出现下图:

  1. chaosblade故障演练

首先进入chaosblade首页https://chaos.console.aliyun.com/,在架构感知里面选择kubernetes监控视图,如下图

选择命名空间

后续由于探针功能异常,无法继续进行演练步骤

4.存在问题

创建ack-ahas-pilot探针后在场景演练中没有frontend可选,如下图:

本以为是ack-ahas-pilot问题,我在heml下删除ack-ahas-pilot后,重新创建会提示已经存在资源,入下图:

上一篇:手把手教你使用ADB卸载手机内置App软件


下一篇:H3C交换机DHCP Server配置的六个方面