kubernetes实践(七)Kubernetes实战

By prince No comments
一.wordpress+mysql案例

1.创建wordpress命名空间

kubectl create namespace wordpress

2.根据wordpress-db.yaml创建资源

wordpress-db.yaml文件

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-deploy
  namespace: wordpress
  labels:
    app: mysql
spec:
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6  
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
          name: dbport
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: rootPassW0rd
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress
        volumeMounts:
        - name: db
          mountPath: /var/lib/mysql
      volumes:
      - name: db
        hostPath:
          path: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: wordpress
spec:
  selector:
    app: mysql
  ports:
  - name: mysqlport
    protocol: TCP
    port: 3306
    targetPort: dbport

创建命令

kubectl apply -f wordpress-db.yaml

查询

kubectl get pods -n wordpress  
kubectl get svc mysql -n wordpress
kubectl describe svc mysql -n wordpress

3.根据wordpress.yaml创建资源

wordpress.yaml文件

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: wordpress-deploy
  namespace: wordpress
  labels:
    app: wordpress
spec:
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          name: wdport
        env:
        - name: WORDPRESS_DB_HOST
          value: 192.168.199.101:3306                     
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  namespace: wordpress
spec:
  type: NodePort
  selector:
    app: wordpress
  ports:
  - name: wordpressport
    protocol: TCP
    port: 80
    targetPort: wdport

创建

kubectl apply -f wordpress.yaml 
#修改其中mysql的ip地址,其实也可以使用service的name:mysql

查询

kubectl get pods -n wordpress 
kubectl get svc -n wordpress # 获取到转发后的端口,如31969

4.访问测试

访问集群中任意宿主机节点的IP:31969,如http://192.168.50.14:31969/

二.Spring Boot项目案例

1.准备Spring Boot项目cicd-demo镜像

(1)创建Controller

@RestController
public class TestController   {
    @RequestMapping
    public String show(){
        return "Hello cicd";
    }
}

(2)pom文件配置docker-maven-plugin插件

<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.1</version>
    <configuration>
        <imageName>registry.cn-hangzhou.aliyuncs.com/xxx/${project.name}</imageName>
        <dockerHost>http://127.0.0.1:2375</dockerHost>
        <baseImage>anapsix/alpine-java</baseImage>
        <entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

(3)打包推送镜像到私有仓库

mvn clean package docker:build -DpushImage -DskipTests

2.部署镜像到Kubernetes

(1)创建cicd-demo命名空间

kubectl create namespace cicd-demo

(2)创建私有仓库密钥

kubectl -n cicd-demo create secret docker-registry aliyun-key \
--docker-server=registry.cn-hangzhou.aliyuncs.com \
-[email protected] \
--docker-password=xxxxxx \
[email protected]

(3)编写Kubernetes配置文件

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: cicd-demo
  namespace: cicd-demo
spec: 
  selector: 
    matchLabels: 
      app: cicd-demo
  replicas: 1
  template: 
    metadata:
      labels: 
        app: cicd-demo
    spec: 
      imagePullSecrets:
      - name: aliyun-key
      containers: 
      - name: cicd-demo
        image: registry.cn-hangzhou.aliyuncs.com/xxx/cicd-demo:1.1
        ports: 
        - containerPort: 8090
---
# 创建Pod的Service
apiVersion: v1
kind: Service
metadata: 
  name: cicd-demo
  namespace: cicd-demo
spec: 
  ports: 
  - port: 80
    protocol: TCP
    targetPort: 8090
  selector: 
    app: cicd-demo
---
# 创建Ingress,定义访问规则,一定要记得提前创建好nginx ingress controller
apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: cicd-demo
  namespace: cicd-demo
spec: 
  rules: 
  - host: cicd.demo.xxx.com
    http: 
      paths: 
      - path: /
        backend: 
          serviceName: cicd-demo
          servicePort: 80

注意imagePullSecrets 定义拉取私有仓库到密钥

(4)创建Kubernetes资源

kubectl apply -f cicd-demo.yaml

(5)查看资源

#pod
kubectl get pods -o wide -n cicd-demo
#service
kubectl get svc -n cicd-demo

(6)访问pod测试

curl pod_id:8090/

(7)配置hosts文件

192.168.50.15 cicd.demo.xxx.com

(8)浏览器访问测试

http://cicd.demo.xxx.com
三.Nacos项目案例

1.准备nacos服务

01 下载nacos-server-1.2.0-beta.1.tar.gz到服务器192.168.1.100:/usr/local/nacos

02 解压:tar -zxvf

03 进入到bin目录执行:sh startup.sh -m standalone [需要有java环境的支持]

04 浏览器访问:192.168.1.100:8848/nacos

2.准备两个Spring Boot项目,名称为user和order,表示两个服务

(1)项目配置

pom文件

<!--引入nacos client依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--引入docker-maven-plugin插件-->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.1.1</version>
    ....
</plugin>

user项目配置文件application.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.100:8848
  application:
    name: user

server:
  port: 8080

user项目Controller文件

@RestController
@RequestMapping("/user")
public class TestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/test")
    public List<ServiceInstance> findServiceInstance() throws Exception{
        //查询指定服务名称下的所有实例的信息
        List<ServiceInstance> list=this.discoveryClient.getInstances("order");
        ServiceInstance serviceInstance=list.get(0);
        URI uri = serviceInstance.getUri();
        System.out.println(uri.toString());
        this.testUrl(uri.toString());
        return list;
    }

    public void testUrl(String urlString){
        URL url;
        try {
            url = new URL(urlString);
            URLConnection co =  url.openConnection();
            co.connect();
            System.out.println("连接可用");
        } catch (Exception e1) {
            System.out.println("连接打不开!");
            url = null;
        }
    }
}

order项目配置文件

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.100:8848
  application:
    name: order

server:
  port: 9090

(2)打包推送user和order镜像到私有仓库

mvn clean package docker:build -DpushImage -DskipTests

(3)编写Kubernetes配置文件

user.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: user
  namespace: nacos-demo
spec: 
  selector: 
    matchLabels: 
      app: user
  replicas: 1
  template: 
    metadata:
      labels: 
        app: user
    spec: 
      imagePullSecrets:
      - name: aliyun-key
      containers: 
      - name: user
        image: registry.cn-hangzhou.aliyuncs.com/xxx/user:0.0.1-SNAPSHOT
        ports: 
        - containerPort: 8080
---
# 创建Pod的Service
apiVersion: v1
kind: Service
metadata: 
  name: user
  namespace: nacos-demo
spec: 
  ports: 
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector: 
    app: user
---
# 创建Ingress,定义访问规则,一定要记得提前创建好nginx ingress controller
apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
  name: user
  namespace: nacos-demo
spec: 
  rules: 
  - host: user.demo.xxx.com
    http: 
      paths: 
      - path: /
        backend: 
          serviceName: user
          servicePort: 80

order.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: order
  namespace: nacos-demo
spec: 
  selector: 
    matchLabels: 
      app: order
  replicas: 1
  template: 
    metadata:
      labels: 
        app: order
    spec: 
      imagePullSecrets:
      - name: aliyun-key
      containers: 
      - name: order
        image: registry.cn-hangzhou.aliyuncs.com/xxx/order:0.0.1-SNAPSHOT
        ports: 
        - containerPort: 8080
---
# 创建Pod的Service
apiVersion: v1
kind: Service
metadata: 
  name: order
  namespace: nacos-demo
spec: 
  ports: 
  - port: 80
    protocol: TCP
    targetPort: 9090
  selector: 
    app: order

(4)创建Kubernetes资源

kubectl create namespace nacos-demo
kubectl apply -f user.yaml
kubectl apply -f order.yaml

(5)查看Kubernetes资源

kubectl get pods -o wide -n nacos-demo
kubectl get svc -o wide -n nacos-demo
kubectl get ingress -o wide -n nacos-demo

(6)访问测试

配置hosts文件

192.168.50.15 user.demo.xxx.com

# 01 集群内
curl user-pod-ip:8080/user/test
kubectl logs -f <pod-name> -c <container-name>

[主要是为了看日志输出,证明user能否访问order]

# 02 集群外,比如浏览器,可以把集群中原来的ingress删除掉
http://user.demo.xxx.com/user/test

发表评论

 

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据