极客时间运维进阶训练营第十三周作业

1、在集群上使用资源配置文件编排运行 demoapp,并使用 Service 完成 Pod 发现和服务发布。

root@k8s-master01:/tmp/k8s# cat v1-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: demoapp-v1
  name: demoapp-v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demoapp-v1
  template:
    metadata:
      labels:
        app: demoapp-v1
    spec:
      containers:
      - image: ikubernetes/demoapp:v1.0
        name: demoapp
---
apiVersion: v1
kind: Service
metadata:
  name: demoapp-v1
spec:
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: demoapp-v1
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  name: demoapp-v1-nodeport
spec:
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: demoapp-v1
  type: NodePort
  #externalTrafficPolicy: Local
root@k8s-master01:/tmp/k8s# kubectl  apply -f v1-demo.yaml
deployment.apps/demoapp-v1 created
service/demoapp-v1 created
service/demoapp-v1-nodeport created
root@k8s-master01:/tmp/k8s# kubectl  get deployment
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
demoapp-v1   3/3     3            3           66s
nfs-server   1/1     1            1           21h
root@k8s-master01:/tmp/k8s# kubectl  get pods -o wide
NAME                                READY   STATUS    RESTARTS       AGE     IP            NODE                    NOMINATED NODE   READINESS GATES
configmaps-volume-demo              1/1     Running   1 (15h ago)    18h     10.244.3.21   k8s-node03.magedu.com   <none>           <none>
demoapp-v1-6dfd48f884-4bhjz         1/1     Running   0              78s     10.244.2.34   k8s-node02.magedu.com   <none>           <none>
demoapp-v1-6dfd48f884-6kldt         1/1     Running   0              78s     10.244.3.28   k8s-node03.magedu.com   <none>           <none>
demoapp-v1-6dfd48f884-mw7bq         1/1     Running   0              78s     10.244.1.25   k8s-node01.magedu.com   <none>           <none>
init-container-demo                 1/1     Running   3 (15h ago)    7d18h   10.244.1.17   k8s-node01.magedu.com   <none>           <none>
liveness-httpget-demo               1/1     Running   62 (15h ago)   7d21h   10.244.3.22   k8s-node03.magedu.com   <none>           <none>
nfs-server-857f859f57-4ttvl         1/1     Running   1 (15h ago)    21h     10.244.3.20   k8s-node03.magedu.com   <none>           <none>
redis-pvc-pod                       1/1     Running   2 (15h ago)    6d8h    10.244.3.24   k8s-node03.magedu.com   <none>           <none>
secrets-env-demo                    1/1     Running   0              14h     10.244.2.27   k8s-node02.magedu.com   <none>           <none>
secrets-volume-demo                 1/1     Running   0              14h     10.244.2.28   k8s-node02.magedu.com   <none>           <none>
securitycontext-capabilities-demo   1/1     Running   3 (15h ago)    7d21h   10.244.1.18   k8s-node01.magedu.com   <none>           <none>
root@k8s-master01:/tmp/k8s# kubectl get svc
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)            AGE
demoapp-v1            ClusterIP   10.103.1.134    <none>        80/TCP             86s
demoapp-v1-nodeport   NodePort    10.109.229.40   <none>        80:30212/TCP       86s
kubernetes            ClusterIP   10.96.0.1       <none>        443/TCP            24d
nfs-server            ClusterIP   10.110.209.63   <none>        2049/TCP,111/UDP   21h
root@k8s-master01:/tmp/k8s# curl 10.103.1.134
iKubernetes demoapp v1.0 !! ClientIP: 192.168.56.161, ServerName: demoapp-v1-6dfd48f884-6kldt, ServerIP: 10.244.3.28!
root@k8s-master01:/tmp/k8s# curl 10.103.1.134
iKubernetes demoapp v1.0 !! ClientIP: 192.168.56.161, ServerName: demoapp-v1-6dfd48f884-mw7bq, ServerIP: 10.244.1.25!
root@k8s-master01:/tmp/k8s# curl 10.103.1.134
iKubernetes demoapp v1.0 !! ClientIP: 192.168.56.161, ServerName: demoapp-v1-6dfd48f884-mw7bq, ServerIP: 10.244.1.25!

root@template-ubuntun2004:~# hostname -i
192.168.56.169
root@template-ubuntun2004:~# curl  192.168.56.166:30212
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.0, ServerName: demoapp-v1-6dfd48f884-6kldt, ServerIP: 10.244.3.28!
root@template-ubuntun2004:~# curl  192.168.56.166:30212
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.0, ServerName: demoapp-v1-6dfd48f884-6kldt, ServerIP: 10.244.3.28!
root@template-ubuntun2004:~# curl  192.168.56.166:30212
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.0, ServerName: demoapp-v1-6dfd48f884-4bhjz, ServerIP: 10.244.2.34!
root@template-ubuntun2004:~# curl  192.168.56.166:30212
iKubernetes demoapp v1.0 !! ClientIP: 10.244.1.1, ServerName: demoapp-v1-6dfd48f884-mw7bq, ServerIP: 10.244.1.25!
View Code

2、要求以配置文件的方式,在集群上编排运行 nginx、wordpress 和 mysql,并使用 Service 完成 Pod 发现和服务发布。

提示:使用 Secret 的方式的为 wordpress 指定要使用 mysql 服务器地址、数据库名称、用户名称和用户密码;
nginx Service:定义为 NodePort 类型;
nginx 的配置文件:要通过 ConfigMap 提供;
wordpress 和 mysql 的数据要保存于 PVC 上。

# 创建namespace
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl create namespace blog
namespace/blog created

# 部署mysql
root@k8s-master01:~/learning-k8s-master/wordpress/mysql# cat 01-secret-mysql.yaml
apiVersion: v1
kind: Secret
metadata:
  creationTimestamp: null
  name: mysql-user-pass
data:
  database.name: d3BkYg==
  root.password: TUBnZUVkdQ==
  user.name: d3B1c2Vy
  user.password: bWFnZURVLmMwbQ==
root@k8s-master01:~/learning-k8s-master/wordpress/mysql# cat 02-pvc-mysql-data.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data
spec:
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-csi

root@k8s-master01:~/learning-k8s-master/wordpress/mysql# cat 03-service-mysql.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql
  name: mysql
spec:
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql
  type: ClusterIP
root@k8s-master01:~/learning-k8s-master/wordpress/mysql# cat 04-deploy-mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:8.0
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: root.password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: user.name
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: user.password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: database.name
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql/
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-data

root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  apply -f mysql/ -n blog
secret/mysql-user-pass created
persistentvolumeclaim/mysql-data created
service/mysql created
deployment.apps/mysql created

root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get secret -n blog
NAME              TYPE     DATA   AGE
mysql-user-pass   Opaque   4      42s
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pvc -n blog
NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-data   Bound    pvc-8850eab3-0965-4ae5-8a62-ef30b29ce135   10Gi       RWO            nfs-csi        57s
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pod -n blog
NAME                    READY   STATUS    RESTARTS   AGE
mysql-97b69dcc8-8ksj9   1/1     Running   0          70s
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get svc -n blog
NAME    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
mysql   ClusterIP   10.106.255.108   <none>        3306/TCP   85s

# 部署wordpress
root@k8s-master01:~/learning-k8s-master/wordpress/wordpress# cat 01-service-wordpress.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress
spec:
  ports:
  - name: fpm
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: wordpress
root@k8s-master01:~/learning-k8s-master/wordpress/wordpress# cat 02-pvc-wordpress-app-data.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-app-data
spec:
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs-csi
root@k8s-master01:~/learning-k8s-master/wordpress/wordpress# cat 03-deployment-wordpress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: wordpress
  name: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wordpress:5.8-fpm
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql
        - name: WORDPRESS_DB_USER
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: user.name
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: user.password
        - name: WORDPRESS_DB_NAME
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: database.name
        volumeMounts:
        - name: wordpress-app-data
          mountPath: /var/www/html/
      volumes:
      - name: wordpress-app-data
        persistentVolumeClaim:
          claimName: wordpress-app-data

root@k8s-master01:~/learning-k8s-master/wordpress/wordpress# cd ..
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl apply -f wordpress/ -n blog
service/wordpress created
persistentvolumeclaim/wordpress-app-data created
deployment.apps/wordpress created

root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pvc -n blog
NAME                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-data           Bound    pvc-8850eab3-0965-4ae5-8a62-ef30b29ce135   10Gi       RWO            nfs-csi        7m41s
wordpress-app-data   Bound    pvc-a1edb97f-fce5-4358-9258-6f3ff50ef00b   10Gi       RWX            nfs-csi        50s

root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pods -n blog
NAME                         READY   STATUS              RESTARTS   AGE
mysql-97b69dcc8-8ksj9        1/1     Running             0          8m28s
wordpress-664cfb496b-pchj2   0/1     ContainerCreating   0          97s
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pods -n blog     NAME                         READY   STATUS    RESTARTS   AGE
mysql-97b69dcc8-8ksj9        1/1     Running   0          10m
wordpress-664cfb496b-pchj2   1/1     Running   0          3m35s


# 部署nginx

root@k8s-master01:~/learning-k8s-master/wordpress/nginx# cat 01-configmap-nginx-conf.yaml
apiVersion: v1
data:
  nginx.conf: |
    server {
            listen 80;
            listen [::]:80;

            server_name magedu.com www.magedu.com;

            index index.php index.html index.htm;

            root /var/www/html;

            location ~ /.well-known/acme-challenge {
                    allow all;
                    root /var/www/html;
            }

            location / {
                    try_files $uri $uri/ /index.php$is_args$args;
            }

            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass wordpress:9000;
                    fastcgi_index index.php;
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_param PATH_INFO $fastcgi_path_info;
            }

            location ~ /\.ht {
                    deny all;
            }

            location = /favicon.ico {
                    log_not_found off; access_log off;
            }
            location = /robots.txt {
                    log_not_found off; access_log off; allow all;
            }
            location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                    expires max;
                    log_not_found off;
            }
    }
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: nginx-conf
root@k8s-master01:~/learning-k8s-master/wordpress/nginx# cat 02-service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  ports:
  - name: http-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
  externalIPs:
  - 192.168.56.100
root@k8s-master01:~/learning-k8s-master/wordpress/nginx# cat 03-deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: nginx
    spec:
      volumes:
      - name: ngxconf
        configMap:
          name: nginx-conf
      - name: wordpress-app-data
        persistentVolumeClaim:
          claimName: wordpress-app-data
      containers:
      - image: nginx:1.20-alpine
        name: nginx
        volumeMounts:
        - name: ngxconf
          mountPath: /etc/nginx/conf.d/
        - name: wordpress-app-data
          mountPath: /var/www/html/
root@k8s-master01:~/learning-k8s-master/wordpress/nginx# cd ..
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  apply -f nginx/ -n blog
configmap/nginx-conf created
service/nginx created
deployment.apps/nginx created
# cd ..
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  apply -f nginx/ -n bl
configmap/nginx-conf created
service/nginx created
deployment.apps/nginx created
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get pods -n blog
NAME                         READY   STATUS    RESTARTS   AGE
mysql-97b69dcc8-8ksj9        1/1     Running   0          22m
nginx-5b9c7b4c8f-xmpmx       1/1     Running   0          36s
wordpress-664cfb496b-pchj2   1/1     Running   0          15m
root@k8s-master01:~/learning-k8s-master/wordpress# kubectl  get configmap -n blog
NAME               DATA   AGE
kube-root-ca.crt   1      23m
nginx-conf         1      52s

root@k8s-master01:~/learning-k8s-master/wordpress/nginx# kubectl  get svc -n blog
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE
mysql       ClusterIP   10.106.255.108   <none>           3306/TCP       26m
nginx       NodePort    10.109.140.16    192.168.56.100   80:31112/TCP   4m44s
wordpress   ClusterIP   10.109.192.8     <none>           9000/TCP       19m


# 验证地址
http://192.168.56.166:31112/wp-admin/install.php
http://192.168.56.100/wp-admin/install.php
View Code

 

posted @ 2023-02-11 07:42  john221100  阅读(34)  评论(0编辑  收藏  举报