1.Explorer Observability

Open the dashboard:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

export MINIKUBE_IP=$(minikube --profile istio-mk ip)

kubectl patch service/grafana -p '{"spec":{"type":"NodePort"}}' -n istio-system
open http://$MINIKUBE_IP:$(kubectl get svc grafana -n istio-system -o 'jsonpath={.spec.ports[0].nodePort}')

kubectl patch service/jaeger-query -p '{"spec":{"type":"NodePort"}}' -n istio-system
open http://$MINIKUBE_IP:$(kubectl get svc jaeger-query -n istio-system -o 'jsonpath={.spec.ports[0].nodePort}')

kubectl patch service/prometheus -p '{"spec":{"type":"NodePort"}}' -n istio-system
open http://$MINIKUBE_IP:$(kubectl get svc prometheus -n istio-system -o 'jsonpath={.spec.ports[0].nodePort}')

kubectl patch service/kiali -p '{"spec":{"type":"NodePort"}}' -n istio-system
open http://$MINIKUBE_IP:$(kubectl get svc kiali -n istio-system -o 'jsonpath={.spec.ports[0].nodePort}')/kiali

Kiali: default username/password: admin/admin

1.1 Grafana

In the grapana. Home->Istio->Istio Workload Dashboard.

Grapha

1.2 Jaeger

Jaeger

Jaeger

Read more »

Understand Microservices architecture requirements and challenges

resource

  • API
  • Discovery
  • Invocation
  • Elasticity
  • Resillience
  • Pipeline
  • Authentication
  • Logging
  • Monitoring
  • Tracing

before Istio

after Istio

The sidecar intercepts all network traffic.

How to add an Istio-Proxy(sidecar)?

istioctl kube-inject -f NormalDeployment.yaml

or

kubectl label namespace myspace istio-injection=enabled

Read more »

Discover CustomResourceDefinitions

Custom Resources extend the API

Custom Controllers provide the functionality - continually maintains the desired state - to monitor its state and reconcile the resource to match with the configuration

https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/

https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/

Custom Resource Definitions (CRDs) in version 1.7

1
2
$ kubectl get crds
$ kubectl api-resources
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
26
27
28
29
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: pizzas.mykubernetes.burrsutter.com
labels:
app: pizzamaker
mylabel: stuff
spec:
group: mykubernetes.burrsutter.com
scope: Namespaced
version: v1beta2
names:
kind: Pizza
listKind: PizzaList
plural: pizzas
singular: pizza
shortNames:
- pz
validation:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
toppings:
type: array
sauce:
type: string

Add Pizzas to your Kubernets Cluster

cheese-pizza.yaml

Read more »

Use Service’s Discovery

Say you have a service mynode in yourspace and a service myapp in myspace. If the myapp wants to access the mynode servcie, the url is:

1
mynode.yourspace.svc.cluster.local:8000 # 8000 is the service port, not the node port.

Configure Liveness and Readiness Probes

kubectl scale --replicas=3 deployment xxx

1
2
StrategyType: RollingUpdate
RollingUpdateStrategy: 1max unavailable, 1max surge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template:
metadata:
labels:
app: myboot
spec:
containers:
- name: myboot
image: myboot:v1
ports:
- containerPort: 8080
livenessProbe:
httpGet:
port: 8080
path: /
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 2
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 3

Once you understand the basics then you can try the advanced demonstration. Where a stateful shopping cart is preserved across a rolling update based on leveraging the readiness probe.

https://github.com/redhat-developer-demos/popular-movie-store

More information on Live & Ready
https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/

Read more »

logs

1
2
3
kubectl get pods
kubectl logs podname -p
kubectl logs podname

exec

1
2
3
kubectl exec -it pod-name /bin/bash
# 查看cgroup配置
cd /sys/fs/cgroup/memory

Constraint CPU & Memory

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
26
27
28
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myboot
name: myboot
spec:
replicas: 1
selector:
matchLabels:
app: myboot
template:
metadata:
labels:
app: myboot
spec:
containers:
- name: myboot
image: 9stepsawesome/myboot:v1
ports:
- containerPort: 8080
resources:
requests:
memory: "300Mi"
cpu: "250m" # 1/4 core
limits:
memory: "400Mi"
cpu: "1000m" # 1 core

ConfigMap

First Let’s see the environment.

Change the environment on deployment:

1
kubectl set env deployment/myboot GREETING="hi"
Read more »

End to End

  1. Find a base image: docker.io, quay.io, gcr.io, registry.redhat.io

  2. Craft your Dockerfile

  3. Configure docker: eval $(minikube --profile myprofile decoder-env)

  4. Build your image: docker build -t liulx/myimage:v1 .

    a. Test via:

    - `docker run -it -p 8080:8080 --name myboot liulx/myimage:v1`
    - `docker run -d -p 8080:8080 --name my boot liulx/myboot:v1`
    - `curl $(minikube --profile myprofile ip):8080`

    b. If remote repo, do docker tag and docker push

    c. docker stop containername to stop testing

  5. kubectl apply -f myDeployment.yaml

  6. kubectl apply -f myService.yaml

  7. Expose a URL via your kubernetes distribution’s load-balancer

docker build

1
docker build -t something/animagename:tag

The .indicates where to find the Dockerfile and assets to be included in the build process.

You can alse explicitly identify the Dockerfile:

  • docker build -t somestring/animagename:tag -f somedirectory/Dockerfile_Production .
  • docker build -t somestring/animagename:tag -f somedirectory/Dockerfile_Testing .
  • docker build -f src/main/docker/Dockerfile.native -t mystuff/myimage:v1 .

Builiding Images

Options Include:

  1. docker build then kubectl run or kubectl create -f deploy.yml
  2. Jib - Maven/Gradle plugin by google
  3. Shift maven plugin by Red hat
  4. s2i - source to image
  5. Tekton - pipeline-based image building
  6. No docker: red hat’s podman, Google’s kaniko, Uber’s makisu
  7. Buildpacks - similar to Heroku & Cloud Foundry
Read more »

Setup

OpenShift is Red Hat’s distribution of Kubernetes

minikube and minishift are essentially equivalent and will be used for the demonstrations/examples below.

Prerequisites

  • Docker or
  • Podman
  • brew install kubectx
  • minikube
  • kubectl

Downloads

Downloads & Install Kubectl CLI

1
2
3
4
5
6
# MacOS
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.14.0/bin/darwin/amd64/kubectl
#
$ chmod +x kubectl
# or
$ brew install kubernetes-cli

Linux & Windows instructions for finding and downloading the a kubectl https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl

Download & Install Minikube Cluster

Read more »

示例代码地址:这里

1. 配置

1.1 基本配置

1.1.1 POM

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>lx</groupId>
<artifactId>deep-into-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>deep-into-spring-boot</name>
<description>深入实践spring boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- 从仓库中查找parent -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

1.1.2 application.yml

将端口设置为8080(默认就是的,并且设置tomcat的字符集为UTF-8

1
2
3
4
server:
port: 8080
tomcat:
uri-encoding: UTF-8

spring boot更多预置参数请参考:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Read more »

1. 环境变量

1.1 GOROOT

GOROOT就是go的安装路径
~/.bash_profile中添加下面语句:

1
2
3
4
5
GOROOT=/usr/local/go
export GOROOT

# 添加到PATH
export PATH=$PATH:$GOROOT/bin

1.2 GOPATH

go install/get/run会用到的目录。它的下面有3个文件夹:

  • bin: golang编译可执行文件存放路径,可自动生成。通常把它也假如到PATH中,用来执行安装的第三方工具
  • pkg: golang编译的.a中间文件存放路径,可自动生成
  • src: 源码路径。按照golang默认约定,go run,go install等命令的当前工作路径(即在此路径下执行上述命令)。

可以设置多个子目录到GOPATH中

1
export GOPATH=~/golib:~/goproject

1.3 GOBIN

Read more »

7.1 映射JPA实体

JPA: Java Persistence API 它是从EJB3中抽取出来的。

  • EntityManagerFactory
  • EntityManager
  • persistence.xml
  • @Entity
  • @Column
  • @Table

要使用JPA,我们需要添加依赖:

1
compile group: 'javax', name: 'javaee-api', version: '7.0'

我们来新建数据:

1
2
3
4
5
6
7
8
@Entity
public class Account {
@Id
private Long id;
private BigDecimal balance;

//...
}

然后配置数据源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Configuration
@ComponentScan(basePackages = "lx.spring.core")
@PropertySource("classpath:prod.properties")
@EnableTransactionManagement
public class AppConfig {

@Autowired
private Environment env;

@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.user"));
dataSource.setPassword(env.getProperty("db.pass"));
return dataSource;
}
}

7.2 Entity Manager和Vendor Adapter

Read more »
0%